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}.
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.
InputThe 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
思路 因为要分开坐并且坐的越多越好,所以就需要减少浪费。为了减少浪费就需要这样:①如果有四个以上的人就把四个人放四人座去(在四人座有的情况下)没有四人座就用二人座补。如果是三个人的话也可以往四人座放②有两个人的话就往二人座放 这样可以减少座位的浪费 ③如果二人座不够的话就可以选择把四人座进行拆分即一个四人座可以变一个单人座和一个双人座。④如果是单人的情况优先往 四人座拆分出的单人座放 没有的话再去拆二人座,二人座也没有就接着拆四人座。按照这样的规则让程序自己执行一下 最后判定一下是否能够满足就行了。
我写的代码比较差所以有点长
#include<stdio.h>
#include<algorithm>
using namespace std;
bool cmp(int x,int y)
{
return x>y;
}
int main()
{
int n,k;
while(~scanf("%d%d",&n,&k))
{
int a[100];
for(int i=0;i<k;i++)
scanf("%d",&a[i]);
sort(a,a+k,cmp);
int s1=2*n,s2=n,s3=0,s4=0;
for(int i=0;i<k;i++)
{
while(a[i]>=3)
{
if(s2>0)
{
s2--;
a[i]-=4;
}
else
{
break;
}
}
while(a[i]>1)
{
if(s1>0)
{
a[i]-=2;
s1--;
}
else
{
break;
}
}
}
if(s1>0||s2>0)
{
for(int i=0;i<k;i++)
{
if(a[i]<=0)
continue;
if(a[i]==1)
{
if(s2>0)
{
s2--;
s3++;
a[i]-=1;
}
else if(s1>0)
{
s1--;
a[i]-=1;
}
}
if(a[i]==2)
{
if(s1>0)
{
s1--;
a[i]-=2;
}
else
{
s2--;
s4++;
a[i]-=2;
}
}
}
for(int i=0;i<k;i++) //s1 2 s2 4 s3后2 s4 后1
{
if(a[i]<=0)
continue;
if(a[i]==2)
{
if(s1>0)
{
s1--;
a[i]-=2;
}
else if(s3>0)
{
s3--;
a[i]-=2;
}
}
if(a[i]==1)
{
if(s4>0)
{
s4--;
a[i]-=1;
}
else if(s1>0)
{
s1--;
a[i]-=1;
}
else if(s3>0)
{
s3--;
a[i]-=1;
}
}
}
}
int f=1;
for(int i=0;i<k;i++)
{
if(a[i]>0)
{
f=0;
break;
}
}
if(f==1)
printf("YES\n");
else
printf("NO\n");
}
}