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.

Sample test(s)
input
  1. 5
  2. 1 2 2 4 3
output
  1. 2
input
  1. 3
  2. 4 1 1
output
  1. 2
input
  1. 4
  2. 0 3 0 4
output
  1. 0

解题心得:

1、这是一个很烦躁的题,需要好好理一下思路,首先应该处理的是2,因为2上可到3,下可到1,所以先将1将2处理掉,移动一个就可以消去两个不合法要求,然后将2全部处理成3和1,2自身和自身组合,移动两个人就可以消去三个不合法要求,如果剩下了一个2则用4或1,如果剩下了一个1就用3。

2、上面说了这么多,不论说没说清楚,看没看懂,反正就是一个贪心的思想。

  1. #include<stdio.h>
  2. using namespace std;
  3. const int maxn = 1e6+10;
  4. int num[maxn];
  5. int num1,num2,num3,num4;
  6. long long step;
  7. int n;
  8.  
  9. void c1()
  10. {
  11. void c2();
  12. int now = num1 / 3;
  13. num3 += now;
  14. num1 = num1 % 3;
  15. step += now * 2;
  16. if(num4 == 0)
  17. {
  18. if(num3 > num1)
  19. {
  20. step += num1;
  21. num4 += num1;
  22. num3 -= num1;
  23. num1 = 0;
  24. }
  25. else if(num3 <= num1)
  26. {
  27. step += num3;
  28. num1 -= num3;
  29. num4 += num3;
  30. num3 = 0;
  31. }
  32. }
  33. if(num1 == 2)
  34. {
  35. step += 1;
  36. num2 += 1;
  37. num1 = 0;
  38. }
  39. c2();
  40. }
  41.  
  42. void c2()
  43. {
  44. if(num1 >= num2)
  45. {
  46. num1 -= num2;
  47. num3 += num2;
  48. step += num2;
  49. num2 = 0;
  50. }
  51. if(num1 < num2)
  52. {
  53. step += num1;
  54. num3 += num1;
  55. num2 -= num1;
  56. num1 = 0;
  57. }
  58. if(num2 != 0)
  59. {
  60. int now = num2*2 / 3;
  61. step += now;
  62. num3 += now;
  63. num2 = num2 * 2 % 3;
  64. if(num2 % 2)
  65. {
  66. num1 += 1;
  67. num2 = 0;
  68. }
  69. else if(num2 == 2)
  70. num2 = 1;
  71. }
  72. else if(num1 < num2 && (num1+num4)>= num2)
  73. {
  74. step += num2;
  75. num4 = num4 - num2 + num1;
  76. num1 = 0;
  77. num2 = 0;
  78. num3 += num2;
  79. }
  80. if((num1 + num4) < num2)
  81. {
  82. step += num1 + num4;
  83. num3 += num4 + num1;
  84. num2 -= num1 + num4;
  85. num1 = 0;
  86. num4 = 0;
  87. }
  88. }
  89.  
  90. void c3()
  91. {
  92. if(num1 == 0)
  93. {
  94. if(num2 == 0)
  95. {
  96. printf("%lld\n",step);
  97. return;
  98. }
  99. }
  100. mark:
  101. if(num1 == 1)
  102. {
  103. if(num2 == 1)
  104. {
  105. step += 1;
  106. printf("%lld\n",step);
  107. return;
  108. }
  109. if(num2 == 0)
  110. {
  111. if(num3 != 0)
  112. {
  113. step += 1;
  114. printf("%lld\n",step);
  115. return;
  116. }
  117. else if(num4 > 1)
  118. {
  119. step += 2;
  120. printf("%lld\n",step);
  121. return;
  122. }
  123. else
  124. {
  125. printf("-1\n");
  126. return ;
  127. }
  128. }
  129. }
  130. if(num1 == 2)
  131. {
  132. if(num2 == 1)
  133. {
  134. step += 1;
  135. num2 -- ;
  136. num1 --;
  137. }
  138. if(num3 >= 2)
  139. {
  140. step+=2;
  141. printf("%lld\n",step);
  142. return;
  143. }
  144. if(num3 == 1)
  145. {
  146. step += 1;
  147. num3 ++;
  148. num1 --;
  149. goto mark;
  150. }
  151. else if(num4 > 1)
  152. step += 2;
  153. else
  154. {
  155. printf("-1\n");
  156. return ;
  157. }
  158. }
  159.  
  160. if(num2 == 0)
  161. {
  162. printf("%lld\n",step);
  163. return;
  164. }
  165. else if(num2 == 1)
  166. {
  167. if(num4 != 0)
  168. {
  169. step++;
  170. printf("%lld\n",step);
  171. return;
  172. }
  173. else if(num3 > 1)
  174. {
  175. step += 2;
  176. printf("%lld\n",step);
  177. return;
  178. }
  179. else
  180. {
  181. printf("-1\n");
  182. return;
  183. }
  184. }
  185. }
  186. int main()
  187. {
  188. while(scanf("%d",&n)!=EOF)
  189. {
  190. num1 = num2 = num3 = step = 0;
  191. for(int i=0; i<n; i++)
  192. {
  193. scanf("%d",&num[i]);
  194. if(num[i] == 1)
  195. num1 ++;
  196. if(num[i] == 2)
  197. num2 ++;
  198. if(num[i] == 3)
  199. num3 ++;
  200. if(num[i] == 4)
  201. num4 ++;
  202. }
  203. c2();//处理2
  204. c1();//处理1
  205. c3();//处理3
  206. }
  207. }

