codeforces 349B(贪心)

时间:2022-07-28
本文章向大家介绍codeforces 349B(贪心),主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

题意描述

Igor has fallen in love with Tanya. Now Igor wants to show his feelings and write a number on the fence opposite to Tanya’s house. Igor thinks that the larger the number is, the more chance to win Tanya’s heart he has.

Unfortunately, Igor could only get v liters of paint. He did the math and concluded that digit d requires a d liters of paint. Besides, Igor heard that Tanya doesn’t like zeroes. That’s why Igor won’t use them in his number.

Help Igor find the maximum number he can write on the fence.

每个数字都有各自的价钱,你有n块钱,问你能够获得的最大数字是多少,如果不能获得,输出-1

思路

要想数字最大,最重要的是数字的位数,其次才是数字的大小,所以我们可以先找出最小的价钱,得到最多位数,然后再依次替换每一位。

AC代码

#include<bits/stdc++.h>
#define x first
#define y second
#define PB push_back
#define mst(x,a) memset(x,a,sizeof(x))
#define all(a) begin(a),end(a)
#define rep(x,l,u) for(ll x=l;x<u;x++)
#define rrep(x,l,u) for(ll x=l;x>=u;x--)
#define IOS ios::sync_with_stdio(false);cin.tie(0);
using namespace std;
typedef unsigned long long ull;
typedef pair<int,int> PII;
typedef pair<long,long> PLL;
typedef pair<char,char> PCC;
typedef long long ll;
const int N=1e6+10;
const int M=1e6+10;
const int INF=0x3f3f3f3f;
int a[10];
void solve(){
    int n;cin>>n;
    int MIN=INF,min_val=0;
    rep(i,1,10){
        cin>>a[i];
        if(a[i]<=MIN){
            MIN=a[i];
            min_val=i;
        }
    }
    if(n<MIN){
        cout<<-1<<endl;
        return;
    }
    vector<int> ans;
    while(1){
        if(n<MIN) break;
        n-=MIN;
        ans.PB(min_val);
    }
    rep(i,0,ans.size()){
        rrep(j,9,1){
            if(a[j]-MIN<=n){
                ans[i]=j;
                n-=(a[j]-MIN);
                break;
            }
        }
    }
    sort(all(ans));
    reverse(all(ans));
    rep(i,0,ans.size()) cout<<ans[i];
    cout<<endl;
}
int main(){
    IOS;
    solve();
    return 0;
}