版权声明:本文原创如果喜欢,欢迎转载。^_^ https://blog.csdn.net/ccutyear/article/details/77175271
思路:最简单直接的做法就是加上无数的特判,然后卡过。主要思路是注意特判时的优先级。在此,主要就是注意以下几组数据:
输入:
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有毒。