本文主要是介绍CF - 158B - Taxi,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
题意:有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;
}
这篇关于CF - 158B - Taxi的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!