hdu 1024 Max Sum Plus Plus(DP缓存)
Problem Description
Now I think you have got an AC in Ignatius.L's "Max Sum" problem. To be a brave ACMer, we always challenge ourselves to more difficult problems. Now you are faced with a more difficult problem.
Given a consecutive number sequence S 1, S 2, S 3, S 4 ... S x, ... S n (1 ≤ x ≤ n ≤ 1,000,000, -32768 ≤ S x ≤ 32767). We define a function sum(i, j) = S i + ... + S j (1 ≤ i ≤ j ≤ n).
Now given an integer m (m > 0), your task is to find m pairs of i and j which make sum(i 1, j 1) + sum(i 2, j 2) + sum(i 3, j 3) + ... + sum(i m, j m) maximal (i x ≤ i y ≤ j x or i x ≤ j y ≤ j x is not allowed).
But I`m lazy, I don't want to write a special-judge module, so you don't have to output m pairs of i and j, just output the maximal summation of sum(i x, j x)(1 ≤ x ≤ m) instead. ^_^
Given a consecutive number sequence S 1, S 2, S 3, S 4 ... S x, ... S n (1 ≤ x ≤ n ≤ 1,000,000, -32768 ≤ S x ≤ 32767). We define a function sum(i, j) = S i + ... + S j (1 ≤ i ≤ j ≤ n).
Now given an integer m (m > 0), your task is to find m pairs of i and j which make sum(i 1, j 1) + sum(i 2, j 2) + sum(i 3, j 3) + ... + sum(i m, j m) maximal (i x ≤ i y ≤ j x or i x ≤ j y ≤ j x is not allowed).
But I`m lazy, I don't want to write a special-judge module, so you don't have to output m pairs of i and j, just output the maximal summation of sum(i x, j x)(1 ≤ x ≤ m) instead. ^_^
Input
Each test case will begin with two integers m and n, followed by n integers S
1, S
2, S
3 ... S
n.
Process to the end of file.
Process to the end of file.
Output
Output the maximal summation described above in one line.
Sample Input
1 3 1 2 3 2 6 -1 4 -2 3 -2 3
Sample Output
6 8
Hint
Huge input, scanf and dynamic programming is recommended.
DP,空间优化:用一个数组足以,时间优化:dp有枚举过程,在计算dp的时候顺便就把枚举结果缓存到一个数组中
package l1;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while(sc.hasNext()) {
int m=sc.nextInt(), n=sc.nextInt();
int[] a = new int[1+n];
for(int i=1; i<=n; i++) a[i]=sc.nextInt();
int[] dp = new int[n+1], preMax = new int[n+1];
int max = -99999999;;
for(int i=1; i<=m; i++) {
max = -99999999;
for(int j=i; j<=n; j++) {
dp[j] = Math.max(dp[j-1]+a[j], preMax[j-1]+a[j]);
preMax[j-1] = max;
max = Math.max(max, dp[j]);
}
}
System.out.println(max);
}
}
}
HDU 1069 Monkey and Banana(最长递增子序列)
Problem Description
A group of researchers are designing an experiment to test the IQ of a monkey. They will hang a banana at the roof of a building, and at the mean time, provide the monkey with some blocks. If the monkey is clever enough, it shall be able to reach the banana by placing one block on the top another to build a tower and climb up to get its favorite food.
The researchers have n types of blocks, and an unlimited supply of blocks of each type. Each type-i block was a rectangular solid with linear dimensions (xi, yi, zi). A block could be reoriented so that any two of its three dimensions determined the dimensions of the base and the other dimension was the height.
They want to make sure that the tallest tower possible by stacking blocks can reach the roof. The problem is that, in building a tower, one block could only be placed on top of another block as long as the two base dimensions of the upper block were both strictly smaller than the corresponding base dimensions of the lower block because there has to be some space for the monkey to step on. This meant, for example, that blocks oriented to have equal-sized bases couldn't be stacked.
Your job is to write a program that determines the height of the tallest tower the monkey can build with a given set of blocks.
The researchers have n types of blocks, and an unlimited supply of blocks of each type. Each type-i block was a rectangular solid with linear dimensions (xi, yi, zi). A block could be reoriented so that any two of its three dimensions determined the dimensions of the base and the other dimension was the height.
They want to make sure that the tallest tower possible by stacking blocks can reach the roof. The problem is that, in building a tower, one block could only be placed on top of another block as long as the two base dimensions of the upper block were both strictly smaller than the corresponding base dimensions of the lower block because there has to be some space for the monkey to step on. This meant, for example, that blocks oriented to have equal-sized bases couldn't be stacked.
Your job is to write a program that determines the height of the tallest tower the monkey can build with a given set of blocks.
Input
The input file will contain one or more test cases. The first line of each test case contains an integer n,
representing the number of different blocks in the following data set. The maximum value for n is 30.
Each of the next n lines contains three integers representing the values xi, yi and zi.
Input is terminated by a value of zero (0) for n.
representing the number of different blocks in the following data set. The maximum value for n is 30.
Each of the next n lines contains three integers representing the values xi, yi and zi.
Input is terminated by a value of zero (0) for n.
Output
For each test case, print one line containing the case number (they are numbered sequentially starting from 1) and the height of the tallest possible tower in the format "Case case: maximum height = height".
Sample Input
1 10 20 30 2 6 8 10 5 5 5 7 1 1 1 2 2 2 3 3 3 4 4 4 5 5 5 6 6 6 7 7 7 5 31 41 59 26 53 58 97 93 23 84 62 64 33 83 27 0
Sample Output
Case 1: maximum height = 40 Case 2: maximum height = 21 Case 3: maximum height = 28 Case 4: maximum height = 342
就是个最长递增子序列
#include <iostream>
#include <algorithm>
using namespace std;
// http://blog.csdn.net/lttree/article/details/26606947
struct box
{
int l,w,h;
} boxs[181];
bool cmp(box b1, box b2)
{
if(b1.l==b2.l) return b1.w<b2.w;
return b1.l<b2.l;
}
int dp[181];
int main()
{
int i,j,n,len,t_num=1;
int z1,z2,z3;
while(cin>>n && n)
{
len = 0;
for(i=0; i<n; ++i)
{
cin >>z1>>z2>>z3;
boxs[len].l=z1,boxs[len].w=z2,boxs[len++].h=z3;
boxs[len].l=z1,boxs[len].w=z3,boxs[len++].h=z2;
boxs[len].l=z2,boxs[len].w=z1,boxs[len++].h=z3;
boxs[len].l=z2,boxs[len].w=z3,boxs[len++].h=z1;
boxs[len].l=z3,boxs[len].w=z1,boxs[len++].h=z2;
boxs[len].l=z3,boxs[len].w=z2,boxs[len++].h=z1;
}
sort(boxs, boxs+len, cmp);
dp[0]=boxs[0].h;
int max_h;
for(i=1; i<len; i++)
{
max_h = 0;
for(j=0; j<i; j++)
{
if(boxs[j].l<boxs[i].l && boxs[j].w<boxs[i].w)
max_h=max(max_h, dp[j]);
}
dp[i]=boxs[i].h+max_h;
}
max_h = 0;
for(i=0;i<len;i++)
max_h=max(max_h,dp[i]);
cout<<"Case "<<t_num++<<": maximum height = "<<max_h<<endl;
}
return 0;
}
HDU1074:Doing Homework(状态压缩DP)
Problem Description
Ignatius has just come back school from the 30th ACM/ICPC. Now he has a lot of homework to do. Every teacher gives him a deadline of handing in the homework. If Ignatius hands in the homework after the deadline, the teacher will reduce his score of the final test, 1 day for 1 point. And as you know, doing homework always takes a long time. So Ignatius wants you to help him to arrange the order of doing homework to minimize the reduced score.
Input
The input contains several test cases. The first line of the input is a single integer T which is the number of test cases. T test cases follow.
Each test case start with a positive integer N(1<=N<=15) which indicate the number of homework. Then N lines follow. Each line contains a string S(the subject's name, each string will at most has 100 characters) and two integers D(the deadline of the subject), C(how many days will it take Ignatius to finish this subject's homework).
Note: All the subject names are given in the alphabet increasing order. So you may process the problem much easier.
Each test case start with a positive integer N(1<=N<=15) which indicate the number of homework. Then N lines follow. Each line contains a string S(the subject's name, each string will at most has 100 characters) and two integers D(the deadline of the subject), C(how many days will it take Ignatius to finish this subject's homework).
Note: All the subject names are given in the alphabet increasing order. So you may process the problem much easier.
Output
For each test case, you should output the smallest total reduced score, then give out the order of the subjects, one subject in a line. If there are more than one orders, you should output the alphabet smallest one.
Sample Input
2 3 Computer 3 3 English 20 1 Math 3 2 3 Computer 3 3 English 6 3 Math 6 3
Sample Output
2 Computer Math English 3 Computer English Math
Hint
In the second test case, both Computer->English->Math and Computer->Math->English leads to reduce 3 points, but the word "English" appears earlier than the word "Math", so we choose the first order. That is so-called alphabet order.
题意:有n门课,每门课有截止时间和完成所需的时间,如果超过规定时间完成,每超过一天就会扣1分,问怎样安排做作业的顺序才能使得所扣的分最小
思路:因为最多只有15门课程,可以使用二进制来表示所有完成的状况
例如5,二进制位101,代表第一门和第三门完成了,第二门没有完成,那么我们可以枚举1~1<<n便可以得出所有的状态
然后对于每一门而言,其状态是t = 1<<i,我们看这门在现在的状态s下是不是完成,可以通过判断s&t是否为1来得到
当得出t属于s状态的时候,我们便可以进行DP了,在DP的时候要记录路径,方便之后的输出
#include <iostream>
#include <string>
#include <cstring>
#include <stack>
#include <algorithm>
using namespace std;
const int inf = 1<<30;
// http://blog.csdn.net/libin56842/article/details/24316493
struct node
{
string name;
int dead,cost;
}a[50];
struct node2
{
int time,score,pre,now;
}dp[1<<15];
int main()
{
int t,i,j,n,s,end;
cin >> t;
while(t--)
{
memset(dp,0,sizeof(dp));
cin >> n;
for(i=0; i<n; i++)
cin>>a[i].name>>a[i].dead>>a[i].cost;
end = 1<<n;
for(s=1;s<end;s++)
{
dp[s].score=inf;
// loop every task as before task
for(i=n-1;i>=0;i--)
{
int tmp=1<<i;
if(s&tmp)
{
int past=s-tmp;
int st = dp[past].time+a[i].cost-a[i].dead;
if(st<0) st=0;
if(st+dp[past].score<dp[s].score)
{
dp[s].score=st+dp[past].score;
dp[s].now=i;
dp[s].pre=past;
dp[s].time=dp[past].time+a[i].cost;
}
}
}
}
stack<int> s;
cout<<dp[end-1].score<<endl;
int tmp = end-1;
while(tmp)
{
s.push(dp[tmp].now);
tmp=dp[tmp].pre;
}
while(!s.empty())
{
cout << a[s.top()].name<<endl;
s.pop();
}
}
return 0;
}