CF808C

1 篇文章 0 订阅
/*
F - Tea Party CodeForces - 808C
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.

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个杯子,每个杯子都有自己的容量,有w毫升的茶,将w毫升的茶分给每个杯子。
使得杯子越大,放入的茶叶越多,每个杯子的茶至少有一半,放入的茶的体积为整数

贪心构造
*/

#include<iostream>
#include<queue>
#include<cstring>
#include<vector>
#include<string>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<set>
#include<cstdlib>
#include<stack>
#include<map>
#include<functional>
#define make_pair m_p
#define deb cout<<"T"<<endl
using namespace std;
typedef long long LL;
const int maxsize = 1e6+7;
const int INF = 0x3f3f3f3f;
const int EXP = 1e-8;

int A[maxsize];
int Index[maxsize];
int n,w;
bool cmp(const int &a ,const int &b)
{
    return A[a]<A[b];
}
int Ans[maxsize];

int main()
{
    freopen("finput.txt","r",stdin);
    while(~scanf("%d%d",&n,&w))
    {
        memset(Ans,0,sizeof Ans);
        bool flag = true;
        for(int i=0;i<n;i++)
        {
            scanf("%d",&A[i]);
        }
        for(int i=0;i<n;i++) Index[i] = i;
        sort(Index,Index+n,cmp);
        for(int i=0;i<n;i++)
        {
            int id = Index[i];
            int v = A[id]/2;
            if(A[id]%2)
                v++;
            if(w>=v)
            {
                w -= v;
                Ans[id]+=v;
            }
            else
            {
                flag = false;
                break;
            }
        }
        if(!flag)
        {
            cout<<-1<<endl;
            continue;
        }
        //多余的水从杯子大的到杯子小的方向给
        for(int i=n-1;i>=0&&w>0;i--)
        {
            int id = Index[i];
            int v = min(A[id]-Ans[id],w);
            w -= v;
            Ans[id] += v;
        }
        for(int i=0;i<n;i++)
        {
            if(i) cout<<" ";
            cout<<Ans[i];
        }
        cout<<endl;
    }

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值