【dp+二分】HDU 1025

33 篇文章 0 订阅
18 篇文章 0 订阅

题目链接: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);
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值