BFS:CF356C-Compartments的更多相关文章

  1. 图的遍历(搜索)算法(深度优先算法DFS和广度优先算法BFS)

    图的遍历的定义: 从图的某个顶点出发访问遍图中所有顶点,且每个顶点仅被访问一次.(连通图与非连通图) 深度优先遍历(DFS): 1.访问指定的起始顶点: 2.若当前访问的顶点的邻接顶点有未被访问的,则 ...

  2. 【BZOJ-1656】The Grove 树木 BFS + 射线法

    1656: [Usaco2006 Jan] The Grove 树木 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 186  Solved: 118[Su ...

  3. POJ 3278 Catch That Cow(bfs)

    传送门 Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 80273   Accepted: 25 ...

  4. POJ 2251 Dungeon Master(3D迷宫 bfs)

    传送门 Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 28416   Accepted: 11 ...

  5. Sicily 1215: 脱离地牢(BFS)

    这道题按照题意直接BFS即可,主要要注意题意中的相遇是指两种情况:一种是同时到达同一格子,另一种是在移动时相遇,如Paris在(1,2),而Helen在(1,2),若下一步Paris到达(1,1),而 ...

  6. Sicily 1048: Inverso(BFS)

    题意是给出一个3*3的黑白网格,每点击其中一格就会使某些格子的颜色发生转变,求达到目标状态网格的操作.可用BFS搜索解答,用vector储存每次的操作 #include<bits/stdc++. ...

  7. Sicily 1444: Prime Path(BFS)

    题意为给出两个四位素数A.B,每次只能对A的某一位数字进行修改,使它成为另一个四位的素数,问最少经过多少操作,能使A变到B.可以直接进行BFS搜索 #include<bits/stdc++.h& ...

  8. Sicily 1051: 魔板(BFS+排重)

    相对1150题来说,这道题的N可能超过10,所以需要进行排重,即相同状态的魔板不要重复压倒队列里,这里我用map储存操作过的状态,也可以用康托编码来储存状态,这样时间缩短为0.03秒.关于康托展开可以 ...

  9. Sicily 1150: 简单魔板(BFS)

    此题可以使用BFS进行解答,使用8位的十进制数来储存魔板的状态,用BFS进行搜索即可 #include <bits/stdc++.h> using namespace std; int o ...

  10. ACM/ICPC 之 靠墙走-DFS+BFS(POJ3083)

    //POJ3083 //DFS求靠左墙(右墙)走的路径长+BFS求最短路 //Time:0Ms Memory:716K #include<iostream> #include<cst ...

随机推荐

  1. Json数组对象和对象数组

    Json的简单介绍 从结构上看,所有的数据最终都可以分成三种类型: 第一种类型是scalar(标量),也就是一个单独的string(字符串)或数字(numbers),比如“北京”这个单独的词. 第二种 ...

  2. Duplicate Emails

    Write a SQL query to find all duplicate emails in a table named Person. +----+---------+ | Id | Emai ...

  3. hibernate课程 初探单表映射3-1 hibernate单表操作简介

    本章简介: 1 单一主键 2 基本类型 3 对象类型 4 组件属性 5 单表操作CRUD实例

  4. js技巧-使用reduce实现更简洁的数组对象去重和数组扁平化

    Array.prototype.reduce()方法介绍: 感性认识reduce累加器: const arr = [1, 2, 3, 4]; const reducer = (accumulator, ...

  5. EPSG:4326

    简单说,"EPSG:4326"指的就是WGS84坐标系 参考 http://blog.csdn.net/cloverwindy/article/details/8663968 AU ...

  6. /etc/syslog.conf日志配置文件详解

    //将info或更高级别的消息送到/var/log/messages,除了mail以外. //其中*是通配符,代表任何设备:none表示不对任何级别的信息进行记录. *.info;mail.none; ...

  7. HDU汉诺塔系列

    这几天刷了杭电的汉诺塔一套,来写写题解. HDU1207 汉诺塔II HDU1995 汉诺塔V HDU1996 汉诺塔VI HDU1997 汉诺塔VII HDU2064 汉诺塔III HDU2077  ...

  8. spa 小程序的研发随笔 (1) --- 前言

    半年前跳槽, 新公司主要研发倾向于小程序的开发.由于之前并没有接触小程序,所以经过半年的实际开发,才敢来做一点笔记. 小程序提供很多组件给开发者使用,但是,实际使用中还是会有很多的问题. 小程序的组件 ...

  9. Extjs4.1+desktop+SSH2 搭建环境 项目能跑起来

    linux开发感觉可能就是日常办公的时候,用别的软件会有问题,java开发还是没什么区别的,换回window开发: push 它: 每次看到右上那红红的叉,我还以为又出错了: 这个项目用resin,下 ...

  10. sql常识

    1.UNION与UNION ALL的区别UNION去重且排序UNION ALL不去重不排序2.sql语句or与union all的执行效率比较:union all>union> in &g ...