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.
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 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.
2 10 8 7
6 4
4 4 1 1 1 1
1 1 1 1
3 10 9 8 10
-1
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.
题目大意:
给你N个杯子,要求最终ΣB【i】=w.
现在有几个要求:
每个杯子中的水不能少于A【i】/2.
而且如果A【i】>A【j】.那么要求必须有:B【i】>B【j】;
让你构造出来B序列。
思路:
我们贪心的按照A【i】从大到小排序即可,然后按照大小顺序依次以A【i】/2为基数向上添加水量。
对应记录下标然后输出即可。
Ac代码:
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define ll __int64
struct node
{
ll val,pos,cm;
}b[1500000];
ll a[1500000];
ll ans[1500000];
int cmp(node a,node b)
{
return a.cm>b.cm;
}
int main()
{
ll n,m;
while(~scanf("%I64d%I64d",&n,&m))
{
ll sum=0;
for(int i=0;i<n;i++)scanf("%I64d",&a[i]);
for(int i=0;i<n;i++)
{
b[i].val=a[i]/2;
b[i].cm=a[i];
if(a[i]%2==1)b[i].val++;
b[i].pos=i;
sum+=b[i].val;
}
sort(b,b+n,cmp);
for(int i=0;i<n;i++)
{
while(sum<m&&b[i].val<a[b[i].pos])
{
sum++;
b[i].val++;
}
ans[b[i].pos]=b[i].val;
}
if(sum==m)
{
for(int i=0;i<n;i++)printf("%I64d ",ans[i]);
printf("\n");
}
else printf("-1\n");
}
}
本文介绍了一种解决茶话会倒茶问题的算法。该问题要求根据杯子容量分配有限的茶水量,确保每个杯子至少装一半且水量遵循特定规则。通过将杯子按容量降序排列并依次填充的方法,实现了合理分配。
393

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



