ZOJ2283 Challenge of Wisdom(DP+二分求最长非上升子序列)

102 篇文章 0 订阅
46 篇文章 0 订阅


Link:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1283


Challenge of Wisdom

Time Limit: 2 Seconds       Memory Limit: 32768 KB

Background

"Then, I want to know whether you're a wise boy!"

Problem

"I have a great deal of lands. They're divided into N*M grids (N, M <= 1,000,000,000). When you are in (x, y), you may move into (x+1, y) or (x, y+1). I have P (P <= 100,000) treasures in the lands; each time, you can pick up anything you like."

"Now, I'll give you a map of my treasures; tell me, wise boy, how many times you need to pick up all the treasures?"

Input

This problem contains multiple test cases.

Each test case begins with 3 integers N, M and P. then followed by P lines, each lines contains 2 numbers: x, y, representing the location of a treasure.

Output

For each test case, output the minimum times I need.

Sample Input

7 7 7
1 2
1 4
2 4
2 6
4 4
4 7
6 6

Sample Output

2


Contest: A Great Beloved and My Gate to Freedom
There is a cx, there is a love_cx.


Author:  JIANG, Yanyan
Source:  JIANG, Yanyan's Contest #2



AC code:

#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<math.h>
#include<string.h>
#include<map>
#include<set>
#include<vector>
#define LL long long
#define INF 0xfffffff
#define PI acos(-1)
#define EPS 1e-6
using namespace std;

struct node{
	int x;
	int y;
}po[100010];

bool cmp(node a,node b)
{
	if(a.x!=b.x)
		return a.x<b.x;
	else
		return a.y<b.y;
}

int n,m,p;

int dp[100010],num[100010],b[100010];

void solve()
{
	int i,MAX,le,ri,mid,ans,temp;
	b[1]=num[1];
	MAX=1;
	dp[1]=1;
	ans=0;
	for(i=2;i<=p;i++)
	{
		if(num[i]<b[MAX])
		{
			MAX++;
			b[MAX]=num[i];
			dp[i]=MAX;
		}
		else
		{
			le=1;
			ri=MAX;
			while(le<=ri)
			{
				mid=(le+ri)>>1;
				if(b[mid]<=num[i])
				{
					temp=mid;
					ri=mid-1;
				}
				else
				{
					le=mid+1;
				}
			}
			b[temp]=num[i];
			dp[i]=temp;
		}
		ans=max(ans,dp[i]);
	}
	printf("%d\n",ans);
}

int main()
{
//	freopen("in.txt","r",stdin);
	int i;
	while(scanf("%d%d%d",&n,&m,&p)!=EOF)
	{
		for(i=1;i<=p;i++)
		{
			scanf("%d%d",&po[i].x,&po[i].y);
		}
		sort(po+1,po+p+1,cmp);
		for(i=1;i<=p;i++)
		{
			num[i]=po[i].y;
		}
		solve();
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

林下的码路

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值