随笔—邀请赛前训—Duff and Meat

题目的意思是给你多组清单,每组包含所需食物的数量,和当天食物的单价,可以囤积食物。求花最少的钱完成清单。

解决这道题目的策略是从头到尾扫描一遍这些清单,再遇到更便宜的价格前都用之前最便宜的价格买食物,再更新当前最便宜的价格,以此类推下去。

数组a[ ]代表各天地食物需求

数组b[ ]代表各天地食物单价

变量cheap代表当前最便宜的单价是哪天

#include<cstdio>
#include<cstring>
#include<iostream>

using namespace std;

#define  MAX(x,y) (((x)>(y)) ? (x) : (y))
#define  MIN(x,y) (((x) < (y)) ? (x) : (y))
#define ABS(x) ((x)>0?(x):-(x))

const int inf = 0x7fffffff;

const int N=100000+10;
int a[N];
int b[N];
int main()
{
    int n;
    cin>>n;
    for(int i=0; i<n; i++){
        scanf("%d%d",a+i,b+i);
    }
    int cheap=0;
    int cnt=a[0];
    long long ans=0;
    for(int i=1; i<n; i++){
        if(b[i] < b[cheap] ){
            ans += b[cheap]*cnt;
            cnt=a[i];
            cheap=i;
        }
        else{
            cnt += a[i];
        }
//        cout<<"ans= "<<ans<<endl;
    }
    ans += b[cheap]*cnt;
    cout<<ans<<endl;
    return 0;
}
原文地址:https://www.cnblogs.com/shawn-ji/p/5537215.html