*C. Compartments----贪心
来源:互联网 发布:厦门长庚医院网络挂号 编辑:程序博客网 时间:2024/05/17 08:35
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.
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.
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.
51 2 2 4 3
2
34 1 1
2
40 3 0 4
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;}
- *C. Compartments----贪心
- Codeforces Round #207 (Div. 1) C. Compartments (贪心)
- codeforce 365 C. Compartments(gready,4级)
- BFS:CF356C-Compartments
- c 贪心
- CodeForces 275C 贪心
- CF 332 C 贪心
- 【贪心】Codefoces 486C
- CF 508C///贪心
- C语言贪心算法
- 【C】贪心算法
- codeforces C. Bits 贪心
- codeforces C. Hometask(贪心)
- 二分贪心专题C
- Codeforces 449C 贪心
- CF 387C 贪心
- C. Fountains 贪心
- C. Naming Company 贪心
- Linux安装tigase7.1
- 电脑系统的重装
- 【华为机试】合唱队
- c++ protected
- Java虚拟机内存的堆区(heap),栈区(stack)和静态区(static/method)
- *C. Compartments----贪心
- 面对对象小结
- 总有你想知道的---Sikuli(1)
- 自定义适配器
- Raytracing 参考资料
- 百练noi 1775:采药
- 第二十四天正则表达式
- python爬虫进阶(二):动态网页爬取
- 如何在windows上将git diff配置成使用Beyond Compare