题目链接http://codeforces.com/problemset/problem/828/A
In a small restaurant there are a tables for one person and b tables for two persons.
It it known that n groups of people come today, each consisting of one or two people.
If a group consist of one person, it is seated at a vacant one-seater table. If there are none of them, it is seated at a vacant two-seater table. If there are none of them, it is seated at a two-seater table occupied by single person. If there are still none of them, the restaurant denies service to this group.
If a group consist of two people, it is seated at a vacant two-seater table. If there are none of them, the restaurant denies service to this group.
You are given a chronological order of groups coming. You are to determine the total number of people the restaurant denies service to.
The first line contains three integers n, a and b (1 ≤ n ≤ 2·105, 1 ≤ a, b ≤ 2·105) — the number of groups coming to the restaurant, the number of one-seater and the number of two-seater tables.
The second line contains a sequence of integers t1, t2, ..., tn (1 ≤ ti ≤ 2) — the description of clients in chronological order. If ti is equal to one, then the i-th group consists of one person, otherwise the i-th group consists of two people.
Print the total number of people the restaurant denies service to.
4 1 2 1 2 1 1
0
4 1 1 1 1 2 1
2
一个饭馆内有两种餐桌:单人桌和双人桌。如果来1个客人,就坐单人桌,如果没有单人桌,就坐双人桌(这个双人桌可以已有一个人坐),如果连双人桌也没有,就拒绝接待该客人。如果同时来2个客人,就坐双人桌,如果没有双人桌,就是不能接待这两位客人。问在两种餐桌数量和当天顾客数量一定的情况下,当天拒绝多少位客人。
解题思路:
就是考察逻辑思维的,不断使用if else 组合。需要注意的是:如果来了一位客人,则优先坐单人桌(也就是如果没有单人桌的情况下,才选择和 只有一位客人的双人桌 拼桌)。其它的都是考察逻辑思维的,认真思考就能做出来。
代码:(不提倡看本人的代码,因为比较乱,理解题意后自己写)
#include<iostream>
using namespace std;
int main()
{
int n,a,b,w[200005],z,len,s2,sum,zz;
while(cin>>n>>a>>b)
{
zz=0;
s2=0;
sum=0;
len=n;
for(int i=0;i<=len-1;i++)
cin>>w[i];
for(int i=0;i<=len-1;i++)
{
if(w[i]==1)
{
if(a>0)
{
a--;
n--;
}
else
{
if(b>0)
{
b--;
s2++;
n--;
}
else
{
if(s2>0)
{
s2--;
n--;
}
else
{
z=i;
break;
}
}
}
}
else
{
if(b>0)
{
b--;
n=n-2;
}
else
sum=sum+2;
}
}
if(zz==1)
continue;
if(z!=0)
{
for(int i=z;i<=len-1;i++)
sum=sum+w[i];
}
cout<<sum<<endl;
}
return 0;
}