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 ...
随机推荐
- qemu 出现Could not access KVM kernel module: No such file or directory failed to initialize KVM: No such file or directory
使用qemu命令 qemu-system-x86_64 -hda image/ubuntu-test.img -cdrom ubuntu-16.04.2-server-amd64.iso -m 102 ...
- 安装flask-mysqldb的时候,提示 mysql_config not found 的解决方法
解决办法: sudo apt-get install libmysqlclient-dev sudo updatedb locate mysql_config 然后进入mysql_config的路径( ...
- Erlang 001--开篇
有段时间没有更新博客了,最近稍微接触了下一门相对小众的语言Erlang,个人感觉学习一段时间有必要总结总结,本文作为该系列的开篇,仅仅列举一些与Java的一些不同点和个人对Erlang的一些主观印象, ...
- 初始html(常用标签)
今天我们来学习Web前端的一些知识,这一阶段需要记忆的东西相对来说比较多,需要花时间记忆以及做好练习. 一.HTML初识 1.web服务本质 import socket def main(): soc ...
- 前端JS电商放大镜效果
前端JS电商放大镜效果: <!DOCTYPE html> <html lang="en"> <head> <meta charset=&q ...
- 基于python3.6的如何爬取百度地图
先前参考了其他的代码,大多数是python2.7写的,而3.6用的类库以及规则有了很大的变动,所以自己写了一个这样的代码,供给大家参考. def get_station(i): station=[] ...
- window.returnValue使用方法
returnValue是javascript中html的window对象的属性,目的是返回窗口值,当用window.showModalDialog函数打开一个IE的模式窗口(模式窗口知道吧,就是打开后 ...
- Openfire+spark在linux上搭建内部聊天系统
一. 实验环境 Ubuntu server14.04 openfire:http://www.igniterealtime.org/downloads/index.jsp spark:http: ...
- STL容器 成员函数 时间复杂度表
Sequence containers Associative containers Headers <vector> <deque> <list> <s ...
- raw_input功能
摘要: raw_input() & input() raw_input的功能是方便的从控制台读入数据. input与raw_input都是Python的内建函数,实现与用户的交互,但是功 ...