随笔分类 - DP
摘要:题意: 给你一段含有 n 个数的序列,将它分成 m 份,问你这 m 份的最大值可以是多少。分析: 状态转移方程: dp[i][j]=max(dp[i][j-1],dp[i-1][k]) 1<k<j dp[i][j]表示 前 j 个数分成 i 组的最大值 ,其中,第 J 个数包含在分好的组里面。 可以用滚动数组 将上面的改成一维的。 View Code #include<stdio.h>#include<string.h>#define INF 9999999#define max(a,b)(a)>(b)?(a):(b)int a[1000007];in
阅读全文
摘要:题意: 给你一个 n 个人的队列,从中去掉一些人使剩下的人满足 任何一个人可以看到他 至少一侧的所有人。分析: 从前往后 和 从 后往前分别 求一次LIS,然后枚举分割点。View Code #include<stdio.h>#include<string.h>int main(){ int res,n,i,j,tmp; double a[1002]; int dp1[1002]; int dp2[1002]; while(scanf("%d",&n)!=EOF) { for(i=1;i<=n;i++) scanf("%l..
阅读全文
摘要:题意: 给你一个 含有 n 个珠子的项链,规定只有相邻的珠子才能合到一起并得到能量,合到一起的到的新的珠子,可以和其相邻的珠子继续合成,前后次序没有要求, 问你最大能的到多大的能量;分析 :用 dp[i][j]来表示从 i 到 j 合成得到的最大能量,则状态转移方程为 dp[i][j]=max(dp[i][j],dp[i][k]+dp[k][j]+a[i].left*a[k].right*a[j].right) 通过枚举 I 和 J 的分割点来得到 dp[i][j]的最大值。View Code #include<stdio.h>#include<string.h>#de
阅读全文
摘要:Description搞ACM不能只局限于看书和刷题,还要把它用到实际工作中,更是要把平时所学用到实际生活中去。下面就是动态规划的经典运用!你追一个MM的时候,需要对该MM身边的各闺中密友都好,这样你追MM这个问题就分解为对其MM朋友的问题,只有把这些问题都解决了,最终你才能追到MM。 该方法适用于聪明的MM,懂得“看一个人,不是看他如何对你,而是看他如何对他人。”的道理,并且对付这样的MM总能得到最优解。 该方法的缺点是开销较大,因为每个子问题都要好好对待。。。。 (如果是MM追GG,可以直接AC。) 所以,需要先把MM的朋友处理好,才能获得最终解。 GG需要获得MM朋友的好感,这样才有利于
阅读全文
摘要:Problem DescriptionYou, the leader of Starship Troopers, are sent to destroy a base of the bugs. The base is built underground. It is actually a huge cavern, which consists of many rooms connected with tunnels. Each room is occupied by some bugs, and their brains hide in some of the rooms. Scientist
阅读全文
摘要:Problem DescriptionA school bought the first computer some time ago(so this computer's id is 1). During the recent years the school bought N-1 new computers. Each new computer was connected to one of settled earlier. Managers of school are anxious about slow functioning of the net and want to kn
阅读全文
摘要:DescriptionLeyni喜欢机器人,这次,他得到了一个机器人,这个机器人能接受两种命令,"T"(向后转),"F"(前进一步)。他还得到了一个用于控制这个机器人的指令序列,他计划恰好修改k次这个指令序列(一次只能修改其中一个指令,同一个指令也可以被反复修改)后让机器人去执行,并使这个机器人最终停下的位置离起点尽量远。他想知道这个机器人最终停下的位置离起点的最远距离,请你帮助他!Input本题有多组测试数据,输入的第一行是一个整数T代表着测试数据的数量,接下来是T组测试数据。对于每组测试数据:第1行 包含一个长度不超过100的仅由"T&qu
阅读全文
摘要:Description设有N*N的方格图(N<=10),我们将其中的某些方格中填入正整数,而其他的方格中则放人数字0。如下图所示(见样例 ,黄色和蓝色分别为两次走的路线,其中绿色的格子为黄色和蓝色共同走过的):A1367142141514B某人从图的左上角的A点出发,可以向下行走,也可以向右走,直到到达右下角的B 点。在走过的路上,他可以取走方格中的数(取走后的方格中将变为数字0)。此人从A点到B点共走两次,试找出2条这样的路径,使得取得的数之和为最大。Input有多组测试数据,每组格式如下: 第一行为一个整数N(表示N*N的方格图),接下来的每行有三个整数,前两个表示位置,第三个数为该
阅读全文
摘要:DescriptionJimmy experiences a lot of stress at work these days, especially since his accident made working difficult. To relax after a hard day, he likes to walk home. To make things even nicer, his office is on one side of a forest, and his house is on the other. A nice walk through the forest, se
阅读全文
摘要:DescriptionSuppose there is a tree named A. All nodes of A have a weight v(0<v<4000000).Now, we will give the definition of "Prime Node".A node is a Prime Node if the following conditions are satisfied.The subtree of A whose root node is b will be marked as B. If all nodes in B have
阅读全文
摘要:DescriptionCc is a lovely monkey. It likes to play the game "catching plates".The game is as follows.There are n pegs in a line numbered from 1 to n. Cc stands on the first peg at the beginning. It is rather hard for Cc to jump from peg i to peg i+1(i+1<=n) or peg i-1(i-1>=1), which
阅读全文
摘要:Description俗话说“月有阴晴圆缺,人有悲欢离合。”。虽然大家都没这么悲催,但是心情的波动在所难免。MM的心情也会有波动,心情好心情值就高,心情不好心情值就低,每个小时都不一样,GG想知道MM最长的上升心情值的子序列,这样GG才好。。。按时间先后给出n个小时的心情值e[i],GG要求一个最长的子序列,使得该子序列e1< e2<e3<e4..<ek,并且使得e1+e2+e3+..+ek的和最大。Input有多组数据,对于每组数据,第一行是一个整数n(<=1000),接下来有n个整数ei。Output每组数据输出一行,该行包含两个数,表示最长子序列的长度和子序
阅读全文
摘要:Problem Description据说在很久很久以前,可怜的兔子经历了人生中最大的打击——赛跑输给乌龟后,心中郁闷,发誓要报仇雪恨,于是躲进了杭州下沙某农业园卧薪尝胆潜心修炼,终于练成了绝技,能够毫不休息得以恒定的速度(VR m/s)一直跑。兔子一直想找机会好好得教训一下乌龟,以雪前耻。最近正值HDU举办50周年校庆,社会各大名流齐聚下沙,兔子也趁此机会向乌龟发起挑战。虽然乌龟深知获胜希望不大,不过迫于舆论压力,只能接受挑战。比赛是设在一条笔直的道路上,长度为L米,规则很简单,谁先到达终点谁就算获胜。无奈乌龟自从上次获胜以后,成了名龟,被一些八卦杂志称为“动物界的刘翔”,广告不断,手头也有
阅读全文
摘要:DescriptionA TV-network plans to broadcast an important football match. Their network of transmitters and users can be represented as a tree. The root of the tree is a transmitter that emits the football match, the leaves of the tree are the potential users and other vertices in the tree are relays
阅读全文
摘要:Problem DescriptionWhile studying the history of royal families, you want to know how wealthy each family is. While you have various 'net worth' figures for each individual throughout history, this is complicated by double counting caused by inheritance. One way to estimate the wealth of a f
阅读全文
摘要:DescriptionBob enjoys playing computer games, especially strategic games, but sometimes he cannot find the solution fast enough and then he is very sad. Now he has the following problem. He must defend a medieval city, the roads of which form a tree. He has to put the minimum number of soldiers on t
阅读全文
摘要:DescriptionLet us define a regular brackets sequence in the following way: 1. Empty sequence is a regular sequence. 2. If S is a regular sequence, then (S) and [S] are both regular sequences. 3. If A and B are regular sequences, then AB is a regular sequence. For example, all of the following sequen
阅读全文
摘要:Recently, researchers on Mars have discovered N powerful atoms. All of them are different. These atoms have some properties. When two of these atoms collide, one of them disappears and a lot of power is produced. Researchers know the way every two atoms perform when collided and the power every two
阅读全文
摘要:Problem DescriptionAfter coding so many days,Mr Acmer wants to have a good rest.So travelling is the best choice!He has decided to visit n cities(he insists on seeing all the cities!And he does not mind which city being his start station because superman can bring him to any city at first but only o
阅读全文
摘要:DescriptionThe Pizazz Pizzeria prides itself in delivering pizzas to its customers as fast as possible. Unfortunately, due to cutbacks, they can afford to hire only one driver to do the deliveries. He will wait for 1 or more (up to 10) orders to be processed before he starts any deliveries. Needless
阅读全文