B. Game of the Rows

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

Examples
input
2 2
5 8
output
YES
input
1 2
7 1
output
NO
input
1 2
4 4
output
YES
input
1 4
2 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).

题意:飞机有N排,每排八个座位 12  3456   78,要求不同的队伍不能相邻.

题解:

很坑的一道题目,因为考虑到放在中间的也可以放在两边,而放在两边的不一定能放在中间。所以我们先尽量往中间放。

我们考虑将每一个数分解成4,2,1 的形式,然后先把4往中间放,如果中间放不下了就把4拆成两个2。

然后放2,如果放不下了就把2拆成两个1。

最后判断一下有没有放完就可以了。

 

#include<iostream>
#include<cstdio>
using namespace std;
int n,m,four,two,one,ans1,ans2;
int main()
{
    int i,j;
    scanf("%d%d",&m,&n);
    ans1=m;ans2=m*2;
    for(i=1;i<=n;i++)
    {
        scanf("%d",&j);
        four+=j/4;j%=4;
        two+=j/2;j%=2;
        one+=j;
    }
    while(four>0&&ans1>0)ans1--,four--;
    two+=four*2;four=0;
    int ans=ans1+ans2;
    while(two>0&&ans>0)ans--,two--;
    ans+=ans1;
    one+=two*2;two=0;
    while(one>0&&ans>0)ans--,one--;
    if(two||four||one)printf("NO\n");
    else printf("YES\n");
    return 0;
}

 

 

 

posted @ 2017-08-19 08:17  kakakakakaka  阅读(304)  评论(0编辑  收藏  举报

Never forget why you start

//鼠标爆炸特效