题目链接:http://codeforces.com/contest/789/problem/A
题意:n种不同颜色的石头,你有2个口袋,每个最多每天装k个颜色相同的石头,问要把n个石头装完需要多少天。
解法:XJB贪一贪就好了。
//CF 789A
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5+10;
int n, k, a[maxn];
int main()
{
scanf("%d%d", &n, &k);
for(int i = 1; i <= n; i++) scanf("%d", &a[i]);
sort(a+1, a+n+1, greater<int>());
int ans = 0, last = 0;
for(int i = 1; i <= n; i++){
ans += a[i] / (2*k);
int cur = a[i] % (2*k);
if(cur == 0) continue;
if(cur > k){
ans++;
continue;
}
else{
if(last){
ans++;
last=0;
}
else{
last = cur;
}
}
}
if(last) ans++;
cout << ans << endl;
}

本文提供 CodeForces 789A 题目的解题思路及实现代码。该题目要求计算将多种颜色的石头分别放入两个口袋所需的最少天数,每个口袋每天最多可以放入 k 种相同颜色的石头。通过贪心算法解决此问题。
589

被折叠的 条评论
为什么被折叠?



