邮票
来源:互联网 发布:redis排序 java 编辑:程序博客网 时间:2024/05/07 01:26
邮票
Description
邮局发行一套票面有n(0<n<=10)种不同面值的邮票。若限每封信所贴的邮票张数不得超过m枚,存在整数r使得用不超过m(0<m<=2n)枚的邮票,可以贴出连续整数1,2,3,…,r值来,找出这种面值数,使得r值最大。
Input
一行,输入N及M
Output
输出R
Sample Input
2 3
Sample Output
7
Source
搜索,回溯
Sulotion
枚举搜索选择的邮票面值,每一次确定一个面值后,就用他更新当前的最大数
for (int j = 1; j <= x; ++j) if (ch >= a[j] && b[ch] > b[ch - a[j]] + 1) b[ch] = b[ch - a[j]] + 1;
Code
#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>#include <algorithm>#include <cmath>#include <stack>#include <map>#include <vector>#include <queue>#define L 1001#define inf 127#define LL long longusing namespace std;int n, m, a[50], b[L], ans, cnt, ch, r, bj;inline int find(int x) { memset(b, inf, sizeof(b)), b[0] = 0, ch = 0; while (1) { ch++; for (int j = 1; j <= x; ++j) if (ch >= a[j] && b[ch] > b[ch - a[j]] + 1) b[ch] = b[ch - a[j]] + 1; if (b[ch] > m) return ch - 1; }}inline void dfs(int x) { if (x == n + 1) {ans = max(ans, cnt); return ;} for (int i = a[x - 1] + 1; i <= cnt + 1; ++i) a[x] = i, cnt = find(x), dfs(x + 1);}int main() { freopen("2221.in", "r", stdin); freopen("2221.out", "w", stdout); a[1] = 1; scanf("%d %d", &n, &m), cnt = m; dfs(2); printf("%d\n", ans); return 0;}
Summary
第二次加了几个不像剪枝的剪枝,但是一直RE,查了好久才发现b数组开太大了??!
0 0
- 邮票
- 邮票
- 邮票
- 邮票
- 邮票问题
- 邮票问题
- 邮票问题
- 邮票问题
- 中国邮票
- 邮票组合
- 邮票问题
- 邮票问题
- 邮票问题
- 邮票问题
- 邮票组合
- 邮票问题
- 剪邮票
- 剪邮票
- C实现折半插入排序
- Linux crontab定时执行任务 命令格式与详细例子
- request的请求转发和response请求重定向
- 在结构体中实现元素固定长度定义
- 二叉树遍历、高度与节点数
- 邮票
- 在word写论文时候关于参考文献的引用
- 数字三角形(顺推2维,逆推,顺推1维)
- 什么是LMS算法(Least mean square)
- Mybatis的简单数据库操作
- 写一个atoi函数
- 堆排序
- php 分页查询
- C/C++中volatile关键字详解