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. Teradata 认证系列 - 1. TCPP这是个啥

    一看历史,好几年没发帖...正好最近在自学teradata认证(学也不一定学的完,最后也不一定去考,仅仅安慰一下不想碌碌无为的内心) 网上一搜,百度上的中文相关资料简直为0.这个不奇怪,毕竟都没什么人 ...

  2. 从零开始的全栈工程师——js篇2.10(对象与构造函数)

    对象与构造函数 一.js数据类型 基本数据类型:string   undefined   null  boolean  number 引用数据类型  Object  array  function 二 ...

  3. Python开发环境Wing IDE如何调试进程异常报告

    Wing IDE的调试器所报告的任何异常,都会在调试器以外的任何代码运行事件中展示出来. 通过使用Debug工具或者是Debug菜单中的Start / Continue继续调试过程的异常检测. Win ...

  4. C# 执行可执行文件

    可以用C#脚本执行可执行文件,一般可以用C# IO流写出.bat脚本,然后顺带执行脚本,然后滑稽.三连... Process proc = null; try { proc = new Process ...

  5. javascript设计模式之外观模式

    /* * 外观模式 * 外观模式的主要意义在于简化类的接口,使其易于调用 */ // 你常常在不经意中使用了外观模式,尤其类库中更多(处理兼容性问题) var addEvent = function ...

  6. Vultr VPS建站攻略 – 一键安装LNMP无面板高性能WEB环境

    在"Vultr VPS建站攻略 - 一键安装宝塔面板架设LNMP/LAMP Web环境"文章中,VULTR中文网分享到我们常用的可视化面板宝塔面板安装在VULTR VPS主机中建站 ...

  7. COGS 1944. 背驮式行走

    ★   输入文件:piggyback.in   输出文件:piggyback.out   简单对比时间限制:1 s   内存限制:256 MB [题目描述] Bessie和她妹妹Elsie白天都在牧场 ...

  8. 初见微服务之RESTful API

    1. REST名称由来 REST全称为Representational State Transfer,即表述性状态转移,最早由Roy Feilding博士在世纪之交(2000年)提出,喜欢追根溯源的朋 ...

  9. Java 使用正则表达式取出图片地址以及跳转的链接地址,来判断死链(一)

    任务:通过driver的getPageSource()获取网页的源码内容,在把网页中图片链接地址和跳转的url地址进行过滤,在get每个请求,来判断是否是死链 如图: 获取网页源码中所有的href,以 ...

  10. 使用VSCode搭建TypeScript开发环境 (重点)

    下载TypeScript 在CMD(Windows系统)或者终端(macOS系统)中输入一下命令: npm install -g typescript 下载VSCode VSCode是我使用过最棒的编 ...