贪心之------Tea Party

该博客讨论了一道关于如何公平分配茶水给不同容积杯子的问题,需要满足每个杯子至少装满一半且不浪费茶水。博主首先尝试了一种错误的贪心策略,然后意识到应该按照杯子容积大小依次倒水,并确保不超过杯子的容积。最后提到了错误代码和正确解法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这道题大致意思就是N个杯子的容积和茶壶的容积,要满足三个条件(1.每一个杯子里面的茶必须严格大于它容积的一半 2.茶壶往所有杯子到满水后没有生于地茶 3.所有的客人必须满意) 使客人满意的话,就是一个杯子里面的茶水必须大于任意一个容积小于它的杯子里面的茶水

Tea Party
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.

思路其实就是用贪心做 我们先往每一个杯子倒上它一半体积的水,然后去改变第一个杯子的茶水量就行了 ,注意这个思路后面是错误的,因为没一个杯子都有自己的容积,所以倒茶水不能超过自己杯子容积,刚开始实在没有注意到,所以WA在了第43组,后来自己又举了几个样例才发现这个,后来修改了一下,只要从体积大小依次倒水(倒水的时候与剩下的可装茶水两和还剩多少茶水进行取小就行了)

这个是错误的代码,警戒自己。

#include<bits/stdc++.h>
#define lson rt<<1,l,m
#define rson rt<<1|1,m+1,r
#define mem(a,b) memset(a,b,sizeof(a))
#define LL long long
#define inf 0x3f3f3f3f
using namespace std;

const int maxn=3e3;

int a[maxn];
struct node
{
    int v,pos;
} bei[maxn*2];
bool cmp(node a,node b)
{
    return a.v>b.v;
}
int main()
{
    int n,V,i,x,y,j;
    while(~scanf("%d%d",&n,&V))
    {
        mem(bei,0);
        for(i=1; i<=n; i++)
        {
            scanf("%d",&a[i]);
            if(a[i]%2==1)
                x=(a[i]+1)/2;
            else
                x=a[i]/2;
            bei[i].v=x;
            bei[i].pos=i;
        }
        sort(bei+1,bei+1+n,cmp);
        int sum=0,p=bei[1].v;
        for(i=1; i<=n; i++)
        {
            if(a[i]/2==p)
            {
                y=i;
                break;
            }
        }
        for(i=1;i<=n;i++)
            sum+=bei[i].v;
        if(sum>V)
            puts("-1");
        else
        {
            for(i=1;i<=n;i++)
            {
                if(y==bei[i].pos)
                    bei[i].v+=V-sum;
            }
            for(i=1; i<=n; i++)
            {
                for(j=1; j<=n; j++)
                {
                    if(bei[j].pos==i)
                        printf("%d%c",bei[j].v,i==n?'\n':' ');
                }
            }
        }
    }
    return 0;
}

这个才是正确AC的

#include<bits/stdc++.h>
#define lson rt<<1,l,m
#define rson rt<<1|1,m+1,r
#define mem(a,b) memset(a,b,sizeof(a))
#define LL long long
#define inf 0x3f3f3f3f
using namespace std;

const int maxn=3e3;

int a[maxn];
struct node
{
    int num,v,pos;
} bei[maxn*3];
bool cmp(node a,node b)
{
    return a.num>b.num;
}
bool cop(node a,node b)
{
    return a.pos<b.pos;
}
int main()
{
    int n,V,i,x;
    while(~scanf("%d%d",&n,&V))
    {
        mem(bei,0);
        int sum=0;
        for(i=1; i<=n; i++)
        {
            scanf("%d",&x);
            bei[i].num=x;
            if(x%2==1)
                x++;
            bei[i].v=x/2;
            bei[i].pos=i;
            sum+=bei[i].v;
        }
        sort(bei+1,bei+1+n,cmp);
        if(sum>V)
            puts("-1");
        else
        {
            sum=V-sum;
            for(i=1;i<=n;i++)
            {
                int y=min(bei[i].num-bei[i].v,sum);
                bei[i].v+=y;
                sum-=y;
                if(sum<=0)
                    break;
            }
            if(sum!=0)
                puts("-1");
            else
            {
                sort(bei+1,bei+1+n,cop);
                for(i=1;i<=n;i++)
                    printf("%d%c",bei[i].v,i==n?'\n':' ');
            }
        }
    }
    return 0;
}
/*
3 15
6 7 8
2 10
8 7
4 4
1 1 1 1
3 10
9 8 10
*/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值