Codeforces 1113A. Sasha and His Trip 题解

原题链接:Codeforces 1113A. Sasha and His Trip
题目大意:你有一个油箱容积为\(V\)的车,每从第\(i\)个城市到第\(i+1\)个城市需要耗费一个单位的油,在第\(i\)个城市加油为\(i\)元每个单位,问从\(1\)号城市到\(n\)号城市最少花费。
题解:很明显的一个贪心,毕竟我们尽量早加油,因为费用便宜,所以,就直接给代码了:

#include <cstdio>
int mn(int a,int b){
    return a<b?a:b;
}
int mx(int a,int b){
    return a>b?a:b;
}
int main(){
    int n,v;
    scanf("%d%d",&n,&v);
    if(v>n-1){
        v=n-1;
    }
    int ans=0;
    int now=0;
    for(int i=1;i<n;i++){
        if(now<n-i){
            ans+=(v-now)*i;
            now=v;
        }
        now--;
    }
    printf("%d\n",ans);
    return 0;
}
上一篇:CF1109D Sasha and Interesting Fact from Graph Theory


下一篇:CF718C Sasha and Array [线段树+矩阵]