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 ...
随机推荐
- android 开发-spinner下拉框控件的实现
Android提供实现下拉框功能的非常实用的控件Spinner. spinner控件需要向xml资源文件中添加spinner标签,如下: <Spinner android:id="@+ ...
- @b.windows.last.use
@b.windows.last.use @b.windows.first.use be_true 一般用在step文件中
- KendoUI 自定义验证:
Html: <label>@LogicNameAttribute.GetLogicName(typeof(Reward).GetProperty("ExtraRewardMone ...
- JAVA反射练习
JAVA反射练习 题目 实现一个方法 public static Object execute(String className, String methodName, Object args[]) ...
- form表单上传域(type="file")的使用----上传文件
一,单个文件的上传 1.html/jsp页面 <%@ page language="java" contentType="text/html; charset=UT ...
- 构建第一个spring boot2.0应用之项目启动运行的几种方式(二)
方法一. 配置Run/Debug Configuration 选择Main Class为项目 Application启动类(入口main方法) (2).进行项目目录,即包含pom.xml的目录下,启 ...
- SQL中如何避免书签查找
1.使用聚集索引 对于聚集索引,索引的叶子页面和表的数据页面相同.因此,当读取聚集索引键列的值时,数据引擎可以读取其他列的值而不需要任何导航.例如前面的区间数据查询的操作,SQLServer通过B树结 ...
- ubuntu常见错误
ubuntu常见错误--Could not get lock /var/lib/dpkg/lock解决 ubuntu常见错误--Could not get lock /var/lib/dpkg/loc ...
- 使用selenium的方式获取网页中图片的链接和网页的链接,来判断是否是死链(二)
上一篇使用Java正则表达式来判断和获取图片的链接以及跳转的网址,这篇使用selenium的自带的API(getAttribute)来获取网页中指定的内容 实现内容:获取下面所有图片的链接地址以及跳转 ...
- iOS keychain注解
+ (NSMutableDictionary *)getKeychainQuery:(NSString *)service { return [NSMutableDictionary dictiona ...