地址: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 Li≤Ai≤RiLi≤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.

1≤N≤1000,1≤S≤106,1≤Ci≤1000,0≤Li≤Ai≤Ri≤10001≤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 Input Sample 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[]; 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=;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+,a+n+,cmp);
sum-=s;
sum=-sum;
if(sum<)
{
for(int i=;i<=n;i++)
{
if(sum+a[i].dat-a[i].l>)
{
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!=)
sum=;
}
else if(sum>)
{
for(int i=;i<=n;i++)
{
if(sum+a[i].dat-a[i].r<)
{
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!=)
sum=;
}
if(sum!=)
{
cout<<"impossible"<<endl;
return ;
}
cout<<ans<<endl;
return ;
}

The 15th UESTC Programming Contest Preliminary M - Minimum C0st cdoj1557的更多相关文章

  1. The 15th UESTC Programming Contest Preliminary C - C0ins cdoj1554

    地址:http://acm.uestc.edu.cn/#/problem/show/1554 题目: C0ins Time Limit: 3000/1000MS (Java/Others)     M ...

  2. The 15th UESTC Programming Contest Preliminary J - Jermutat1on cdoj1567

    地址:http://acm.uestc.edu.cn/#/problem/show/1567 题目: Jermutat1on Time Limit: 3000/1000MS (Java/Others) ...

  3. The 15th UESTC Programming Contest Preliminary B - B0n0 Path cdoj1559

    地址:http://acm.uestc.edu.cn/#/problem/show/1559 题目: B0n0 Path Time Limit: 1500/500MS (Java/Others)    ...

  4. The 15th UESTC Programming Contest Preliminary K - Kidd1ng Me? cdoj1565

    地址:http://acm.uestc.edu.cn/#/problem/show/1565 题目: Kidd1ng Me? Time Limit: 3000/1000MS (Java/Others) ...

  5. The 15th UESTC Programming Contest Preliminary G - GC?(X,Y) cdoj1564

    地址:http://acm.uestc.edu.cn/#/problem/show/1564 题目: G - GC?(X,Y) Time Limit: 3000/1000MS (Java/Others ...

  6. The 15th UESTC Programming Contest Preliminary H - Hesty Str1ng cdoj1551

    地址:http://acm.uestc.edu.cn/#/problem/show/1551 题目: Hesty Str1ng Time Limit: 3000/1000MS (Java/Others ...

  7. The 15th UESTC Programming Contest Preliminary D - Destr0y City cdoj1558

    地址:http://acm.uestc.edu.cn/#/problem/show/1558 题目: D - Destr0y City Time Limit: 3000/1000MS (Java/Ot ...

  8. 【set】【可持久化Trie】The 16th UESTC Programming Contest Preliminary K - Will the circle be broken

    题意:You are given an array A of N non-negative integers and an integer M. Find the number of pair(i,j ...

  9. 【字符串哈希】The 16th UESTC Programming Contest Preliminary F - Zero One Problem

    题意:给你一个零一矩阵,q次询问,每次给你两个长宽相同的子矩阵,问你它们是恰好有一位不同,还是完全相同,还是有多于一位不同. 对每行分别哈希,先一行一行地尝试匹配,如果恰好发现有一行无法对应,再对那一 ...

随机推荐

  1. sdut 2153:Clockwise(第一届山东省省赛原题,计算几何+DP)

    Clockwise Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 Saya have a long necklace with ...

  2. hrbustoj 1142:围困(计算几何基础题,判断点是否在三角形内)

    围困 Time Limit: 1000 MS     Memory Limit: 65536 K Total Submit: 360(138 users) Total Accepted: 157(12 ...

  3. Windows下安装Apache 2.2.21图文教程

    https://www.jb51.net/article/52086.htm 本文详细介绍了在Windows平台上安装Apache的过程,希望对初次安装Apache的朋友有所帮助. 1. 软件准备 我 ...

  4. A*寻路算法(曼哈顿距离)

    前一些天,在群有人问到A*算法的问题. 之前我已经有实现过,并将之放到github上(https://github.com/XJM2013/A_Star).有兴趣的能够下载下来看看. 这里上传了一个相 ...

  5. ItcastOA_整体说明_准备环境

    1. 整体说明 1.1. 项目说明 1.1.1. OA概述 OA是Office Automation的缩写,本意为利用技术的手段提高办公的效率,进而实现办公的自动化处理.实现信息化.无纸化办公,可方便 ...

  6. Tiny4412 Android 5.0 编译系统学习笔记

    1.Android 编译系统概述 Build 系统中最主要的处理逻辑都在 Make 文件中,而其他的脚本文件只是起到一些辅助作用. 整个 Build 系统中的 Make 文件可以分为三类: ① Bui ...

  7. 【黑金原创教程】【TimeQuest】【第二章】TimeQuest模型角色,网表概念,时序报告

    声明:本文为黑金动力社区(http://www.heijin.org)原创教程,如需转载请注明出处,谢谢! 黑金动力社区2013年原创教程连载计划: http://www.cnblogs.com/al ...

  8. 【黑金原创教程】 FPGA那些事儿《概念篇》

    简介一本讲述非软硬片上系统的书,另外还是低级建模的使用手册. 目录[黑金原创教程] FPGA那些事儿<概念篇>:File01 - 结构的玩笑[黑金原创教程] FPGA那些事儿<概念篇 ...

  9. 领悟 JavaScript 中的面向对象

    JavaScript是基于对象的语言,我们可以使用面向对象的思想去开发js代码. JavaScript是基于对象的语言. 可以使用面向对象的思想,但是不少人对这一点理解得并不全面. 在 JavaScr ...

  10. Linux系统CPU核数等信息查看

    版权声明:本文为原创文章,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明. https://blog.csdn.net/fgf00/article/details/52584 ...