Educational Codeforces Round 21 C
来源:互联网 发布:80端口打不开 编辑:程序博客网 时间:2024/05/17 02:09
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; }
- Educational Codeforces Round 21 C
- 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
- Educational Codeforces Round 21
- Educational Codeforces Round 21
- Educational Codeforces Round 21
- Educational Codeforces Round 5 C
- Educational Codeforces Round 26 C
- codeforces Educational Codeforces Round 21 C. Tea Party
- codeforces Educational Codeforces Round 16-C
- Educational Codeforces Round 21 A
- Educational Codeforces Round 21-D
- Educational Codeforces Round 21-G
- Educational Codeforces Round 21E
- Educational Codeforces Round 21 -DEG
- 关于Mysql5.7修改root密码ERROR 1054的问题
- 读取信息对象层级的函数
- fetch学习(一)-入门篇
- Java super关键字的使用
- libmysqlclient.so.16()缺失的解决方案
- Educational Codeforces Round 21 C
- Java代码性能优化
- 《微服务架构与实践》
- eclipse插件开发以及打包使用
- Anchors in SSD
- FZU-2150 Fire Game
- SpringMVC的环境搭建
- Java从入门到精通_6 运算符
- React Native-MAC下React Native环境搭建和创建HelloWorld项目