比赛 EYOI与SBOI开学欢乐赛12th 评测结果 AAAATTTTTT
题目名称 坐标压缩 最终得分 40
用户昵称 yrtiop 运行时间 12.104 s
代码语言 C++ 内存使用 29.77 MiB
提交时间 2022-10-17 21:22:23
显示代码纯文本
#include <bits/stdc++.h>
#define pb emplace_back

const int maxn = 3e5 + 5;
int n,a[maxn],s,c[maxn],cnt,pos[maxn];
std::vector<int> G[maxn];

int ls[maxn << 2],rs[maxn << 2],sum[maxn << 2];

void pushup(int i) {
	sum[i] = std::max(sum[i << 1] , sum[i << 1 | 1]);
	return ;
}

void build(int i,int l,int r) {
	ls[i] = l;
	rs[i] = r;
	sum[i] = 0;
	if(l == r)return ;
	int mid = l + r >> 1;
	build(i << 1 , l , mid);
	build(i << 1 | 1 , mid + 1 , r);
	return ;
}

void modify(int i,int pos,int val) {
	if(ls[i] == rs[i]) {
		sum[i] = val;
		return ;
	}
	int mid = ls[i] + rs[i] >> 1;
	if(pos <= mid)modify(i << 1 , pos , val);
	else modify(i << 1 | 1 , pos , val);
	pushup(i);
	return ;
}

int query(int i,int l,int r) {
	if(ls[i] >= l&&rs[i] <= r)return sum[i];
	if(ls[i] > r||rs[i] < l)return 0;
	int mid = ls[i] + rs[i] >> 1,s = 0;
	if(l <= mid)s = std::max(s , query(i << 1 , l , r));
	if(r > mid)s = std::max(s , query(i << 1 | 1 , l , r));
	return s;
}

bool check(int k) {
	int ans = 0;
	build(1 , 1 , n);
	for(int i = 1;i <= cnt;++ i) {
		for(auto& v : G[i]) {
			int l = std::max(v - k , 1),r = std::min(v + k , n);
			pos[v] = query(1 , l , r) + 1;
			ans += pos[v];
			if(ans > s)return false;
		}
		for(auto& v : G[i]) {
			modify(1 , v , pos[v]);
		}
	}
	return ans <= s;
}

void work() {
	scanf("%d %d",&n,&s);
	cnt = 0;
	for(int i = 1;i <= n;++ i)
		scanf("%d",&a[i]),c[++ cnt] = a[i];
	std::sort(c + 1 , c + 1 + cnt);
	cnt = std::unique(c + 1 , c + 1 + cnt) - c - 1;
	for(int i = 1;i <= n;++ i)
		a[i] = std::lower_bound(c + 1 , c + 1 + cnt , a[i]) - c;
	for(int i = 1;i <= cnt;++ i)
		G[i].clear();
	for(int i = 1;i <= n;++ i)
		G[a[i]].pb(i);
	
	int l = 0,r = n;
	while(l <= r) {
		int mid = l + r >> 1;
		if(check(mid))l = mid + 1;
		else r = mid - 1;
	}
	
	printf("%d\n",r + 1);
	return ;
}

int main() {
	freopen("coord.in","r",stdin);
	freopen("coord.out","w",stdout);
	int T;
	scanf("%d",&T);
	while(T --)work();
	return 0;
}