|
|
|
|
|
摘要:
多重背包问题,一般有三种思路优化:1.用2^n拆分数量,转化为01背包。2.当成完全背包来做,需要用一个数组记录每个物品的数量,当使用次数超过时结束。3.用单调队列优化(目前还不懂的说)。上次做男人八题里的coin时我用的是第二种思路,这次我用的是第一种。 1 #include <stdio.h> 2 #include <string.h> 3 bool dp[60006]; 4 int a[10],b[100]; 5 int cnt,sum; 6 bool read() 7 { 8 int i; 9 bool f = 0;10 sum = 0;11 for(i... 阅读全文
posted @ 2012-08-29 08:44
Siriuslzx
阅读(180)
推荐(0)
编辑
摘要:
0、1背包,直接做会超时。做了这题我才知道,01背包是能用搜索做的,搜索的时候剪枝很重要。(几个月后……)光棍节前夕,小T同志来找我,说他遇到了一个题解,里面说是受别人代码的启发,并附上原址,点开一看,居然是我?!我姓福了。真的很感谢这位兄弟,他尊重产权,更重要的是给了我信心。本来我都快要放弃了,现在我又坚定了信念,一定要好好搞下去。说实话,他的博客写得比我好,因为他详细地写出了思路。这时候小T 吐槽了:“其实你也是抄的。”好吧,我受之有愧,我是参考了江财大牛的思路,不过我的代码已经和他的不一样了,核心部分DFS做了逻辑上的改动。 1 #include <stdio.h> 2 #i 阅读全文
posted @ 2012-08-29 01:01
Siriuslzx
阅读(255)
推荐(0)
编辑
摘要:
题意:求一个环中最大区间和,区间长度 <= n。 用单调队列优化Dp,核心内容是dp[i] = max(sum[j]) - sum[i-1]。这题最后的输出有很多要求,如果有多个解,输出起始位置最小的;如果还有多个解,输出长度最小的。其实完全不用考虑,因为我遍历的时候就是按起始位置从小到大的,如果一个解和最优解相同,并不更新,记录的就是起始最小的结果。另一个要求亦然。 1 #include <stdio.h> 2 #include <string.h> 3 const int N = 100010; 4 const int INF = 1000000000; 5 阅读全文
posted @ 2012-08-29 00:55
Siriuslzx
阅读(218)
推荐(0)
编辑
摘要:
稍微复杂一些的0、1背包,要把背包尽可能装满,直到再也装不下任何物品,求总装法。这题是不用考虑W的,先把V升序排序,从小到大枚举,当前物品为剩下未使用的物品中体积最小的,那么比当前小的一定是要使用的。这种思路是n^3的,后面的物品会被重复计算,如果从后往前枚举就能避免这个问题,复杂度为n^2。 1 #include <stdio.h> 2 #include <string.h> 3 #include <algorithm> 4 using namespace std; 5 int dp[1005],sum[35],w[35]; 6 int main() 7 阅读全文
posted @ 2012-08-29 00:48
Siriuslzx
阅读(171)
推荐(0)
编辑
摘要:
题目给出插队的过程,求最后队的序列。我当初想开一个大数组模拟,用1表示有人,0表示没人,从后往前推,每确定一个人,就把他所在位置的1改为0,剩下的人再根据0,1算序号。这个想法我放弃了,因为复杂度是O(n)的。后来我才恍然大悟,线段树不就是把对区间的操作从O(n)化为O(logn)的么?这样问题就解决了。 1 #include <stdio.h> 2 #include <string.h> 3 const int N = 200010; 4 int p[N],w[N],order[N]; 5 int tree[N<<2],D; 6 void update(i 阅读全文
posted @ 2012-08-29 00:38
Siriuslzx
阅读(126)
推荐(0)
编辑
摘要:
Poj中少有的中文题。这题其实有个术语叫“威佐夫博弈”,我当初找规律时试过二进制、斜率,没想到第三次试黄金分割就对了。 说说我的思路吧,我会尽量用通俗的语言,因为有时候看到术语我会头晕。这题属于最简单的博弈类型,“状态争夺”,就是只有两个人,要想赢就要抢夺一个特殊状态,而一般谁面对这个状态谁倒霉,所以要把这个留给对手,它的术语叫“必败态”。举个例子,一堆石子,每次能拿走1,2,3块,两人轮流拿,取走最后所有石子的人胜。那必败态就是石子数为4n,对方面对这个状态,无论怎么取,你都能相应地取来维持数量为4的倍数,直到只剩4个,然后呢?没有然后了。顺便说一下,结束的状态也属于必败态,比如没有石子了, 阅读全文
posted @ 2012-08-29 00:31
Siriuslzx
阅读(443)
推荐(0)
编辑
摘要:
这题和poj 1915一样,用bfs做走马步。现在再看当时的代码,真是好幼稚啊。 1 #include <stdio.h> 2 #include <string.h> 3 int X[] = {-2,-2,-1,-1,1,1,2,2}, 4 Y[] = {-1,1,-2,2,-2,2,-1,1}; 5 struct Point 6 { 7 int x,y; 8 void init(int a,int b) 9 {x = a; y = b;}10 }q[70],s,t;11 int head,tail,map[9][9];13 void bfs()14 {15 ... 阅读全文
posted @ 2012-08-29 00:26
Siriuslzx
阅读(182)
推荐(0)
编辑
摘要:
经典的搜索题,我用的是简单的暴搜,其实这题用排除法做会更快。 1 #include <stdio.h> 2 #include <string.h> 3 #define index(a,b) 3*(a/3)+(b/3) 4 bool r[10][10],c[10][10],sodu[10][10]; 5 int map[10][10]; 6 bool find; 7 void dfs(int n) 8 { 9 int i,j,k,x,y;10 if(find) return ;11 if(n >= 81)12 {13 find = 1;14 ... 阅读全文
posted @ 2012-08-29 00:22
Siriuslzx
阅读(139)
推荐(0)
编辑
摘要:
同poj 1059,判断是否为前缀。 1 #include <stdio.h> 2 #include <string.h> 3 const int N = 100000; 4 int next[N][10],cnt=0; 5 bool f=1,end[N],vis[N]; 6 void inst(char *s) 7 { 8 int u=0,p; 9 for(;*s;s++)10 {11 p = *s - '0';12 if(end[u])13 {f = 0; return ;}14 if(!next[u]... 阅读全文
posted @ 2012-08-29 00:19
Siriuslzx
阅读(149)
推荐(0)
编辑
摘要:
字典树问题,判断一个串是不是另一个的前缀。 1 #include <stdio.h> 2 #include <string.h> 3 int next[1000][2],cnt=0; 4 bool f=1,end[1000],vis[1000]; 5 void inst(char *s) 6 { 7 int u=0,p; 8 for(;*s;s++) 9 {10 p = *s - '0';11 if(!next[u][p])12 {13 cnt++;14 next[cnt][... 阅读全文
posted @ 2012-08-29 00:16
Siriuslzx
阅读(169)
推荐(0)
编辑
摘要:
求字符串循环同构中字典序最小的,用最小表示法做的。 1 #include <stdio.h> 2 #include <string.h> 3 const int N = 10010; 4 char s[N]; 5 int main() 6 { 7 int n,i,j,k,t,l; 8 scanf("%d",&n); 9 while(n--)10 {11 scanf("%s",s);12 l = strlen(s);13 i = k = 0; j = 1;14 while(i<l && j<l & 阅读全文
posted @ 2012-08-29 00:13
Siriuslzx
阅读(136)
推荐(0)
编辑
摘要:
简单字典树。 1 #include <stdio.h> 2 #include <string.h> 3 const int N = 200000; 4 int nxt[N][26],word[N]; 5 char eng[N/2+10][11]; 6 bool end[N]; 7 int cnt,cur,p,f; 8 void insert(char *t, char *s) 9 {10 cur = 0;11 while(*t)12 {13 p = *t - 'a';14 if(!nxt[cur][p])15 {16 ... 阅读全文
posted @ 2012-08-29 00:09
Siriuslzx
阅读(155)
推荐(0)
编辑
摘要:
强连通分量题,涉及求强连通分量和缩点,基本是抄的模板,我也是把这题的代码当模板放这里。做这题的一个收获,缩点后为构造强连通分量,最少要添加几条有向边:统计入度和出度为0的点的数量,取其大者。 1 #include <stdio.h> 2 #include <string.h> 3 #include <vector> 4 using namespace std; 5 const int N = 105; 6 int stk[N],col[N],dep[N],low[N]; 7 int cnt,cols,top,ans1,ans2; 8 bool inq[N]; 阅读全文
posted @ 2012-08-29 00:07
Siriuslzx
阅读(127)
推荐(0)
编辑
摘要:
一道递归构造图形的题,做的时候一直TLE,多想想优化才过的。 1 #include <stdio.h> 2 #include <string.h> 3 char map[2500][2500]; 4 char tplt[6][6]; 5 int n; 6 void build(int m, int x, int y) 7 { 8 int i,j,sum = 1; 9 for(i = 1; i < m; i++) sum *= n;10 if(m == 1)11 for(i = x; i < x+n; i++)12 for(j = ... 阅读全文
posted @ 2012-08-29 00:01
Siriuslzx
阅读(175)
推荐(0)
编辑
|
|