0
点赞
收藏
分享

微信扫一扫

codeforce 365 C. Compartments(gready,4级)





C. Compartments



time limit per test



memory limit per test



input



output



n

The students want to swap with other people, so that no compartment with students had bored students. To swap places with another person, you need to convince him that it is really necessary. The students can not independently find the necessary arguments, so they asked a sympathetic conductor for help. The conductor can use her life experience to persuade any passenger to switch places with some student.

However, the conductor does not want to waste time persuading the wrong people, so she wants to know what is the minimum number of people necessary to persuade her to change places with the students. Your task is to find the number.

After all the swaps each compartment should either have no student left, or have a company of three or four students.



Input



n (1 ≤ n ≤ 106) — the number of compartments in the carriage. The second line contains n integersa1, a2, ..., an showing how many students ride in each compartment (0 ≤ ai). It is guaranteed that at least one student is riding in the train.



Output



-1" (without the quotes). In another case, print the smallest number of people you need to persuade to swap places.



Sample test(s)



input



5 1 2 2 4 3



output



2



input



3 4 1 1



output



2



input



4 0 3 0 4



output



0







思路:贪心;见码

#include<cstdio>
#include<cstring>
#include<iostream>
#include<cmath>
#include<queue>
#include<algorithm>
#define FOR(i,a,b) for(int i=a;i<=b;++i)
#define clr(f,z) memset(f,z,sizeof(f))
using namespace std;
int f[6],ans;
void out()
{
printf("%d 1:%d 2:%d 3:%d 4:%d\n",ans,f[1],f[2],f[3],f[4]);
}
int main()
{
int n;
while(~scanf("%d",&n))
{
int x;clr(f,0);
FOR(i,1,n)
{
scanf("%d",&x);
++f[x];
}
ans=0;
///1->2;
int zmin=min(f[2],f[1]);
f[3]+=zmin;ans+=zmin;
f[2]-=zmin;f[1]-=zmin;
///2 self
f[3]+=2*(f[2]/3);ans+=2*(f[2]/3);
f[2]%=3;
// out();
// out();
///1->1 to 3
f[3]+=f[1]/3;ans+=2*(f[1]/3);
f[1]%=3;

if(f[1]<=f[3]){ans+=f[1];f[1]=0;}///1->3
else f[2]+=f[1]/2,ans+=f[1]/2,f[1]%=2; ///1->1 2

if(f[1])///left 1
{
if(f[1]<=f[3])ans+=f[1];///1->3
else if(f[4]>=f[1]*2)ans+=f[1]*2;///4->1
else ans=-1;///impossble
}
else if(f[2])
{
if(f[2]==2)ans+=2;///2->2 4
else if(f[2]==1)
{
if(f[4])ans++;///4->2 3,3
else if(f[3]>=2)ans+=2;///2->3 4
else ans=-1;
}
}
printf("%d\n",ans);
}
}




举报

相关推荐

0 条评论