Constructing Roads In JGShining's Kingdom HDU - 1025(二分优化)

本文介绍了一个基于二分查找优化的最长上升子序列算法,用于解决资源丰富城市与资源匮乏城市之间的资源配对问题,确保道路不交叉且实现最大数量的配对。

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.

Hint
Huge input, scanf is recommended.

题意

在富裕城市与贫困城市之间修建道路,不能交叉且一对一,最多能修建几条。

分析

因为保证了一条线上的城市有序,所以求另一条线上最长上升子序列即可,直接求解n*n肯定会tle,所以要用到二分来优化。
二分查找:每次与中间的值比较,如果小了,就取上区间,大了就取下区间。

#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;

#define maxn 500500
int a[maxn]; 
int dp[maxn];
int find(int x,int low,int high)
{
	while(low<=high)
	{
		int mid=(high+low)>>1;
		if(dp[mid]<a[x])
		{
			low=mid+1;
		}
		else high=mid-1;
	}
	return low;
}
int main()
{
	int n,cnt=0;
	while(~scanf("%d",&n))
	{
		for(int i=1;i<=n;i++)
		{
			int u,v;
			scanf("%d%d",&u,&v);
			a[u]=v;
		}
		memset(dp,0,sizeof(dp));
		int len=0;//记录最长结果
		for(int i=1;i<=n;i++){
			if(a[i]>dp[len]){
				dp[++len]=a[i];
			}
			else {
				dp[find(i,1,len)]=a[i];//每次遇到一个小于此值的就在前面找一个将其替换,以此来满足最长,因为相对大小不影响结果
			}
		}
		if(len==1)
		printf("Case %d:\nMy king, at most %d road can be built.\n",++cnt,len);
		else printf("Case %d:\nMy king, at most %d roads can be built.\n",++cnt,len);

	}
	return 0;
}
<think>嗯,用户遇到了一个Java异常,具体是ClassNotFoundException,找不到com.intellij.codeInsight.editorActions.FoldingData这个类。这个问题发生在构造DataFlavor的时候,类型是application/x-java-jvm-local-objectref。用户可能是在使用IntelliJ IDEA或者基于IntelliJ平台开发的工具时遇到了这个问题。我需要先理解这个异常的原因,然后给出解决方案。 首先,ClassNotFoundException意味着JVM在运行时找不到指定的类。这里提到的FoldingData类属于IntelliJ的codeInsight模块,通常用于代码折叠相关的功能。可能的情况包括: 1. **版本不兼容**:用户使用的IDE版本和某个插件或项目依赖的IntelliJ API版本不一致。比如插件是针对较新版本的IDE开发的,而用户使用的是旧版本,导致缺少某些类。 2. **插件冲突或损坏**:安装的插件可能损坏,或者多个插件之间存在冲突,导致无法正确加载所需的类。 3. **缓存问题**:IDE的缓存可能损坏,导致类加载器找不到正确的资源。 4. **项目配置错误**:项目可能错误地配置了IntelliJ的依赖库,比如在Gradle或Maven项目中引用了错误的SDK版本。 接下来需要考虑解决方法。通常处理ClassNotFound的方法包括清理缓存、更新软件、检查插件和验证项目配置。针对IntelliJ的问题,常见的解决步骤是: 1. **清理缓存并重启**:通过File -> Invalidate Caches来清理缓存,这可以解决很多类加载问题。 2. **检查IDE和插件更新**:确保IDE和所有插件都是最新版本,避免版本不兼容。 3. **禁用或重新安装插件**:特别是最近安装的插件,可能有冲突或损坏。 4. **检查项目依赖**:如果是自定义开发插件或项目,确认依赖的IntelliJ SDK版本是否正确。 另外,用户的问题中多次出现相同的异常,可能是在某个操作中反复触发,比如复制粘贴或代码折叠功能的使用。需要进一步询问用户出现异常的具体操作场景,或者直接建议通用解决方法。 最后,需要用中文清晰解释原因和步骤,确保用户能理解并逐步操作解决问题。</think>### 问题原因分析 你遇到的 `ClassNotFoundException: com.intellij.codeInsight.editorActions.FoldingData` 错误,是 **IntelliJ IDEA(或基于其平台的IDE)运行时因类加载失败** 导致的。具体原因可能包括以下情况: --- ### 1. **IDE版本与插件/项目配置不兼容** - **核心问题**:`FoldingData` 是 IntelliJ 平台内部用于处理代码折叠的类。如果: - 你使用的 IDE 版本 **较旧**,而插件或项目依赖了 **新版本** 的 API。 - 你使用的插件 **未适配当前 IDE 版本**,或项目配置错误地引用了 IntelliJ 内部类。 - **触发场景**:尝试复制代码片段、使用代码折叠功能,或插件试图通过剪贴板传递 `FoldingData` 对象时。 --- ### 2. **插件损坏或冲突** - 某些插件(如代码格式化、剪贴板管理插件)可能 **错误地依赖 IntelliJ 内部类**,或插件文件损坏导致加载失败。 --- ### 3. **IDE缓存损坏** - IDE 的缓存文件可能记录了旧版本类的引用路径,与新版本冲突或数据损坏。 --- ### 解决步骤 #### 第一步:清理缓存并重启 1. 点击菜单栏:`File -> Invalidate Caches...` 2. 选择 **Invalidate and Restart**(清理并重启)。 3. 观察问题是否解决。 --- #### 第二步:更新IDE和插件 1. 检查 IDE 是否为最新版本:`Help -> Check for Updates`。 2. 更新所有已安装的插件:`Settings -> Plugins -> Installed`,禁用可疑插件(尤其是最近安装的)。 --- #### 第三步:修复项目配置(开发场景) 如果问题出现在 **自行开发的插件或项目** 中: 1. 检查 `build.gradle` 或 `pom.xml` 中的 IntelliJ 依赖版本: ```gradle // Gradle 示例:确保版本与当前 IDE 一致 intellij { version '2023.1.5' // 修改为你的 IDE 版本 } ``` 2. 避免直接引用 `com.intellij.codeInsight.editorActions` 等内部包(非公开API)。 --- #### 第四步:重置IDE配置(终极方案) 如果上述方法无效: 1. 备份设置:`File -> Manage IDE Settings -> Export Settings`。 2. 重置 IDE:删除配置目录([官方文档](https://www.jetbrains.com/help/idea/tuning-the-ide.html#config-directory) 中查找路径)。 --- ### 附加说明 - **DataFlavor 与剪贴板**:异常中提到的 `DataFlavor` 是 Java 剪贴板数据传输的标识。此错误可能发生在尝试通过剪贴板传递 IDE 内部对象时,但目标环境缺少相关类支持。 - **内部类风险**:`com.intellij` 开头的包多为 IntelliJ 内部实现,第三方插件应使用[官方公开API](https://plugins.jetbrains.com/docs/intellij/api-changes-list.html)。 若问题持续,建议提供以下信息进一步排查: 1. IDE 具体版本(如 IntelliJ 2023.1.5) 2. 复现操作步骤(如复制代码、触发代码折叠) 3. 最近安装的插件列表。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值