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 has nrows, 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
Copy
2 2
5 8
output
Copy
YES
input
Copy
1 2
7 1
output
Copy
NO
input
Copy
1 2
4 4
output
Copy
YES
input
Copy
1 4
2 2 1 2
output
Copy
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层,每一层的布局如图,给出k个队伍,每个队伍ai个人,不同的队伍不能紧挨着,问是否这n层可以呈纳足够的人。
思路:先分给中间那4块,然后再分左右的2块,再处理1的情况。
统计a[i]/4有多少个,用s4(n)不断减去。
然后s2(2*n,因为每一层有两个两块的),s2+s4,加上4剩余的那部分,然后统计a[i]/2有多少个,用s2不断减去。
ai也紧跟着处理,减去已经布置好的位置。
最后用s1=s2+s4,因为因为占用的2可以旁边放一个1,所以要加上s4,不断减去ai,最后判断。
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int maxn=100+66;
const ll mod=1e9+7;
int n,m;
int a[maxn];
int main()
{
scanf("%d %d",&n,&m);
for(int i=1;i<=m;i++)
{
scanf("%d",&a[i]);
}
int s4=n;
for(int i=1;i<=m;i++)
{
int num=min(s4,a[i]/4);
s4-=num;
a[i]=a[i]-num*4;
}
int s2=2*n+s4;
for(int i=1;i<=m;i++)
{
int num=min(s2,a[i]/2);
s2-=num;
a[i]=a[i]-num*2;
}
int s1=s2+s4;
for(int i=1;i<=m;i++)
{
s1-=a[i];
}
if(s1>=0)printf("YES\n");
else printf("NO\n");
return 0;
}