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中, 连续最长长度大于 ...
随机推荐
- [LeetCode] 73. Set Matrix Zeroes 矩阵赋零
Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in-place. Exampl ...
- 【Python学习之八】设计模式和异常
环境 虚拟机:VMware 10 Linux版本:CentOS-6.5-x86_64 客户端:Xshell4 FTP:Xftp4 python3.6 一.设计模式1.单例模式确保某一个类只有一个实例, ...
- 【VS开发】Visual C++内存泄露检测—VLD工具使用说明
Visual C++内存泄露检测-VLD工具使用说明 一. VLD工具概述 Visual Leak Detector(VLD)是一款用于Visual C++的免费的内存泄露检测工具.他的 ...
- 高级UI-UI绘制流程
UI的绘制流程和事件分发,属于Android里面的重点内容,在做自定义UI的时候,更是应该了解UI的绘制流程是如何的,此篇文章就是说明UI的绘制流程,事件分发前面已经详细讲过了 UI绘制流程探索 这里 ...
- C++静态成员变量必须定义
静态成员变量在类中仅仅是声明,没有定义,所以要在类的外面定义,实际上是给静态成员变量分配内存,否则不能使用,编译不会通过. class A { public: static int a; //声明但未 ...
- [IOT] - Raspbian Buster 设置固定 IP
背景 Raspberry Pi 4 + Raspbian Buster 配置步骤 1. 打开文件 "/etc/dhcpcd.conf" 进行配置. 2. 有线网卡配置固定IP in ...
- BZOJ3791 作业(DP)
题意: 给出一个长度为n的01序列: 你可以进行K次操作,操作有两种: 1.将一个区间的所有1作业写对,并且将0作业写错: 2.将一个区间的所有0作业写对,并且将1作业写错: 求K次操作后最多写对了多 ...
- MySQL事务和事务隔离级别
1.概述 事务就是对数据库数据进行更改(包括insert.update.delete等)操作的一个执行单元,通常有一条或多条更改语句组成.在同一个事务中的更改操作要么同时成功,要么同时失败. 事务具有 ...
- Windows系统中环境变量不展开的问题
Windows系统中环境变量不展开的问题 问题现象:Windows.System32等系统目录里命令,无法通过Path搜索路径来执行.查看Path环境变量结果如下: D:\>echo %Path ...
- UCOSIII时间片轮转调度
OS_RATE_HZ const OSCfg_TickRate_Hz = (OS_RATE_HZ )OS_CFG_TICK_RATE_HZ; #define OS_CFG_TICK_RATE_HZ 2 ...