CodeFroces 839B Game of the Rows(模拟)
来源:互联网 发布:2017好用的网络硬盘 编辑:程序博客网 时间:2024/06/17 13:17
这道题坑点还是有点烦的。完全是面向数据编程了。。。
解法:
核心就是最大化利用每个座位,所以尽量塞满。包括下面引入一个single,有1就肯定扔进去。
我是首先把每个部队里面的人数都处理到4一下(这个好处理,就是尽量塞满中间四人座,没有四人座就塞两边,这样保证塞的一定是满的)
然后将每个部队剩下的人数排序,从大到小往里面塞。
如果中间还有完全空闲的行
如果部队有三人,那就行数++。(放中间 或者是 两个二人座 是等效的)
如果有两人,那么就可以用一个single标记一下只可以塞一个人的个数++,行数++。(2已经是最多的部队了,所以最终还是要用中间一排的)
如果有一人,如果有single,最好肯定是用一个single(这样肯定是满的),如果没有single那就相当于坐了一个二人座,并且多出多一行二人座。(1已经是人数最多的部队了,那么最终还是要用中间一排的)
如果只剩下左边右边二人座了。
部队人数大于等于二,就用一个二人座。(这样肯定是满的)
如果有剩下一人,有single就用single(肯定是满座)如果没有,那就用二人座(都只剩下一个人的部队了,所以必须用二人座了,否则就不存在合理情况)
最后,已知每个部队还有多少人了,肯定没有一个的了,如果有三个的那就不可能产生single,所以也没有三个人的部队。只可能剩下两个的,此时连二人座都没有了,只能找single,判断一下single够不够用即可。
一直看看是否成立即可。
代码如下:
#include<bits/stdc++.h> using namespace std;int main() {int n, k, left = 0, mid = 0, right = 0;int a[105] = {0};scanf("%d%d", &n, &k);int lt = n, single = 0;for(int i = 0; i < k; i++) {scanf("%d", &a[i]);}int ans = 0;for(int i = 0; i < k; i++) {int tmp = min(a[i] / 4, n - mid);a[i] -= tmp * 4;mid += tmp;while(a[i] >= 4) {if(left < lt) {a[i] -= 2;left++;} else if(right < n) {a[i] -= 2;right++;} else {ans = 1;}}}if(!ans) {sort(a, a + k);for(int i = k - 1; i >= 0; i--) {if(a[i] <= 0)break;if(mid < n) {if(a[i] == 1 && single)single--;else if(a[i] == 3)mid++;else if(a[i] == 2)mid++, single++;else if(a[i] == 1)mid++, lt++;a[i] = 0;} else {if(a[i] == 1 && single)single--, a[i] -= 1;else if(left < lt) {left++, a[i] -= 2;} else if(right < n){right++, a[i] -= 2;}if(a[i] <= 0)continue; if(a[i] == 1 && single)single--, a[i] -= 1;else if(left < lt) {left++, a[i] -= 2;} else if(right < n){right++, a[i] -= 2;}}}for(int i = 0; i < k; i++) {if(a[i] > 0)single -= a[i];}if(single < 0)ans = 1;}if(ans)cout << "NO" << '\n';elsecout << "YES" << '\n';return 0;}
阅读全文
0 0
- CodeFroces 839B Game of the Rows(模拟)
- codeforces 839 B Game of the Rows
- Codeforces 839B-Game of the Rows
- Codeforces 839B Game of the Rows (贪心)
- 839B Game of the Rows(水,思维,贪心)
- Codeforces 839 B. Game of the Rows (贪心)
- Codeforce 839 -- B Game of the Rows 【思维 + 贪心】
- Codeforce-839B Game of the Rows 思维
- Codeforces 839B Game of the Rows 贪心
- codeforces 839A 428 Div2 B Game of the Rows
- codeforces 839B Game of the Rows(思路题)
- CodeForces-839B Game of the Rows(思维、细节)
- codeforces 839b 之 Game of the Rows
- codeforces#839 B Game of the Rows 思维,贪心
- Game of the Rows codeforces 839B(贪心)
- B. Game of the Rows(贪心)
- Codeforces Round #428 (Div. 2) B.Game of the Rows(贪心+炒鸡多坑的模拟)
- Codeforces Round #428 (Div. 2)-贪心&模拟-B. Game of the Rows
- zypper(OpenSuse中的软件在线安装工具)的使用及suse 11.0 最新的中国源
- 敌兵布阵 HDU
- 根据作业浅析正则表达式
- BZOJ4276: [ONTAK2015]Bajtman i Okrągły Robin
- suse zypper
- CodeFroces 839B Game of the Rows(模拟)
- 8.19(军训DAY 3)
- 华为机试-坐标移动
- Linux上面安装oracle11g的全过程
- 带环链表返回起始节点
- 通过ssh的公钥/私钥方式远程登录linux
- spring之如何创建bean(1、自动装配)
- FileDescriptor使用
- 换博客