codeforces 349B(贪心)

87 篇文章 1 订阅
16 篇文章 1 订阅

题意描述

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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值