Duff and Meat(贪心)
Duff is addicted to meat! Malek wants to keep her happy for n days. In order to be happy in i-th day, she needs to eat exactly ai kilograms of meat.
There is a big shop uptown and Malek wants to buy meat for her from there. In i-th day, they sell meat for pi dollars per kilogram. Malek knows all numbers a1, ..., anand p1, ..., pn. In each day, he can buy arbitrary amount of meat, also he can keep some meat he has for the future.
Malek is a little tired from cooking meat, so he asked for your help. Help him to minimize the total money he spends to keep Duff happy for n days.
Input
The first line of input contains integer n (1 ≤ n ≤ 105), the number of days.
In the next n lines, i-th line contains two integers ai and pi (1 ≤ ai, pi ≤ 100), the amount of meat Duff needs and the cost of meat in that day.
Output
Print the minimum money needed to keep Duff happy for n days, in one line.
Examples
Input
- 3
- 1 3
- 2 2
- 3 1
Output
- 10
Input
- 3
- 1 3
- 2 1
- 3 2
Output
- 8
Note
In the first sample case: An optimal way would be to buy 1 kg on the first day, 2 kg on the second day and 3 kg on the third day.
In the second sample case: An optimal way would be to buy 1 kg on the first day and 5 kg (needed meat for the second and third day) on the second day.
用贪心的思想来想,如果肉的价格低的话可以按照低的囤货,从而保证当前是价格最低的,也就是当前最优解,从而最后得到的一定也是最优解
代码:
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
int main() {
int n;
cin>>n;
int a,p;
int sum=0;
int min=0x3f3f3f;
for(int t=0; t<n; t++) {
scanf("%d%d",&a,&p);
if(p<min) {
min=p;
}
sum+=a*min;
}
cout<<sum<<endl;
return 0;
}
Duff and Meat(贪心)的更多相关文章
- Codeforces Round #326 (Div. 2) A. Duff and Meat 水题
A. Duff and Meat Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/588/probl ...
- A. Duff and Meat
A. Duff and Meat time limit per test 1 second memory limit per test 256 megabytes input standard inp ...
- Codeforce 588A - Duff and Meat (贪心)
Duff is addicted to meat! Malek wants to keep her happy for n days. In order to be happy in i-th day ...
- Codeforces Round #326 (Div. 2)-Duff and Meat
题意: Duff每天要吃ai千克肉,这天肉的价格为pi(这天可以买好多好多肉),现在给你一个数值n为Duff吃肉的天数,求出用最少的钱满足Duff的条件. 思路: 只要判断相邻两天中,今天的总花费 = ...
- 题解 CF588A 【Duff and Meat】
题意 有一个人,想吃 $n$ 天肉,第 $i$ 天需要吃 $a[i]$ 块肉,第 $i$ 天超市肉价为每块 $b[i]$ 元,买来的肉可以留到后面吃,求这个人在每天都吃到肉的情况下花费的最小值. 题目 ...
- Duff and Meat - CF 588A
题目大意:有一个人他有每天需要吃ai千克肉,并且当天肉的价格是pi,问N天后他至少需要花费多少钱买肉. 分析:注意一下,他是可以提前买好肉放着的. 代码如下: #include<iostream ...
- 省钱加油(Fuel Economy)题解
题目 农夫约翰决定去做一个环游国家旅行,为了不让他的奶牛们感到孤单,于是他决定租一辆货车带领他的奶牛们一起去旅行.这辆货车的油箱最多可以承载G 个单位的油,同时为了简化问题,规定每一个单位的油可以行使 ...
- Codeforces Round #326 (Div. 2) C. Duff and Weight Lifting 水题
C. Duff and Weight Lifting Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest ...
- "贪心"的考试
先来落实比较简单的T3,T4 T3 团结的队伍 team 题目描述 众所周知,长郡信息组是一个团结友爱的优秀团队.为了弘扬团队精神,践行社会主义核心价值观,秉承朴实沉毅的校训,信息组决定外出游玩(. ...
随机推荐
- POJ1041 John's trip
John's trip Language:Default John's trip Time Limit: 1000MS Memory Limit: 65536K Total Submissions: ...
- bzoj 3307: 雨天的尾巴 线段树合并
题目大意: N个点,形成一个树状结构.有M次发放,每次选择两个点x,y对于x到y的路径上(含x,y)每个点发一袋Z类型的物品.问完成所有发放后,每个点存放最多的是哪种物品. 题解: 首先我们为每一个节 ...
- 冷备手工完全恢复(recover database,recover tablespace,recover datafile)
冷备手工完全恢复 1. 手工完全恢复三种级别: recover database: 所有或大部分datafile丢失,一般是在mount状态完成.recover tablespace: 非关 ...
- spark减少提交jar包处理
spark一个应用,算上依赖一百多兆.每一次都如此,坑. 首先是<packing>jar</packing>这只为打包为jar,在plugin中增加一个assembly插件,这 ...
- YARN指令
如果是使用了Cloudera来安装到此路径下: /opt/cloudera/parcels/CDH-5.10.2-1.cdh5.10.2.p0.5/bin 执行: sudo ./yarn applic ...
- 问题7:如何实现用户的历史记录功能(最多n条)
实例:制作猜字游戏,添加历史记录功能,显示用户最近猜过的数字 解决方案:使用容量为n的队列存储历史记录 使用标准库colections中的deque,一个双端循环队列 程序退出前,可以使用pickle ...
- 排序----demo----
排序1---冒泡法: 单向冒泡排序的基本原理就是:对于给定的n个数据,从第一个数据开始一次对相邻的两个数据进行比较,当前面的数据大于后面的数据时,交换位置,进行一轮比较和换位后,n个数据中最大的那个被 ...
- [hdu4960]Another OCD Patient(区间dp)
题意:给出n个数,把这n个数合成一个对称的集合.每个数只能合并一次. 解题关键:区间dp,dp[l][r]表示l-r区间内满足条件的最大值.vi是大于0的,所以可以直接双指针确定. 转移方程:$dp[ ...
- 具体问题:3、hibernate跟Mybatis/ ibatis 的区别,为什么选择?
第一章 Hibernate与MyBatis Hibernate 是当前最流行的O/R mapping框架,它出身于sf.net,现在已经成为Jboss的一部分. Mybatis 是另外一种优秀 ...
- Entity Framework Code-First(9.7):DataAnnotations - Table Attribute
DataAnnotations - Table Attribute: Table attribute can be applied to a class. Default Code-First con ...