HDOJ 5125 magic balls DP

DP

magic balls

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 561    Accepted Submission(s): 168

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 integer N(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.

Output

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

Sample Input

2
5 3
5 1
4 2
3 1
2 4
3 1
5 4
5 1
4 2
3 1
2 4
3 1

Sample Output

4
4

Source

BestCoder Round #20

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

const int maxn=1111;

int n,m;
int a[maxn],b[maxn];
int dp[maxn][maxn][2];

int main()
{
	int T_T;
	scanf("%d",&T_T);
	while(T_T--)
	{
		scanf("%d%d",&n,&m);
		for(int i=1;i<=n;i++)
		{
			scanf("%d%d",a+i,b+i);
		}
		memset(dp,-1,sizeof(dp));
		dp[0][0][0]=0;
		for(int i=1;i<=n;i++)
		{
			dp[i][0][0]=1; dp[i][1][1]=1;
			for(int j=1;j<i;j++)
			{
				int e=min(j,m);
				if(a[i]>a[j])
				{
					for(int k=0;k<=e;k++)
					{
						if(dp[j][k][0]>=0)
							dp[i][k][0]=max(dp[i][k][0],dp[j][k][0]+1);
					}
				}
				if(a[i]>b[j])
				{
					for(int k=0;k<=e;k++)
					{
						if(dp[j][k][1]>=0)
							dp[i][k][0]=max(dp[i][k][0],dp[j][k][1]+1);
					}
				}
				if(b[i]>a[j])
				{
					for(int k=0;k<=e;k++)
					{
						if(dp[j][k][0]>=0)
							dp[i][k+1][1]=max(dp[i][k+1][1],dp[j][k][0]+1);
					}
				}
				if(b[i]>b[j])
				{
					for(int k=0;k<=e;k++)
					{
						if(dp[j][k][1]>=0)
							dp[i][k+1][1]=max(dp[i][k+1][1],dp[j][k][1]+1);
					}
				}
			}
		}
		int ans=1;
		for(int i=1;i<=n;i++)
		{
			int e=min(i,m);
			for(int j=0;j<=e;j++)
			{
				ans=max(ans,max(dp[i][j][0],dp[i][j][1]));
			}
		}
		printf("%d\n",ans);
	}
	return 0;
}
时间: 2024-10-15 11:18:40

HDOJ 5125 magic balls DP的相关文章

hdu 5125 magic balls(dp)

题目链接:hdu 5125 magic balls #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int maxn = 1005; int N, M, dp[maxn][maxn], A[maxn], B[maxn]; int vec[maxn][maxn], c[maxn]; void init () { scanf("%d%d&quo

HDU 5125 magic balls(线段树+DP)

magic balls Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 323    Accepted Submission(s): 90 Problem Description The town of W has N people. Each person takes two magic balls A and B every day.

HDU 5125 Magic Ball DP+树状数组

由于只要找1~x 中的最大值,然后线段树又容易MLE,所以这里可以用树状数组搞. #include <cstdio> #include <cstring> #include <algorithm> #include <map> #include <set> #include <bitset> #include <queue> #include <stack> #include <string> #i

hdoj 5125 Little Zu Chongzhi&#39;s Triangles【状态压缩dp】

题目:hdoj 5125 Little Zu Chongzhi's Triangles 题意:给出n个木棍的长度,然后问这些木棍所能组成三角形的最大面积. 分析:这个题目用状态压缩解,因为木棍的最大个数为12 我们枚举所有状态,用状态对应位的 0 和 1 表示这个木棍是否选择,然后如果某个状态选择的木棍是3的话,判断是否可以组成,可以的话dp[st] = 三角形面积 然后大于三的,分解之后得出转移方程dp[st] = max(dp[st],dp[i] + dp[st - i]) (i | (st

hdu magic balls

magic balls Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 74    Accepted Submission(s): 10 Problem Description The town of W has N people. Each person takes two magic balls A and B every day.

BestCoder Round #25 1002 Harry And Magic Box [dp]

传送门 Harry And Magic Box Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 165    Accepted Submission(s): 64 Problem Description One day, Harry got a magical box. The box is made of n*m grids. Ther

HDOJ 5155 Harry And Magic Box DP

dp[i][j] 表示 长宽为i,j的矩形的可能的总数 dp[i][j+1] 可由 dp[i][j] 推过来,枚举dp[i][j]所保留的行数(1...i)即可 Harry And Magic Box Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 441    Accepted Submission(s): 209 Problem D

hdu 4323 Magic Number( DP )

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4323 Magic Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 1728    Accepted Submission(s): 705 Problem Description There are many magic num

HDU 5147 Harry And Magic Box dp+组合数

点击打开链接 Harry And Magic Box Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 197    Accepted Submission(s): 97 Problem Description One day, Harry got a magical box. The box is made of n*m grids.