题意:给出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. 发送验证码60s倒计时实现

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  2. Java 序列化Serializable具体解释(附具体样例)

    Java 序列化Serializable具体解释(附具体样例) 1.什么是序列化和反序列化 Serialization(序列化)是一种将对象以一连串的字节描写叙述的过程:反序列化deserializa ...

  3. caffe学习--cifar10学习-ubuntu16.04-gtx650tiboost--1g--02

    caffe学习--cifar10学习-ubuntu16.04-gtx650tiboost--1g--02 训练网络: caffe train -solver examples/cifar10/cifa ...

  4. caffe训练自己的图片进行分类预测--windows平台

    caffe训练自己的图片进行分类预测 标签: caffe预测 2017-03-08 21:17 273人阅读 评论(0) 收藏 举报  分类: caffe之旅(4)  版权声明:本文为博主原创文章,未 ...

  5. MongoDB资料汇总专题(转)

    原文地址:http://blog.nosqlfan.com/html/3548.html 1.MongoDB是什么 MongoDB介绍PPT分享 MongoDB GridFS介绍PPT两则 初识 Mo ...

  6. 果壳、推库、虎秀、知乎、it世界

    果壳.推库.虎秀.知乎.it世界

  7. WPF 后台Render线程崩溃, Exception from HRESULT: 0x88980406

    近期遇到一个问题.窗口在Loaded同一时候Resize会出现黑屏或者直接崩溃, 调查发现是WPF后端的Render线程渲染UI到DirectX时崩溃. 硬件环境:WES7 + .Net4.0 关于W ...

  8. Windows8-x64 VMWare安装Linux CentOS6-x64

    本文參考了:http://www.cnblogs.com/seesea125/archive/2012/02/25/2368255.html 其内容相当具体,以至于我还没依照其步骤做完.系统就已经安装 ...

  9. 基于struts2的学生报道管理系统(附github源码地址)

    本项目参考了<java web轻量级开发全体验>,加入了对mysql的支持. 一.基本业务功能 通过struts2框架,结合mysql数据库构建一个学生报到管理系统,来模拟学生报到登记的过 ...

  10. Qtree3

    题目描述 给出N个点的一棵树(N-1条边),节点有白有黑,初始全为白 有两种操作: 0 i : 改变某点的颜色(原来是黑的变白,原来是白的变黑) 1 v : 询问1到v的路径上的第一个黑点,若无,输出 ...