当前位置: 首页 > 工具软件 > Yogurt > 使用案例 >

P1376 [USACO05MAR]Yogurt factory 机器工厂

夏振国
2023-12-01

P1376 [USACO05MAR]Yogurt factory 机器工厂

解题思路:贪心。到达第i周的时候,判断一下是此时的C[i]*Y[i]低,还是往上的某一周到这一周的成本加存储费低。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double lf;
typedef unsigned long long ull;
typedef pair<ll,int>P;
const int inf = 0x7f7f7f7f;
const ll INF = 1e16;
const int N = 1e4+10;
const ull base = 131;
const ll mod =  1e9+7;
const double PI = acos(-1.0);
const double eps = 1e-4;

inline int read(){int x=0,f=1;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}return x*f;}
inline string readstring(){string str;char s=getchar();while(s==' '||s=='\n'||s=='\r'){s=getchar();}while(s!=' '&&s!='\n'&&s!='\r'){str+=s;s=getchar();}return str;}
int random(int n){return (int)(rand()*rand())%n;}
void writestring(string s){int n = s.size();for(int i = 0;i < n;i++){printf("%c",s[i]);}}
ll fast_power(ll a,ll p){
    ll ans = 1;
    while(p){
        if(p&1) ans = (ans*a)%mod;
        p >>= 1;
        a = (a*a)%mod;
    }
    return ans;
}

ll a[N],b[N];

int main(){
    //srand((unsigned)time(NULL));
    //freopen(  "out.txt","w",stdout);
    int n = read(),s = read();
    ll ans = 0;
    for(int i = 1;i <= n;i++){
        a[i] = read();b[i] = read();
    }
    int last = 1;
    ll num = a[1];
    ans += a[1]*b[1];
    for(int i = 2;i <= n;i++){
        if(b[i]*num+(i-last)*s*b[i] < a[i]*b[i]){
            ans += b[i]*num+(i-last)*s*b[i];
        }else {
            ans += a[i]*b[i];
            last = i;
            num = a[i];
        }
    }
    cout<<ans<<endl;
    return 0;
}

 类似资料: