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 ...
随机推荐
- poj 2891 扩展欧几里得迭代解同余方程组
Reference: http://www.cnblogs.com/ka200812/archive/2011/09/02/2164404.html 之前说过中国剩余定理传统解法的条件是m[i]两两互 ...
- POJ3041Asteroids(最小点覆盖+有点小抽象)
Asteroids Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 18289 Accepted: 9968 Descri ...
- linux c学习笔记----进程创建(fork,wait,waitpid)
1.pid_t fork(); (1)当一个进程调用了fork 以后,系统会创建一个子进程.这个子进程和父进程不同的地方只有他的进程ID 和父进程ID,其他的都是一样.就象符进程克隆(clone)自己 ...
- MySQL 中 where id in (1,2,3,4,...) 的效率问题讨论
MySQL ACMAIN_CHM06-26 16:36 等级 84次回复 [求证&散分]MySQL 中 where id in (1,2,3,4,...) 的效率问题讨论 庆祝本月大版得 ...
- jquery------.mouseover()和.mouseout()的高级效果使用
index.jsp <div style="width:100%;height:40px;background-color:#aaa;position:absolute;"& ...
- html5+ 获取当前设备的加速度信息
getCurrentAcceleration 获取当前设备的加速度信息 void plus.accelerometer.getCurrentAcceleration( successCB, error ...
- 锋利的jQuery-1--jQuery对象和DOM对象以及相互转化
DOM对象: document object model,文档对象模型,每一份dom都可以表示成一棵树. 如下图所示,代码省略 在这颗dom树种,h3, p, ul以及ul的3个li子节点都是dom元 ...
- Developing a plugin framework in ASP.NET MVC with medium trust
http://shazwazza.com/post/Developing-a-plugin-framework-in-ASPNET-with-medium-trust.aspx January 7, ...
- express快速搭建web server
安装express4.x npm install -g express npm install -g express-generator //express命令行工具在4.x分离出来了 express ...
- 一机双mysql的安装和启动注意事项目
./configure --prefix=/usr/local/mysql5.1/ --with-mysqld-user=mysql --sysconfdir=/usr/local/mysql5.1/ ...