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(1≤T≤20) (the data for N>100 less than 6 cases), indicating the number of test cases.
Each test case begins with two integerN(1≤N≤1000) and M(0≤M≤1000) ,indicating the number of people and the number of the wizard’s energy. Next N lines contains two integer ai and bi(1≤ai,bi≤109) ,indicating the balls’ volume.
Each test case begins with two integer
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
- hdoj5125magic balls【dp】
- POJ3783 Balls DP动态规划
- 264C - Choosing Balls DP
- HDOJ 5125 magic balls DP
- hdu 5125 magic balls(dp)
- POJ 3783 Balls(dp)
- poj-3783-Balls dp/线性dp
- Codeforces 67C. Sequence of Balls DP!
- codeforce 67C - Sequence of Balls(dp)
- SGU 183 Painting the balls (DP优化)
- sgu183:Painting the balls(dp+优化)
- codeforces 264C Choosing Balls (dp)
- codeforces 67C Sequence of Balls (dp)
- POJ 3783 Balls (线性dp 智力题)
- CodeForces 264C Choosing Balls dp(水
- hdu 5194 DZY Loves Balls (dp)
- SGU183 Painting the balls(DP+优化)
- hdoj 5570 balls 【概率dp 求期望】
- 欢迎使用CSDN-markdown编辑器
- 深入理解Hadoop集群和网络
- 关于js和css的一些实用笔记
- 在MyEclipse中设置struts.xml自动提示功能
- 探索HTML5本地存储功能运用技巧(1)
- hdoj5125magic balls【dp】
- react native之知乎日报源码分析一
- 【汇总】比赛低级错误
- IOS单元测试工具之OCMockito
- 如何实时查看mysql当前连接数
- STDIN_FILENO的作用与stdin的区别!!!
- Java Socket编程 详解
- 【杭电】[2051]Bitset
- 另一种方式实现京东顶部宽度全屏