codeforces 808C——Tea Party(贪心)

本文介绍了一种解决茶话会上如何公平分配有限茶水量的算法。该算法确保每位参与者至少获得其杯子容量一半的茶水,且水量为整数。通过排序与逐步填充策略,实现了条件下的最优解。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.



首先对杯子按照容量排序,首先每个杯子分别刚好到(m+1)/2毫升的水,这样可以保证大于一半而且是整数,然后如果水不够,说明不可能,如果水多了,则从最后一个开始,往前依次加满。如果每个杯子都满了水还是没用完也不行。


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <iomanip>
using namespace std;

#define _ ios::sync_with_stdio(false)
const int MAXN = 110;
const int INF=0x7fffffff;


int n;
struct cup{
	int num;
	int m;
	int t;
}a[MAXN];
int cmp(cup x,cup y){
	return x.m<y.m;
}
int cc(cup x,cup y){
	return x.num<y.num;
}
int main(){
	int w;
	scanf("%d%d",&n,&w);
	for(int i=1;i<=n;i++){
		scanf("%d",&a[i].m);
		a[i].num=i;
	}
	int MAX=0;
	int ok=1;
	sort(a+1,a+n+1,cmp);
	for(int i=1;i<=n;i++){
		int x=(a[i].m+1)/2;
		//cout<<x<<endl;
		if(w<x){
			ok=0;
			break;
		}
		if(i==n){
			x=w;
			if(a[i].m<x){
				w-=(a[i].m-a[i].t);
				a[i].t=a[i].m;
				for(int j=n-1;j>=1;j--){
					int y=a[j].m;
					y=min(w+a[j].t,y);
					w-=(y-a[j].t);
					a[j].t=y;
					if(w<=0)
						break;
				}
				if(w>0){
					ok=0;
					break;
				}
			}else{
				a[i].t=x;
			}
		}
		if(i!=n)
			a[i].t=x;
		w-=x;
	}
	if(!ok){
		puts("-1");
		return 0;
	}
	sort(a+1, a+n+1, cc);
	for(int i=1;i<=n;i++){
		printf("%d%c",a[i].t,i==n?'\n':' ');
	}
}







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值