[CF808C] Tea Party(贪心)

题目链接:http://codeforces.com/contest/808/problem/C

题意:n个茶杯,每个茶杯有容量。现在给一壶茶,总量为w。希望倒茶满足条件:每杯茶要超过容量的一半,并且w被倒光,茶杯内的茶水为整数,容量大的杯子内的茶不允许比容量小的杯子内的茶水少。

 

特判不满足情况,然后将茶水给每一杯倒至一半以上。然后按照容量从大到小每次倒一个单位,直到倒完为止。

 

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 
 4 typedef long long LL;
 5 const int maxn = 200200;
 6 int n, w, b[maxn];
 7 pair<int,int> a[maxn];
 8 
 9 int main() {
10     // freopen("in", "r", stdin);
11     while(~scanf("%d%d",&n,&w)) {
12         for(int i = 1; i <= n; i++) {
13             scanf("%d", &a[i].first);
14             a[i].second = i;
15         }
16         sort(a+1, a+n+1);
17         int tot = 0;
18         for(int i = 1; i <= n; i++) {
19             b[a[i].second] = (int)ceil(a[i].first / 2.0);
20             tot += b[a[i].second];
21         }
22         if(tot > w) {
23             puts("-1");
24             continue;
25         }
26         w -= tot;
27         while(w) {
28             for(int id = n; id >= 1; id--) {
29                 if(!w) break;
30                 if(b[a[id].second] < a[id].first) {
31                     b[a[id].second]++; w--;
32                 }
33             }
34         }
35         for(int i = 1; i <= n; i++) {
36             printf("%d%c", b[i], i==n?'\n':' ');
37         }
38     }
39     return 0;
40 }

 

转载于:https://www.cnblogs.com/kirai/p/6861840.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值