hdoj5125magic balls【dp】

来源:互联网 发布:单片机工程师待遇 编辑:程序博客网 时间:2024/05/16 00:51

magic balls

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 845    Accepted Submission(s): 255


Problem Description
The town of W has N people. Each person takes two magic balls A and B every day. Each ball has the volume ai and bi. People often stand together. The wizard will find the longest increasing subsequence in the ball A. The wizard has M energy. Each point of energy can change the two balls’ volume.(swap(ai,bi)).The wizard wants to know how to make the longest increasing subsequence and the energy is not negative in last. In order to simplify the problem, you only need to output how long the longest increasing subsequence is.
 

Input
The first line contains a single integer T(1T20)(the data for N>100 less than 6 cases), indicating the number of test cases.
Each test case begins with two integer N(1N1000) and M(0M1000),indicating the number of people and the number of the wizard’s energy. Next N lines contains two integer ai and bi(1ai,bi109),indicating the balls’ volume.
 

Output
For each case, output an integer means how long the longest increasing subsequence is.
 

Sample Input
25 35 14 23 12 43 15 45 14 23 12 43 1
 

Sample Output
44
 

Source
BestCoder Round #20
 

#include<iostream>#include<cstdio>#include<cstdlib>#include<cstring>#include<algorithm>#include<cmath>#include<list>#include<queue>#include<vector>using namespace std;const int maxn=10010;struct Node{    int ans,ener;}dp[maxn][2];int balla[maxn],ballb[maxn];int main(){    int t,i,j,k,n,m;    scanf("%d",&t);    while(t--){        scanf("%d%d",&n,&m);        for(i=0;i<n;++i){            scanf("%d%d",&balla[i],&ballb[i]);        }        int maxans=0;        for(i=0;i<n;++i){            dp[i][0].ans=dp[i][1].ans=1;            dp[i][0].ener=m;dp[i][1].ener=m-1;            Node max1,max2,max3,max4;            max1.ans=max2.ans=max3.ans=max4.ans=0;            for(j=0;j<i;++j){                if(balla[i]>balla[j]&&dp[j][0].ans+1>max1.ans){                    max1.ans=dp[j][0].ans+1;max1.ener=dp[j][0].ener;                }                if(balla[i]>ballb[j]&&dp[j][1].ans+1>max2.ans){                    max2.ans=dp[j][1].ans+1;max2.ener=dp[j][1].ener;                }                if(ballb[i]>balla[j]&&dp[j][0].ans+1>max3.ans&&dp[j][0].ener){                    max3.ans=dp[j][0].ans+1;max3.ener=dp[j][0].ener-1;                }                if(ballb[i]>ballb[j]&&dp[j][1].ans+1>max4.ans&&dp[j][1].ener){                    max4.ans=dp[j][1].ans+1;max4.ener=dp[j][1].ener-1;                }            }            if(max2.ans>max1.ans){                max1.ans=max2.ans;                max1.ener=max2.ener;            }            if(max4.ans>max3.ans){                max3.ans=max4.ans;                max3.ener=max4.ener;            }            if(max1.ans>dp[i][0].ans){                dp[i][0].ans=max1.ans;                dp[i][0].ener=max1.ener;            }            if(max3.ans>dp[i][1].ans){                dp[i][1].ans=max3.ans;                dp[i][1].ener=max3.ener;            }            maxans=max(maxans,max(dp[i][1].ans,dp[i][0].ans));        }        printf("%d\n",maxans);    }    return 0;}


0 0