*C. Compartments----贪心

来源:互联网 发布:厦门长庚医院网络挂号 编辑:程序博客网 时间:2024/05/17 08:35

C. Compartments
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

A team of students from the city S is sent to the All-Berland Olympiad in Informatics. Traditionally, they go on the train. All students have bought tickets in one carriage, consisting of n compartments (each compartment has exactly four people). We know that if one compartment contain one or two students, then they get bored, and if one compartment contain three or four students, then the compartment has fun throughout the entire trip.

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

The first line contains integer 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 ≤ 4). It is guaranteed that at least one student is riding in the train.

Output

If no sequence of swapping seats with other people leads to the desired result, print number "-1" (without the quotes). In another case, print the smallest number of people you need to persuade to swap places.

Examples
input
51 2 2 4 3
output
2
input
34 1 1
output
2
input
40 3 0 4
output
0

题目链接:http://codeforces.com/contest/356/problem/C


http://blog.csdn.net/cowboy90/article/details/12978245

题解是看懂了,但是为什么我没有想到这个题用贪心做,或者说为什么贪心的话这样贪心,还是需要加深这个题的理解,加星,回看。

代码:

#include <cstdio>#include <string>#include <iostream>using namespace std;int main(){    int n,i,ans;    scanf("%d",&n);    int a[5]= {0};    ans=0;    while(n--){        scanf("%d",&i);        a[i]++;    }    if(a[1]>=a[2]){        ans+=a[2];        a[1]-=ans;        a[2]-=ans;        a[3]+=ans;        int k=a[1]/3;        ans+=k*2;        a[3]+=k;        if(a[1]%3==0)            printf("%d\n",ans);        else if(a[1]%3==1){            if(a[3]>0)                printf("%d\n",ans+1);            else{                if(a[4]>1)                    printf("%d\n",ans+2);                else                 printf("-1\n");            }        }        else if(a[1]%3==2){            if(a[4]>0)                printf("%d\n",ans+2);            else{                if(a[3]>1)                    printf("%d\n",ans+2);                else                     printf("-1\n");            }        }    }    else{        ans+=a[1];        a[2]-=ans;        a[3]+=ans;        int k=a[2]/3;        ans+=k*2;        a[3]+=k*2;        if(a[2]%3==0)            printf("%d\n",ans);        else if(a[2]%3==1){            if(a[4]>0)                printf("%d\n",ans+1);            else if(a[3]>1)                printf("%d\n",ans+2);            else                printf("-1\n");        }        else            printf("%d\n",ans+2);    }    return 0;}