贪心之------Tea Party

这道题大致意思就是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
*/
发布了30 篇原创文章 · 获赞 8 · 访问量 1288

猜你喜欢

转载自blog.csdn.net/qq_33961229/article/details/98068672