Educational Codeforces Round 21 C. Tea Party 貪心

題目:

C. Tea Party
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard 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.


這個題,對於前四個條件還是很好滿足的,最後一個條件要求大杯子裏的水,不能比小杯子小,那麼很顯然要排序,然後很好想的一個策略就是先把大杯子倒滿,然後如果還有水,就倒次小杯子。注意的是輸出的時候,是按ID來的,所以我們應該弄個結構體來封裝杯子的標號,杯子的容積,杯子已經有的水。然後怎樣保證輸出的時候方便?

用一個map<a,b>m,先sort後,a表示杯子的標號,b表示排序後杯子的id,建立一個由a→b的一個hash

code:

#include<cstdio>
#include<map>
#include<algorithm>
#define MIN(a,b) (a<b?a:b)
using namespace std;
const int MAXN=100+5;
struct friends{
    int ID,cup,has;
}q[MAXN];
int a[MAXN],n,w;
bool cmp(friends x,friends y){
    return x.cup<y.cup;
}
int main(){
    scanf("%d%d",&n,&w);
    for(int i=0;i<n;++i){
        q[i].ID=i;
        scanf("%d",&q[i].cup);
    }
    sort(q,q+n,cmp);
    map<int,int>m;
    for(int i=0;i<n;++i)
        m[q[i].ID]=i;
    //ID -> i
    int sum=0;
    for(int i=0;i<n;++i){
        sum+=(q[i].cup-1)/2+1;
        q[i].has=(q[i].cup-1)/2+1;
    }
    if(sum>w){
        printf("-1\n");
        return 0;
    }
    w-=sum;
    for(int i=n-1;i>=0;--i){
        int pour=min(q[i].cup-q[i].has,w);//min(杯子最多還能裝多少,還剩多少水)
        w-=pour;
        q[i].has+=pour;
        if(w==0)break;
    }
    if(w>0){
        printf("-1\n");
    }else{
        for(int i=0;i<n;++i)
            printf("%d ",q[m[i]].has);//現在的i代表杯子原來的ID噢
    }
    return 0;
}

//ps:其實沒必要用map,就用普通的數組就可以了,只不過養成了壞習慣。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章