Educational Codeforces Round 21 C Tea Party
来源:互联网 发布:用什么软件做蹭饭地图 编辑:程序博客网 时间:2024/06/04 19:51
Polycarp invited all his friends to the tea party to celebrate the holiday. He hasn cups, one for each of hisn friends, with volumesa1, 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 cupi contains less tea than cupj butai > 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 andw (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 108 7
6 4
4 41 1 1 1
1 1 1 1
3 109 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个茶杯,水壶有int k的水,每个茶杯的水至少要倒容量的一半且不能为分数,而且水壶必须要倒完,而且容量大的茶杯的茶不能比容量小的少。不满足这些条件的就输出-1,否则输出没个茶杯装多少水。
分析:简单谈心法,先看看所有杯子能否倒满,如果能的话从大的杯子开始倒直到全部倒满或者茶壶空。
#include <iostream>#include <algorithm>#include <cstring>#include <cstdio>using namespace std;struct node{ int va; int ke;}a[128],b[128];bool cmp1(node q,node w){ return q.va<w.va;}bool cmp2(node q,node w){ return q.ke<w.ke;}int main(){ int n,w; while(cin>>n>>w){ int tem; memset(b,0,sizeof(b)); for(int i=0;i<n;i++){ cin>>tem; a[i].ke=i; b[i].ke=i; a[i].va=tem; } sort(a,a+n,cmp1); for(int i=n-1;i>=0;i--){ w-=(a[i].va+1)/2; b[a[i].ke].va+=(a[i].va+1)/2; } if(w<0) cout<<-1<<endl; else{ for(int i=n-1;i>=0;i--){ if(a[i].va-b[a[i].ke].va<=w){ w-=a[i].va-b[a[i].ke].va; b[a[i].ke].va=a[i].va; } else { b[a[i].ke].va+=w; w=0; break; } } if(w>0) cout<<-1; else for(int i=0;i<n;i++) printf("%d ",b[i].va); cout<<endl; } } return 0;}
- Educational Codeforces Round 21 C. Tea Party
- Educational Codeforces Round 21 C. Tea Party
- Educational Codeforces Round 21 C. Tea Party
- Educational Codeforces Round 21-----C. Tea Party
- Educational Codeforces Round 21 C Tea Party
- codeforces Educational Codeforces Round 21 C. Tea Party
- Educational Codeforces Round 21 808C Tea Party 【贪心算法】
- Educational Codeforces Round 21 C. Tea Party 贪心
- Codeforces Educational Codeforces Round 21 [5.30] Tea Party&Array Division&Average Sleep Time&Lucky
- Codeforces 808C Tea Party
- Codeforces 808C Tea Party
- Educational Codeforces Round 21 C
- 【codeforces 808】C.Tea Party(贪心)
- Codeforces 808C Tea Party 题解
- 【Codeforces 808 C. Tea Party】+ sort
- Codeforces 808C Tea Party【贪心】
- 【Codeforces 808C】【贪心】Tea Party题解
- Educational Codeforces Round 21
- 弹幕刷屏之术——Android无时间线弹幕实现
- 宏求余
- 杭电oj1009(贪心算法)
- matplotlib绘图、图片处理、绘制文字
- 正则表达式
- Educational Codeforces Round 21 C Tea Party
- CSS
- B. Arpa’s obvious problem and Mehrdad’s terrible solution
- java 线程综合学习代码
- OpenMPI 实现CPU并行计算模拟GA算法
- 递归
- 从文档流认识html
- 石英晶体振荡器的频率求法
- 设置或清除特定的位