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次询问,每次给你两个长宽相同的子矩阵,问你它们是恰好有一位不同,还是完全相同,还是有多于一位不同. 对每行分别哈希,先一行一行地尝试匹配,如果恰好发现有一行无法对应,再对那一 ...
随机推荐
- 【MongoDB】嵌套数组查询方案
From:http://stackoverflow.com/questions/12629692/querying-an-array-of-arrays-in-mongodb 数据 db.multiA ...
- Effective C++ Item 10,11 Have assignment operators return a reference to *this Handle assignment to self in operator =
If you want to concatenate assignment like this int x, y, z; x = y = z = 15; The convention is to ma ...
- 编程之美 set 8 区间重合判断
Leetcode 上有连续的两道题, 一个是 insert interval, 一个是 merge interval, 其中 insert interval 是 merge interval. 其中 ...
- 第十篇:Linux中权限的再讨论( 上 )
前言 在Linux系统中,用户分为 个权限位.好了,很多朋友对于Linux权限的了解就仅限于此了.但,Linux目录权限和文件权限一样吗?内核对于权限的检查过程又是怎样的? 如果你不清楚,本文将为你解 ...
- 如何使用phpmyadmin建立外键约束
之前都是用sql语句进行的主外键的关联,现在用可视化的phpmyadmin感觉方便了很多,但是在做主外键约束的时候却十中找不到操作在哪里.网上搜索的也是千奇百怪五花八门的,都说的很晦涩,很多都说需要使 ...
- JSP状态管理 及 Cookie实例
HTTP协议的无状态性 无状态是指,当浏览器发送请求给服务器的时候,服务器响应客户端的请求. 但是当同一个浏览器再次发送请求给了服务器的时候,服务器并不知道它就是刚才那个浏览器. 简单地说,就是服务器 ...
- java项目规范
一.命名规范 1. 项目名全部小写 2. 包名全部小写 3. 类名首字母大写,如果类名由多个单词组成,每个单词的首字母都要大写. 如:public class MyFirstClass{} 4. 变量 ...
- python 时间与时间戳之间的转换
https://blog.csdn.net/kl28978113/article/details/79271518 对于时间数据,如2016-05-05 20:28:54,有时需要与时间戳进行相互的运 ...
- CentOS7系统基本操作
查看网卡命令 [root@localhost ~]# ip addr 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state ...
- MobileNet
MobileNet (Efficient Convolutional Neural Networks for Mobile Vision Applications)--Google CVPR-2017 ...