HDU1025 nlogn的LIS

本文介绍了一种针对最长递增子序列(LIS)问题的高效解决方案,通过优化传统DP算法,采用一种新颖的方法实现O(log n)的时间复杂度。文章提供了一个具体的编程实例,详细解释了如何使用维护最小值的DP数组结合二分查找技术来解决大规模数据集问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Problem Description

JGShining’s kingdom consists of 2n(n is no more than 500,000) small cities which are located in two parallel lines.

Half of these cities are rich in resource (we call them rich cities) while the others are short of resource (we call them poor cities). Each poor city is short of exactly one kind of resource and also each rich city is rich in exactly one kind of resource. You may assume no two poor cities are short of one same kind of resource and no two rich cities are rich in one same kind of resource.

With the development of industry, poor cities wanna import resource from rich ones. The roads existed are so small that they’re unable to ensure the heavy trucks, so new roads should be built. The poor cities strongly BS each other, so are the rich ones. Poor cities don’t wanna build a road with other poor ones, and rich ones also can’t abide sharing an end of road with other rich ones. Because of economic benefit, any rich city will be willing to export resource to any poor one.

Rich citis marked from 1 to n are located in Line I and poor ones marked from 1 to n are located in Line II.

The location of Rich City 1 is on the left of all other cities, Rich City 2 is on the left of all other cities excluding Rich City 1, Rich City 3 is on the right of Rich City 1 and Rich City 2 but on the left of all other cities … And so as the poor ones.

But as you know, two crossed roads may cause a lot of traffic accident so JGShining has established a law to forbid constructing crossed roads.

For example, the roads in Figure I are forbidden.

In order to build as many roads as possible, the young and handsome king of the kingdom - JGShining needs your help, please help him. ^_^

Input

Each test case will begin with a line containing an integer n(1 ≤ n ≤ 500,000). Then n lines follow. Each line contains two integers p and r which represents that Poor City p needs to import resources from Rich City r. Process to the end of file.

Output

For each test case, output the result in the form of sample.
You should tell JGShining what’s the maximal number of road(s) can be built.

Sample Input

2
1 2
2 1
3
1 2
2 3
3 1

Sample Output

Case 1:
My king, at most 1 road can be built.

Case 2:
My king, at most 2 roads can be built.

这题就是升级版lis
按照之前的思路,是肯定超时的因为有50万的数据量
普通的n^2LIS的思路是开一个五十万大小的DP数组
dp[500001][2】这样的数组,第一个用来记录当前最小值,第二个用来记录当前的长度,当遍历的目前值两个都比过去的大的时候就进行更新…..
以前觉得这个LIS真的挺好的,现在看看真的蠢

看了别人的博客以后发现了新的方法
那就是维护一个dp【500001】一个就够了,地址用来存len的长度,里边的值是对应长度里最小的那个….
因为长度这个东西是一定连续的所以不存在浪费内存这一回事…遍历起来也方便
用了这个以后,就能发现这个DP数组里边存的数是单调递增的这也是可以证明的

来我们非形式化的证明一下子
这个循环不变式的初始化是由一个空的都是0的数组组成
然后迭代一次第一个数一定比0大也就是比dp【0】大,那么就一定会自动更新dp【1】;
后边的多次迭代只会把每一个值弄得比自己更小,但是后一个也一定比前一个的更新前的值更大,因此在循环不变式结束的时候这个数组一定也是单调递增的

因此这时候直接上二分法就是logn辣~~~~

这个二分写的我感觉也有些别扭,因为他不是从里边找一个数而是找到一个小区间…

然后我就写的非常奇怪…
不过以后可以记得找区间和找本数是一样的….
最后头=尾的地方一定就是要更新的值,头比整个序列还大的时候就说明没有比他大的了….
直接开新的位置就可以了。没必要拿出来单独写

#include<iostream>
#include<algorithm>
#include<string>
#include<cstdlib>
using namespace std;
int zhi[500001];
int dp[500001];
int len;
int main()
{
    int n;
    int ji = 0;
    while (cin >> n)
    {
        memset(dp, 0, sizeof(dp));
        len = 0;
        int y,o;
        for (int a = 1;a <= n;a++)
        {
            scanf("%d%d", &y,&o);
            zhi[y] = o;
        }
        for (int a = 1;a <= n;a++)
        {
            if (zhi[a] > dp[len])
            {
                len++;
                dp[len] = zhi[a];
            }
            else
            {
                int tou=1, wei=len;
                int w = 0;
                while (tou<=wei&&w==0)
                {
                    int mid = (tou + wei) / 2;
                    if (zhi[a] > dp[mid])tou = mid + 1;
                    else
                    {
                        if (zhi[a] > dp[mid - 1])
                        {
                            dp[mid] = zhi[a];
                            w = 1;
                        }
                        else wei = mid - 1;
                    }
                }
            }
        }
        if(len==1)printf("Case %d:\nMy king, at most 1 road can be built.\n\n", ++ji);
        else printf("Case %d:\nMy king, at most %d roads can be built.\n\n", ++ji,len);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值