Game of the Rows CodeForces

来源:互联网 发布:安卓源码之家 编辑:程序博客网 时间:2024/05/16 02:41

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 ≤ 100001 ≤ 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 aidenotes 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.

Example
Input
2 25 8
Output
YES
Input
1 27 1
Output
NO
Input
1 24 4
Output
YES
Input
1 42 2 1 2
Output
YES
Note

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).


比较坑的一道CF题目,重点是贪心的方法,代码不是很重要;

贪心策略:

1:先安排中间的四个座位,然后剩下的两侧的座位就可以随便坐了。

2:中间的座位先安排一组的人坐一起,就是a[i]>=4的组;

3:a[i]==1的组在中间安排的时候竖着排,到n之后再竖着排。

4:比较坑的是安排中间四个座位的时候a[i]=2的情况,两组横着,一组竖着。

解释不清了,自己理解吧。

用这个题做个纪念,重要的还是自己解得过程。

#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>using namespace std;int a[110];int main(){int n,k;cin >> n >> k;int sum = 0;int f1 = 1;int ok = 1;for(int i=1; i<=k; i++){   cin >> a[i];    if(a[i]>=4 && f1)   {    sum += a[i] - a[i]%4;    if(sum > n*4) {   sum -= a[i] - a[i]%4;   int w = a[i]/4;   for(int k=1; k<=w; k++)   {      if(sum + 4 > n*4)break;  sum += 4; a[i] -= 4;    }   f1 = 0; continue; }  a[i] = a[i]%4;    }    }    int sum1 = sum;    int sum2 = sum;    if(sum == n*4)    {       for(int i=1; i<=k; i++)  {    sum1 += a[i] - a[i]%2; if(a[i]%2==1) sum1 += 2;  }   if(sum1 > 8*n) ok = 0;    }    else    {       int yu = n - sum/4;   for(int i=1; i<=k; i++)       {         if(yu<=0)break;         if(a[i]==3) {sum2 += 4; a[i]=0; yu--;}   }   if(yu > 0)   { int yi = 0; for(int i=1; i<=k; i++)    {      if(a[i]==1) {yi++;}     }     sum2 += yi*2;     int mmp = 0;     if(yi >= yu*2)     {      yu = 0;     yi -= yu*2; }     else if(yi < yu*2)     {       if(yi<=yu)       {        mmp = yi; yu -= yi; yi = 0;   }   else if(yi>yu)   {       mmp = yu-yi+yu;yu = 0;   } } int er = 0; for(int i=1; i<=k; i++) { if(a[i]==2) ++er; } //cout << er << endl; /*for(int i=1; i<=k; i++)     {     if(a[i]!=2)continue;if(yu>0)     {     sum2 += 4; yu--;}else sum2 += 2; }*/ if(mmp>=er) { sum2 += er*2; } else if(mmp < er) { sum2 += mmp*2; er -= mmp; if(yu > 0) {       int pw = er/3*2;   if(yu>=pw)   {    sum2 += er/3*8;    er = 0;   }   else if(yu<pw)   {     sum2 += yu/2*8;     er -= yu/2*3;     yu = yu%2;     sum2 += yu*4;     er--;     sum2 += er*2;   }  }else if(yu==0) { sum2 += er*2; } }   }   else if(yu==0) // 四连坐没有了    {     for(int i=1; i<=k; i++)     {     if(a[i]==3){sum2 += 4;continue;} if(a[i]!=0) sum2 += 2;}   }    if(sum2 > 8*n )ok = 0;}    //cout << sum2 << endl;if(!ok) cout << "NO" << endl;else cout << "YES\n" << endl;  return 0;} 
水波.