题意:给出1~9数字对应的费用以及一定的费用,让你输出所选的数字所能组合出的最大的数值。

析:DP,和01背包差不多的,dp[i] 表示费用最大为 i 时,最多多少位,然后再用两个数组,一个记录路径,一个记录是数字几即可。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#define debug() puts("++++");
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std; typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 1e6 + 5;
const int mod = 1e6;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
}
int a[15];
int dp[maxn], p[maxn], path[maxn];
int cnt[15]; void print(int ans){
memset(cnt, 0, sizeof cnt);
while(ans != -1){
++cnt[path[ans]];
ans = p[ans];
}
for(int i = 9; i > 0; --i)
while(cnt[i]--) printf("%d", i);
printf("\n");
} int main(){
while(scanf("%d", &n) == 1){
memset(dp, 0, sizeof dp);
memset(p, -1, sizeof p);
for(int i = 1; i < 10; ++i) scanf("%d", a+i);
for(int i = 1; i < 10; ++i)
for(int j = a[i]; j <= n; ++j)
if(dp[j] < dp[j-a[i]]+1){
dp[j] = dp[j-a[i]] + 1;
p[j] = j - a[i];
path[j] = i;
}
else if(dp[j] == dp[j-a[i]]+1 && path[j] < i){
path[j] = i;
p[j] = j - a[i];
}
if(dp[n] == 0) puts("-1");
else print(n);
}
return 0;
}

CodeForces 349B Color the Fence (DP)的更多相关文章

  1. codeforces 349B Color the Fence 贪心,思维

    1.codeforces 349B    Color the Fence 2.链接:http://codeforces.com/problemset/problem/349/B 3.总结: 刷栅栏.1 ...

  2. Codeforces 349B - Color the Fence

    349B - Color the Fence 贪心 代码: #include<iostream> #include<algorithm> #include<cstdio& ...

  3. 【贪心】Codeforces 349B.Color the Fence题解

    题目链接:http://codeforces.com/problemset/problem/349/B 题目大意 小明要从9个数字(1,2,--,9)去除一些数字拼接成一个数字,是的这个数字最大. 但 ...

  4. 349B - Color the Fence

    Color the Fence Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Su ...

  5. Codeforces D. Color the Fence(贪心)

    题目描述: D. Color the Fence time limit per test 2 seconds memory limit per test 256 megabytes input sta ...

  6. codeforces B. Color the Fence 解题报告

    题目链接:http://codeforces.com/problemset/problem/349/B 题目意思:给定v升的颜料和9个需要花费ad 升的颜料,花费ad 升的颜料意味着得到第d个数字,现 ...

  7. ACM Color the fence

    Color the fence 时间限制:1000 ms  |  内存限制:65535 KB 难度:2   描述 Tom has fallen in love with Mary. Now Tom w ...

  8. Codeforces 484E Sign on Fence(是持久的段树+二分法)

    题目链接:Codeforces 484E Sign on Fence 题目大意:给定给一个序列,每一个位置有一个值,表示高度,如今有若干查询,每次查询l,r,w,表示在区间l,r中, 连续最长长度大于 ...

  9. NYOJ-791 Color the fence (贪心)

    Color the fence 时间限制:1000 ms  |  内存限制:65535 KB 难度:2   描述 Tom has fallen in love with Mary. Now Tom w ...

随机推荐

  1. .NET MVC 4 实现邮箱激活账户功能

    这篇文章是<.NET MVC 4 实现用户注册功能>的后续开发,实现发送激活链接到注册用户邮箱,用户在邮箱打开链接后激活账户的功能. 首先实现发送邮件的功能,在管理用户注册的control ...

  2. YARN和MapReduce的内存设置參考

    怎样确定Yarn中容器Container,Mapreduce相关參数的内存设置,对于初始集群,由于不知道集群的类型(如cpu密集.内存密集)我们须要依据经验提供给我们一个參考配置值,来作为基础的配置. ...

  3. cocos2d-x lua 中使用protobuf并对http进行处理

    cocos2d-x lua 中使用protobuf并对http进行处理 本文介绍 cocos2d-x lua 中使用http 和 基于cocos2d-x 对lua http的封装(部分ok) 本博客链 ...

  4. oracle指定访问某表或某视图

    在oracle中,想创建一个账号,然后只能只读地访问指定的表,怎么搞? 一.为特定的表创建视图 创建视图的时候还可以加上过滤条件,连访问哪些数据都可以指定. create or replace vie ...

  5. Mac 操作系统安装 SVN server教程(Subversion With Mac OS X Tutorial)

    Find recent articles on my github page: rubyrobot.github.io © 2006-2014 Imagine Ecommerce Subversion ...

  6. Linux就该这么学--命令集合5(用户与组管理命令)

    1.useradd命令用于创建新用户:(useradd [选项] 用户名) 附录: -d 指定用户的家目录 -D 展示默认值 -e 账号有效截止日期,格式:YYY-MM-DD -g 指定一个初始用户组 ...

  7. eclipse 中PlantUML的安装和使用

    安装: 填写的地址:http://hallvard.github.io/plantuml/ 安装完plantUML后,还要下载一个Graphviz https://pan.baidu.com/s/1g ...

  8. POJ2389 —— 高精度乘法

    直接上代码了: #include<iostream> #include<cstdio> #include<cstring> #include<cstdlib& ...

  9. Codeforces Round #261 (Div. 2) B. Pashmak and Flowers 水题

    题目链接:http://codeforces.com/problemset/problem/459/B 题意: 给出n支花,每支花都有一个漂亮值.挑选最大和最小漂亮值得两支花,问他们的差值为多少,并且 ...

  10. 详细阐述ping命令中请求超时与无法访问的区别

    1.Request timed out 这是大家经常碰到的提示信息,很多文章中说这是对方机器置了过滤ICMP数据包,从上面工作过程来看,这是不完全 正确的,至少有下几种情况. (1) 对方已关机,或者 ...