Codeforces 839B Game of the Rows - 贪心
Daenerys Targaryen has an army consisting of k groups of soldiers, the i-th group contains ai soldiers. She wants to bring her army to the other side of the sea to get the Iron Throne. She has recently bought an airplane to carry her army through the sea. The airplane has nrows, each of them has 8 seats. We call two seats neighbor, if they are in the same row and in seats {1, 2}, {3, 4}, {4, 5}, {5, 6} or {7, 8}.
A row in the airplane
Daenerys Targaryen wants to place her army in the plane so that there are no two soldiers from different groups sitting on neighboring seats.
Your task is to determine if there is a possible arranging of her army in the airplane such that the condition above is satisfied.
The first line contains two integers n and k (1 ≤ n ≤ 10000, 1 ≤ k ≤ 100) — the number of rows and the number of groups of soldiers, respectively.
The second line contains k integers a1, a2, a3, ..., ak (1 ≤ ai ≤ 10000), where ai denotes the number of soldiers in the i-th group.
It is guaranteed that a1 + a2 + ... + ak ≤ 8·n.
If we can place the soldiers in the airplane print "YES" (without quotes). Otherwise print "NO" (without quotes).
You can choose the case (lower or upper) for each letter arbitrary.
2 2
5 8
YES
1 2
7 1
NO
1 2
4 4
YES
1 4
2 2 1 2
YES
In the first sample, Daenerys can place the soldiers like in the figure below:

