Codeforces D. Color the Fence(贪心)
题目描述:
2 seconds
256 megabytes
standard input
standard output
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 ad 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.
The first line contains a positive integer v (0 ≤ v ≤ 106). The second line contains nine positive integers a1, a2, ..., a9 (1 ≤ ai ≤ 105).
Print the maximum number Igor can write on the fence. If he has too little paint for any digit (so, he cannot write anything), print -1.
5
5 4 3 2 1 2 3 4 5
55555
2
9 11 1 12 5 8 9 10 6
33
0
1 1 1 1 1 1 1 1 1
-1
思路:
题目意思是给9个数字的价钱,和现在拥有的钱,看能不能买,能的话把能买到数字凑成最大的数是多少。
刚开始,想得简单了(只想法),就知道位数越多越好,那我就选最便宜的买咯,全买最便宜的,也想复杂了,还创建结构体,在sort写了个cmp结构体排序,输入的时候就直接忽略钱不够的数字,从钱最少的且数值最大的开始。(该简单的时候不简单,该复杂的时候又想简单了ε=(´ο`*))))
结果这个策略不行,为啥,有没有可能我的钱花不完,然后把剩下的钱买够得到的,尽可能多的情况下数值又尽量大的颜料。emmm,有道理,但是既然剩下的钱都可以买比现在的更便宜的颜料,那我直接全部买这种便宜的颜料数量上岂不更多?所以是不可能的啦。(一开始就这个思路还写了个递归,最后就像写bug一样把自己绕进去了QAQ)
那么,我要是不用全部买最便宜的颜料后剩下的钱呢?
什么意思,就是我假设可以买n个最便宜的颜料,嗯,那我买n-1个试试,剩下的钱看能不能买个9?买两个9?两个不行,那我能不能再买个8,两个8?,...,这样从大到小一直下去,就保证了数字位数最大,而且数值最大。代码对我来说有一点tricky。
知识点:贪心
代码:
#include <iostream>
#include <algorithm>
#include <memory.h>
#define INF 0x3f3f3f3f
using namespace std;
int n;
int pri[];
int minm;
int main()
{
cin >> n;
int flag = ;
minm = INF;
for(int i = ;i<;i++)
{
cin >> pri[i];
if(n>pri[i])
{
flag = ;
}
if(pri[i]<minm)
{
minm = pri[i];
}
}
if(flag==)
{
cout << - << endl;
}
else
{
int sum = n/minm;
for(int i = sum;i>;i--)
{
for(int j = ;j>;j--)
{
if(n>=pri[j]&&(n-pri[j])/minm==i-)//可以买j,而且剩下的钱还可以买i-1个数
{
n -= pri[j];
cout << j;
}
}
}
cout << endl;
}
return ;
}
Codeforces D. Color the Fence(贪心)的更多相关文章
- codeforces 349B Color the Fence 贪心,思维
1.codeforces 349B Color the Fence 2.链接:http://codeforces.com/problemset/problem/349/B 3.总结: 刷栅栏.1 ...
- Codeforces 349B - Color the Fence
349B - Color the Fence 贪心 代码: #include<iostream> #include<algorithm> #include<cstdio& ...
- 【贪心】Codeforces 349B.Color the Fence题解
题目链接:http://codeforces.com/problemset/problem/349/B 题目大意 小明要从9个数字(1,2,--,9)去除一些数字拼接成一个数字,是的这个数字最大. 但 ...
- codeforces B. Color the Fence 解题报告
题目链接:http://codeforces.com/problemset/problem/349/B 题目意思:给定v升的颜料和9个需要花费ad 升的颜料,花费ad 升的颜料意味着得到第d个数字,现 ...
- CodeForces 349B Color the Fence (DP)
题意:给出1~9数字对应的费用以及一定的费用,让你输出所选的数字所能组合出的最大的数值. 析:DP,和01背包差不多的,dp[i] 表示费用最大为 i 时,最多多少位,然后再用两个数组,一个记录路径, ...
- NYOJ-791 Color the fence (贪心)
Color the fence 时间限制:1000 ms | 内存限制:65535 KB 难度:2 描述 Tom has fallen in love with Mary. Now Tom w ...
- nyoj 791——Color the fence——————【贪心】
Color the fence 时间限制:1000 ms | 内存限制:65535 KB 难度:2 描述 Tom has fallen in love with Mary. Now Tom w ...
- ACM Color the fence
Color the fence 时间限制:1000 ms | 内存限制:65535 KB 难度:2 描述 Tom has fallen in love with Mary. Now Tom w ...
- Codeforces 484E Sign on Fence(是持久的段树+二分法)
题目链接:Codeforces 484E Sign on Fence 题目大意:给定给一个序列,每一个位置有一个值,表示高度,如今有若干查询,每次查询l,r,w,表示在区间l,r中, 连续最长长度大于 ...
随机推荐
- vue前端项目中excel文件下载
模仿 https://github.com/PanJiaChen/vue-element-admin/ 的下载 创建文件夹vendor 创建文件 Export2Excel.js 内容: /* esli ...
- python常用英文单词
application 应用程式 应用.应用程序 application framework 应用程式框架.应用框架 应用程序框架 architecture 架构.系统架构 体系结构 argument ...
- springmvc+spring+mybatis的整合(推荐新手看看)
下面开始整合(由于有点多就不一一介绍了) 配置sqlMapConfig.xml(空文件即可) <?xml version="1.0" encoding="UTF-8 ...
- Hystrix学习笔记
服务降级: DashBoard: 例中的8001为要监控的地址:
- LVS(3种模式+10种调度算法)
一.LVS简介 LVS(Linux Virtual Server)即Linux虚拟服务器,是由章文嵩博士主导的开源负载均衡项目,目前LVS已经被集成到Linux内核模块中.该项目在Linux内核中实现 ...
- Android--创建快捷方式
需要权限: <uses-permission android:name="com.android.launcher.permission.READ_SETTINGS" /&g ...
- visual studio code 命令行创建发布一个项目
安装vs core 打开按ctrl+~键打开终端 输入dir看看有什么文件,输入md Test 创建一个文件夹,选择文件后创建一个项目 输入dotnet可以查看信息 执行dotnet --help或者 ...
- spring源码解析前瞻
很多人有疑问:为什么要读源码?读源码有什么用?我也一直问自己这些问题,读源码非常枯燥,工作中又用不到,慢慢的自己读源码越发现自己知识的不足,无法把知识串起来,形成知识体系.从单系统中常用的Spring ...
- 强大的VIM
个人感觉,vim用熟了,比任何编辑器都好用,VIM的许多特性节省了时间和击键次数,并可以完成一些其他编辑器无法完成的功能,这里在网上找了几个经典案例,记录一下. 与大部分其它编辑器不同,进入 Vim ...
- bat 设置多个静态ip
@echo off REM 本地连接名称 set NetName="本地连接 2" REM 默认IP netsh interface ipv4 set address name=% ...