摘要:pku3468: http://poj.org/problem?id=3468题意:给定一段区间,其初始数值给定,Q [a,b]表示求区间[a,b]的和,C [a,b]c表示将区间[a,b]的所有数值加上c解法:线段树lazy思想:由于整段要更新,所以每次只需更新父节点,并用lazy标记,当遇到lazy!=0时,更新下一代,同时标记,这样一直往下更新。code:#include<iostream>#include<cstdio>#include<cstdlib>#include<cstring>#include<algorithm>u
阅读全文
摘要:hdu1166: http://acm.hdu.edu.cn/showproblem.php?pid=1166题意:给出k个正整数,第i个正整数ai代表第i个工兵营地里开始时有ai个人,接下来有一些命令(增加、减少、查询、结束),输出每次查询结果。解法:线段树code:#include<iostream>#include<cstdio>#include<cstdlib>#include<cstring>#include<algorithm>using namespace std;const int inf=1<<29;st
阅读全文
摘要:开关灯问题:有n个人和n盏灯,第一个人开所有的灯,第二个人按所有2的倍数的灯的开关,第三个人按所有3的倍数的灯的开关,依此类推,求最后多少盏灯亮着解法:按奇数次则亮,偶数次则灭,所以因子数为奇数亮,所以平方数亮,所以即求n内平方数个数,为(int)sqrt(n),如n=16,根号16=4,所以平方数有1*1,2*2,3*3,4*4,即1、4、9、16四个。code:#include<stdio.h>#include<math.h>int main(){ int t,n,m; scanf("%d",&t); while(t--) { scanf
阅读全文
摘要:题意:求1-n内与n互质的数的个数code:#include<iostream>#include<cstdio>#include<cstdlib>int phi[10000];int main(){ int n,i,cnt,j,s; while(scanf("%d",&n)!=EOF) { cnt=0;s=n; for(i=2;i*i<=n;i++) //求质因数 { if(n%i==0) { phi[cnt++]=i; whil...
阅读全文
摘要:题意:求1<=x<=n内不超过x的所有与x互质的数的个数解法:欧拉公式code:#include<iostream>#include<cstdio>#include<cstdlib> const int N=100; int phi[N],prime[N];int main() { int i,j; prime[0]=prime[1]=0; for(i=2;i<N;i++) { prime[i]=1; } for(i=2;i*i<N;i++) { if(prime[i]) { ...
阅读全文
摘要:hdu1372: http://acm.hdu.edu.cn/showproblem.php?pid=1372题意:一张8*8棋盘,横坐标用a~h表示,纵坐标用1~8表示,每对数据给出马的初始位置和目标位置,问需要多少步能到达code:#include<iostream>#include<cstdio>#include<cstdlib>#include<algorithm>using namespace std;int dx[]={-2,2,-1,1,-2,2,-1,1};int dy[]={1,-1,-2,-2,-1,1,2,2};int q[
阅读全文
摘要:hdu2579: http://acm.hdu.edu.cn/showproblem.php?pid=2579题意:“#”代表石头,如果走的步数是k的倍数,石头会消失,求最小时间(步数)code:#include<iostream>#include<cstdio>#include<cstring>#include<cstdlib>#include<algorithm>using namespace std;const int inf=1<<29;char v[150][150];int q[150*150*150][2],
阅读全文
摘要:hdu2102: http://acm.hdu.edu.cn/showproblem.php?pid=2102 题意:迷宫的入口是S(0,0,0),公主的位置用P表示,时空传输机用#表示,墙用*表示,平地用.表示。骑士们一进入时空传输机就会被转到另一层的相对位置,但如果被转到的位置是墙 的话,那骑士们就会被撞死。骑士们在一层中只能前后左右移动,每移动一格花1时刻。层间的移动只能通过时空传输机,且不需要任何时间。code:#include<iostream>#include<cstdio>#include<cstring>#include<cstdlib
阅读全文
摘要:hdu1728: http://acm.hdu.edu.cn/showproblem.php?pid=1728题意:求最少转弯数,开始时方向不定,所以第一次算不成转弯code:#include<iostream>#include<cstdio>#include<cstdlib>int dx[]={1,-1,0,0};int dy[]={0,0,1,-1};int q[150*600][2],d[150][150][4];char v[150][150];const int inf=1<<29;int main(){ int i,j,m,n,x,y
阅读全文
摘要:hdu1171: http://acm.hdu.edu.cn/showproblem.php?pid=1171题意:给定设备种类n,接下来输出n行,v[i]、m[i]表示有m[i]个价值为v[i]的设备,求将这些设备分为两部分,每部分的价值,要求两部分的价值相差最小。解法:混合背包,背包最大容量为总价值sum的一半s,要求价值尽量大,则f[s]和sum-f[s]即为答案。 code:#include<iostream>#include<cstdio>#include<algorithm>using namespace std;int v[100],m[100
阅读全文
摘要:hdu2191: http://acm.hdu.edu.cn/showproblem.php?pid=2191题意:容量为v的背包,有n种物品,每种物品时有限个的,有不同体积及价值,求最大价值解法:多重背包code:#include<iostream>#include<cstdio>#include<algorithm>int v[200],w[200],c[200],f[200];int max(int a,int b){ if(a>b) return a; else return b;}int main(){ int t,n,m,i,j,k,x..
阅读全文
摘要:hdu1712: http://acm.hdu.edu.cn/showproblem.php?pid=1712题意:输入课程数n和总天数m,再输入矩阵n*m,v[i][j]表示花费j天在课程i上得到价值为v[i][j]解法:分组背包问题:组数为n,总容量为m,每件物品费用为c[i]=j,价值为w[i]=v[i][j]code:#include<iostream>#include<cstdio>#include<cstdlib>int max(int a,int b){ if(a>b) return a; else return b;}int f[120
阅读全文
摘要:hdu2602: http://acm.hdu.edu.cn/showproblem.php?pid=2602题意:有n个物品和容量为v的背包,每种物品只可取一个,且有不同价值及体积,求最大价值 解法:01背包code:#include<iostream>#include<cstdio>#include<algorithm>using namespace std;int max(int a,int b){ if(a>b) return a; else return b;}int v[1010],w[1010],f[1010];int main(){ .
阅读全文
摘要:hdu2546: http://acm.hdu.edu.cn/showproblem.php?pid=2546题意:输入n,表示有n种菜可购买,再输入n个数v[i],表示菜的价格,再输入m,表示卡上有m元,规定若卡上余额大于等于5则可购买任意价钱的物品(即使买后余额为负),否则不可购买任何物品,求最后卡上最小余额解法:01背包+贪心:最贵的物品肯定要被购买到,所以先选出来最后购买。先对除了最贵的物品外其余物品进行01背包处理,总容量为m-5,物品费用和价值都为w[i]。code:#include<iostream>#include<cstdio>#include<
阅读全文
摘要:pku1178: http://poj.org/problem?id=1178题意:给出一行字符串,每对字母+数字表示棋盘上的一点,如A表示横坐标为1,B表示横坐标为2等,第一对表示王,只可上下左右移动,后面每一对表示一个马,走法跟象棋一样(没有算马脚),若马与王到了同一个格子,马可以带着王走,求总的最少需要多少步可使所有马和王汇集到一个格子里解法:floyd+3个枚举:先用floyd算出马在每个点到其他点的最小步数,再3层枚举:第一层枚举所有成员聚集点,第二层枚举马接王的点,第三层枚举求出马接王再到达聚集点中需要步数最小的马code:#include<iostream>#incl
阅读全文
摘要:pku1125: http://poj.org/problem?id=1125题意:群发短信:给出n个群发者,输出的第i行的第一个输出k表示第i个发信息者可以给k个人发短信,接着输出k对数据,每对两个数分别表示被联系者的编号和与其联系时间,问由哪个人群发短信花费的时间最短,转化为最短路问题即为:给出每个点到其他点的距离,求所有点到其他点最长距离的最小值,并输出该起点解法:floyd求出每个点到各点的最短路,再枚举各点,求出各个最大值,再取最小值code:#include<iostream>#include<cstdio>#include<cstdlib>in
阅读全文
摘要:hdu2141: http://acm.hdu.edu.cn/showproblem.php?pid=2141题意:给出3个数列a、b、c,再给出s个x,问是否存在a[i]+b[j]+c[k]=x解法:先让a+b,得到和数列sum,并排序,再给c排序,再二分搜sum中是否存在sum[i]=x-c[j]code:#include<iostream>#include<cstdio>#include<cstdlib>#include<algorithm>using namespace std;bool cmp(int a,int b){ return
阅读全文
摘要:hdu2272: http://acm.hdu.edu.cn/showproblem.php?pid=2272题意:给出n个人的预期排名,问怎样的排名使得所有人的实际排名与预期排名之差的绝对值的和最小贪心法:将预期排名从小到大排序,再从1~n名依次给他们排名,则所求值最小。code:#include<iostream>#include<cstdio>#include<cstdlib>#include<cmath>#include<algorithm>using namespace std;struct str{ char s[30];
阅读全文
摘要:hdu2117: http://acm.hdu.edu.cn/showproblem.php?pid=2117题意:输入n和m(1<=n<=10^7,1<=m<=10^5),求1/n小数点后第m个数解法:模拟除法。code:#include<iostream>#include<cstdio>#include<cstdlib>int main(){ int m,n,s,x; while(scanf("%d%d",&n,&m)!=EOF) { s=1; for(int i=0;i<=m;i++)
阅读全文
摘要:hdu2116: http://acm.hdu.edu.cn/showproblem.php?pid=2116题意:输入k(2<=k<=64),再输入x,y(-2^(k-1)<=x,y<=2^(k-1)-1),判断x+y是否溢出(即是否超出[-2^(k-1),2^(k-1)-1])解法:若直接相加可能溢出,所以用max-x>y和min-x<y来判断.code:#include<iostream>#include<cstdio>#include<cstdlib>int main(){ long long int x,y,ma
阅读全文
摘要:hdu2115: http://acm.hdu.edu.cn/showproblem.php?pid=2115题意:输入n组名字和对应的时间(分:秒),要求按时间长度由短到长排序,并输出对应排名,若时间一样,则按名字字典序排序,名次可以并列。code:#include<iostream>#include<cstdio>#include<cstdlib>#include<cstring>#include<algorithm>using namespace std;struct name{ char v[200],w[200]; int
阅读全文
摘要:hdu2114: http://acm.hdu.edu.cn/showproblem.php?pid=2114题意:求x=1^3+2^3+3^3+……+n^3结果的后四位。解法:原式x=((n*(n+1))/2)^2,求后四位,即求x%10000,由公式(a*b)%m=((a%m)*(b%m))%m,(a/2)%m=(a%m)/2可求。code:#include<iostream>#include<cstdio>#include<cstdlib>int n;int main(){ int s; while(scanf("%d",&
阅读全文
摘要:hdu2138: http://acm.hdu.edu.cn/showproblem.php?pid=2138题意:给出n个数,求素数个数解法:为了不超时,加速法:求出65535内的素数,对于65535以后的数,若不能被这些素数整除,则为素数。证明如下:对于大于65535的整形非素数,有两种情况,一种是至少有一个因子在65535内,另一种是所有因子都在65535内,但是65536*65535>int,所以第二种不可能。code:#include<iostream>#include<cstdio>#include<cstdlib>int v[65536]
阅读全文
摘要:hdu2135: http://acm.hdu.edu.cn/showproblem.php?pid=2135题意:旋转一个n*n的矩阵,m为负代表逆时针,m为正代表顺时针,输出旋转后的矩阵code:#include<iostream>#include<cstdio>#include<cstdlib>#include<cmath>char v[20][20];int main(){ int m,n; while(scanf("%d%d",&n,&m)!=EOF) { getchar(); for(int i=0
阅读全文
摘要:hdu2133: http://acm.hdu.edu.cn/showproblem.php?pid=2133题意(解法):已知0年1月1日是周6,输入一个日期,问星期几code:#include<iostream>#include<cstdio>#include<cstdlib>#include<cstring>int tianshu1[]={31,28,31,30,31,30,31,31,30,31,30,31};int tianshu2[]={31,29,31,30,31,30,31,31,30,31,30,31};char tab[7][
阅读全文
摘要:hdu2100: http://acm.hdu.edu.cn/showproblem.php?pid=2100解法:高精度加法-26进制,A~Z分别代表0~26,求法和10进制一样code:#include<iostream>#include<cstdio>#include<cstdlib>#include<cstring>char a[300],b[300],c[300],d[300];int max(int x,int y){ if(x>y) return x; else return y;}int main(){ int x,y,s;
阅读全文
摘要:hdu1856: http://acm.hdu.edu.cn/showproblem.php?pid=1856题意:输入n对朋友,求最多的朋友集合(直接或间接为朋友)解法:并查集:集合的合并及查询。code:#include<iostream>#include<cstdio>#include<cstdlib>const int maxn=10000002;int fa[maxn],ans[maxn];int find(int x) //路径压缩,复杂度为常数{ int fx=fa[x]; if(x!=fx)fa[x]=find(fx); return fa[
阅读全文
摘要:hdu1798: http://acm.hdu.edu.cn/showproblem.php?pid=1798题意:给出两个圆的圆心坐标和半径,求两圆相交面积code:#include<iostream>#include<cstdlib>#include<cstdio>#include<cmath>const double pi=acos(double(-1));double min(double x,double y){ if(x>y) return y; else return x;}int main(){ double x1,y1,r
阅读全文
摘要:pku1018: http://poj.org/problem?id=1018题意:给出n种设备,每种设备有m家公司出售,对应宽带b[n][m]和价格p[n][m],现要求每种设备各选一个,要求所选设备bmin的最小值与所选设备总价格p的比值最大解法:贪心+枚举:将所有设备的b值从小到大枚举,再选出符合价格最小的设备。code:#include<iostream>#include<cstdio>#include<cstdlib>int b[150][150],p[150][150],m[150];const int inf=1<<29;int m
阅读全文
摘要:pku1083: http://poj.org/problem?id=1083题意:有一条走廊,走廊两边为房间,一边的编号都为奇数(1、3……、399),一边的编号都为偶数(2、4……、400),现要从某个房间搬东西到另一房间,走廊很窄,不能共用,即若要用到同一段走廊,不能同时搬,搬一次需时10,求最小搬家时间解法:贪心:因为如果不冲突的话可以同时进行,所以最小次数为走廊需要用到的次数最多那段的次数,再乘以10即为最小时间。code:#include<iostream>#include<cstdio>#include<cstdlib>int ans[500]
阅读全文
摘要:hdu1677: http://acm.hdu.edu.cn/showproblem.php?pid=1677题意:给出m个嵌套娃娃的数据(宽w、高h),求嵌套后最少娃娃数解法:贪心法+dp:类似最少拦截系统,这个问题其实是用dp求最长子序列的长度,先按宽从小到大排序,则小号可能可以嵌套在大号中,再依次判断后面的h是否比前面的大,若是,则取前面中h较大者(由贪心法可知)。code:#include<iostream>#include<cstdio>#include<cstdlib>#include<algorithm>using namespac
阅读全文
摘要:hdu3177: http://acm.hdu.edu.cn/showproblem.php?pid=3177题意:向一个体积为V的洞搬进东西,物品体积为v[i].a,需要的移动体积为v[i].b,问能否全部搬进洞中解法:贪心法:对于两件物品a1,b1;a2,b2,若先放1再放2,则V-a1>b2即V>a1+b2;若先放2再放1,则V-a2>b1即V>a2+b1,用贪心法可知要取a1+b2与a2+b1中较小者,即b-a大者先取code:#include<iostream>#include<cstdio>#include<cstdlib>
阅读全文
摘要:hdu2037: http://acm.hdu.edu.cn/showproblem.php?pid=2037题意:求最多不相交区间:给出n个节目开始a[i]和结束b[i]的时间,求最多可看多少个节目方法:贪心法:将b[i]从小到大排序,每次取第一个区间,再剔除与所取区间相交的区间。如样例应取(1,3)、(3,4)、(5,10)、(10,15)、(15,19)code:#include<iostream>#include<cstdio>#include<cstdlib>#include<algorithm>using namespace std;
阅读全文
摘要:hdu1257: http://acm.hdu.edu.cn/showproblem.php?pid=1257题意:导弹拦截系统:第一发炮弹能够到达任意的高度,但是以后每一发炮弹都不能超过前一发的高度,v[i]为依次发来的导弹高度,求最少需要多少套拦截系统解法:贪心法:如对于样例,首先启动第一套,拦截前三个导弹后,限定值为155,再启动第二套,限定值为300,对于后面的导弹,采用贪心法,与前面的限定值作比较,采用限定值最接近该高度且小于该限定值的系统,如果没有这种系统,则另启动新系统。code:#include<iostream>#include<cstdio>#inc
阅读全文
摘要:hdu1548: http://acm.hdu.edu.cn/showproblem.php?pid=1548题意:坐电梯,给出层数n,求从a到b最少要按多少次键(每层楼有一个数c[i],表示可以搭到第i-c[i]和第i+c[i]层,但只能到达1至n层,求最少需要按多少次按钮可到达目的地。解法:bfs(也可用dij求最短路)code:#include<iostream>#include<cstdio>#include<cstdlib>int v[250][250],k[250],d[250],q[3000*300];const int inf=2<&l
阅读全文
摘要:hdu1233: http://acm.hdu.edu.cn/showproblem.php?pid=1233题意:全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路相连,只要能间接通过公路可达即可),并要求铺设的公路总长度为最小。请计算最小的公路总长度。解法:最小生成树:Kruskalcode:#include<iostream>#include<cstdio>#include<cstdlib>#include<algorithm>using namespace std;int v[10000],k[10000],w[10000],r[
阅读全文
摘要:hdu1232: http://acm.hdu.edu.cn/showproblem.php?pid=1232题意:给几条边(可能重复给),求至少还需几条边能使各点直接或者间接联系解法:并查集:把已知的有联系的边减掉。code:#include<iostream>#include<cstdio>#include<cstdlib>int fa[1005],n,m;void solve(int a,int b){ for(int i=1;i<=n;i++) if(fa[i]==a) fa[i]=b;}int main(){ int ans,a,b; ...
阅读全文
摘要:hud1875: http://acm.hdu.edu.cn/showproblem.php?pid=1875题意:给出c个点的坐标,若点与点之间的距离大于1000或小于10则不能建桥,建桥费用为每米100元,求使得所有点能相通的最小费用,若不能相通,则输出“oh!"。解法:最小生成树prim。code:#include<iostream>#include<cstdio>#include<cstdlib>#include<cstring>#include<cmath>#include<algorithm>usin
阅读全文
摘要:hdu1879: http://acm.hdu.edu.cn/showproblem.php?pid=1879题意:给出修建各条路的费用和是否已修(1为已修,0为未修),求要使所有点直接或者间接相连的最少费用解法:只需将已修的路的费用改为0,然后求最小生成树即可。法一:prim:code:#include<iostream>#include<cstdlib>#include<cstdlib>#include<algorithm>using namespace std;int v[110][110],sign[110];const int inf=
阅读全文
摘要:hdu3342: http://acm.hdu.edu.cn/showproblem.php?pid=3342题意:判断是否有环,无环输出YES,有环输出NO解法:dfscode:#include<iostream>#include<cstdio>#include<cstdlib>#include<algorithm>using namespace std;int v[150][150],c[150],n,m;bool dfs(int k){ c[k]=-1; for(int i=0;i<n;i++) { if(v[k][i]) { ...
阅读全文
摘要:hdu2112: http://acm.hdu.edu.cn/showproblem.php?pid=2112题意:求最短路,输入的各个节点是用字符串表示的。解法:Dijkstra,将字符串转为编号,用int数组存入邻接矩阵code:#include<iostream>#include<cstdio>#include<cstring>#include<cstdlib>#include<algorithm>using namespace std;const int inf=1<<29;char v[200][50];int
阅读全文
摘要:hdu1195: http://acm.hdu.edu.cn/showproblem.php?pid=1195题意:求解密最少步骤:密码为四位数,每次操作可将一个数加一或减一(1减一变为9,9加一变为1),也可互换相邻数字,求最少需要多少操作数能解密解法:bfs:加一减一和交换位置都属于状态转移code:#include<iostream>#include<cstdio>#include<cstdlib>int q[1000*1000][4],v[4],key[4],dis[10][10][10][10],an1[4],bn1[4],cn1[4],dn1[4
阅读全文
摘要:hdu1253: http://acm.hdu.edu.cn/showproblem.php?pid=1253题意:3层迷宫:一个A*B*C的立方体,可以被表示成A个B*C的矩阵,要从(0,0,0)到(A-1,B-1,C-1),若时间小于t,输出时间,否则输出-1.解法:bfs:开三维数组d[i][j][k]表示在第k个矩阵的点(i,j)上,队列要进点和k。code:#include<iostream>#include<cstdio>#include<cstdlib>int d[60][60][60];char v[60][60][120];struct a
阅读全文
摘要:hdu1072: http://acm.hdu.edu.cn/showproblem.php?pid=1072题意:0代表墙,1代表空地,2代表始点,3代表终点,4代码时间重置,炸弹限时6s,每走一步耗时1s,如果到达4可重置为6s,求最少逃脱时间,逃脱不了输出-1解法:bfs:开三维数组d[i][j][k]表示到达(i,j)点时剩余时间为k,此时的最优值,队列开二维q[rear][0]进点,q[rear][1]进剩余时间code:#include<iostream>#include<cstdio>#include<cstdlib>int d[10][10]
阅读全文
摘要:hdu2151: http://acm.hdu.edu.cn/showproblem.php?pid=2151题意:给出n棵树、p、t分别表示虫的起始树和终点树,m为m分钟后到达t,求最多有多少中走法能从p到t,每次移动只能移向左或者右边的树解法:dp:dp[i][j]表示到达第j棵树,前i分钟的走法数,则转移方程为:dp[i][j]=dp[i-1][j-1]+dp[i-1][j+1],注意初始化。code:#include<iostream>#include<cstdio>#include<cstdlib>#include<algorithm>
阅读全文
摘要:hdu1069: http://acm.hdu.edu.cn/showproblem.php?pid=1069题意:用长方体叠梯子,大的放下面,小的放上面,求最高高度解法:排序+枚举+dp:先按面积大小将所有长方体进行排序,再枚举放在上面和下面的长方体,用v[i].ah表示以i为底的小梯子的高度,则v[i].ah+=max(v[j].ah)(0<=j<i),表示原来的梯子叠上以j为底的梯子的高度,最后再去最大值。code:#include<iostream>#include<cstdio>#include<cstdlib>#include<
阅读全文
摘要:hdu2845: http://acm.hdu.edu.cn/showproblem.php?pid=2845题意:吃豆:如果吃了某个豆(x,y),则x-1和x+1两行都不能吃,(x,y-1)、(x,y+1)也不能吃,求最多能吃多少豆解法:dp:先算行,再算列,列的算法和行的类似,dp[i][j]表示在第i行中,直到第(i,j)个点的最优值,有状态转移方程:dp[i][j]=max(dp[i][j-1],dp[i][j-2]+v[i][j]),再计算列的,方程类似,为dp1[i]=max(dp1[i-1],dp1[i-2]+dp[i][n-1]).code:#include<iostre
阅读全文
摘要:hdu1159: http://acm.hdu.edu.cn/showproblem.php?pid=1159题意:求最长共同子序列(可不连续)的长度解法:dp:dp[i][j]表示第一个字符串的前i个字符与第二个字符串的前j个字符的最长共同子序列,则有两种情况,一种是a[i]==b[j],此时dp[i][j]=dp[i-1][j-1]+1;另一种是不等,则dp[i][j]=max(dp[i-1][j],dp[i][j-1]).code:#include<iostream>#include<cstdio>#include<cstdlib>#include&l
阅读全文
摘要:hdu1087: http://acm.hdu.edu.cn/showproblem.php?pid=1087题意:跳格,每个格有一个分数值,只能从分数值低的跳到分数值高的,求最大值解法:dp:dp[i]表示直到第i个格子的最优值,则转移方程为:dp[i]=max(dp[i],dp[j]+v[i])(0<j<i)code:#include<iostream>#include<cstdio>#include<algorithm>using namespace std;int v[1001],dp[1001];int max(int a,int b)
阅读全文
摘要:hdu1003: http://acm.hdu.edu.cn/showproblem.php?pid=1003题意:求最大子序列及其首尾位置code:#include<iostream>#include<cstdio>#include<cstdlib>using namespace std;int v[200000];int main(){ int t,n,a,b,c,d,ans,sum,l; cin >> t; for(int j=1;j<=t;j++) { cin>>n;l=0; for(int k=0;k<n;k++
阅读全文
摘要:pku1157: http://poj.org/problem?id=1157题意:有n种花和m个花瓶(m>=n),每种花放入不同的花瓶会有不同的美感值,每个花瓶只能放一种花,求最大的美感总值解法:dp:dp[i][j]表示到第i种花为止取v[i][j]的最优值,转移方程为:dp[i][j]=max(dp[i-1][k]+v[i][j])(0<=k<j),表示取第i-1种花的最优值加上v[i][j]。code:#include<iostream>#include<cstdio>#include<cstdlib>#include<alg
阅读全文
摘要:pku2904: http://poj.org/problem?id=2904题意:给k个油桶,要试验其能承受的爆炸底线,求对于最坏的情况(爆炸底线m最大)时需要试验的最少次数,比如k=1,m=10,则需从1开始试验,直到油桶爆炸,便可知起爆炸底线为该值-1,若为最坏情况,即为10,则要试验1+2+3……+10。若有2个油桶,可任意取一个值i试验,若油桶没有爆炸,则说明底线>i,否则<i且只剩一个油桶,需要从1开始试验(否则再炸了就没有油桶可以试验了)解法:dp:dp[i][k][j]表示用第i个油桶从k到j试验的最优值,方程为dp[i][k][j]=min(dp[i][k][j]
阅读全文
摘要:pku1050: http://poj.org/problem?id=1050题意:求最大子矩阵code:#include<iostream>#include<cstdio>#include<cstdlib>int v[200][200],b[200];const int inf=1<<29;int main(){ int n; while(scanf("%d",&n)!=EOF) { for(int i=0;i<n;i++) { for(int j=0;j<n;j++) scanf("%d&qu
阅读全文
摘要:hdu2059: http://acm.hdu.edu.cn/showproblem.php?pid=2059题意:乌龟骑着充满电的电动车从起点出发,途中有n个充电站,离起点的距离为p[i],电动车每次充电后电动时间为t,乌龟电动速度为v1,脚踩电动车速度为v2,兔子恒速为vr,问龟兔输赢。解法:dp:优化前方程:dp[i][j]表示乌龟上次充电的站点为j,为从起点到i站时间,dp[i][j]=min(dp[i][j],x+dp[j][k]),dp[j][k]为在站点k充电后到j,从起点到j站花费的最短时间(最短因为已经算过了),x为从j站到i站花费的时间,需要3个for。可优化为一维,dp[
阅读全文
摘要:hdu1176: http://poj.org/problem?id=1179题意:有一一维坐标系,从0编号到10,一个人站在5上,现在天上正在掉馅饼,这个人每秒只能移动一个单位,所以第一秒只能接住掉在4、5、6上的馅饼,现给出某个点在某个时间 点有馅饼,问最多能接到多少馅饼解法:dp:dp[i][j]表示直到第j秒站在i上接到的最多馅饼数,则有转移方程:dp[i][j]=v[i][j]+max(dp[i-1][j-1],dp[i][j-1],dp[i+1][j-1])。code:#include<iostream>#include<cstdio>#include<
阅读全文
摘要:pku1179: http://poj.org/problem?id=1179题意:给一个多边形,每个点有一个数字,点与点之间有一个字符('t'表示相加,'x'表示相乘),问断了哪条边后剩下数运算后的结果最大(运算结果会有多种,取其最大数),输出最大结果及断了的边(可能有多种)解法:dp:开三维数组dp[i][j][0]表示从i算到j的最大值,dp[i][j][1]表示从i算到j的最小值,要求出最小值是因为对于乘法,可能最大值是由两个最小值相乘得到的(如两个负数)code:#include<iostream>#include<cstdio>
阅读全文
摘要:pku1088:求矩阵的最长降序路线: http://poj.org/problem?id=1088解法1:dp+dfs记忆性搜索:dp[x][y]=max(dp[i][j]+1)((i,j)为(x,y)的上下左右点),所以要先算i,j的最优值,所以需要用到递归code1:#include<iostream>#include<cstdio>#include<cstdlib>int v[150][150],ans[150][150],n,m;int dx[]={0,0,-1,1};int dy[]={-1,1,0,0};int dp(int a,int b){
阅读全文