跳到正文

题解

题解:P12914 [POI 2020/2021 R2] 沙滩游客 / Plażowicze

xlhl约 3 分钟
原文
文章信息与版本

文章信息

发布
2025年11月10日
收录
2025年12月30日
更新
2026年11月15日
篇幅
约 3 分钟
题目
P12914

版本

  1. v2当前
  2. v1

首先考虑暴力怎么打,对于每次询问 kk,用优先队列找出当前最大的间距,把间距从中间分裂,如此重复 kk 次就能找到最终位置,单次时间复杂度 O(n+klog⁡n)O(n + k \log n)。

进一步优化,kk 可能很大,最初每段间距分裂成了很多小区间,大概长这样:

每一层变到下一层时,长度减半,段数翻倍,而每一层中对应的会是连续的一段操作,所以用优先队列维护当前间隔长度和对应段数,取出长度最长的段进行变化。因为每次段数都会翻倍,这样单次询问时间变为 O(n+log⁡nlog⁡k)O(n + \log n \log k),有多次询问所以还是无法通过。

把所有询问按 kk 从小到大排序,进行分裂时看这次能求出哪些 kk 的答案,总时间复杂度为 O(n+zlog⁡nlog⁡k)O(n + z \log n \log k),可以通过。

C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=1e6+10;
int n,X,z,x[N];
struct query{int w,id;}k[N];
bool cmp(query x,query y){return x.w<y.w;}
struct frac{int a,b;}ans[N];

inline bool operator <(const frac x,const frac y){
	return x.a*y.b<x.b*y.a;}
inline bool operator ==(const frac x,const frac y){
	return x.a*y.b==x.b*y.a;}
inline frac operator *(const frac x,const frac y){
	int t=__gcd(x.a*y.a,x.b*y.b);
	return (frac){x.a*y.a/t,x.b*y.b/t};
}
inline frac operator +(const int x,const frac y){
	int t=__gcd(y.a+x*y.b,y.b);
	return (frac){(y.a+x*y.b)/t,y.b};
}

struct node{
	frac len;
	int gs,id;
	friend bool operator <(const node x,const node y){
		return x.len<y.len||(x.len==y.len&&x.id>y.id);
	}
};
priority_queue<node>q;
signed main(){
	cin>>n>>X>>z;
	for(int i=1;i<=n;i++)
		scanf("%lld",&x[i]);
	for(int i=1;i<=z;i++)
		scanf("%lld",&k[i].w),k[i].id=i;
	sort(k+1,k+1+z,cmp);
	for(int i=2;i<=n;i++)
		q.push({(frac){x[i]-x[i-1],1},1,i});
	int p=1,res=0;
	while(p<=z){
		node a=q.top();
		frac tmp=a.len;
		q.pop();
		while(p<=z&&res+a.gs>=k[p].w)
			ans[k[p].id]=x[a.id-1]+(frac){2*(k[p].w-res)-1,2}*a.len,p++;
		res+=a.gs,q.push({(frac){tmp.a,tmp.b*2},a.gs*2,a.id});
	}
	for(int i=1;i<=z;i++)
	    printf("%lld/%lld\n",ans[i].a,ans[i].b);
	return 0;
}
原文