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 (贪心)的更多相关文章

  1. CodeForces 158B Taxi(贪心)

    贪心,注意优先级,4单独,3与1先匹配,2与2匹配(注意判断2有没有剩下),然后2与两个1匹配,最后4个1匹配就可以了. #include<iostream> #include<cs ...

  2. Codeforces 158B:Taxi

    B. Taxi time limit per test 3 seconds memory limit per test 256 megabytes input standard input outpu ...

  3. Codeforces 158 B. Taxi[贪心/模拟/一辆车最多可以坐4人同一个群的小朋友必须坐同一辆车问最少需要多少辆车]

    http://codeforces.com/problemset/problem/158/B B. Taxi time limit per test 3 seconds memory limit pe ...

  4. 【Codeforces 158B】Taxi

    [链接] 我是链接,点我呀:) [题意] 每辆车可以载重4个人. 一共有n个组,每个组分别有s[i]个人. 要求每个组的人都在同一辆车里面. 问最少需要多少辆车 [题解] 将每个组的人数从小到大排序. ...

  5. codeforces 724D(贪心)

    题目链接:http://codeforces.com/contest/724/problem/D 题意:给定一个字符串和一个数字m,选取一个一个子序列s,使得对于字符串中任意长度为m的子序列都至少含有 ...

  6. Codeforces 626G Raffles(贪心+线段树)

    G. Raffles time limit per test:5 seconds memory limit per test:256 megabytes input:standard input ou ...

  7. Cut 'em all! CodeForces - 982C(贪心dfs)

    K - Cut 'em all! CodeForces - 982C 给一棵树 求最多能切几条边使剩下的子树都有偶数个节点 如果n是奇数 那么奇数=偶数+奇数 不管怎么切 都会有奇数 直接打印-1 贪 ...

  8. CodeForces - 940E - Cashback +贪心+DP

    传送门:CodeForces - 940E - Cashback 题意:在一个长度为n的数组中,可以分出长度为 k 连续的多个数组b(每个数组 b 的 k 可不相同),然后,可以对每个数组 b 进行删 ...

  9. Codeforces 515C 题解(贪心+数论)(思维题)

    题面 传送门:http://codeforces.com/problemset/problem/515/C Drazil is playing a math game with Varda. Let’ ...

随机推荐

  1. Linux命令应用大词典-第10章 Shell相关命令

    10.1 commond:抑制正常的Shell函数查找 10.2 exec:使用执行命令替换当前的shell进程 10.3 bash:GNU的Bourne-Again Shell解释器 10.4 bu ...

  2. Unity 特殊目录

    其他目录 Application.persistentDataPath:webGL平台只能使用这个

  3. 【C++模版之旅】项目中一次活用C++模板(traits)的经历 -新注解

    问题与需求: 请读者先看这篇文章,[C++模版之旅]项目中一次活用C++模板(traits)的经历. 对于此篇文章提出的问题,我给出一个新的思路. talking is cheap,show me t ...

  4. Java开发工程师(Web方向) - 02.Servlet技术 - 第1章.Servlet

    第1章--Servlet Servlet简介 Servlet应用于? 浏览器发出HTTP请求,服务器接收请求后返回响应给浏览器. 接收请求后到返回响应之间: 服务器将请求对象转交给Servlet容器 ...

  5. 【WXS数据类型】Boolean

    属性: 名称 值类型 说明 [Boolean].constructor [String] 返回值为“Boolean”,表示类型的结构字符串 方法: 原型:[Boolean].toString() 说明 ...

  6. 解决ssh_exchange_identification:read connection reset by peer 原因

    服务器改了密码,试过密码多次后出现: ssh_exchange_identification: read: Connection reset by peer 可以通过ssh -v查看连接时详情 Ope ...

  7. 【第三章】MySQL数据库的字段约束:数据完整性、主键、外键、非空、默认值、自增、唯一性

    一.表完整性约束 作用:用于保证数据的完整性和一致性==============================================================约束条件  说明PRIM ...

  8. [Data Structures and Algorithms - 1] Introduction & Mathematics

    References: 1. Stanford University CS97SI by Jaehyun Park 2. Introduction to Algorithms 3. Kuangbin' ...

  9. hosts_allow配置了却不生效

    hosts_allow配置了却不生效 配置了两台白名单的机器,一台生效一台不生效,google后的结果都是更新libwrap.so  安装openssh等等..(问题还是没有解决) 经过对比发现,原来 ...

  10. aria2 on ubuntu

    http://www.5yun.org/9102.html http://jpollo.logdown.com/posts/160847-aria2c-and-yaaw aria2c --enable ...