C. Tea Party
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.
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.
大致题意:就是有n个朋友带了n个茶杯来喝茶,假设编号为1~n,每个茶杯都有自己的一个最大容量ai,
然后你有w的茶水给朋友倒茶,每个人茶杯中的茶水量不能少于自己的茶杯的最大容量的一半,然后茶杯容量大的人的茶水量不能少于茶杯容量少的,问你能否满足朋友,可以的话输出给每个人倒的茶水,不能的话输出-1。(茶水是整数)
思路:简单的模拟,先给每个人倒杯子容量一半的水,然后按杯子容量大到小,把剩余的茶水都给他们倒满。
代码如下
#include<iostream>
#include<algorithm>
#include<string>
#include<cstdio>
#include<cstring>
#include<queue>
#include<vector>
#include<cstring>
#include<cmath>
#define LL long long
#define ULL unsigned long long
using namespace std;
struct node
{
int w;
int num;
}e[105];//记录杯子的容量以及编号,进行排序
bool cmp(node a,node b)
{
return a.w>b.w;
}
int v[105],w[105];//v[]存给每个人倒的茶水量,w[]存杯子的容量
int main()
{
int n,W;
int sum=0;
scanf("%d%d",&n,&W);
for(int i=1;i<=n;i++)
{
scanf("%d",&w[i]);
e[i].w=w[i];
e[i].num=i;
if(e[i].w%2==0)
{
sum+=v[i]=e[i].w/2;
}
else
{
sum+=v[i]=e[i].w/2+1;
}
}
if(sum>W)//剪枝
printf("-1");
else
{
sum=W-sum;
sort(e+1,e+1+n,cmp);
int i=1;
while(sum)
{
int num=e[i].num;
if(w[num]-v[num]<sum)
{
sum=sum-(w[num]-v[num]);
v[num]=w[num];
i++;
}
else
{
v[num]+=sum;
break;
}
}
for(int i=1;i<=n;i++)
printf("%d ",v[i]);
}
return 0;
}