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 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 |
4 |
Source
#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的更多相关文章
- 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 ...
- The 15th UESTC Programming Contest Preliminary J - Jermutat1on cdoj1567
地址:http://acm.uestc.edu.cn/#/problem/show/1567 题目: Jermutat1on Time Limit: 3000/1000MS (Java/Others) ...
- 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) ...
- 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) ...
- 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 ...
- 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 ...
- 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 ...
- 【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 ...
- 【字符串哈希】The 16th UESTC Programming Contest Preliminary F - Zero One Problem
题意:给你一个零一矩阵,q次询问,每次给你两个长宽相同的子矩阵,问你它们是恰好有一位不同,还是完全相同,还是有多于一位不同. 对每行分别哈希,先一行一行地尝试匹配,如果恰好发现有一行无法对应,再对那一 ...
随机推荐
- 编程之美 set 11 买书问题
题目 书店搞促销, 同时购买多卷书时, 有机会享受优惠 2本优惠 5%, 3本 10%, 4 本 20% 5 本 25% 设计算法, 求解购买一本书的最低价格 分析 1. 第一个感觉是一次购买的越多省 ...
- C语言字符数组和字符串
用来存放字符的数组称为字符数组,例如: char a[10]; //一维字符数组 char b[5][10]; //二维字符数组 char c[20]={'c', ' ', 'p', 'r', 'o' ...
- instance method '*****' not found (return type defaults to 'id')
博文转载至 http://blog.csdn.net/cerastes/article/details/38025801 return type defaultsnot foundiOSwarning ...
- 传参方法:sharedApplication, NSUserDefaults, protocol 和 delegate(实例)
本文转载至 http://blog.csdn.net/learnios/article/details/8442201 分类: 功能模块2012-12-27 10:22 109人阅读 评论(0) 收 ...
- .net Session延长过期时间
一.全局网站(即服务器)级 IIS-网站-属性-Asp.net-编辑配置-状态管理-会话超时(分钟)-设置为120,即为2小时,即120分钟后如果当前用户没有操作,那么Session就会自动过期. 二 ...
- 【BZOJ4551】[Tjoi2016&Heoi2016]树 并查集
[BZOJ4551][Tjoi2016&Heoi2016]树 Description 在2016年,佳媛姐姐刚刚学习了树,非常开心.现在他想解决这样一个问题:给定一颗有根树(根为1),有以下两 ...
- Microsoft License Keys – Volume
VLK Product Group Product KeyOffice XP Applications P3HBK-F86Y2-374PQ-KW92R-B36VTOffice 2003 Suites ...
- ios 将图片做成圆形
UIImageView * imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"oiuyfdsa.png ...
- dbForge mysql数据库比对
Comparison选项卡,新建一个表结构比较, (将source库的表结构变化应用到target库) 下面示例中,source用positec_uat, target用positec_pro ...
- Javascript-const 常量
const 常量 常量是块级作用域,很像使用 let语句定义的变量.常量的值不能通过重新赋值来改变,并且不能重新声明. 此声明创建一个常量,其作用域可以是全局或本地声明的块. 与var变量不同,全局常 ...