In the second sample, there is no way to place the soldiers in the plane since the second group soldier will always have a seat neighboring to someone from the first group.
In the third example Daenerys can place the first group on seats (1, 2, 7, 8), and the second group an all the remaining seats.
In the fourth example she can place the first two groups on seats (1, 2) and (7, 8), the third group on seats (3), and the fourth group on seats (5, 6).
题目大意 (题目太长简(长)洁(了),无法概述)
因为4个挨在一起的位置很复杂,所以考虑先把所有4个位置挨在一起的座位干掉。
当存在某个队还没被安排的人数大于等于4,那么就扔一个这样的座位给他们,如果不存在这样的座位,那就分配一个两个座的座位。如果分配出了问题,直接输出NO吧。
然后考虑剩余人数为3的队伍,要么安排4座,要么安排1个两座剩1人(余下来的一人等会儿考虑,并加入暂时无法安排的人)。
此时,所有队伍剩余人数不超过2人。
继续考虑剩余人数为2的队伍,如果存在4座,那么旁边就会多出可以坐1人的座位,否则就安排坐2座,如果没有2座,就把暂时无法安排的人数加2。
最后所有剩下的座位显然都是要1个人1个人地坐了,所以计算一下,判断还可以坐的人数是否大于等于暂时无法安排的人数。
Code
/**
* Codeforces
* Problem#839B
* Accepted
* Time: 15ms
* Memory: 2100k
*/
#include <bits/stdc++.h>
using namespace std; int n, k;
int rest[], cnt[]; inline void init() {
scanf("%d%d", &n, &k);
rest[] = n << , rest[] = n;
for(int i = , x; i <= k; i++) {
scanf("%d", &x);
while(x > ) {
if(rest[])
rest[]--, x -= ;
else if(rest[])
rest[]--, x -= ;
else {
puts("NO");
exit();
}
}
if(x)
cnt[x]++;
}
} inline void solve() {
while(cnt[]--) {
if(rest[])
rest[]--, rest[]++;
else if(rest[])
rest[]--;
else
cnt[] += ;
}
puts((rest[] * + rest[] + rest[] >= cnt[]) ? ("YES") : ("NO"));
} int main() {
init();
solve();
return ;
}
Codeforces 839B Game of the Rows - 贪心的更多相关文章
- CodeForces 839B - Game of the Rows | Codeforces Round #428 (Div. 2)
血崩- - /* CodeForces 839B - Game of the Rows [ 贪心,分类讨论] | Codeforces Round #428 (Div. 2) 注意 2 7 2 2 2 ...
- Codeforces 839B - Game of the Rows
839B - Game of the Rows 思路:先放4个的,然后再放2个的,最后再放1个的. 代码: #include<bits/stdc++.h> using namespace ...
- Codeforces 839B Game of the Rows【贪心】
B. Game of the Rows time limit per test:1 second memory limit per test:256 megabytes input:standard ...
- Codeforces 437C The Child and Toy(贪心)
题目连接:Codeforces 437C The Child and Toy 贪心,每条绳子都是须要割断的,那就先割断最大值相应的那部分周围的绳子. #include <iostream> ...
- Codeforces Round #546 (Div. 2) D 贪心 + 思维
https://codeforces.com/contest/1136/problem/D 贪心 + 思维 题意 你面前有一个队列,加上你有n个人(n<=3e5),有m(m<=个交换法则, ...
- 【Codeforces 738D】Sea Battle(贪心)
http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...
- Codeforces Educational Codeforces Round 3 C. Load Balancing 贪心
C. Load Balancing 题目连接: http://www.codeforces.com/contest/609/problem/C Description In the school co ...
- Codeforces Testing Round #12 B. Restaurant 贪心
B. Restaurant Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/597/problem ...
- Codeforces 437D The Child and Zoo(贪心+并查集)
题目链接:Codeforces 437D The Child and Zoo 题目大意:小孩子去參观动物园,动物园分非常多个区,每一个区有若干种动物,拥有的动物种数作为该区的权值.然后有m条路,每条路 ...
随机推荐
- shiro loginUrl拦截无效
logUrl不拦截 或者 只跳转到/login.jsp 不跳到自己设置登录链接 在springmvc或事务那里 开启spring的显示代理(即cglib),并将shiro的安全管理器交给spring管 ...
- Linux 重启nginx
重启 1.验证nginx配置文件是否正确 方法一:进入nginx安装目录sbin下,输入命令./nginx -t 看到如下显示nginx.conf syntax is ok nginx.conf te ...
- 第四章 HTML5概述
HTML5概述1.HTML5优势:解决跨浏览器问题:部分代替原来的js更明确地语义支持:不再单纯使用div增强WEB应用程序地功能:拖拽API等 2.HTML5语法改变标签不再区分大小写元素可以省略结 ...
- C语言---选择结构和循环结构
C语言的两种选择语句,1) if语句; 2) switch语句; 在if判断语句中,一般使用关系表达式. 关系运算符: <.<=.>.>=.==.!= 关系表达式:用关系运算符 ...
- QString和char*互转
1. QString转为char * // QString转QByteArray QByteArray sr = strQ.toLocal8Bit(); int len = sr.length(); ...
- [12]Windows内核情景分析 --- MDI
Mdl意为'内存映射描述符'.'缓冲描述符',一个mdl就代表一个缓冲.(任意一块物理内存,可以同时映射到用户地址空间和系统地址空间的) 设备IO方式分为三种:缓冲方式.直接IO方式.直接方式 缓冲方 ...
- VM虚拟机配置固定IP
linux下vmware桥接模式.静态ip上外网的配置 http://blog.csdn.net/zdh_139/article/details/73456654 虚拟机网络改成桥接模式 vi /et ...
- jQuery事件--keypress([[data],fn])和trigger(type,[data])
keypress([[data],fn]) 概述 当键盘或按钮被按下时,发生 keypress 事件 keypress 事件与 keydown 事件类似.当按钮被按下时,会发生该事件.它发生在当前获得 ...
- SQL提交数据三种类型
在数据库的插入.删除和修改操作时,只有当事务在提交到数据库时才算完成. SQL语句提交数据有三种类型:显式提交.隐式提交及自动提交. [1]显式提交 显式提交.即用COMMIT命令直接完成的提交方式. ...
- 只打开一次浏览器,生成html测试报告<小紧张中......>
from selenium import webdriverimport unittestimport time class Blog(unittest.TestCase): "" ...