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 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}.
思路:因为中间的4个位置比较特殊,可以坐2+1,或者是4,或者是1+1(这个太浪费位置,下下策), 旁边的2个位置能坐2,或者1个人;
先统计这k个队伍的能分成几个4,几个2,几个1(我用x4,x2,x1表示),这里的1一定是要占2个位置的。
首先要保证n个位置够坐(x4*4+x2*2+x1*2<=8*n),然后要保证中间的4连座(共有n个)不能是2+2的情况,每个4人座一定要坐的有1,或者是坐的有4,即x1+x4>=n。(让4和1先去坐中间)
但还有一个样例
2 7
2 2 2 2 2 2 2 答案:YES
如果没有8*n - (x4*4+x2*2+x1*2)>0 那么我就有多余的位置了,那么我就可以让一个2变成两个1,然后再判断x1+x4>=n;
AC代码:
#include <stdio.h>
#include <iostream>
#include <string>
#include <string.h>
using namespace std;
int main()
{
int n,k;
scanf("%d%d",&n,&k);
int ans1=0,ans2=0,ans4=0,a;
while(k--)
{
scanf("%d",&a);
if(a>=4)
{
ans4+=a/4;
a=a-a/4*4;
}
if(a>=2)
{
ans2++;
a-=2;
}
if(a) ans1++;
}
int x=n*8-ans4*4-ans1*2-ans2*2;
if(x>=0&&ans4+ans1>=n-x)
printf("YES\n");
else
printf("NO\n");
}