摘要: 第一道背包问题,0-1背包,参考网上一位大牛写的做的。状态方程:dp[i][w] = max{dp[i-1][w], dp[i-1][w-obj[i].wei] + obj[i].val]},但这样会超内存,需要一个空间复杂度的优化将dp改为一维,这招看来以后得常用,具体见转载的《背包九讲》。 明天好好读读《背包九讲》,在多做几道dp变形题,练习在于精不在多。#include <iostream>using namespace std;const int mMax = 3500;//待选物品个数const int nMax = 14000;//最大容量struct{ int wei 阅读全文
posted @ 2010-11-30 23:57 yangleo 阅读(316) 评论(0) 推荐(0) 编辑
摘要: 这题是水题,主要是用到了C++字符串和cmath中的pow函数。 最近课程有点多,做POJ时间不够,还是要抓紧一些,尤其是动态规划、搜索题等要多练。#include <iostream>#include <string>#include <cmath>using namespace std;int main(){ string s; while(1){ cin>>s; if (s == "0") break; long result = 0; int k = 1; string::iterator it; for (it = 阅读全文
posted @ 2010-11-30 22:24 yangleo 阅读(317) 评论(0) 推荐(0) 编辑