The 15th UESTC Programming Contest Preliminary M - Minimum C0st cdoj1557

地址:http://acm.uestc.edu.cn/#/problem/show/1557

题目:

Minimum C0st

Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others)

There are NN numbers where the ithith is AiAi.

In order to make the sum of them changed to SS, you can make some numbers increased by one or decreased by one.

But you should spend CiCi dollars to increase or decreased AiAi by one, and the new AiAi should satisfies LiAiRiLi≤Ai≤Ri.

Any number can be operated any times. What's the minimum cost to make the sum of all numbers changed to SS?

Input

The first line contains two integers N,SN,S,

Next NN lines each line contains four integers Ai,Ci,Li,RiAi,Ci,Li,Ri.

1N1000,1S106,1Ci1000,0LiAiRi10001≤N≤1000,1≤S≤106,1≤Ci≤1000,0≤Li≤Ai≤Ri≤1000

Output

If there is no solutions, print impossible.

Otherwise, print one integer indicates the minimum cost to make the sum of all numbers changed to SS.

Sample input and output

Sample InputSample Output
3 6
1 1 1 3
1 2 1 3
1 3 1 3
4

Source

The 15th UESTC Programming Contest Preliminary
 
题目:排序后贪心即可
 
代码:
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;

struct nmb
{
    int dat,cost,l,r;
}a[1010];

bool cmp(nmb a,nmb b)
{
    return a.cost<b.cost;;
}

int n,s,sum,ans,last;

int main()
{
    cin>>n>>s;
    for(int i=1;i<=n;i++)
    {
        scanf("%d%d%d%d",&a[i].dat,&a[i].cost,&a[i].l,&a[i].r);
        sum+=a[i].dat;
    }
    sort(a+1,a+n+1,cmp);
    sum-=s;
    sum=-sum;
    if(sum<0)
    {
        for(int i=1;i<=n;i++)
        {
            if(sum+a[i].dat-a[i].l>0)
            {
                last=i;
                break;
            }
            ans+=(a[i].dat-a[i].l)*a[i].cost;
            sum+=a[i].dat-a[i].l;
        }
        ans-=sum*a[last].cost;
        if(last!=0)
            sum=0;
    }
    else if(sum>0)
    {
        for(int i=1;i<=n;i++)
        {
            if(sum+a[i].dat-a[i].r<0)
            {
                last=i;
                break;
            }
            ans+=(a[i].r-a[i].dat)*a[i].cost;
            sum-=a[i].r-a[i].dat;
        }
        ans+=sum*a[last].cost;
        if(last!=0)
            sum=0;
    }
    if(sum!=0)
    {
        cout<<"impossible"<<endl;
        return 0;
    }
    cout<<ans<<endl;
    return 0;
}

 

 

转载于:https://www.cnblogs.com/weeping/p/6629468.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值