Codeforces 588.A Duff and Meat
来源:互联网 发布:留美幼童 知乎 编辑:程序博客网 时间:2024/06/06 07:09
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 pidollars per kilogram. Malek knows all numbers a1, ..., an and 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.
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.
Print the minimum money needed to keep Duff happy for n days, in one line.
31 32 23 1
10
31 32 13 2
8
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.
题意:给出每天的肉的需要的量和每天肉的价格,求在n天后所花金额最少。
分析:该题贪心解决。自上往下遍历,若后几天的肉的价格均大于今天的价格,则今天买完后几天的肉。遍历直到遇到一天价格小于今天的价格再开始一轮遍历。总的n数遍历一次即可,时间复杂度为O(logn)。见AC代码:
#include<stdio.h>const int maxn=100005;int a[maxn],p[maxn];int main(){int n;while(~scanf("%d",&n)){for(int i=0; i<n; i++)scanf("%d%d",&a[i],&p[i]);long long res=0;for(int i=0; i<n; i++){int sum=0,j,s=p[i];//以s保存初始值 因为i会发生月前 导致加上的值发生改变for( j=i; j<n; j++){if(p[j]>=p[i])sum+=a[j];else{i=j-1;break;//break后 i++}}res+=sum*s;if(j==n)//当某个i值一直遍历到最后时 需要将其的值更新为n-1 即退出整个循环i=n-1;}printf("%I64d\n",res);}}在书写代码的时候遇到一些问题。贪心循环时i的跳转、变量的区间和值的改变千万需要注意。注意以一个价格遍历至最后时需要更新i的值,要不然会重复计算导致运算出错。
在书写代码时需要严谨慎重,周全考虑每一步后再书写胜过出错调试。贪心这方面还是需要多敲多练。
特记下,以备后日回顾。
- Codeforces 588.A Duff and Meat
- Codeforces#326-A - Duff and Meat-贪心
- Codeforces Round #326 A. Duff and Meat
- Codeforces 588 A. Duff and Meat
- CodeForces-588A Duff and Meat
- CodeForces 588A Duff and Meat 贪心
- Codeforces 558 A Duff and Meat
- codeforces 588 A Duff and Meat
- Codeforces 588A Duff and Meat 【dp】
- CodeForces 588A. Duff and Meat
- A. Duff and Meat
- A. Duff and Meat
- 588A. Duff and Meat
- Codeforces Round #326 (Div. 2) A. Duff and Meat
- Codeforces Round #326 (Div. 2) 588 A. Duff and Meat
- Codeforces Round #326 (Div. 2) A. Duff and Meat
- Codeforces Round #326 (Div. 2) A. Duff and Meat
- 【Codeforces Round 326 (Div 2)A】【贪心】Duff and Meat 屯肉前溯花费最低
- 简单的动画
- 四种方案解决ScrollView嵌套ListView问题
- 在block中执行push操作崩溃
- Java Basics Part 7/20 - Modifier Types
- 长连接 概念
- Codeforces 588.A Duff and Meat
- Educational Codeforces Round 15 D 数学推导
- 集合框架和泛型框架
- mysql 结果集去重复值并合并成一行
- 真机测试Monkey
- 仿滴滴打车 车辆运动
- A. Robot Bicorn Attack
- Effetive Objective - C 1.Objective 16.全能初始化方法
- 浅析深究什么是中间件 发布时间: 2016-06-10 作者:本站编辑 浏览次数:128 摘要: 因为工作的原因,我从金蝶集团调入金蝶中间件公司工作以来,经常遇到一个问题就是中间件公司是个什么