题目
B. Game of the Rows
time limit per test:1 second
memory limit per test:256 megabytes
input:standard input
output:standard output
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 n 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}.
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.
Input
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.
Output
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.
分别将每组输入按照4、2、1的人数划分,得x4、x2、x1,x1需要占座两个,所以若总人数sum+x1>n*8时则不成立,输出NO,否则,x4+x1>=n时成立(因为此时3、4可放x2,5空,6放x1;或者x1在中间间隔放开;因为此时sum+x1<=n*8,所以一定可以放下所有人),输出YES,否则,用x2填满n*2+n-x4,剩下部分拆成一个人后填满n-x4-x1,若有人剩余,则不成立,输出NO,否则输出YES
4、5也为相邻座位
代码如下:
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
const int MAXN=10010;
int main()
{
int n,k;
while(scanf("%d%d",&n,&k)!=EOF)
{
int x1=0,x2=0,x4=0;
int sum=0;
for(int i=0;iint num;
scanf("%d",&num);
sum+=num;
x4+=num/4;
num%=4;
x2+=num/2;
x1+=num%2;
}
if(sum+x1>n*8)
{
cout<<"NO"<continue;
}
if(x4+x1>=n)
{
cout<<"YES"<continue;
}
int temp=n-x1-x4;
if(x2<=n-x4+n*2)
{
cout<<"YES"<continue;
}
x2-=n-x4+n*2;
x2*=2;
if(temp>=x2)
{
cout<<"YES"<continue;
}
else
{
cout<<"NO"<continue;
}
}
return 0;
}