感觉这两个题很像,解法相同。
Restaurant Tables
题意:给顾客安排座位,如果顾客是一个人,就安排一个位置的,如果没有就安排两个座位的,但只占一个位置。顾客是两个人就安排两个位置。
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define ll long long
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
const int Max=100;
int main()
{
int n,num1,num2,k=0;
int x,ans=0;
while(scanf("%d%d%d",&n,&num1,&num2)!=EOF)
{
k=0,ans=0;
for(int i=0; i<n; i++)
{
scanf("%d",&x);
if(x==1)
{
if(num1)
{
num1--;
}
else if(num2)
{
num2--;
k++;
}
else if(k)
{
k--;
}
else ans+=1;
}
else
{
if(num2)
{
num2--;
}
else ans+=2;
}
}
printf("%d\n",ans);
}
return 0;
}
Game of the Rows
题意:给队伍安排座位,有2个座的和4个座的。不同的队伍不能安排到一起。
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<cmath>
#define mem(a,b) memset(a,b,sizeof(a))
const int Max=1e5;
const int INF=0x3f3f3f3f;
using namespace std;
int a[Max];
int main()
{
int m,n,i;
while(scanf("%d%d",&m,&n)!=EOF)
{
int sum=0;
int sum1=m*8;
int k=0,flag=0;
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
sum+=a[i];
}
if(k>m||sum>sum1)
{
printf("NO\n");
continue;
}
int x1=m;
int x2=m*2;
k=0;
sort(a,a+n);
for(i=n-1;i>=0;i--)
{
while(a[i]>0)
{
if(x1)
{
a[i]-=4;
x1--;
if(a[i]==-3)
x2++;
if(a[i]==-2)
k++;
}
else if(x2){
a[i]-=2;
x2--;
}
else if(k)
{
a[i]-=1;
k--;
}
else {
flag=1;
break;
}
}
if(flag==1)
break;
}
if(flag==1)
printf("NO\n");
else printf("YES\n");
}
return 0;
}