[180609] A fumble fish week(BISTU第九届C语言基本技能比赛 解题报告)
比赛链接:http://acm.bistu.edu.cn/acmhome/contest.do?&method=contestDetail&contestId=66 (外网可能上不去)
宁姐姐的姿势水平很高啊,感谢珂学家宁姐姐带给我们的短暂的三个小时的快乐。)虽然没看过原著,但是确实被动画虐到了QAQ。
安利一下DP专题:
https://vjudge.net/contest/231974
https://vjudge.net/contest/232302
https://vjudge.net/contest/232929
https://vjudge.net/contest/233059
下面是解题报告。。。。。。。。。。。。。
。。。。。
。。。。
。。。
。。
。
参考代码:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <iostream> 2 #include <cstdio> 3 #include <algorithm> 4 #include <vector> 5 #include <queue> 6 #include <stack> 7 #include <string> 8 #include <cstring> 9 10 using namespace std; 11 12 const int maxn = 1000; 13 int n; 14 int f[maxn]; 15 16 signed main() { 17 // freopen("in.txt","r", stdin); 18 f[0] = 0, f[1] = 1, f[2] = 2; 19 for(int i = 3; i < maxn; i++) { 20 f[i] = f[i-1] + f[i-2]; 21 } 22 int T; 23 scanf("%d", &T); 24 while(T--) { 25 scanf("%d", &n); 26 printf("%d\n", f[n]); 27 } 28 return 0; 29 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <iostream> 2 #include <cstdio> 3 #include <algorithm> 4 #include <vector> 5 #include <queue> 6 #include <stack> 7 #include <string> 8 #include <cstring> 9 10 using namespace std; 11 12 const int maxn = 3030; 13 int n; 14 int f[maxn]; 15 16 signed main() { 17 // freopen("in.txt","r", stdin); 18 while(~scanf("%d", &n)) { 19 for(int i = 1; i <= n; i++) { 20 scanf("%d", &f[i]); 21 } 22 int tot = 0; 23 for(int i = 1; i <= n; i++) { 24 for(int j = i + 1; j <= n; j++) { 25 if(f[i] > f[j]) tot++; 26 } 27 } 28 printf("%d\n", tot); 29 } 30 return 0; 31 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <iostream> 2 #include <cstdio> 3 #include <algorithm> 4 #include <vector> 5 #include <queue> 6 #include <stack> 7 #include <string> 8 #include <cstring> 9 #include <set> 10 11 using namespace std; 12 13 typedef long long LL; 14 typedef struct Edge { 15 LL u, v, w, next; 16 }Edge; 17 18 const LL inf = 100000000000LL; 19 const LL maxn = 1111; 20 const LL maxm = 100100; 21 LL cnt, dhead[maxn]; 22 LL cur[maxn], dd[maxn]; 23 Edge dedge[maxm]; 24 LL S, T, N; 25 26 void init() { 27 memset(dhead, -1, sizeof(dhead)); 28 for(LL i = 0; i < maxn; i++) dedge[i].next = -1; 29 S = 0, cnt = 0; 30 } 31 32 void adde(LL u, LL v, LL w, LL c1=0) { 33 dedge[cnt].u = u, dedge[cnt].v = v, dedge[cnt].w = w; 34 dedge[cnt].next = dhead[u], dhead[u] = cnt++; 35 dedge[cnt].u = v, dedge[cnt].v = u, dedge[cnt].w = c1; 36 dedge[cnt].next = dhead[v], dhead[v] = cnt++; 37 } 38 39 bool bfs(LL s, LL t, LL n) { 40 queue<LL> q; 41 for(LL i = 0; i < n; i++) dd[i] = inf; 42 dd[s] = 0; 43 q.push(s); 44 while(!q.empty()) { 45 LL u = q.front(); q.pop(); 46 for(LL i = dhead[u]; ~i; i=dedge[i].next) { 47 if(dd[dedge[i].v] > dd[u] + 1 && dedge[i].w > 0) { 48 dd[dedge[i].v] = dd[u] + 1; 49 if(dedge[i].v == t) return 1; 50 q.push(dedge[i].v); 51 } 52 } 53 } 54 return 0; 55 } 56 57 LL dinic(LL s, LL t, LL n) { 58 LL st[maxn], top; 59 LL u; 60 LL flow = 0; 61 while(bfs(s, t, n)) { 62 for(LL i = 0; i < n; i++) cur[i] = dhead[i]; 63 u = s, top = 0; 64 while(cur[s] != -1) { 65 if(u == t) { 66 LL tp = inf; 67 for(LL i = top-1; i >= 0; i--) { 68 tp = min(tp, dedge[st[i]].w); 69 } 70 flow += tp; 71 for(LL i = top - 1; i >= 0; i--) { 72 dedge[st[i]].w -= tp; 73 dedge[st[i]^1].w += tp; 74 if(dedge[st[i]].w == 0) top = i; 75 } 76 u = dedge[st[top]].u; 77 } 78 else if(cur[u] != -1 && dedge[cur[u]].w > 0 && dd[u] + 1 == dd[dedge[cur[u]].v]) { 79 st[top++] = cur[u]; 80 u = dedge[cur[u]].v; 81 } 82 else { 83 while(u != s && cur[u] == -1) { 84 u = dedge[st[--top]].u; 85 } 86 cur[u] = dedge[cur[u]].next; 87 } 88 } 89 } 90 return flow; 91 } 92 93 LL n, m; 94 95 signed main() { 96 // freopen("in.txt","r", stdin); 97 LL QAQ, u, v, w; 98 scanf("%I64d", &QAQ); 99 while(QAQ--) { 100 init(); 101 scanf("%I64d%I64d",&n,&m); 102 S = 0, T = n + 1, N = T + 1; 103 // adde(S, 0, inf); 104 for(LL i = 0; i < m; i++) { 105 scanf("%I64d%I64d%I64d",&u,&v,&w); 106 if(v == 0) { 107 adde(u, T, w); 108 // printf("%I64d %I64d %I64d\n", u, T, w); 109 } 110 else { 111 adde(u, v, w); 112 // printf("%I64d %I64d %I64d\n", u, v, w); 113 } 114 } 115 printf("%I64d\n",dinic(S, T, N)); 116 } 117 return 0; 118 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <iostream> 2 #include <cstdio> 3 #include <algorithm> 4 #include <vector> 5 #include <queue> 6 #include <stack> 7 #include <string> 8 #include <cstring> 9 #include <set> 10 11 using namespace std; 12 13 typedef pair<int, int> pii; 14 typedef pair<pii, int> piii; 15 typedef struct Edge { 16 int v, w, next; 17 }Edge; 18 const int maxn = 1111; 19 const int maxm = 100100; 20 const int inf = 0x7f7f7f7f; 21 int n, m; 22 int W[maxn]; 23 24 int dis[maxn]; 25 int ret[maxn]; 26 int ee; 27 int head[maxn]; 28 Edge e[maxm]; 29 priority_queue<piii, vector<piii>, greater<piii> > pq; 30 31 void init() { 32 ee = 0; 33 memset(head, -1, sizeof(head)); 34 } 35 36 void adde(int u, int v) { 37 e[ee].v = v, e[ee].w = 1, e[ee].next = head[u], head[u] = ee++; 38 e[ee].v = u, e[ee].w = 1, e[ee].next = head[v], head[v] = ee++; 39 } 40 41 signed main() { 42 // freopen("in.txt","r", stdin); 43 int T, u, v; 44 scanf("%d", &T); 45 while(T--) { 46 init(); 47 memset(ret, 0, sizeof(ret)); 48 memset(dis, 0x7f, sizeof(dis)); 49 scanf("%d%d",&n,&m); 50 for(int i = 1; i <= n; i++) { 51 scanf("%d", &W[i]); 52 } 53 for(int i = 0; i < m; i++) { 54 scanf("%d%d",&u,&v); 55 adde(u, v); 56 } 57 dis[1] = 0; 58 ret[1] = W[1]; 59 pq.push(piii(pii(0, W[1]), 1)); 60 while(!pq.empty()) { 61 piii tmp = pq.top(); pq.pop(); 62 int pdis = tmp.first.first; 63 int pw = tmp.first.second; 64 int u = tmp.second; 65 if(dis[u] < pdis) continue; 66 for(int i = head[u]; ~i; i=e[i].next) { 67 int v = e[i].v; 68 int w = e[i].w; 69 if(dis[v] > dis[u] + w) { 70 dis[v] = dis[u] + w; 71 ret[v] = ret[u] + W[v]; 72 pq.push(piii(pii(dis[v], ret[v]), v)); 73 } 74 else if(dis[v] == dis[u] + w) { 75 ret[v] = max(ret[v], ret[u]+W[v]); 76 pq.push(piii(pii(dis[v], ret[v]), v)); 77 } 78 } 79 } 80 printf("%d %d\n", dis[n], ret[n]); 81 } 82 return 0; 83 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <iostream> 2 #include <cstdio> 3 #include <algorithm> 4 #include <vector> 5 #include <queue> 6 #include <stack> 7 #include <string> 8 #include <cstring> 9 #include <set> 10 #include <cmath> 11 12 using namespace std; 13 14 const double eps = 1e-10; 15 const double PI = acos(-1.0); 16 17 double sgn(double x) { 18 if(fabs(x) < eps) return 0; 19 if(x < 0) return -1; 20 return 1; 21 } 22 23 typedef struct Point { 24 double x, y; 25 Point() {} 26 Point(double _x, double _y) { 27 x = double(_x), y = double(_y); 28 } 29 Point operator -(const Point& b) const { 30 return Point(x-b.x, y-b.y); 31 } 32 double operator ^(const Point& b) const { 33 return x * b.y - y * b.x; 34 } 35 }Point; 36 37 typedef struct Line { 38 Point s, e; 39 }Line; 40 41 bool inter(Line l1, Line l2) { 42 return 43 max(l1.s.x, l1.e.x) >= min(l2.s.x, l2.e.x) && 44 max(l2.s.x, l2.e.x) >= min(l1.s.x, l1.e.x) && 45 max(l1.s.y, l1.e.y) >= min(l2.s.y, l2.e.y) && 46 max(l2.s.y, l2.e.y) >= min(l1.s.y, l1.e.y) && 47 sgn((l2.s-l1.e)^(l1.s-l1.e)) * sgn((l2.e-l1.e)^(l1.s-l1.e)) <= 0 && 48 sgn((l1.s-l2.e)^(l2.s-l2.e)) * sgn((l1.e-l2.e)^(l2.s-l2.e)) <= 0; 49 } 50 51 const int maxn = 111111; 52 int n; 53 Line cross; 54 Point p[maxn]; 55 56 signed main() { 57 // freopen("in.txt","r", stdin); 58 int QAQ; 59 double a1, a2, b1, b2; 60 scanf("%d", &QAQ); 61 while(QAQ--) { 62 scanf("%d", &n); 63 cin >> cross.s.x >> cross.s.y >> cross.e.x >> cross.e.y; 64 for(int i = 1; i <= n; i++) { 65 cin >> a1 >> b1; 66 p[i] = Point(a1, b1); 67 } 68 int tot = 0; 69 Point sen = Point(0, 0); 70 for(int i = 1; i <= n; i++) { 71 Line tmp; 72 tmp.e = sen, tmp.s = p[i]; 73 if(inter(tmp, cross)) { 74 tot++; 75 } 76 } 77 cout << tot << endl; 78 } 79 return 0; 80 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <iostream> 2 #include <cstdio> 3 #include <algorithm> 4 #include <vector> 5 #include <queue> 6 #include <stack> 7 #include <string> 8 #include <cstring> 9 #include <set> 10 11 using namespace std; 12 13 const int maxn = 111; 14 int n; 15 string s[maxn]; 16 string aim; 17 set<string> ret; 18 19 signed main() { 20 // freopen("in.txt","r", stdin); 21 while(~scanf("%d", &n)) { 22 ret.clear(); 23 for(int i = 1; i <= n; i++) { 24 cin >> s[i]; 25 } 26 cin >> aim; 27 for(int i = 1; i <= n; i++) { 28 for(int j = 0; j < aim.length(); j++) { 29 bool flag = 0; 30 for(int k = 0; k < s[i].length(); k++) { 31 if(aim[j+k] != s[i][k]) { 32 flag = 1; 33 break; 34 } 35 } 36 if(!flag) { 37 ret.insert(s[i]); 38 } 39 } 40 } 41 // sort(ret.begin(), ret.end()); 42 int tot = 0; 43 for(set<string>::iterator it = ret.begin(); it != ret.end(); it++) { 44 cout << *it << " \n"[tot == ret.size()-1]; 45 tot++; 46 } 47 } 48 return 0; 49 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <iostream> 2 #include <cstdio> 3 #include <algorithm> 4 #include <vector> 5 #include <queue> 6 #include <stack> 7 #include <string> 8 #include <cstring> 9 10 using namespace std; 11 12 typedef pair<int, int> pii; 13 const int maxn = 1010; 14 const int dx[11] = {0, 0, 1, -1}; 15 const int dy[11] = {1, -1, 0, 0}; 16 int n, m; 17 int sx, sy; 18 int G[maxn][maxn]; 19 bool vis[maxn][maxn]; 20 21 bool ok(int x, int y) { 22 return x >= 1 && x <= n && y >= 1 && y <= m; 23 } 24 25 void bfs(int sx, int sy) { 26 memset(vis, 0, sizeof(vis)); 27 queue<pii> q; 28 vis[sx][sy] = 1; 29 q.push(pii(sx, sy)); 30 int tot = 1; 31 while(!q.empty()) { 32 pii tmp = q.front(); q.pop(); 33 for(int i = 0; i < 4; i++) { 34 int x = tmp.first + dx[i]; 35 int y = tmp.second + dy[i]; 36 if(!ok(x, y)) continue; 37 if(vis[x][y]) continue; 38 if(G[x][y] != 0) continue; 39 vis[x][y] = 1; 40 q.push(pii(x, y)); 41 tot++; 42 } 43 } 44 printf("%d\n", tot); 45 } 46 47 signed main() { 48 // freopen("in.txt","r", stdin); 49 int T; 50 scanf("%d", &T); 51 while(T--) { 52 scanf("%d%d",&n,&m); 53 for(int i = 1; i <= n; i++) { 54 for(int j = 1; j <= m; j++) { 55 scanf("%d", &G[i][j]); 56 if(G[i][j] == 2) { 57 sx = i, sy = j; 58 } 59 } 60 } 61 bfs(sx, sy); 62 } 63 return 0; 64 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <iostream> 2 #include <cstdio> 3 #include <algorithm> 4 #include <vector> 5 #include <queue> 6 #include <stack> 7 #include <string> 8 #include <cstring> 9 #include <set> 10 11 using namespace std; 12 13 typedef pair<int, int> pii; 14 const int maxn = 1111; 15 pii sen[maxn]; 16 int n, k; 17 18 signed main() { 19 // freopen("in.txt","r", stdin); 20 int T, a, b; 21 scanf("%d", &T); 22 while(T--) { 23 scanf("%d%d",&n,&k); 24 for(int i = 1; i <= n; i++) { 25 scanf("%d%d",&a,&b); 26 sen[i].first = b, sen[i].second = a; 27 } 28 sort(sen+1, sen+n+1); 29 int tot = 0; 30 for(int i = 1; i <= n; i++) { 31 while(sen[i].second && k >= sen[i].first) { 32 tot++; 33 k -= sen[i].first; 34 sen[i].second--; 35 } 36 } 37 printf("%d\n", tot); 38 } 39 return 0; 40 }