CF - 158B - Taxi
2013-03-31 21:25
176 查看
题意:有n群小朋友要乘车,每群小朋友的人数最少1人,最多4人,一辆车最多可以坐4人,同一个群的小朋友必须坐同一辆车,问最少需要多少辆车。
题目链接:http://codeforces.com/problemset/problem/158/B
——>>贪心,读入时统计各团人数,4人的肯定要一车;3人的也肯定要一车,且能加1人就多加1人;2人的两两一车,最后若剩有1团2人的,则其占1车且能加1人就多加1人;最后1人1人的4个团一车。
#include <cstdio>
#include <algorithm>
using namespace std;
const int maxn = 100000 + 10;
int s[maxn];
int main()
{
int n, i;
while(~scanf("%d", &n))
{
int one = 0, two = 0, three = 0, four = 0;
for(i = 0; i < n; i++)
{
scanf("%d", &s[i]);
switch(s[i])
{
case 1:one++;break;
case 2:two++;break;
case 3:three++;break;
case 4:four++;break;
}
}
int cnt = four + three + two/2;
if(one > three) one -= three;
else one = 0;
two %= 2;
if(two == 1)
{
cnt++;
if(one > 2) one -= 2;
else one = 0;
}
cnt += one / 4;
if(one % 4 != 0) cnt++;
printf("%d\n", cnt);
}
return 0;
}
题目链接:http://codeforces.com/problemset/problem/158/B
——>>贪心,读入时统计各团人数,4人的肯定要一车;3人的也肯定要一车,且能加1人就多加1人;2人的两两一车,最后若剩有1团2人的,则其占1车且能加1人就多加1人;最后1人1人的4个团一车。
#include <cstdio>
#include <algorithm>
using namespace std;
const int maxn = 100000 + 10;
int s[maxn];
int main()
{
int n, i;
while(~scanf("%d", &n))
{
int one = 0, two = 0, three = 0, four = 0;
for(i = 0; i < n; i++)
{
scanf("%d", &s[i]);
switch(s[i])
{
case 1:one++;break;
case 2:two++;break;
case 3:three++;break;
case 4:four++;break;
}
}
int cnt = four + three + two/2;
if(one > three) one -= three;
else one = 0;
two %= 2;
if(two == 1)
{
cnt++;
if(one > 2) one -= 2;
else one = 0;
}
cnt += one / 4;
if(one % 4 != 0) cnt++;
printf("%d\n", cnt);
}
return 0;
}
相关文章推荐
- codeforces 158B - Taxi
- CF 158D Ice Sculptures
- codeforces 158B(Taxi) Java
- 快速切题CF 158B taxi 构造 && 82A double cola 数学观察 难度:0
- codeforces-158B-Taxi题解
- 158B - Taxi
- 158B_Taxi
- 158B - Taxi
- CodeForces 158 B. Taxi(模拟)
- CodeForces 158 B. Taxi(模拟)
- Problem--158B--Codeforces--Taxi
- Codeforces 158B - Taxi
- CF——Taxi
- CF 319C(Kalila and Dimna in the Logging Industry-斜率DP,注意叉积LL溢出)
- CF 567D(One-Dimensional Battle Ships-二分)
- 精简cf安装流程
- 0526 CF#801A&G2n#D-Straight «A»
- CF 327D - Block Tower 数学题 DFS 初看很难,想通了就感觉很简单
- CF Round 418(div2) E题解
- CF 322B Ciel and Flowers 贪心水题