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

解题心得:

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

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

#include<stdio.h>
using namespace std;
const int maxn = 1e6+10;
int num[maxn];
int num1,num2,num3,num4;
long long step;
int n; void c1()
{
void c2();
int now = num1 / 3;
num3 += now;
num1 = num1 % 3;
step += now * 2;
if(num4 == 0)
{
if(num3 > num1)
{
step += num1;
num4 += num1;
num3 -= num1;
num1 = 0;
}
else if(num3 <= num1)
{
step += num3;
num1 -= num3;
num4 += num3;
num3 = 0;
}
}
if(num1 == 2)
{
step += 1;
num2 += 1;
num1 = 0;
}
c2();
} void c2()
{
if(num1 >= num2)
{
num1 -= num2;
num3 += num2;
step += num2;
num2 = 0;
}
if(num1 < num2)
{
step += num1;
num3 += num1;
num2 -= num1;
num1 = 0;
}
if(num2 != 0)
{
int now = num2*2 / 3;
step += now;
num3 += now;
num2 = num2 * 2 % 3;
if(num2 % 2)
{
num1 += 1;
num2 = 0;
}
else if(num2 == 2)
num2 = 1;
}
else if(num1 < num2 && (num1+num4)>= num2)
{
step += num2;
num4 = num4 - num2 + num1;
num1 = 0;
num2 = 0;
num3 += num2;
}
if((num1 + num4) < num2)
{
step += num1 + num4;
num3 += num4 + num1;
num2 -= num1 + num4;
num1 = 0;
num4 = 0;
}
} void c3()
{
if(num1 == 0)
{
if(num2 == 0)
{
printf("%lld\n",step);
return;
}
}
mark:
if(num1 == 1)
{
if(num2 == 1)
{
step += 1;
printf("%lld\n",step);
return;
}
if(num2 == 0)
{
if(num3 != 0)
{
step += 1;
printf("%lld\n",step);
return;
}
else if(num4 > 1)
{
step += 2;
printf("%lld\n",step);
return;
}
else
{
printf("-1\n");
return ;
}
}
}
if(num1 == 2)
{
if(num2 == 1)
{
step += 1;
num2 -- ;
num1 --;
}
if(num3 >= 2)
{
step+=2;
printf("%lld\n",step);
return;
}
if(num3 == 1)
{
step += 1;
num3 ++;
num1 --;
goto mark;
}
else if(num4 > 1)
step += 2;
else
{
printf("-1\n");
return ;
}
} if(num2 == 0)
{
printf("%lld\n",step);
return;
}
else if(num2 == 1)
{
if(num4 != 0)
{
step++;
printf("%lld\n",step);
return;
}
else if(num3 > 1)
{
step += 2;
printf("%lld\n",step);
return;
}
else
{
printf("-1\n");
return;
}
}
}
int main()
{
while(scanf("%d",&n)!=EOF)
{
num1 = num2 = num3 = step = 0;
for(int i=0; i<n; i++)
{
scanf("%d",&num[i]);
if(num[i] == 1)
num1 ++;
if(num[i] == 2)
num2 ++;
if(num[i] == 3)
num3 ++;
if(num[i] == 4)
num4 ++;
}
c2();//处理2
c1();//处理1
c3();//处理3
}
}

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. NopI 导出数据

    protected void exportAward(DataSet dsResult) { if (dsResult != null) { string fileName = System.Web. ...

  2. C 碎片一 计算机知识

    一.计算机知识 1, 计算机组成及工作原理 计算机是硬件和软件的结合体.硬件由主机箱和外部设备组成,主机主要包括CPU.内存.主板.硬盘.光驱.各种扩展卡.连接线.电源等:外部设备包括鼠标.键盘等.软 ...

  3. java.sql.SQLException: Zero date value prohibited 报错分析

    今天在使用 iReview 复习词条时,发现 review 页面始终不会跳到下一个词条,应该是前台或者后台出现 BUG 了. 查看浏览器控制台,看到 500 报错,那应该是后台的问题. 登录后台,先查 ...

  4. java8 api简介(一)

    from https://www.aliyun.com/jiaocheng/785076.html 摘要:函数式编程详解:前言:现在有很多公司都用了jdk8,但是函数式编程也许没有用上,jdk8也提供 ...

  5. html便民查询各个工具类实例代码分享(支持pc和移动端)

    1.手机号码查询 <iframe id="api_iframe_51240" name="api_iframe_51240" src="&quo ...

  6. 网页游戏中PK系统的实现

    在游戏开发过程中,写过一个简单的PK系统面板,涉及到前端和后端的交互,我将自己制作的流程分享给大家,大概流程是这样:前端发送PK邀请给后端,后端受到请求后将信息返回给前端处理,先来看下整个流程图及思路 ...

  7. sweetalert 1.0多次回调函数bug

    一个删除功能,原来的实现方式(注释部分)有多次的回调,会出现第二个swal窗口不显示,回调函数体不执行的情况.后来的解决方式是使用bootstrap的modal模态框,删除成功后显示模态框,模态框关闭 ...

  8. 无法通过CTRL+空格及SHIFT+CTRL调出输入法的解决方案

    打开任务管理器: 运行:CTFMON.EXE

  9. 在vue-cli中引入图片不能正常显示

    我们用vue-cli构建项目的时候,图片的地址是后台的,可是在template中item.img放到src中是不能正常显示的为什么? 原因是:url-loader无法解析js动态生成的路径. 解决: ...

  10. html body上有一条空白!!!

    html body 上莫名其妙的就出现了一条空白,怎么搞都搞不定,弄了一下午...... 解决了!!! 格式问题/