codeforces 723 A,B,C,D
来源:互联网 发布:淘宝怎么搜索关键词 编辑:程序博客网 时间:2024/04/29 20:05
A - The New Year: Meeting Friends
水题,中位数。。。
#include <cstdio>#include <iostream>#include <algorithm>using namespace std;int main(){ int a,b,c; while(scanf("%d%d%d", &a, &b, &c) != EOF){ int ans1 = max(max(a, b), c); int ans2 = min(min(a, b), c); printf("%d\n", ans1 - ans2); } return 0;}
B - Text Document Analysis
模拟,细节问题注意一下即可,尤其是到了字符串结束的时候还需要进判断。
#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>using namespace std;const int maxn = 300;char s[maxn];int main(){ int ans1, ans2, cnt, flag, n; while(scanf("%d", &n) != EOF){ ans1 = ans2 = cnt = flag = 0; scanf("%s", s); for(int i = 0; i < n; i++){ if(s[i] == '_'){ if(!flag) ans1 = max(cnt, ans1); else{ if(cnt){ // printf("i = %d\n", i); ans2++; } } cnt = 0; } else if(s[i] == '('){ ans1 = max(cnt, ans1); cnt = 0; flag = 1; } else if(s[i] == ')'){ if(cnt){ // printf("i = %d\n", i); ans2++; } flag = 0; cnt = 0; } else cnt++; } ans1 = max(ans1, cnt); printf("%d %d\n", ans1, ans2); } return 0;}
C - Polycarp at the Radio
题意较难理解。其实(1,m)出现的最大次数应该为(n/m),这样才能保证使bj的最小值最大,然后模拟处理一下字符串即可。
#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>using namespace std;const int maxn = 2005;int a[maxn], b[maxn], vis[maxn];int main(){ int n,m; while(scanf("%d%d", &n, &m) != EOF){ memset(vis, 0, sizeof(vis)); int cnt = n/m; for(int i = 0; i < n; i++){ scanf("%d", &a[i]); if(a[i] <= m && a[i] >= 1) vis[a[i]]++; } int ans = 0; for(int i = 1; i <= m; i++){ if(vis[i] < cnt) ans += (cnt - vis[i]); } printf("%d %d\n", cnt, ans); int i, flag = 1; for(i = 0; i < n; i++){ while(vis[flag] >= cnt && flag <= m) flag++; if(flag > m) break; if(a[i] >= 1 && a[i] <= m){ // printf("vis[%d] = %d, flag = %d\n", a[i], vis[a[i]], flag); if(vis[a[i]] > cnt){ vis[a[i]]--; vis[flag]++; b[i] = flag; } else b[i] = a[i]; } else{ vis[flag]++; b[i] = flag; } } for(; i < n; i++) b[i] = a[i]; int fi = 0; for(int i = 0; i < n; i++){ if(!fi) printf("%d", b[i]), fi = 1; else printf(" %d", b[i]); } printf("\n"); } return 0;}
D - Lakes in Berland
bfs求连通块
#include <queue>#include <cstdio>#include <vector>#include <cstring>#include <iostream>#include <algorithm>using namespace std;const int maxn = 55;char maze[maxn][maxn];int n, m, k, vis[maxn][maxn];bool flag;int dir[4][2] = {{-1,0},{1,0},{0,-1},{0,1}};struct Node{ int x, y; Node(){} Node(int a,int b):x(a), y(b){}};struct loc{ int x, y, val; loc(int a,int b,int c):x(a), y(b), val(c){} bool operator < (const loc temp)const{ return val < temp.val; }};vector <loc> vec;int judge(int x,int y){ if(x == 0 || x == n-1 || y == 0 || y == m-1) return false; return true;}int bfs1(int x,int y){ flag = true; int ans = 1; queue <Node> que; que.push(Node(x, y)); flag = flag & judge(x, y); vis[x][y] = 1; while(!que.empty()){ Node cur = que.front(); que.pop(); for(int i = 0; i < 4; i++){ int xx = cur.x + dir[i][0]; int yy = cur.y + dir[i][1]; if(xx < 0 || xx >= n || yy < 0 || yy >= m || vis[xx][yy]) continue; if(maze[xx][yy] == '.'){ flag = flag&judge(xx, yy); ans++; vis[xx][yy] = 1; que.push(Node(xx, yy)); } } } return ans;}void bfs2(int x,int y){ queue <Node> q; memset(vis, 0, sizeof(vis)); q.push(Node(x, y)); maze[x][y] = '*'; vis[x][y] = 1; while(!q.empty()){ Node cur = q.front(); q.pop(); for(int i = 0; i < 4; i++){ int xx = cur.x + dir[i][0]; int yy = cur.y + dir[i][1]; if(xx < 0 || xx >= n || yy < 0 || yy >= m || vis[xx][yy]) continue; if(maze[xx][yy] == '.'){ maze[xx][yy] = '*'; vis[xx][yy] = 1; q.push(Node(xx, yy)); } } }}void solve(){ memset(vis, 0, sizeof(vis)); vec.clear(); for(int i = 0; i < n; i++){ for(int j = 0; j < m; j++){ if(maze[i][j] == '.' && !vis[i][j]){ int ans = bfs1(i, j); if(flag) vec.push_back(loc(i, j, ans)); } } } sort(vec.begin(), vec.end()); // for(int i = 0; i < vec.size(); i++) printf(" %d", vec[i]); int ret = 0; for(int i = 0; i < vec.size()-k; i++){ ret += vec[i].val; bfs2(vec[i].x, vec[i].y); } printf("%d\n", ret); for(int i = 0; i < n; i++) printf("%s\n", maze[i]);}int main(){ while(scanf("%d%d%d", &n, &m, &k) != EOF){ for(int i = 0; i < n; i++) scanf("%s", maze[i]); solve(); } return 0;}
0 0
- codeforces 723 A,B,C,D
- codeforces 239 (div2) A B C D
- codeforces 208(div2) A+B+C+D
- codeforces 445 A、B、C、D
- codeforces 250 div2 A B C D
- CodeForces 412(A,B,C,D,E)
- Codeforces Round #351 A B C D
- Codeforces Round #377 A.B.C.D
- codeforces 817 A B C D
- Codeforces Educational Round #18(Codeforces 792 A B C D)
- Codeforces-Educational Codeforces Round 32-(A,B,C,D)
- Codeforces Round #179 (Div. 2)A、B、C、D
- Codeforces Round #186 (Div. 2)A、B、C、D、E
- Codeforces Round #198 (Div. 1)(A,B,C,D)
- Codeforces Round #202 (Div. 2) (A、B、C、D)
- Codeforces Round #211 (Div. 2)(A,B,C,D)
- Codeforces Round #220 (Div. 2)(A,B,C,D)
- Codeforces Round#229 DIV2 A,B,C,D
- Leetcode 120. Triangle 三角形问题(动态规划经典) 解题报告
- hash的基本原理
- 【字符串】字符串的全排列
- 代码访问mysql的时候,提示访问的表不存在,具体原因在这里
- java程序员面试笔试题(六)
- codeforces 723 A,B,C,D
- 【DP 训练】Alibaba, ACM/ICPC SEERC 2004, UVa1632
- 2016年四川省TI杯电子设计竞赛B题
- 浙大 PAT b1041
- CSS3 media queries + jQuery实现响应式导航方法步骤详解
- linux的网络模型---epoll
- 浙大 PAT b1042
- Java编程思想 第十八章 Java I/O系统
- 矩阵迹的性质