codeforces 839B Game of the Rows(思路题)

来源:互联网 发布:三星s7解网络锁工具 编辑:程序博客网 时间:2024/05/16 02:49

题目链接

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

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

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


题意:现在有k个队,飞机有n排座位,每排能做8个人,不同队伍的人不能坐相邻的位置。相邻情况有5种(1,2)(3,4)(4,5)(5,6)(7,8)这n排座位是否够坐。(k个队的总人数小于8*n)

思路:因为中间的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");}









原创粉丝点击