题意:卖酸奶,有n周,每一周可以生产任意单位的酸奶,当前周生产1单位酸奶需要的费用是c,会客户共需要y单位的酸奶,
也可以将酸奶储存下来,可以储存任意长时间,任意多的酸奶,每周的储存费用都是一个常数S (答案会超int)
思路:
这道题说是贪心,但再限制几个条件就只能用dp做了,其实把这道题看成dp也没问题
我们考虑第i周,令当前周的最小花费为cost[i],那么很显然,cost[i]=min(cost[i-1],c[i])
没有必要存下数组把所有cost都处理,直接边输边做就可以了
代码:
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<algorithm>
const int MAXN=10005;
typedef long long LL;
using namespace std;
LL N,S,y[MAXN],cost=0x3f3f3f3f;
LL ans,c[MAXN];
int main()
{
scanf("%I64d%I64d",&N,&S);
for(int i=1;i<=N;i++)
{
scanf("%I64d%I64d",&c[i],&y[i]);
cost=min(c[i],cost);
ans+=cost*y[i]; cost+=S;
}
printf("%I64d",ans);
}