这题一开始用了dfs(注释部分),结果TLE,后来想了DP方法,f[i] = f[j] + f[i-j], j = 1, 2... i/2, 还是TLE,网上搜了别人的代码,发现自己的状态方程有问题,应该是f[i] = f[i-stamp[j]]+1, j = 1...N。这样j从1到N的话复杂度大大降低了。

 /*
 ID: yingzho1
 LANG: C++
 TASK: stamps
 */
 #include <iostream>
 #include <fstream>
 #include <string>
 #include <map>
 #include <vector>
 #include <set>
 #include <algorithm>
 #include <stdio.h>
 #include <queue>
 #include <cstring>
 #include <cmath>
 #include <list>
 #include <cstdio>
 #include <cstdlib>

 using namespace std;

 ifstream fin("stamps.in");
 ofstream fout("stamps.out");

 ;

 int K, N;

 /*bool check(int cur, vector<int> &stamp, int total, int dep) {
     if (cur < 0 || total < 0) return false;
     if (cur == 0) return true;
     for (int i = dep; i < stamp.size(); i++) {
         if (check(cur-stamp[i], stamp, total-1, i)) return true;
     }
     return false;
 }

 bool cmp(const int a, const int b) {return a > b;}*/

 int main()
 {
     fin >> K >> N;
     vector<int> stamp(N);
     set<int> S;
     ; i < N; i++) {
         fin >> stamp[i];
         S.insert(stamp[i]);
     }
 //    sort(stamp.begin(), stamp.end(), cmp);
 /*    if (stamp[stamp.size()-1] != 1) {
         fout << 0 << endl;
         return 0;
     }*/
     vector<);
     ;
     while (cur) {
         //cout << cur << endl;
         );
         else {
             int tmp = inf;
             //for (int i = 1; i <= cur/2; i++) tmp = min(tmp, f[i]+f[cur-i]);
             ; i < N; i++) {
                 );
             }
             if (tmp > K) break;
             f.push_back(tmp);
         }
         cur++;
     }
     fout << cur- << endl;

     /*int cur = 2;
     while (check(cur, stamp, K, 0)) {
         //cout << cur << endl;
         cur++;
     }
     fout << cur-1 << endl;*/

     ;
 }

USACO Section 3.1: Stamps的更多相关文章

  1. 【USACO 3.1】Stamps (完全背包)

    题意:给你n种价值不同的邮票,最大的不超过10000元,一次最多贴k张,求1到多少都能被表示出来?n≤50,k≤200. 题解:dp[i]表示i元最少可以用几张邮票表示,那么对于价值a的邮票,可以推出 ...

  2. USACO Section 1.3 题解 (洛谷OJ P1209 P1444 P3650 P2693)

    usaco ch1.4 sort(d , d + c, [](int a, int b) -> bool { return a > b; }); 生成与过滤 generator&& ...

  3. USACO Section 3.3: Riding the Fences

    典型的找欧拉路径的题.先贴下USACO上找欧拉路径的法子: Pick a starting node and recurse on that node. At each step: If the no ...

  4. USACO Section 3.3 Camlot(BFS)

    BFS.先算出棋盘上每个点到各个点knight需要的步数:然后枚举所有点,其中再枚举king是自己到的还是knight带它去的(假如是knight带它的,枚举king周围的2格(网上都这么说,似乎是个 ...

  5. [IOI1996] USACO Section 5.3 Network of Schools(强连通分量)

    nocow上的题解很好. http://www.nocow.cn/index.php/USACO/schlnet 如何求强连通分量呢?对于此题,可以直接先用floyd,然后再判断. --------- ...

  6. USACO Section 5.3 Big Barn(dp)

    USACO前面好像有类似的题目..dp(i,j)=min(dp(i+1,j),dp(i+1,j+1),dp(i,j+1))+1  (坐标(i,j)处无tree;有tree自然dp(i,j)=0) .d ...

  7. USACO Section 1.3 Prime Cryptarithm 解题报告

    题目 题目描述 牛式的定义,我们首先需要看下面这个算式结构: * * * x * * ------- * * * <-- partial product 1 * * * <-- parti ...

  8. USACO Section 1.1 Your Ride Is Here 解题报告

    题目 问题描述 将字符串转变为数字,字母A对应的值为1,依次对应,字母Z对应的值为26.现在有一个字符串,将其中的每个字符转变为数字之后进行累乘,最终的结果对47求余数. 题目给你两个字符串,其中的字 ...

  9. USACO Section 1.1-1 Your Ride Is Here

    USACO 1.1-1 Your Ride Is Here 你的飞碟在这儿 众所周知,在每一个彗星后都有一只UFO.这些UFO时常来收集地球上的忠诚支持者.不幸的是,他们的飞碟每次出行都只能带上一组支 ...

随机推荐

  1. MSDN:Code First 迁移

    来自:MSDN:Code First 迁移,完全照搬! 本演练将提供对实体框架中 Code First 迁移的概述.您可以完成整个演练,也可以跳至自己感兴趣的主题.主题如下: 启用迁移 生成并运行迁移 ...

  2. Maven安装和配置,eclipse创建Maven项目

    提示:使用Maven需要先安装jdk. 下载安装Maven 一.下载最新版的Maven,下载地址:http://maven.apache.org/download.cgi 二.将Maven下载到E:\ ...

  3. [REFERENCE] Real-Time-Normal-Map-Dxt-Compression

    DXT5N & 3Dc(aka BC5) compression in common code & SIMD: http://mrelusive.com/publications/pa ...

  4. date format 精辟讲解

    link: http://stackoverflow.com/questions/19533933/nsdateformatter-how-to-convert-wed-23-oct-2013-045 ...

  5. ios 分类(Category)

      今天研究了类别,都是网上找的资料,类别的作用 类别主要有3个作用:       (1)将类的实现分散到多个不同文件或多个不同框架中.       (2)创建对私有方法的前向引用.       (3 ...

  6. 微信连WiFi关注公众号流程更新 解决ios微信扫描二维码不关注就能上网的问题

    前几天鼓捣了一下微信连WiFi功能,设置还蛮简单的,但ytkah发现如果是ios版微信扫描微信连WiFi生成的二维码不用关注公众号就可以直接上网了,而安卓版需要关注公众号才能上网,这样就少了很多ios ...

  7. js返回上一页报网页过期问题解决

    1.detail详细页面加上返回按钮,返回到list列表页 <input type="button" value=" 返回 " onclick=" ...

  8. Codeforces 294B Shaass and Bookshelf(记忆化搜索)

    题目 记忆化搜索(深搜+记录状态) 感谢JLGG //记忆话搜索 //一本书2中状态,竖着放或者横着放 //初始先都竖着放,然后从左边往右边扫 #include<stdio.h> #inc ...

  9. LA 3350

    The NASA Space Center, Houston, is less than 200 miles from San Antonio, Texas (the site of the ACM ...

  10. 大一暑假为期五周的ACM实验室培训结束了(2013.8.24)

    没想到,我的大学里第一个暑假,9周的时间只有最初的两周在家待着,接下来的7周将会在学校度过. 说真的,这是我上学以来,第一次真正好好利用的假期.在这五周里,周一.三.五下午学长都会给我们讲点知识,之后 ...