Educational Codeforces Round 21 C. Tea Party 贪心

题目:

C. Tea Party
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Polycarp invited all his friends to the tea party to celebrate the holiday. He has n cups, one for each of his n friends, with volumes a1, a2, ..., an. His teapot stores w milliliters of tea (w ≤ a1 + a2 + ... + an). Polycarp wants to pour tea in cups in such a way that:

  • Every cup will contain tea for at least half of its volume
  • Every cup will contain integer number of milliliters of tea
  • All the tea from the teapot will be poured into cups
  • All friends will be satisfied.

Friend with cup i won't be satisfied, if there exists such cup j that cup i contains less tea than cup j but ai > aj.

For each cup output how many milliliters of tea should be poured in it. If it's impossible to pour all the tea and satisfy all conditions then output -1.

Input

The first line contains two integer numbers n and w (1 ≤ n ≤ 100).

The second line contains n numbers a1, a2, ..., an (1 ≤ ai ≤ 100).

Output

Output how many milliliters of tea every cup should contain. If there are multiple answers, print any of them.

If it's impossible to pour all the tea and satisfy all conditions then output -1.

Examples
input
2 10
8 7
output
6 4 
input
4 4
1 1 1 1
output
1 1 1 1 
input
3 10
9 8 10
output
-1
Note

In the third example you should pour to the first cup at least 5 milliliters, to the second one at least 4, to the third one at least 5. It sums up to 14, which is greater than 10 milliliters available.


这个题,对于前四个条件还是很好满足的,最后一个条件要求大杯子里的水,不能比小杯子小,那么很显然要排序,然后很好想的一个策略就是先把大杯子倒满,然后如果还有水,就倒次小杯子。注意的是输出的时候,是按ID来的,所以我们应该弄个结构体来封装杯子的标号,杯子的容积,杯子已经有的水。然后怎样保证输出的时候方便?

用一个map<a,b>m,先sort后,a表示杯子的标号,b表示排序后杯子的id,建立一个由a→b的一个hash

code:

#include<cstdio>
#include<map>
#include<algorithm>
#define MIN(a,b) (a<b?a:b)
using namespace std;
const int MAXN=100+5;
struct friends{
    int ID,cup,has;
}q[MAXN];
int a[MAXN],n,w;
bool cmp(friends x,friends y){
    return x.cup<y.cup;
}
int main(){
    scanf("%d%d",&n,&w);
    for(int i=0;i<n;++i){
        q[i].ID=i;
        scanf("%d",&q[i].cup);
    }
    sort(q,q+n,cmp);
    map<int,int>m;
    for(int i=0;i<n;++i)
        m[q[i].ID]=i;
    //ID -> i
    int sum=0;
    for(int i=0;i<n;++i){
        sum+=(q[i].cup-1)/2+1;
        q[i].has=(q[i].cup-1)/2+1;
    }
    if(sum>w){
        printf("-1\n");
        return 0;
    }
    w-=sum;
    for(int i=n-1;i>=0;--i){
        int pour=min(q[i].cup-q[i].has,w);//min(杯子最多还能装多少,还剩多少水)
        w-=pour;
        q[i].has+=pour;
        if(w==0)break;
    }
    if(w>0){
        printf("-1\n");
    }else{
        for(int i=0;i<n;++i)
            printf("%d ",q[m[i]].has);//现在的i代表杯子原来的ID噢
    }
    return 0;
}

//ps:其实没必要用map,就用普通的数组就可以了,只不过养成了坏习惯。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值