BFS:CF356C-Compartments
1 second
256 megabytes
standard input
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.
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.
5
1 2 2 4 3
2
3
4 1 1
2
4
0 3 0 4
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的更多相关文章
- 图的遍历(搜索)算法(深度优先算法DFS和广度优先算法BFS)
图的遍历的定义: 从图的某个顶点出发访问遍图中所有顶点,且每个顶点仅被访问一次.(连通图与非连通图) 深度优先遍历(DFS): 1.访问指定的起始顶点: 2.若当前访问的顶点的邻接顶点有未被访问的,则 ...
- 【BZOJ-1656】The Grove 树木 BFS + 射线法
1656: [Usaco2006 Jan] The Grove 树木 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 186 Solved: 118[Su ...
- POJ 3278 Catch That Cow(bfs)
传送门 Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 80273 Accepted: 25 ...
- POJ 2251 Dungeon Master(3D迷宫 bfs)
传送门 Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 28416 Accepted: 11 ...
- Sicily 1215: 脱离地牢(BFS)
这道题按照题意直接BFS即可,主要要注意题意中的相遇是指两种情况:一种是同时到达同一格子,另一种是在移动时相遇,如Paris在(1,2),而Helen在(1,2),若下一步Paris到达(1,1),而 ...
- Sicily 1048: Inverso(BFS)
题意是给出一个3*3的黑白网格,每点击其中一格就会使某些格子的颜色发生转变,求达到目标状态网格的操作.可用BFS搜索解答,用vector储存每次的操作 #include<bits/stdc++. ...
- Sicily 1444: Prime Path(BFS)
题意为给出两个四位素数A.B,每次只能对A的某一位数字进行修改,使它成为另一个四位的素数,问最少经过多少操作,能使A变到B.可以直接进行BFS搜索 #include<bits/stdc++.h& ...
- Sicily 1051: 魔板(BFS+排重)
相对1150题来说,这道题的N可能超过10,所以需要进行排重,即相同状态的魔板不要重复压倒队列里,这里我用map储存操作过的状态,也可以用康托编码来储存状态,这样时间缩短为0.03秒.关于康托展开可以 ...
- Sicily 1150: 简单魔板(BFS)
此题可以使用BFS进行解答,使用8位的十进制数来储存魔板的状态,用BFS进行搜索即可 #include <bits/stdc++.h> using namespace std; int o ...
- ACM/ICPC 之 靠墙走-DFS+BFS(POJ3083)
//POJ3083 //DFS求靠左墙(右墙)走的路径长+BFS求最短路 //Time:0Ms Memory:716K #include<iostream> #include<cst ...
随机推荐
- angularjs $state.go页面不刷新数据
假如进入market/beian/add添加数据,保存提交后回退market/beian列表页,没有自动更新数据,必须得手动下拉刷新才会出来 $state.go("marketBeian&q ...
- 报错:Program "sh" not found in PATH
参考原文:http://vin-mail.blog.163.com/blog/static/37895280201211932919513/ 如果你按照我的方法 先配置了cygwin的环境变量,在打开 ...
- php编译安装过程中遇到问题
编译安装PHP时遇到的问题 问题1: configure: error: xml2-config not found. Please check your libxml2 installation. ...
- jqGrid中multiselect: true 操作checkbox
在jqGrid中设置multiselect: true可以实现全选的操作,但怎么设置被选中的checkbox里面的值呢,做法如下:jQuery("#listTable").jqGr ...
- YII2 定义页面提示
控制器里面这样写: 单条消息: 键值是规定好的,不要去自定义哦! \Yii::$app->getSession()->setFlash('error', 'This is the mess ...
- js中arr.sort的用法
sort(sortfunction)为JS的数组对象(Array)的一个方法,提供排序功能 参数 sortFunction 为可选项,是用来确定排序原则的js函数, 这个函数有两个参数,分别代表每次排 ...
- 【mydigitallife.info】如何禁用Aero窗口自动最大化
Go to Control Panel. Click on Ease of Access link or Ease of Access Center icon. Select Change how y ...
- 解决Postgresql服务启动又关闭的问题
查看日志发现如下错误消息:%t LOG: could not receive data from client: An operation was attempted on something tha ...
- java中利用JOptionPane类弹出消息框的部分例子
转: http://www.cnblogs.com/wangxiuheng/p/4449917.html http://blog.csdn.net/penjie0418/article/details ...
- Bootstrap历练实例:响应式标签页
<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...