【dp+二分】HDU 1025

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1025

题目大意:现在有两条平行线,给出两条线上的点的若干个匹配关系,求最多有多少对使得匹配连线不相交

思路:非常明显,这个题可以转化成按照一个量排序,求另一个量的最长上升子序列,但是这个题的范围很大,N^2要TLE……所以得用n上升子序列logn的办法求最长 



代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <vector>
#include <queue>
using namespace std;
const int N = 500010;
int a[N];
int f[N];
int n;
int main()
{
    int cas = 0;
    while(scanf("%d", &n) != EOF)
    {
        for(int i = 1; i <= n; i++)
        {
            int x, y;
            scanf("%d%d", &x, &y);
            a[x] = y;
        }
        f[1] = a[1];
        int tot = 1;
        for(int i = 2; i <= n; i++)
        {
            int l = 1, r = tot;
            while(l <= r)
            {
                int mid = (l + r) / 2;
                if(f[mid] > a[i]) r = mid - 1;
                else l = mid + 1;
            }
            f[l] = a[i];
            if(l > tot) tot++;
        }
        if(tot == 1)
            printf("Case %d:\nMy king, at most %d road can be built.\n\n", ++cas, tot);
        else printf("Case %d:\nMy king, at most %d roads can be built.\n\n",++cas, tot);
    }

}

全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务