题意:给出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. Django-wsgi实例

    wsgiref实现了wsgi,他会将复杂的http请求经过处理,得到Django需要的格式,可以说他是一个接口,一端传入数据,一端处理数据 传统的socket实现 import socket def ...

  2. Kristen Grauman

    http://www.cs.utexas.edu/~grauman/ CV         Publications         Code           Data        Short ...

  3. Struts2拦截器 解决登录问题

    一.了解Struts2 拦截器[Interceptor] 拦截器的工作原理如图  拦截器是由每一个action请求(request)都包装在一系列的拦截器的内部,通过redirectAction再一次 ...

  4. session.use_cookies有什么作用,

    session.use_cookies有什么作用, @ini_set('session.use_cookies', 1);什么意思 若session.use_cookies = 1 sessionid ...

  5. JAVA变量初始化赋值null

     在Java中,null值表示引用不指向任何对象.运行过程中系统发现使用了这样一个引用时·可以立即停止进一步的访问,不会给系统带来任何危险. 1.如果是对象的field的话那么系统在初始化对象的时候会 ...

  6. SIP学习笔记 -- RFC 3261

    1.SDP (rfc 4566)    1)用于交换参数    2)内容分三部分Session description, Time description and Media description ...

  7. 青岛理工ACM交流赛 J题 数格子算面积

    数格子算面积 Time Limit: 1000MS Memory limit: 262144K 题目描述 给你一个多边形(用’\’和’/’表示多边形的边),求多边形的面积. 输入  第一行两个正整数h ...

  8. 渗透模型 Percolation Models(一个物理模型的广泛应用)

    转自:http://mooc.guokr.com/note/15357/ http://mooc.guokr.com/user/0298406005/note/ 模型思想: 有水流下来,是否会渗入地面 ...

  9. Loadrunner脚本自动关联和手动关联

    关于Loadrunner关联一.什么时候需要关联 1.关联的含义        关联(correlation):在脚本回放过程中,客户端发出请求,通过关联函数所定义的左右边界值(也就是关联规则),在服 ...

  10. YUV420数据和字符信息如何利用滤镜方法进行编码?

    YUV420数据和字符信息如何利用滤镜方法进行编码?我希望用ffmpeg中的filter方法,把YUV420数据和字符信息一起编码,该怎么办呢? 本人目前只实现了把yuv420的数据进行h.264的编 ...