codeforces 808C

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.

题意:

         给n个杯子倒茶,每个杯子的容量为ai,一共茶的容量为w,有以下规则:1,每杯中的茶的容量至少为杯子容量的一半;2,杯子中茶的容量为整数;3,所有的茶必须倒到杯子中;4,每个人都是满意的,也就是如果i的杯子容量比j的杯子容量大,则茶的容量也必须比j的容量大,否则就是不满意。

思路:

         贪心,先将每个杯子倒(ai+1)/2的茶,如果在所有杯子都倒够体积一半的茶前w<0,则输出-1,然后按杯子体积大小排序,从大到小往杯子里倒茶,第一大的满了就往第二大的杯子倒,以此类推知道w=0;

注意:

         记录杯子出现的初始位置,和输出格式。

#include<iostream>
#include<bits/stdc++.h>
using namespace std;
struct Node
{
    int v;
    int id;
}node[110];
int t[110];
bool cmp(Node x, Node y)
{
    return x.v>y.v;
}
int main()
{
    int n,w;
    cin.sync_with_stdio(false);
    while(cin>>n>>w)
    {
        for(int i=0;i<n;i++)
        {
            cin>>node[i].v;
            node[i].id=i;
        }
        bool flag=true;
        for(int i=0;i<n;i++)
        {
            if(node[i].v&1)
                t[i]=node[i].v/2+1, w-=t[i];

            else
                t[i]=node[i].v/2, w-=t[i];
            if(w<0)
            {
                flag=false;
                printf("-1\n");
                break;
            }
        }
        if(!flag)
            continue;
        sort(node,node+n,cmp);
        for(int i=0;i<n;i++)
        {
            if(w>(node[i].v-t[node[i].id]))
            {
                int d=node[i].v-t[node[i].id];
                 t[node[i].id]=node[i].v;
                 w-=d;
            }
            else
            {
                t[node[i].id]+=w;
                break;
            }

        }
        for(int i=0;i<n-1;i++)
            cout<<t[i]<<' ';
        cout<<t[n-1]<<endl;
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值