CF - 808C. Tea Party - 排序+贪心

1.题目描述:

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.


2.题意概述:

n个茶杯 m是总共的水 然后n个茶杯的大小每个茶杯里至少有一半的水,然后就是杯子大的水不能比杯子小的水少。问你倒出来的结果。

3.解题思路:

考虑贪心,按备注容量排序,每次贪心倒一半,如果该杯子容量和上一个相等则也倒和上一个相等的容量,否则倒上一个容量+1倍。如果贪心倒完还有剩余,就从最后一个杯子开始尽量加满。

这题挺细节的,当时看题不仔细,wa得很不应该。

4.AC代码:

#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
#define maxn 100100
#define lson root << 1
#define rson root << 1 | 1
#define lent (t[root].r - t[root].l + 1)
#define lenl (t[lson].r - t[lson].l + 1)
#define lenr (t[rson].r - t[rson].l + 1)
#define N 1111
#define eps 1e-6
#define pi acos(-1.0)
#define e exp(1.0)
using namespace std;
const int mod = 1e9 + 7;
typedef long long ll;
typedef unsigned long long ull;
struct node
{
	int v, c, id;
} p[maxn];
bool cmp1(node a, node b)
{
	return a.v < b.v;
}
bool cmp2(node a, node b)
{
	return a.id < b.id;
}
int main()
{
#ifndef ONLINE_JUDGE
	freopen("in.txt", "r", stdin);
	freopen("out.txt", "w", stdout);
	long _begin_time = clock();
#endif
	int n, w;
	while (~scanf("%d%d", &n, &w))
	{
		for (int i = 0; i < n; i++)
		{
			scanf("%d", &p[i].v);
			p[i].id = i;
		}
		sort(p, p + n, cmp1);
		bool flag = 0;
		for (int i = 0; i < n; i++)
		{
			if (i > 0 && p[i].v == p[i - 1].v)
				p[i].c = p[i - 1].c;
			else
			{
				int need = (p[i].v + 1) / 2;
				p[i].c = p[i - 1].c;
				if (p[i].c < need)
					p[i].c = need;
				
			}
			w -= p[i].c;
			if (w < 0)
			{
				flag = 1;
				break;
			}
		}
		if (flag)
			puts("-1");
		else
		{
			if (w > 0)
			{
				for (int i = n - 1; i >= 0; i--)
				{
					int need = p[i].v - p[i].c;
					if (w >= need)
					{
						w -= need;
						p[i].c = p[i].v;
					}
					else
					{
						p[i].c += w;
						break;
					}
				}
			}
			sort(p, p + n, cmp2);
			for (int i = 0; i < n; i++)
				if (i == 0)
					printf("%d", p[i].c);
				else
					printf(" %d", p[i].c);
			puts("");
		}
	}
#ifndef ONLINE_JUDGE
	long _end_time = clock();
	printf("time = %ld ms.", _end_time - _begin_time);
#endif
	return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值