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

来源:互联网 发布:起点数据库 编辑:程序博客网 时间:2024/05/22 15:07


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-6using 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;elsereturn 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;}