Codeforces Round #194 (Div. 1) / 333B Chips(贪心+代码优化)
来源:互联网 发布:同花顺mac版下载 编辑:程序博客网 时间:2024/05/01 09:51
Gerald plays the following game. He has a checkered field of size n × n cells, where m various cells are banned. Before the game, he has to put a few chips on some border (but not corner) board cells. Then for n - 1 minutes, Gerald every minute moves each chip into an adjacent cell. He moves each chip from its original edge to the opposite edge. Gerald loses in this game in each of the three cases:
- At least one of the chips at least once fell to the banned cell.
- At least once two chips were on the same cell.
- At least once two chips swapped in a minute (for example, if you stand two chips on two opposite border cells of a row with even length, this situation happens in the middle of the row).
In that case he loses and earns 0 points. When nothing like that happened, he wins and earns the number of points equal to the number of chips he managed to put on the board. Help Gerald earn the most points.
The first line contains two space-separated integers n and m (2 ≤ n ≤ 1000, 0 ≤ m ≤ 105) — the size of the field and the number of banned cells. Next m lines each contain two space-separated integers. Specifically, the i-th of these lines contains numbers xi and yi (1 ≤ xi, yi ≤ n) — the coordinates of the i-th banned cell. All given cells are distinct.
Consider the field rows numbered from top to bottom from 1 to n, and the columns — from left to right from 1 to n.
Print a single integer — the maximum points Gerald can earn in this game.
3 12 2
0
3 0
1
4 33 13 23 3
1
In the first test the answer equals zero as we can't put chips into the corner cells.
In the second sample we can place one chip into either cell (1, 2), or cell (3, 2), or cell (2, 1), or cell (2, 3). We cannot place two chips.
In the third sample we can only place one chip into either cell (2, 1), or cell (2, 4).
简单贪心,考虑相邻的两条边即可,详见代码。
完整代码:(未优化)
/*92ms,0KB*/#include<cstdio>bool isban[2][1000];int main(){int n, m, x, y, count = 0;scanf("%d%d", &n, &m);while (m--){scanf("%d%d", &x, &y);isban[0][x] = true;isban[1][y] = true; ///只统计相邻的两条边就行}for (int i = 2; i < n; ++i){if (!isban[0][i])count++;if (!isban[1][i])count++;}///在这种情况下两个chips(芯片/小木片)重合,上面多算了一次countif (n & 1 && !(isban[0][(n + 1) >> 1] || isban[1][(n + 1) >> 1]))count--;printf("%d", count);return 0;}
优化版:
/*62ms,0KB*/#include<cstdio>bool isban[2][1000];int main(){int n, m, x, y;scanf("%d%d", &n, &m);int count = (n << 1) - 4;isban[0][1]=true;isban[0][n]=true;isban[1][1]=true;isban[1][n]=true;while (m--){///只统计相邻的两条边就行scanf("%d%d", &x, &y);if (!isban[0][x]){--count;isban[0][x] = true;}if (!isban[1][y]){--count;isban[1][y] = true;}}///在这种情况下两个chips(芯片/小木片)重合,上面多算了一次countif (n & 1 && !(isban[0][(n + 1) >> 1] || isban[1][(n + 1) >> 1]))--count;printf("%d", count);return 0;}
- Codeforces Round #194 (Div. 1) / 333B Chips(贪心+代码优化)
- Codeforces Round #194 (Div. 2) D. Chips
- 贪心 CF 333B Chips
- Codeforces Round #310 (Div. 1) B. Case of Fugitive 贪心
- Codeforces Round #366 (Div. 1) B. Ant Man (贪心)
- Codeforces Round #380 Div. 1 B. Sea Battle(贪心)
- Codeforces Round #202 (Div. 2)B-贪心
- Codeforces Round #428 (Div. 2) B 贪心
- Codeforces Round #194 (Div. 1) / 333A Secrets(贪心)
- [SPFA的SLF优化] Codeforces Round #257 (Div. 1) B
- Codeforces Round #317 [AimFund Thanks-Round] (Div. 1) B. Minimization 贪心 dp
- Codeforces Beta Round #75 (Div. 2)---A. Chips
- CF 333 B.Chips CodeForces【伪容斥】
- Codeforces Round #109 (Div. 2) B (贪心)
- Codeforces Round #111 (Div. 2) B Unlucky Ticket (贪心)
- Codeforces Round #277.5 (Div. 2)---B. BerSU Ball (贪心)
- Codeforces Round #307 (Div. 2) B. ZgukistringZ (贪心 + 暴力)
- Codeforces Round #334 (Div. 2) B. More Cowbell (贪心)
- 每日一题(74) - 求二叉树中节点的最大距离
- C#文件夹路径
- 如何成为一个C++高级程序员
- RAC-DG搭建步骤及一些应用分析
- windows远程桌面访问ubuntu12.04
- Codeforces Round #194 (Div. 1) / 333B Chips(贪心+代码优化)
- struct inode 和 struct file
- 笔记、|语录
- hdu2952之搜索
- 提高网站(web应用)访问速度的几点建议(前端)
- 我的C++成长之路
- js实现鼠标绘制层拖拽特效
- UVAlive 6152 Bits Equalizer 解题报告
- jsp传递数据格式