再出一道升级版的题目!
查看原帖
再出一道升级版的题目!
1381257
zhengyi0402楼主2025/2/6 10:36

如果出一个这题的升级版的话,可以把数据加强至 n5×106n \le 5 \times 10^6

应为排列的关系,所以可以通过重排列法将时间复杂度压缩至 O(nlogn)O(n \log n)

O(nlogn)Θ(3.3×107)O(n \log n) \approx \Theta(3.3 \times 10^7),可以通过 1 秒的时间限;

但是 O(nlog2n)Θ(2.0×108)O(n \log_2 n) \approx \Theta(2.0 \times 10^8),再加上输入,输出波动,很难通过 1 秒。

所以我想,可以再出一道升级版的题目。

附上 O(nlogn)O(n \log n) 的代码:

#include<bits/stdc++.h>
#define int long long
#define inf 0x3f3f3f3f3f3f3f3fll
#define atn 222222
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
inline int read(){
	int x=0,f=1;char ch=getchar();
	while (ch<'0'||ch>'9'){if (ch=='-') f=-1;ch=getchar();}
	while (ch>='0'&&ch<='9'){x=x*10+ch-48;ch=getchar();}
	return x*f;
}
inline void out(int x){
	if(x<0)putchar('-'),x=-x;
	if(x>9)out(x/10);
	putchar((x%10)|0x30);
}int a[111111],b[111111],g[111111],m[111111],ans = 0; 
signed main(){
	ios_base::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	int n = read();
	for(int i = 1;i <= n;++i)a[i]=read(),m[a[i]]=i;
	for(int i = 1;i <= n;++i){b[i]=read();int tt = m[b[i]];b[i]=tt;}
	for(int i = 1;i <= n;++i){
		int pos=lower_bound(g+1,g+ans+1,b[i])-g;
		g[pos]=b[i];
		ans=max(ans,pos);
	}out(ans);
	return 0;
	//十年OI一场空,define int 见祖宗。
	//十年OI一场空,不开long long见祖宗。
}
2025/2/6 10:36
加载中...