*C. Compartments----贪心

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
5
1 2 2 4 3
output
2
input
3
4 1 1
output
2
input
4
0 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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值