这道题,参考罗进瑶同学的题解写了好半天,改了好半天……在此衷心的感谢!
洛谷题号:P1930
继续阅读
分类目录归档:算法
洛谷/USACO “破锣摇滚”乐队 Raucous Rockers
洛谷题号:P2736
又是动规,只会DFS……
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | #include<iostream> #include<cstdio> #include<string> #include<algorithm> #include<queue> #include<list> #include<cstring> #include<cassert> using namespace std; int n, t, m; int song[25]; int dfs(int cur_song, int cur_cd, int rem_min){ if (cur_song == n + 1){ if (cur_cd <= m){ return 0; } return -2e9; } if (song[cur_song] <= rem_min){ return max(dfs(cur_song + 1, cur_cd, rem_min), dfs(cur_song + 1, cur_cd, rem_min - song[cur_song]) + 1); } else{//song[cur_song]>rem_min return max(dfs(cur_song + 1, cur_cd, rem_min), dfs(cur_song + 1, cur_cd + 1, t - song[cur_song]) + 1); } } int main(){ ios::sync_with_stdio(false); cin >> n >> t >> m; for (int i = 1; i <= n; i++){ cin >> song[i]; if (song[i] > t){ i--; n--; } } if (n == 0){ cout << 0 << endl; return 0; } cout << dfs(1, 1, t) << endl; } |
洛谷/USACO 电网 Electric Fences
洛谷题号:P2735
用的什么皮克定理,从来没听说过,看的题解。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | #include<iostream> #include<cstdio> #include<string> #include<algorithm> #include<queue> #include<list> #include<cstring> #include<cassert> using namespace std; int n, m, p; int gcd(int x, int y){ if (x == 0)return y; return gcd(y%x, x); } int main(){ ios::sync_with_stdio(false); cin >> n >> m >> p; int e = gcd(n, m) + gcd(abs(p - n), m) + p; printf("%d", p*m/2 + 1 - e / 2); } |
洛谷/USACO 商店购物 Shopping Offers
洛谷题号:P2732
实在是被这五维的完全背包恶心了一把……
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | int s, b; int dp[6][6][6][6][6]; int goods[1000]; int buy[6]; struct plan{ int n; int k[6]; int p; }plans[200]; int No = 0; int main(){ ios::sync_with_stdio(false); cin >> s; for (int i = 1; i <= s; i++){ cin >> plans[i].n; for (int j = 1; j <= plans[i].n; j++){ int ci, ki; cin >> ci >> ki; if (goods[ci] == 0)goods[ci] = ++No; plans[i].k[goods[ci]] = ki; } cin >> plans[i].p; } cin >> b; for (int i = 1; i <= b; i++){ int ci, pi, ki; cin >> ci >> ki >> pi; if (goods[ci] == 0)goods[ci] = ++No; buy[goods[ci]] = ki; s++; plans[s].n = 1; plans[s].k[goods[ci]] = 1; plans[s].p = pi; } memset(dp, 0x7f, sizeof(dp)); dp[0][0][0][0][0] = 0; for (int z = 1; z <= s; z++){ for (int i = plans[z].k[1]; i <= buy[1]; i++){ for (int j = plans[z].k[2]; j <= buy[2]; j++){ for (int l = plans[z].k[3]; l <= buy[3]; l++){ for (int x = plans[z].k[4]; x <= buy[4]; x++){ for (int y = plans[z].k[5]; y <= buy[5]; y++){ dp[i][j][l][x][y] = min(dp[i][j][l][x][y], dp[i - plans[z].k[1]][j - plans[z].k[2]][l - plans[z].k[3]][x - plans[z].k[4]][y - plans[z].k[5]]+plans[z].p); } } } } } } cout << dp[buy[1]][buy[2]][buy[3]][buy[4]][buy[5]] << endl; } |
洛谷/USACO 游戏 A Game
洛谷题号:P2734
挺难的一道区间型动规,并不会做,看的题解,需要继续学习:
参考题解:https://www.luogu.org/wiki/show?name=%E9%A2%98%E8%A7%A3+P2734 作者: redbag 管理员 更新时间: 2016-08-12 11:04
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | #include<iostream> #include<cstdio> #include<string> #include<algorithm> #include<queue> #include<list> #include<cstring> #include<cassert> using namespace std; int n; int num[105],sum[105]; int f[105][105]; int main(){ ios::sync_with_stdio(false); cin >> n; for (int i = 1; i <= n; i++){ cin >> num[i]; sum[i] = sum[i - 1] + num[i]; f[i][i] = num[i]; } for (int i = n - 1; i >= 1; i--){ for (int j = i + 1; j <= n; j++){ f[i][j] = max(sum[j] - sum[i - 1] - f[i + 1][j], sum[j] - sum[i - 1] - f[i][j - 1]); } } cout << f[1][n] << " " << sum[n] - f[1][n] << endl; } |
洛谷/USACO 骑马修栅栏 Riding the Fences
洛谷题号:P2731
参考题解:作者: 约修亚_RK 更新时间: 2016-10-28 23:59
作者: 曹彦臣 更新时间: 2016-09-07 21:31
https://www.luogu.org/wiki/show?name=%E9%A2%98%E8%A7%A3+P2731
一开始用的是Fleury’s algorithm,T了1个点:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | #include<iostream> #include<string> #include<algorithm> #include<queue> #include<list> #include<cstring> using namespace std; int f, rk = 0, mnode = 0,ans; int arr[505][505]; int path[10000]; int degree[505]; bool check(int num){ for (int i = 1; i <= mnode; i++){ for (int j = 1; j <= mnode; j++){ if (arr[i][j]!=0) return false; } } ans = num; return true; } bool dfs(int node,int depth){ path[depth] = node; bool flag = false; for (int i = 1; i <= mnode; i++){ if (arr[node][i]){ flag = true; arr[node][i]--; arr[i][node]--; if (dfs(i, depth + 1))return true; arr[node][i]++; arr[i][node]++; } } if (!flag&&check(depth))return true; path[depth] = 0; return false; } int main(){ ios::sync_with_stdio(false); cin >> f; for (int i = 1; i <= f; i++){ int u, v; cin >> u >> v; arr[u][v]++; arr[v][u]++; degree[u]++; degree[v]++; mnode = max(mnode, u); mnode = max(mnode, v); } bool odd = false; for (int i = 1; i <= mnode; i++){ if (degree[i] % 2 == 1){ odd = true; if (dfs(i, 1))break; } } if (!odd)dfs(1, 1); for (int i = 1; i <= ans; i++){ cout << path[i] << endl; } } |
后来改用Hierholzer’s algorithm,A了。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | int f, rk = 0, mnode = 0,ans; int arr[505][505]; int path[10000]; int degree[505]; bool check(int num){ for (int i = 1; i <= mnode; i++){ for (int j = 1; j <= mnode; j++){ if (arr[i][j]!=0) return false; } } ans = num; return true; } void Euler(int node){ //新版本 for (int i = 1; i <= mnode; i++){ while (arr[node][i] > 0){ arr[node][i]--; arr[i][node]--; Euler(i); } } path[++ans] = node; } int main(){ ios::sync_with_stdio(false); cin >> f; for (int i = 1; i <= f; i++){ int u, v; cin >> u >> v; arr[u][v]++; arr[v][u]++; degree[u]++; degree[v]++; mnode = max(mnode, u); mnode = max(mnode, v); } bool odd = false; for (int i = 1; i <= mnode; i++){ if (degree[i] % 2 == 1){ odd = true; Euler(i); break; } } if (!odd)Euler(1); for (int i = ans; i >=1; i--){ cout << path[i] << endl; } } |
洛谷/USACO 魔板 Magic Squares
洛谷题号:P2730
THUOJ上也有这道题目,题目输入输出格式略微有不同,基本原理相同。但是THUOJ不能使用任何数据结构,全部都要自己写。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 | #include<iostream> #include<string> #include<algorithm> #include<queue> using namespace std; struct status{ int arr[9]; }; string s_result; template <typename T> struct node{ T data; node<T> * next; }; template <typename T> struct list{ node<T> * head; int _size; list(){ _size = 0; head = NULL; } ~list(){ node <T> * current = head; node <T> * next; while (current != NULL){ next = current->next; delete current; current = next; } } void insert(T & data){ _size++; node<T> * ptr = new node<T>; ptr->data = data; ptr->next = head; head = ptr; } bool search(T & target){ node<T> * ptr = head; while (ptr != NULL){ if (ptr->data == target)return true; ptr = ptr->next; } return false; } }; list<int> hashtable[50021]; inline int h(const int m){ return (2 * m + 29) % 50021; } inline int status2int(status m){ int t = 0; for (int i = 1; i <= 8; i++){ t *= 10; t += m.arr[i]; } return t; } inline status int2status(int t){ status m; for (int i = 8; i >= 1; i--){ m.arr[i] = t % 10; t /= 10; } return m; } inline int change(int status_m, char type){ status m = int2status(status_m); int t; switch (type){ case 'A': swap(m.arr[1], m.arr[8]); swap(m.arr[2], m.arr[7]); swap(m.arr[3], m.arr[6]); swap(m.arr[4], m.arr[5]); break; case 'B': t = m.arr[4]; for (int i = 4; i >= 2; i--)m.arr[i] = m.arr[i - 1]; m.arr[1] = t; t = m.arr[5]; for (int i = 5; i <= 7; i++)m.arr[i] = m.arr[i + 1]; m.arr[8] = t; break; case 'C': t = m.arr[2]; m.arr[2] = m.arr[7]; m.arr[7] = m.arr[6]; m.arr[6] = m.arr[3]; m.arr[3] = t; break; } return status2int(m); } int BFS(int m){ queue<int> q; queue<int> qi; queue<string> qs; q.push(12345678); qi.push(0); qs.push(""); while (!q.empty()){ int t = q.front(); q.pop(); int time = qi.front(); qi.pop(); string str = qs.front(); qs.pop(); if (t == m){ s_result = str; return time; } else if (time > 30){ continue; } else{ int s = change(t, 'A'); if (!hashtable[h(s)].search(s)){ hashtable[h(s)].insert(s); q.push(s); qi.push(time + 1); qs.push(str + 'A'); } s = change(t, 'B'); if (!hashtable[h(s)].search(s)){ hashtable[h(s)].insert(s); q.push(s); qi.push(time + 1); qs.push(str + 'B'); } s = change(t, 'C'); if (!hashtable[h(s)].search(s)){ hashtable[h(s)].insert(s); q.push(s); qi.push(time + 1); qs.push(str + 'C'); } } } return -1; } int main(){ ios::sync_with_stdio(false); status m; for (int i = 1; i <= 8; i++){ cin >> m.arr[i]; } cout << BFS(status2int(m)) << endl; cout << s_result << endl; } |
洛谷/USACO 丑数 Humble Numbers
洛谷题号:P2723
参考题解:作者: redbag 管理员 更新时间: 2016-08-01 23:15 https://www.luogu.org/wiki/show?name=%E9%A2%98%E8%A7%A3+P2723
这个暴力还是有一定思想的,一般人根本想不到的…………
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | #include<iostream> #include<algorithm> #include<cstring> #include<vector> #include<string> #include<cassert> #include<map> #include<queue> using namespace std; int k, n; long long a[105], s[200000], f[200000]; int main(){ ios::sync_with_stdio(false); cin >> k >> n; for (int i = 1; i <= k; i++){ cin >> a[i]; } f[0] = 1; for (int i = 1; i <= n; i++){ long long m = 2e10; for (int j = 1; j <= k; j++){ while (a[j] * f[s[j]] <= f[i - 1])s[j]++; m = min(a[j] * f[s[j]], m); } f[i] = m; } cout << f[n] << endl; } |
洛谷/USACO 01串 Stringsobits
洛谷题号:P2727
参考题解:作者: 罗进瑶 更新时间: 2017-08-04 10:49 https://www.luogu.org/wiki/show?name=%E9%A2%98%E8%A7%A3+P2727
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | #include<iostream> #include<algorithm> #include<cstring> #include<vector> #include<string> #include<cassert> #include<map> #include<queue> using namespace std; long long dp[40][40];//dp[i][j]表示长度为i,最多含有j个1的二进制数的个数 int main(){ ios::sync_with_stdio(false); long long n, l, i; cin >> n >> l >> i; for (int i = 0; i <= n; i++)dp[i][0] = dp[0][i] = 1; for (int i = 1; i <= n; i++){ for (int j = 1; j <= l; j++){ dp[i][j] = dp[i - 1][j] + dp[i - 1][j - 1]; } } long long k = i; for (int i = n; i >= 1; i--){ if (k > dp[i - 1][l]){ cout << "1"; k -= dp[i - 1][l]; l--; } else cout << "0"; } cout << endl; } |
洛谷/USACO 联系 Contact
洛谷题号:P2724
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | #include<iostream> #include<algorithm> #include<cstring> #include<vector> #include<string> #include<cassert> #include<map> using namespace std; map<string, int> m; string s,st; int a, b, n; struct sorted{ int times; string str; bool operator < (sorted & s2){ if (times != s2.times)return times>s2.times; else if (str.length() != s2.str.length())return str.length() < s2.str.length(); else{ for (int i = 0; i < str.length(); i++){ if (str[i] != s2.str[i]){ return str[i] < s2.str[i]; } } return true; } } }sarr[200000]; int sarr_iter = 0; int main(){ ios::sync_with_stdio(false); cin >> a >> b >> n; while (cin >> st)s.append(st); for (int len = a; len <= b; len++){ if (len > s.length())break; for (int i = 0; i <= s.length() - len; i++){ m[s.substr(i, len)]++; } } for (auto i = m.begin(); i != m.end(); i++){ sarr[++sarr_iter].times = (*i).second; sarr[sarr_iter].str = (*i).first; } sort(sarr + 1, sarr + sarr_iter + 1); int cur_freq_num = 0; int cur_freq_value = -1, cur_value_times = 0; for (int i = 1; cur_freq_num<=n&&i<=sarr_iter; i++){ if (sarr[i].times != cur_freq_value){ cur_freq_num++; if (cur_freq_num > n)break; if (cur_value_times % 6 != 0)cout << endl; cur_freq_value = sarr[i].times; cur_value_times = 1; cout << cur_freq_value << endl << sarr[i].str; } else{ if (cur_value_times % 6 != 0)cout << " "; cout << sarr[i].str; cur_value_times++; if (cur_value_times % 6 == 0)cout << endl; } } if (cur_value_times % 6 != 0)cout << endl; } |