codeforces 839A 428 Div2 B Game of the Rows

版权声明:本文原创如果喜欢,欢迎转载。^_^ https://blog.csdn.net/ccutyear/article/details/77175271

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


题意:每行有8个座位,其中1、2相邻,3、4相邻,4、5相邻(这一点是在题面中一眼看不出来的),5、6相邻,7、8相邻。现在有k支队伍,每支队伍有不同的人数,问是否能使队伍所有人都座下。同时,要求不同的队伍不能相邻。

思路:最简单直接的做法就是加上无数的特判,然后卡过。主要思路是注意特判时的优先级。在此,主要就是注意以下几组数据:

输入:

2 7

2 2 2 2 2
输出:

YES

输入:

1 4

2  2 1 2

输出:

YES

输入:

1 4

2 2 2 2

输出:

NO

AC代码:

#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>

#define MAX 10005
using namespace std;
int main( )
{
	int n,m;
	while(cin >> n >> m){
		int seat4 = n,seat2 = n*2,seat1 = 0;
		int need_seat4,need_seat2;
		for(int j = 0; j < m; j++){
			int a;
			cin >> a;
			while(a > 0){
				if(seat4 > 0 && a >= 3)
					seat4--,a -= 4;
				else if(seat2 > 0 && a >= 2)
					seat2--,a -= 2;
				else if(a == 1)
					seat1--,a--;
				else if(seat4 > 0)
					seat4--,a -= 2,seat1++;
				else seat1--,a--;
			}
		}
		if(seat4 >= 0 && seat4*2 + seat2 + seat1 >= 0)
			cout << "YES" << endl;
		else
			cout << "NO" << endl;
	}
	return 0; 
}

不过不用特判直接暴力的也有:

#include<iostream>
#include<cstring>

#define MAX 10005
using namespace std;
int num[MAX];
int c[5];
int main( )
{
	int n,m;
	while(cin >> n >> m){
		c[2] = n*2;
		c[4] = n;
		int a;
		memset(num,0,sizeof(num));
		for(int j = 0; j < m; j++) cin >> a,num[a]++;
		for(int j = MAX; j; j--){
			while(num[j]--){
				int k;
				for(k = 4; k; k--){
					if(c[k]){
						int t = min(j,k);
						num[j - t]++;
						c[k]--;
						if(k - t - 1 > 0) c[k - t - 1]++;
						break;
					}
				}
				if(!k){
					cout << "NO" << endl;
					goto skip; 
				}
			}
		}
		cout << "YES" << endl;
		skip:;
	}
}
不过在codeforces上还是能看到其他跟牛的代码(差距还是很大):

#include<iostream>
int i,n,k,a,s,j;
main(){
for(std::cin>>n>>k;i<k;i++)std::cin>>a,a%2?a++,j++:0,s+=a;
std::cout<<(s>8*n||s==8*n&&k==4*n&&j<n?"NO":"YES");
}

         这个B题还是比较坑的,在终测结束后。8000+ 人只有200+人通过。只能说这个B有毒。





猜你喜欢

转载自blog.csdn.net/ccutyear/article/details/77175271