1012. 友好城市

Palmia国有一条横贯东西的大河,河有笔直的南北两岸,岸上各有位置各不相同的N个城市。

北岸的每个城市有且仅有一个友好城市在南岸,而且不同城市的友好城市不相同。

每对友好城市都向政府申请在河上开辟一条直线航道连接两个城市,但是由于河上雾太大,政府决定避免任意两条航道交叉,以避免事故。

编程帮助政府做出一些批准和拒绝申请的决定,使得在保证任意两条航线不相交的情况下,被批准的申请尽量多。

输入格式

第1行,一个整数N,表示城市数。

第2行到第n+1行,每行两个整数,中间用1个空格隔开,分别表示南岸和北岸的一对友好城市的坐标。

输出格式

仅一行,输出一个整数,表示政府所能批准的最多申请数。

数据范围

1N50001≤N≤5000,
0xi100000≤xi≤10000

输入样例:

7
22 4
2 6
10 3
15 12
9 8
17 17
4 2

输出样例:

4
分析:
先对一边进行排序,在对另一边求单调上升子序列的最大值,(一边单调上升对应可满足城市之间不交叉)

普通解法:
#include<iostream>
#include<algorithm>

using namespace std;

const int N = 5050;
typedef pair<int,int> PII;
int n;
PII q[N];
int dp[N];


int main(){
    scanf("%d",&n);
    for(int i = 1;i <= n;i++) scanf("%d%d",&q[i].first,&q[i].second);
    
    sort(q+1 ,q+n+1);
    int res = 0;
    for(int i =1;i <= n;i++){
        dp[i] = 1;
        for(int j = 1;j <= i;j++){
            if(q[i].second> q[j].second)
                dp[i] = max(dp[i],dp[j] + 1);
        }
        res = max(res,dp[i]);
    }
    cout << res;
}

花式解法:
 1 #include<iostream>
 2 #include<algorithm>
 3 
 4 using namespace std;
 5 
 6 const int N = 5050;
 7 typedef pair<int,int> PII;
 8 int n;
 9 PII q[N];
10 int dp[N];
11 
12 bool comp(pair<int,int>a,pair<int,int>b){
13     return a.second < b.second;
14 }
15 
16 int main(){
17     scanf("%d",&n);
18     for(int i = 1;i <= n;i++) scanf("%d%d",&q[i].first,&q[i].second);
19     
20     sort(q+1 ,q+n+1,comp);
21     int res = 0;
22     for(int i =1;i <= n;i++){
23         dp[i] = 1;
24         for(int j = 1;j <= i;j++){
25             if(q[i].first > q[j].first)
26                 dp[i] = max(dp[i],dp[j] + 1);
27         }
28         res = max(res,dp[i]);
29     }
30     cout << res;
31 }

猜你喜欢

转载自www.cnblogs.com/luyuan-chen/p/11617286.html