Codeforces Round #428 (Div. 2) Game of the Rows (思维)
来源:互联网 发布:百度云总显示网络异常 编辑:程序博客网 时间:2024/05/22 20:39
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 hasn rows, 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}.
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 25 8
YES
1 27 1
NO
1 24 4
YES
1 42 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).
这道题很有意思,以至于rank前几名都重判出错。其实细想一下,假若队伍有1或2个人,她得占2个人的位置,假若有3个或4个人,占4个人的位置...7个人或8个人,占8个人的位置。我们的前提是1~2个人的队伍优先坐边上,3~4个人的队伍优先坐中间,这样显然最优。这道题的迷点就在于4个两个人的队伍不能坐在一起,所以来想一下, 三个2一个以1可以坐在一排,两个2一个4,两个2一个3都可以坐在一排,所以我们只需要看看队伍人数为1,3,4的能不能跟2都搭配起来。
有一点需要注意,坐座位的过程中还可能会产生2的人的队伍,比如6人的队伍,中间坐了4个之后会剩下一个2,这个2就必须当成两人的队伍,再比如5个人的队伍会剩下1个人的队伍。
具体代码里说:
#include<cstdio>#include<iostream>#include<algorithm>#include<queue>#include<stack>#include<cstring>#include<string>#include<vector>#include<cmath> #include<map>using namespace std;typedef long long ll;const int maxn = 1e5+5;const int ff = 0x3f3f3f3f;int n;int k;int a[maxn];//记录队伍人数int num[4];//记录某种队伍的个数int main(){scanf("%d %d",&n,&k);for(int i = 1;i<= k;i++)scanf("%d",&a[i]);int sum = n*4;//总的双人座位数for(int i = 1;i<= k;i++)if(a[i] == 2)num[2]++;else if(a[i] == 1)num[1]++;else{num[3]+= a[i]/4;num[a[i]%4]++;//这里用来记录做完中间4个的大座后新产生的几人队伍}for(int i = 1;i<= k;i++){sum-= ((a[i]+1)/2);//一个队伍一个队伍的入座if(sum< 0)break;}if(sum< 0)//不够了直接NOcout<<"NO"<<endl;else if(sum> 0||num[1]*3+num[3]*2>= num[2])//够并且能搭配起来就YEScout<<"YES"<<endl;elsecout<<"NO"<<endl;//搭配不起来NOreturn 0;}
更多精写文章
- Codeforces Round #428 (Div. 2) Game of the Rows (思维)
- Codeforces Round #428 (Div. 2) 839B. Game of the Rows(思维+贪心)
- Codeforces Round #428 (Div. 2) 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
- codeforces #428 Div.2 B. Game of the Rows 贪心、思维
- Codeforces 428(div 2)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(贪心+炒鸡多坑的模拟)
- Codeforces Round #428 (Div. 2) B. Game of the Rows(贪心)
- Codeforces Round #428 (Div. 2) B. Game of the Rows(贪心,补题)
- CF —— Codeforces Round #428 (Div. 2) B. Game of the Rows
- Codeforces Round #428 (Div. 2)-贪心&模拟-B. Game of the Rows
- CodeForces-839B Game of the Rows(思维、细节)
- codeforces#839 B Game of the Rows 思维,贪心
- Codeforces Round #426 (Div. 2)The Meaningless Game(思维+二分)
- Codeforces Round #426 (Div. 2)The Meaningless Game(思维+二分)
- Codeforces Round #426 (Div. 2)The Meaningless Game(思维+二分)
- websocket:如何建立在同一台client和同一台server创建多个连接
- Bellman_ford 求最短路模板
- 工作中小细节总结(三)
- Spring AOP---切面编程基础(动态代理)
- MySQL学习笔记——20170811
- Codeforces Round #428 (Div. 2) Game of the Rows (思维)
- Java基础总结-基本语法概念-1
- LeetCode: 66. Plus One
- C/C++指针错误与调试相关学习总结
- Linux命令:pwd,切换用户,切换宿主目录,.,..,-,相对路径绝对路径
- Linux下编程求绝对路径和读取目录
- js显示动态时间的方法
- 活了这么久,突然感觉像白开水
- HttpClient4.5.3设置代理(Proxy)访问目标地址