Codeforces Round #428 (Div. 2) Game of the Rows (思维)

来源:互联网 发布:百度云总显示网络异常 编辑:程序博客网 时间:2024/05/22 20:39

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


这道题很有意思,以至于rank前几名都重判出错。其实细想一下,假若队伍有1或2个人,她得占2个人的位置,假若有3个或4个人,占4个人的位置...7个人或8个人,占8个人的位置。我们的前提是1~2个人的队伍优先坐边上,3~4个人的队伍优先坐中间,这样显然最优。这道题的迷点就在于4个两个人的队伍不能坐在一起,所以来想一下, 三个2一个以1可以坐在一排,两个2一个4,两个2一个3都可以坐在一排,所以我们只需要看看队伍人数为1,3,4的能不能跟2都搭配起来。

有一点需要注意,坐座位的过程中还可能会产生2的人的队伍,比如6人的队伍,中间坐了4个之后会剩下一个2,这个2就必须当成两人的队伍,再比如5个人的队伍会剩下1个人的队伍。

具体代码里说:


#include<cstdio>#include<iostream>#include<algorithm>#include<queue>#include<stack>#include<cstring>#include<string>#include<vector>#include<cmath> #include<map>using namespace std;typedef long long ll;const int maxn = 1e5+5;const int ff = 0x3f3f3f3f;int n;int k;int a[maxn];//记录队伍人数int num[4];//记录某种队伍的个数int main(){scanf("%d %d",&n,&k);for(int i = 1;i<= k;i++)scanf("%d",&a[i]);int sum = n*4;//总的双人座位数for(int i = 1;i<= k;i++)if(a[i] == 2)num[2]++;else if(a[i] == 1)num[1]++;else{num[3]+= a[i]/4;num[a[i]%4]++;//这里用来记录做完中间4个的大座后新产生的几人队伍}for(int i = 1;i<= k;i++){sum-= ((a[i]+1)/2);//一个队伍一个队伍的入座if(sum< 0)break;}if(sum< 0)//不够了直接NOcout<<"NO"<<endl;else if(sum> 0||num[1]*3+num[3]*2>= num[2])//够并且能搭配起来就YEScout<<"YES"<<endl;elsecout<<"NO"<<endl;//搭配不起来NOreturn 0;}

更多精写文章

阅读全文
2 0
原创粉丝点击