Educational Codeforces Round 21 808C 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.
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
题解: 先将数组按大小 大到小排序
然后每个杯子倒一半 把剩下的继续从大到小依次到茶
直到倒完 再排序到原来的样子
#include <stdio.h>
#include <bits/stdc++.h>
#define mod 1000000007
typedef long long ll;
using namespace std;
class Tea{
public:
int a,w,id;
};
Tea t[105];
int n,w;
int cmp1(Tea a,Tea b){ return a.a>b.a;}
int cmp2(Tea a,Tea b){ return a.id<b.id;}
int main(){
cin>>n>>w;
for(int i=0;i<n;i++){
cin>>t[i].a;
t[i].id=i;
}
sort(t,t+n,cmp1); //杯子大到小排序
for(int i=0;i<n;i++){
w-=(t[i].a+1)/2; //每个杯子至少装一半
t[i].w=(t[i].a+1)/2; //保存已经装了多少
}
for(int i=0;i<n;i++){
int x=t[i].a-t[i].w;
if(w>=x){
t[i].w+=x;w-=x;
}else if(w>0){
t[i].w+=w;w=0;
}else
break;
}
sort(t,t+n,cmp2); //按照id 排序 排回原位
if(w<0) cout<<-1;
else
for(int i=0;i<n;i++)
cout<<t[i].w<<" ";
return 0;
}