邮票

来源:互联网 发布: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

第一次写了个什么剪枝都没有的搜索,最后T了几秒中

第二次加了几个不像剪枝的剪枝,但是一直RE,查了好久才发现b数组开太大了??!

0 0