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;
}
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法