USACO Section 3.1: Stamps
这题一开始用了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的更多相关文章
- 【USACO 3.1】Stamps (完全背包)
题意:给你n种价值不同的邮票,最大的不超过10000元,一次最多贴k张,求1到多少都能被表示出来?n≤50,k≤200. 题解:dp[i]表示i元最少可以用几张邮票表示,那么对于价值a的邮票,可以推出 ...
- 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&& ...
- USACO Section 3.3: Riding the Fences
典型的找欧拉路径的题.先贴下USACO上找欧拉路径的法子: Pick a starting node and recurse on that node. At each step: If the no ...
- USACO Section 3.3 Camlot(BFS)
BFS.先算出棋盘上每个点到各个点knight需要的步数:然后枚举所有点,其中再枚举king是自己到的还是knight带它去的(假如是knight带它的,枚举king周围的2格(网上都这么说,似乎是个 ...
- [IOI1996] USACO Section 5.3 Network of Schools(强连通分量)
nocow上的题解很好. http://www.nocow.cn/index.php/USACO/schlnet 如何求强连通分量呢?对于此题,可以直接先用floyd,然后再判断. --------- ...
- 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 ...
- USACO Section 1.3 Prime Cryptarithm 解题报告
题目 题目描述 牛式的定义,我们首先需要看下面这个算式结构: * * * x * * ------- * * * <-- partial product 1 * * * <-- parti ...
- USACO Section 1.1 Your Ride Is Here 解题报告
题目 问题描述 将字符串转变为数字,字母A对应的值为1,依次对应,字母Z对应的值为26.现在有一个字符串,将其中的每个字符转变为数字之后进行累乘,最终的结果对47求余数. 题目给你两个字符串,其中的字 ...
- USACO Section 1.1-1 Your Ride Is Here
USACO 1.1-1 Your Ride Is Here 你的飞碟在这儿 众所周知,在每一个彗星后都有一只UFO.这些UFO时常来收集地球上的忠诚支持者.不幸的是,他们的飞碟每次出行都只能带上一组支 ...
随机推荐
- jekyll : 使用github托管你的博客
使用github托管你的博客 效果: http://wuya1234.github.io/blog/2013/11/09/start-github-blog/ 样式神马的还没整 电脑系统 我使用的是m ...
- winform - BackgroundWorker
http://www.cnblogs.com/happy555/archive/2007/11/07/952315.html 在VS2005中添加了BackgroundWorker组件,该组件在多线程 ...
- codeforces 161D Distance in Tree 树形dp
题目链接: http://codeforces.com/contest/161/problem/D D. Distance in Tree time limit per test 3 secondsm ...
- javascript中关于坐标 大小 的描述
window对象 有效桌面的大小,除去桌面下面的任务栏的高度 window.screen.availHeight : window.screen.availWidth : 浏览器窗口的左上角相对于 ...
- APM终端用户体验监控分析(下)
一.前言 [APM 终端用户体验监控分析(上)][1]从 APM 终端用户产品特性.使用建议.以及从[真实用户体验][2]和[模拟性能监控][3]两方面入手给大家进行了简单的分享. 本文为下篇,将给大 ...
- IE8中能继续使用Expression的解决方案
在实际工作中,长的报表需要固定表头,比如DataGrid等控件. 过去在用IE8以前版本的时候,只需要在css中加上 position:relative ; top:expresion(this.of ...
- 深入浅出ES6(十三):类 Class
作者 Jason Orendorff github主页 https://github.com/jorendorff 你可能觉得之前讲解的内容略显复杂,今天我们就讲解一些相对简单的内容,不再是生成器 ...
- (转)Android: NDK编程入门笔记
转自: http://www.cnblogs.com/hibraincol/archive/2011/05/30/2063847.html 为何要用到NDK? 概括来说主要分为以下几种情况: 1. 代 ...
- C连接oracle(PROC*C)
1. 安装oralce 10g 2.建立数据库和用户 配置VS2005环境 proc需要嵌入式环境 在C/C++常规里面加入 D:\oracle\product\10.2.0\db_1\precom ...
- lintcode:排颜色 II
排颜色 II 给定一个有n个对象(包括k种不同的颜色,并按照1到k进行编号)的数组,将对象进行分类使相同颜色的对象相邻,并按照1,2,...k的顺序进行排序. 样例 给出colors=[3, 2, 2 ...