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次询问,每次给你两个长宽相同的子矩阵,问你它们是恰好有一位不同,还是完全相同,还是有多于一位不同. 对每行分别哈希,先一行一行地尝试匹配,如果恰好发现有一行无法对应,再对那一 ...
随机推荐
- TPS和事务响应时间的关系
例子:一个高速路有10个入口,每个入口每秒钟只能进1辆车 1.请问1秒钟最多能进几辆车? TPS=10 2.每辆车需要多长时间进行响应? reponse time = 1 3.改成20辆车 ...
- 论坛模块__发帖时使用FCKeditor
论坛模块__发帖时使用FCKeditor 测试 <html> <head> <meta http-equiv="content-type" conte ...
- C++ enum 枚举类型
1. 枚举类型浅谈 假设我们要设计一个打开文件的函数, 打开文件由三种状态: input, output 和 append. 不使用枚举, 我们可能会写出如下的代码 const int input = ...
- __del__()
__del__() 是类的内置函数,用于定义在脚本退出之前要执行的代码,因为有这个特性,通常被用来在脚本退出前关闭文件.关闭数据库连接.关闭网络连接等操作 [root@localhost ~]$ ca ...
- sql中IN的含义
in后面可以是查询集合,也可以是诸如('1','2','3','4') 例: UPDATE CC_PROBLEM_INFO SET Z_STATUS = 1 WHERE CONTENT_ID IN ( ...
- Mysql 知识点总结
1.创建数据库 mysqladmin 下面是一个简单的例子,创建名为 yiibai_tutorials1 的数据库. D:\software\mysql--winx64\bin> mysq ...
- 四 Android Studio打包EgretApp (热更新)
官网教程: http://developer.egret.com/cn/github/egret-docs/Native/native/hotUpdate/index.html 和Eclipse一样, ...
- Android开源天气预报应用Weather-Lite
由于最近繁忙的工作告一段落,要清闲一段时间,所以可以好好利用这段时间给自己充充电. 于是乎这个app就诞生了.本着开源的精神我把它放在了github上.这里如果有Android初学者的话不妨学习一下, ...
- C++中,将单精度浮点数转换成2进制数
在C++里,实数(float)是用四个字节即三十二位二进制位来存储的.其中有1位符号位,8位指数位和23位有效数字位.实际上有效数字位是24位,因为第一位有效数字总是"1",不必存 ...
- Hadoop伪分布安装详解(二)
目录: 1.修改主机名和用户名 2.配置静态IP地址 3.配置SSH无密码连接 4.安装JDK1.7 5.配置Hadoop 6.安装Mysql 7.安装Hive 8.安装Hbase 9.安装Sqoop ...