【CodeForces - 808C Tea Party】 贪心
2017-08-16 09:50
435 查看
Polycarp invited all his friends to the tea party to celebrate the holiday. He has ncups, one for each of his n friends, with volumes a1, a2, ..., an.
His teapot stores wmilliliters 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.
Input
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
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.
Example
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个容量为a1, a2...an的茶杯,现在茶壶中有w体积的茶水,要求倒完茶水后要使所有客人开心,开心的条件是所有茶杯中的茶水不小于茶杯容积的一半,且容积大的茶杯中茶水不能少于容积小的茶杯中的茶水。
分析:先判断所有茶杯中倒入一半茶水时所需的茶水是否大于茶壶中的茶水。如果大于则输出-1。先给每个茶杯中加入一半的茶水,根据题意,我么可以将茶杯的容积排序,然后将剩余的茶水倒入大容积的茶杯中。最后按照茶杯序列排序输出即可。
代码如下:
#include <map> #include <cmath> #include <queue> #include <stack> #include <vector> #include <cstdio> #include <string> #include <cstring> #include <iostream> #include <algorithm> #define LL long long using namespace std; const int MX = 1e5 + 5; const int mod = 1e9 + 7; const int INF = 2e9 + 5; struct node{ int x, num, ans; }a[MX]; bool cmp1(node a, node b){ return a.x < b.x; } bool cmp2(node a, node b){ return a.num < b.num; } int main() { int n, k; scanf("%d%d", &n, &k); int need = 0; for(int i = 1; i <= n; i++){ scanf("%d", &a[i].x); a[i].num = i; need += (a[i].x+1)/2; } if(need > k){ printf("-1\n"); return 0; } sort(a+1, a+n+1, cmp1); for(int i = 1; i <= n; i++){ a[i].ans = (a[i].x+1)/2; k -= a[i].ans; } //cout << k << endl; for(int i = n; i >= 1; i--){ if(k >= (a[i].x - a[i].ans)){ k -= (a[i].x - a[i].ans); a[i].ans = a[i].x; } else{ a[i].ans += k; break; } } sort(a+1, a+1+n, cmp2); printf("%d", a[1].ans); for(int i = 2; i <= n; i++){ printf(" %d", a[i].ans); } printf("\n"); return 0; }
相关文章推荐
- CF - 808C. Tea Party - 排序+贪心
- 808C - Tea Party
- 【Codeforces 808 C. Tea Party】+ sort
- codeforces 913 C. Party Lemonade【思维贪心】
- 【CodeForces】---Party Lemonade(贪心&&思维&&递归)
- Codeforces 808C Tea Party【贪心】
- codeforces 913 c Party Lemonade(贪心+DP)
- Codeforces 549B Looksery Party [贪心]
- codeforces 808C——Tea Party(贪心)
- 【Codeforces 808C】【贪心】Tea Party题解
- Educational Codeforces Round 21 C. Tea Party 贪心
- CodeForces - 808C-贪心-思维
- Codeforces 913D - Too Easy Problems(贪心+优先队列)
- Uva 1315 - Crazy tea party 解题报告(找规律)
- codeforces 798 D. Mike and distribution 二维贪心
- codeforces 675C C. Money Transfers(贪心)
- Codeforces 115A. Party
- CodeForces 708A. Letters Cyclic Shift (字符串处理,贪心)
- CodeForces 416C Booking System(贪心)
- CodeForces - 799B T-shirt buying —— 贪心 优先队列