CodeForces - 158B.Taxi (贪心)
CodeForces - 158B.Taxi (贪心)
题意分析
首先对1234的个数分别统计,4人组的直接加上即可。然后让1和3成对处理,只有2种情况,第一种是1多,就让剩下的1和2组队处理,另外一种是3多,那么结果就加上3的个数,再单独处理2.
对于1和2组队处理的讨论:首先分配2,有2种情况,一种是2正好分配完了,另外一种就2还剩下2个人(正好剩下1组)。就一起处理这个2个人和1剩下的人。
把每次处理的结果都加起来即可。
代码
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <sstream>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <cmath>
#define INF 0x3f3f3f3f
#define nmax 5
#define MEM(x) memset(x,0,sizeof(x))
using namespace std;
int a[nmax];
int main()
{
//freopen("in.txt","r",stdin);
int n;
while(scanf("%d",&n) != EOF){
MEM(a);
for(int i = 0; i<n;++i){
int temp;
scanf("%d",&temp);
a[temp]++;
}
int ans = a[4];
if(a[3]>=a[1]){//3 more than 1
ans+=a[3];
if(a[2] %2 == 0) ans+=a[2]/2;
else ans+=a[2]/2+1;
}else{// 1 more than 3
ans+=a[3]; a[1]-=a[3];//1 3 组队
if(a[2] %2 == 0){ans+=a[2]/2;a[2] = 0;}
else{ans+=a[2]/2;a[2] = 1;}
ans+=a[1]/4; a[1] = a[1] % 4;
if((a[1] + a[2] * 2) % 4 == 0) ans+=(a[1] + a[2] * 2)/4;
else ans+=(a[1] + a[2] * 2)/4 +1;
}
printf("%d\n",ans);
}
return 0;
}
CodeForces - 158B.Taxi (贪心)的更多相关文章
- CodeForces 158B Taxi(贪心)
贪心,注意优先级,4单独,3与1先匹配,2与2匹配(注意判断2有没有剩下),然后2与两个1匹配,最后4个1匹配就可以了. #include<iostream> #include<cs ...
- Codeforces 158B:Taxi
B. Taxi time limit per test 3 seconds memory limit per test 256 megabytes input standard input outpu ...
- Codeforces 158 B. Taxi[贪心/模拟/一辆车最多可以坐4人同一个群的小朋友必须坐同一辆车问最少需要多少辆车]
http://codeforces.com/problemset/problem/158/B B. Taxi time limit per test 3 seconds memory limit pe ...
- 【Codeforces 158B】Taxi
[链接] 我是链接,点我呀:) [题意] 每辆车可以载重4个人. 一共有n个组,每个组分别有s[i]个人. 要求每个组的人都在同一辆车里面. 问最少需要多少辆车 [题解] 将每个组的人数从小到大排序. ...
- codeforces 724D(贪心)
题目链接:http://codeforces.com/contest/724/problem/D 题意:给定一个字符串和一个数字m,选取一个一个子序列s,使得对于字符串中任意长度为m的子序列都至少含有 ...
- Codeforces 626G Raffles(贪心+线段树)
G. Raffles time limit per test:5 seconds memory limit per test:256 megabytes input:standard input ou ...
- Cut 'em all! CodeForces - 982C(贪心dfs)
K - Cut 'em all! CodeForces - 982C 给一棵树 求最多能切几条边使剩下的子树都有偶数个节点 如果n是奇数 那么奇数=偶数+奇数 不管怎么切 都会有奇数 直接打印-1 贪 ...
- CodeForces - 940E - Cashback +贪心+DP
传送门:CodeForces - 940E - Cashback 题意:在一个长度为n的数组中,可以分出长度为 k 连续的多个数组b(每个数组 b 的 k 可不相同),然后,可以对每个数组 b 进行删 ...
- Codeforces 515C 题解(贪心+数论)(思维题)
题面 传送门:http://codeforces.com/problemset/problem/515/C Drazil is playing a math game with Varda. Let’ ...
随机推荐
- python中的class正确用法
class Dog: def __init__(self, name): self.name = name self.tricks = [] # creates a new empty list fo ...
- react-native windows系统 红屏报assets缺失 500错误
指定版本,react-native是facebook用mac系统开发的,windows系统兼容较差,新版本更是问题很多, 相对老版本更加稳定 react-native init demo --vers ...
- textview的阴影线
android:shadowColor="#000000" android:shadowDx="1" android:shadowDy="1" ...
- 6.2 element和elements
为什么这个要单独拿出来说,因为我在很多群里面看见很多人不能区分这个! 因为之前的包有点问题,另外后续还会更换app,因为部分app可能没有符合的案例场景,我需要找到那个场景给大家做个实例..便于大家跟 ...
- Siki_Unity_1-6_C#编程初级教程(未学)
Unity 1-6 C#编程初级教程 任务1:C#和.Net框架 C#是.Net里的一个成分 2002年微软发布第一个.Net框架(多平台,行业标准,安全性) .Net框架 IDE编程工具 --产生- ...
- Python基础 之 文件操作
文件操作 一.路径 文件绝对路径:d:\python.txt 文件相对路径:在IDEA左边的文件夹中 二.编码方式 utf-8 gbk... 三.操作方式 1.只读 r 和 rb 绝对路径的打开操作 ...
- Logistic回归和SVM的异同
这个问题在最近面试的时候被问了几次,让谈一下Logistic回归(以下简称LR)和SVM的异同.由于之前没有对比分析过,而且不知道从哪个角度去分析,一时语塞,只能不知为不知. 现在对这二者做一个对比分 ...
- UVa 340 - Master-Mind Hints 解题报告 - C语言
1.题目大意 比较给定序列和用户猜想的序列,统计有多少数字位置正确(x),有多少数字在两个序列中都出现过(y)但位置不对. 2.思路 这题自己思考的思路跟书上给的思路差不多.第一个小问题——位置正确的 ...
- c# html 导出excel
[CustomAuthorize] public FileResult ExportCustomerManagerVisitExcel(string dateType, string r ...
- BZOJ 4736 温暖会指引我们前行 LCT+最优生成树+并查集
题目链接:http://uoj.ac/problem/274 题意概述: 没什么好概述的......概述了题意就知道怎么做了......我懒嘛 分析: 就是用lct维护最大生成树. 然后如果去UOJ上 ...