【Codeforces 808C】【贪心】Tea Party题解
2017-05-30 17:25
531 查看
C. Tea Party
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
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.
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.
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.
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
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.
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.
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.
#include <iostream> #include <cstdio> #include <cstring> #include <string> #include <set> #include <queue> #include <algorithm> #include <vector> #include <cstdlib> #include <cmath> #include <ctime> #include <stack> #define INF 2100000000 #define LL long long #define clr(x) memset(x, 0, sizeof(x)) #define ms(a, x) memset(x, a, sizeof(x)) using namespace std; template <class T> inline void read(T &x) { int flag = 1; x = 0; char ch = (char)getchar(); while(ch < '0' || ch > '9') { if(ch == '-') flag = -1; ch = (char)getchar(); } while(ch >= '0' && ch <= '9') { x = (x<<1)+(x<<3)+ch-'0'; ch = (char)getchar(); } x *= flag; } const int maxn = 110; int n,w; struct cup { int num,m,t; } a[maxn]; inline int cmp(cup x, cup y) { return x.m < y.m; } inline int cmp2(cup x, cup y) { return x.num < y.num; } int main() { read(n); read(w); for(int i = 1; i <= n; i++) read(a[i].m), a[i].num = i; int ok = 1; sort(a+1, a+n+1, cmp); for(int i = 1; i <= n; i++) { int x = (a[i].m+1)>>1; if(w < x) { ok = 0; break; } if(i == n) { x = w; if(a[i].m < x) { w -= a[i].m-a[i].t; a[i].t = a[i].m; for(int j = n-1; j >= 1; j--) { int y = a[j].m; y = min(w+a[j].t, y); w -= y-a[j].t; a[j].t = y; if(w <= 0) break; } if(w > 0) { ok = 0; break; } } else a[i].t=x; } if(i != n) a[i].t = x; w -= x; } if(!ok) { puts("-1"); return 0; } sort(a+1, a+n+1, cmp2); for(int i = 1; i <= n; i++) printf("%d ",a[i].t); return 0; }
相关文章推荐
- codeforces 808C——Tea Party(贪心)
- Codeforces 777E/778C 题解 (贪心)
- 【CodeForces 729C】【二分+贪心】Road to Cinema 题解
- 【CodeForces - 808C Tea Party】 贪心
- CodeForces - 808C C. Tea Party (贪心)
- 7_6_S题 Not Equal on a Segment题解 [codeforces 622C] (贪心)
- CodeForces - 808C Tea Party
- 【codeforces 808】C.Tea Party(贪心)
- CodeForces - 808C-贪心-思维
- [贪心] Codeforces 723C #375 (Div. 2) C. Polycarp at the Radio
- Codeforces 813B The Golden Age 题解
- Codeforces 161 B. Discounts (贪心)
- 【Codeforces 713B&714D】 Searching Rectangles【贪心】
- codeforces 655B B. Mischievous Mess Makers(贪心)
- Codeforces 500B New Year Permutation [贪心]
- Codeforces 549G Happy Line [贪心] [数学]
- 【codeforces】 ZeptoLab Code Rush 2015 A,B,C,D,E题解
- Codeforces 432C Prime Swaps【筛法素数预处理+贪心】
- 【Codeforces 578B】【贪心】"Or" Game
- codeforces 187 (div2)题解