codeforces B. Color the Fence 解题报告
题目链接:http://codeforces.com/problemset/problem/349/B
题目意思:给定v升的颜料和9个需要花费ad 升的颜料,花费ad 升的颜料意味着得到第d个数字,现在要求在所有的花费不超过v升的情况下,使得这些数字组合起来是最大的。
一开始直接从最小花费的颜料着手,如果花费的颜料是相同的,就转到从d(也就是位数)最大贪心。这样测试9就开始卡住了。
受到乌冬兄的指点迷津,终于有了思路,陆陆续续改了很多次,终于过了。以下摘自他的语录,白话文,大家请谅解:
稳用颜料最少,最大的数字,先保证位数最长,然后再将剩余颜料从大数字开始贪心
因为要数最大,所以根据“数”的比较顺序:
1。比较位数
2。从高位开始比较
从数字比较的方法,推出贪心既思路
由于本人悟性太低,下面是更详细的解说:
1、先稳到用最小颜料既数字d,假设它价值为c
2、先构造出s = v div c个d,求出剩余颜料r = v mod c
3、从最高位扫描s,从大到小枚举可替换数字d' >= d,假设价值为c':若c'-c <= r,则替换当前位置的d为d', r -= c' - c
4、最后得出替换后的s, s'即为所求
注意:价值即一个数字要使用的颜料量
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
using namespace std; const int maxn = 1e6 + ;
struct paint
{
int index; // 数字
int num; // 该数字所花费的颜料量
} a[maxn], b[maxn]; int s[maxn]; // 用于保存结果 int cmp(paint a, paint b)
{
if (a.num != b.num) // 先保证用的颜料量最少
return a.num < b.num;
return a.index > b.index; // 再保证数字最大
} int main()
{
int i, j, v, tmp, t1;
while (scanf("%d", &v) != EOF)
{
// freopen("in.txt", "r", stdin);
for (i = ; i <= ; i++)
{
a[i].index = i;
scanf("%d", &a[i].num);
b[i].index = i; //b用于保存未排序前的序列,以便下面从大数字开始枚举
b[i].num = a[i].num;
}
sort(a+, a+, cmp);
if (v < a[].num) // v比最少花费的颜料更少
printf("-1\n");
else
{
tmp = v / a[].num; // 得出最大位数
if (v % a[].num == ) // 如果刚好可以除尽,最大的数就是tmp个a[1].num的数。
{
for (i = ; i < tmp; i++)
printf("%d", a[].index);
printf("\n");
}
else
{
for (i = ; i < tmp; i++)
{
s[i] = a[].index; // 先得出目前来说最长的数字,但可能不是最终结果
}
int r = v % a[].num; // 余数
t1 = r;
for (i = ; i < tmp; i++)
{
for (j = ; j >= ; j--) // 从最大的位数开始枚举
{
if (b[j].num - a[].num <= r && a[].index < b[j].index) // 没有超过余数且数字比原来的排列数字的位数要大
{
s[i] = b[j].index;
r = r - (b[j].num - a[].num); // 余数要有所减少
break;
}
}
}
if (t1 == r) // 如果根本没有可替换的数,那么就和刚好除尽的是同一种情况
{
for (i = ; i < tmp; i++)
printf("%d", a[].index);
printf("\n");
}
else
{
for (i = ; i < tmp; i++) // 否则有替换的就输出新的最大数字
{
printf("%d", s[i]);
}
printf("\n");
}
}
}
}
return ;
}
codeforces B. 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 Educational Round 92 赛后解题报告(A-G)
Codeforces Educational Round 92 赛后解题报告 惨 huayucaiji 惨 A. LCM Problem 赛前:A题嘛,总归简单的咯 赛后:A题这种**题居然想了20m ...
- codeforces 476C.Dreamoon and Sums 解题报告
题目链接:http://codeforces.com/problemset/problem/476/C 题目意思:给出两个数:a 和 b,要求算出 (x/b) / (x%b) == k,其中 k 的取 ...
- POJ 2054 Color a Tree解题报告
题干 Bob is very interested in the data structure of a tree. A tree is a directed graph in which a spe ...
- Codeforces Round #382 (Div. 2) 解题报告
CF一如既往在深夜举行,我也一如既往在周三上午的C++课上进行了virtual participation.这次div2的题目除了E题都水的一塌糊涂,参赛时的E题最后也没有几个参赛者AC,排名又成为了 ...
- Codeforces 349B - Color the Fence
349B - Color the Fence 贪心 代码: #include<iostream> #include<algorithm> #include<cstdio& ...
- Codeforces D. Color the Fence(贪心)
题目描述: D. Color the Fence time limit per test 2 seconds memory limit per test 256 megabytes input sta ...
- codeforces B.Fence 解题报告
题目链接:http://codeforces.com/problemset/problem/363/B 题目意思:给定整数n和k,需要从n个数中找出连续的k个数之和最小,输出这连续的k个数中的第一个数 ...
- 【LeetCode】276. Paint Fence 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetco ...
随机推荐
- Java中的继承、封装、多态、抽象
1.继承 java 和某些面向对象语言(如 c++)在实现继承的不同之处在于java只支持单继承,不支持多重继承.即java 中一个类只能继承于另一个类.我们将被继承的类称之为父类(基类),继承类称之 ...
- tarjan算法求强连通分量
先上代码: #include <iostream> #include <cstring> #include <vector> #include <stack& ...
- 【基础语法】a++与++a的区别
package com.on.learn.e2; /** * @author lj * 自增:a++与++a a++是指本行表达式不使用a自增后的值,++a是指本行开始就已经使用a自增后的值 * */ ...
- jQuery的插入
append(content) 概述 : 向每个匹配的元素内部追加内容. 这个操作与对指定的元素执行appendChild方法,将它们添加到文档中的情况类似 append(function(index ...
- Logistic Regression and Gradient Descent
Logistic Regression and Gradient Descent Logistic regression is an excellent tool to know for classi ...
- jQuery.cookie.js
一.jQuery.Cookie.js插件是一个轻量级的Cookie管理插件. 下载:http://github.com/carhartl/jquery-cookie/zipball/v1.4.1 特别 ...
- nuget pack
nuget spec nuget setApiKey yourkeynuget pack PluginMvc.Framework.csproj -Prop Configuration=Releasen ...
- centOS6.4 extundelete工具恢复rm -rf 删除的目录
PS:补充下,我在fedora 19上运行的时候遇到的一个问题: [root@localhost extundelete-]# ./configure Configuring extundelete ...
- 基于Node.js + jade + Mongoose 模仿gokk.tv
原文摘自我的前端博客,欢迎大家来访问 http://www.hacke2.cn 关于gokk 大学的娱乐活动基本就是在寝室看电影了→_→,一般都会选择去goxiazai.cc上看,里面的资源多,质量高 ...
- UESTC 1817 Complete Building the Houses
Complete Building the Houses Time Limit: 2000MS Memory Limit: 65535KB 64bit IO Format: %lld & %l ...