CodeForces - 913C(学长买酒)

    科技2026-02-17  14

    链接:https://vjudge.net/contest/398847#problem/B 题意:林学长要买mL的酒,有n种酒,单价为a1~ai;每种酒的升数为2^i,求至少买m升酒的最小花费。 思路:贪心 选择性价比最高的酒进行购买,一直买到性价比最低的。出现不能整除就分为两种:一种再买一瓶当前的酒;另一种为 余下的升数买下一种性价比低的酒

    #include<bits/std.c++> using namespace std; #define IOS ios::sync_with_stdio(false) #define ll long long const int inf=0x3f3f3f3f; const long long linf=0x3f3f3f3f3f3f3f3f; struct wine{ ll c,v; }; bool compare(wine a, wine b){ return a.c*b.v<a.v*b.c; } wine a[35]; ll ans=linf, res,sum; int main(){ int n, m; cin>>n>>m; for(int i=0;i<n;i++){ cin>>a[i].c; a[i].v=1<<i; } sort(a,a+n,compare); res=m; int l=0; while(l<n&&res>0){ sum+=res/a[l].v*a[l].c; res%=a[l].v; if(res==0){ ans=min(sum,ans); } else{ ans=min(ans,sum+a[l].c); } l++; } cout<<ans; return 0; }
    Processed: 0.022, SQL: 9