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.
Examples
2 10
8 7
6 4
4 4
1 1 1 1
1 1 1 1
3 10
9 8 10
-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.
题目链接:CodeForces - 808C
题意:
给你一个含有w升水的茶壶,以及N个杯子,每一个杯子的容量为ai升,
让给你这N个杯子倒水,使之满足以下条件:
1.每一个杯子至少有一半以上的水,(包含一半
2.每一个杯子中水的含量为整升
3.茶壶的水全倒完。
4,不存在这样一对杯子,A和B,A的体积大于B的体积,但是A杯子中的水比B少。
不能满足就输出-1,能满足就输出这N个杯子中分别的水量。
思路:创建一个结构体来描述杯子,
然后先把所有杯子加入一半的水。如果不能完成这样,就输出-1.
然后排序,以杯子的体积从大到小加满水,然后输出答案就好了。
细节见我的AC代码:
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <queue> #include <stack> #include <map> #include <set> #include <vector> #define sz(a) int(a.size()) #define all(a) a.begin(), a.end() #define rep(i,x,n) for(int i=x;i<n;i++) #define repd(i,x,n) for(int i=x;i<=n;i++) #define pii pair<int,int> #define pll pair<long long ,long long> #define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0) #define MS0(X) memset((X), 0, sizeof((X))) #define MSC0(X) memset((X), ' ', sizeof((X))) #define pb push_back #define mp make_pair #define fi first #define se second #define eps 1e-6 #define gg(x) getInt(&x) using namespace std; typedef long long ll; inline void getInt(int* p); const int maxn=1000010; const int inf=0x3f3f3f3f; /*** TEMPLATE CODE * * STARTS HERE ***/ struct node { int v; int id; }a[maxn]; int n,w; bool cmp(node one,node two) { return one.v<two.v; } int ans[maxn]; int main() { gg(n); gg(w); repd(i,1,n) { gg(a[i].v); a[i].id=i; } sort(a+1,a+1+n,cmp); repd(i,1,n) { ans[a[i].id]=(a[i].v+1)>>1; w-=ans[a[i].id]; } if(w<0) { printf("-1 "); return 0; } for(int i=n;i>=1;i--) { if(w>=(a[i].v-ans[a[i].id])) { w-=(a[i].v-ans[a[i].id]); ans[a[i].id]=a[i].v; }else { // w=0; ans[a[i].id]+=w; w=0; } } repd(i,1,n) { printf("%d ",ans[i] ); } return 0; } inline void getInt(int* p) { char ch; do { ch = getchar(); } while (ch == ' ' || ch == ' '); if (ch == '-') { *p = -(getchar() - '0'); while ((ch = getchar()) >= '0' && ch <= '9') { *p = *p * 10 - ch + '0'; } } else { *p = ch - '0'; while ((ch = getchar()) >= '0' && ch <= '9') { *p = *p * 10 + ch - '0'; } } }