25/1/18 模拟赛做题记录

B

题意

给定一个序列 a1n,你需要回答 m 个询问,每个询问给定 l,r,你需要回答满足 li<jr,ai=aj 的最小的 ji,即区间内最近的两个相同数的距离,若不存在,输出 -1

思路

我们设 bi 表示与位置 i 的元素权值相等且在 i 之后的最小位置。

因为答案要求 [l,r] 之间的最小距离,所以相当于求 minlir{bi|(i+bir)}。我们发现 (i+bir) 并不好控制,而 lir 却是很简单的,所以不妨离线询问,并将其按照右端点排序。之后遍历他们的所有询问,每一次询问前先将 i+bir 的所有数加入线段树的位置 i,每一次求 [l,r] 的最小值。

时间复杂度 O(nlogn)

点击查看代码
#include<bits/stdc++.h>

#define int ll
#define ll long long
#define i128 __int128

#define mem(a,b) memset((a),(b),sizeof(a))
#define m0(a) memset((a),0,sizeof(a))
#define m1(a) memset(a,-1,sizeof(a))
#define lb(x) ((x)&-(x))
#define lc(x) ((x)<<1)
#define rc(x) (((x)<<1)|1)
#define pb(G,x) (G).push_back((x))
#define For(a,b,c) for(int a=(b);a<=(c);a++)
#define Rep(a,b,c) for(int a=(b);a>=(c);a--)
#define in1(a) a=read()
#define in2(a,b) a=read(), b=read()
#define in3(a,b,c) a=read(), b=read(), c=read()
#define fst first 
#define scd second 
#define dbg puts("IAKIOI")

using namespace std;

int read() {
	int x=0,f=1; char c=getchar();
	for(;c<'0'||c>'9';c=getchar()) f=(c=='-'?-1:1); 
	for(;c<='9'&&c>='0';c=getchar()) x=(x<<1)+(x<<3)+(c^48);
	return x*f;
}
void write(int x) { if(x>=10) write(x/10); putchar('0'+x%10); }

const int mod = 998244353;
int qpo(int a,int b) {int res=1; for(;b;b>>=1,a=(a*a)%mod) if(b&1) res=res*a%mod; return res; }
int inv(int a) {return qpo(a,mod-2); }

#define maxn 1000050

int n,m;
int a[maxn];
map<int,int> mp;

struct node{
	int idx,l,r;
	bool operator<(const node&x) const {
		return r<x.r;
	}
}b[maxn],qu[maxn];

struct SegT {
	int tr[maxn<<2];
	void pre() {For(i,0,(maxn<<2)-1) tr[i]=1e18; }
	void pu(int idx) {tr[idx]=min(tr[lc(idx)],tr[rc(idx)]);}
	void add(int idx,int l,int r,int k,int val) {
		if(l==r) return tr[idx]=val,void();
		int mid=l+r>>1;
		if(k<=mid) add(lc(idx),l,mid,k,val);
		else add(rc(idx),mid+1,r,k,val);
		pu(idx);
	}
	int query(int idx,int l,int r,int L,int R) {
		int res=1e18;
		if(L<=l&&r<=R) return tr[idx];
		int mid=l+r>>1;
		if(L<=mid) res=min(res,query(lc(idx),l,mid,L,R));
		if(R>mid) res=min(res,query(rc(idx),mid+1,r,L,R));
		return res;
	}
}Tr;

int ans[maxn];

void work() {
	in2(n,m);
	For(i,1,n) {
		in1(a[i]);
		if(mp[a[i]]) {
			b[mp[a[i]]]={mp[a[i]],mp[a[i]],i};
		}
		mp[a[i]]=i;
	}
	For(i,1,n) if(!b[i].idx) {
		b[i].idx=i,b[i].r=1e18;
	}
	For(i,1,m) {
		in2(qu[i].l,qu[i].r);
		qu[i].idx=i;
	}
	sort(qu+1,qu+m+1);
	sort(b+1,b+n+1);
	int idx=0;
	Tr.pre();
	For(i,1,m) {
		while(idx+1<=n&&b[idx+1].r<=qu[i].r) {
			idx++; Tr.add(1,1,n,b[idx].idx,b[idx].r-b[idx].l);
		}
		ans[qu[i].idx]=Tr.query(1,1,n,qu[i].l,qu[i].r);
		if(ans[qu[i].idx]>=1e18) ans[qu[i].idx]=-1;
	}
	For(i,1,m) cout<<ans[i]<<'\n';
}

signed main() {
//	ios::sync_with_stdio(false); 
//	cin.tie(0); cout.tie(0);
	int _=1;
//	_=read();
//	cin>>_;
	For(i,1,_) {
		work();
	}
	return 0;
}

C

题意

原题链接:CF1508B

如果有一个 1n 的排列 p1n 满足对于所有 1i<n 都有 pi1pi+1,则我们称排列 p 是「几乎有序」的。
给定 n,k,你需要求出字典序第 k 小的「几乎有序」的长度为 n 的排列或报告其不存在。

思路

首先,题目给出的 ai1ai+1,这意味着如果排列中有下降的子序列,一定形如:{a,a1,a2,,ax,b,b1,b2,}
所以题目变成给序列化分成若干段,每一段反转后得到的第 k 小,总共只有 2n1 种情况。
首先,我们知道最小肯定是 {1,2,3,,n}。我们发现每一次反转越靠后,改变的字典序就越大。所以我们不难发现,当一个间隙切割时为 0,不切割时为 1,那么从左往右的 01 串组成的二进制数 v,构造出的序列的字典序大小是 v+1
另:因为 2621018,所以只要 n62 就一定有解。

点击查看代码
#include<bits/stdc++.h>
#define int ll
#define ll long long
#define i128 __int128

#define mem(a,b) memset((a),(b),sizeof(a))
#define m0(a) memset((a),0,sizeof(a))
#define m1(a) memset(a,-1,sizeof(a))
#define lb(x) ((x)&-(x))
#define lc(x) ((x)<<1)
#define rc(x) (((x)<<1)|1)
#define pb(G,x) (G).push_back((x))
#define For(a,b,c) for(int a=(b);a<=(c);a++)
#define Rep(a,b,c) for(int a=(b);a>=(c);a--)
#define in1(a) a=read()
#define in2(a,b) a=read(), b=read()
#define in3(a,b,c) a=read(), b=read(), c=read()
#define fst first 
#define scd second 
#define dbg puts("IAKIOI")

using namespace std;

int read() {
	int x=0,f=1; char c=getchar();
	for(;c<'0'||c>'9';c=getchar()) f=(c=='-'?-1:1); 
	for(;c<='9'&&c>='0';c=getchar()) x=(x<<1)+(x<<3)+(c^48);
	return x*f;
}
void write(int x) { if(x>=10) write(x/10); putchar('0'+x%10); }

const int mod = 998244353;
int qpo(int a,int b) {int res=1; for(;b;b>>=1,a=(a*a)%mod) if(b&1) res=res*a%mod; return res; }
int inv(int a) {return qpo(a,mod-2); }

#define maxn 200050

int n,k;

void work() {
	in2(n,k); k--;
	if(n<=62&&(k>>(n-1))) return puts("-1"),void();
	for(int l=1,r=1;l<=n;l=r+1,r++) {
		while(r<n&&n-r<=62&&((k>>(n-1-r))&1)) r++;//n-r<62 是因为最后一个 0 的位置和位置 n 的距离不会超过 62,因为 2^62>10^18
		Rep(j,r,l) cout<<j<<' ';
	}
	puts("");
}

signed main() {
//	ios::sync_with_stdio(false); 
//	cin.tie(0); cout.tie(0);
	int _=1;
//	_=read();
//	cin>>_;
	For(i,1,_) {
		work();
	}
	return 0;
}

D

题意

给定序列 a1n 以及 m 个询问,每次询问给定 l,r,p,求: minlLRr(i=LRai)modp 即询问区间 [l,r] 的所有子区间和在模意义下的最小值,注意先取模再求最小值。

思路

我们设 sumi=i=li。首先对于每一次询问,如果 lr+1>p,那么根据鸽巢原理,一定有两个相同的数。否则我们遍历区间,设当前遍历到 i,我们要找到最大的小于等于 sumisumj(ji)。用权值树状数组维护前缀最大值即可。时间复杂度看上去是 O(nmlogp),实际上是 O(mplogp),原因是每一次遍历的长度大小不超过 p

点击查看代码
#include<bits/stdc++.h>
#define int ll
#define ll long long
#define i128 __int128

#define mem(a,b) memset((a),(b),sizeof(a))
#define m0(a) memset((a),0,sizeof(a))
#define m1(a) memset(a,-1,sizeof(a))
#define lb(x) ((x)&-(x))
#define lc(x) ((x)<<1)
#define rc(x) (((x)<<1)|1)
#define pb(G,x) (G).push_back((x))
#define For(a,b,c) for(int a=(b);a<=(c);a++)
#define Rep(a,b,c) for(int a=(b);a>=(c);a--)
#define in1(a) a=read()
#define in2(a,b) a=read(), b=read()
#define in3(a,b,c) a=read(), b=read(), c=read()
#define fst first 
#define scd second 
#define dbg puts("IAKIOI")

using namespace std;

int read() {
	int x=0,f=1; char c=getchar();
	for(;c<'0'||c>'9';c=getchar()) f=(c=='-'?-1:1); 
	for(;c<='9'&&c>='0';c=getchar()) x=(x<<1)+(x<<3)+(c^48);
	return x*f;
}
void write(int x) { if(x>=10) write(x/10); putchar('0'+x%10); }

const int mod = 998244353;
int qpo(int a,int b) {int res=1; for(;b;b>>=1,a=(a*a)%mod) if(b&1) res=res*a%mod; return res; }
int inv(int a) {return qpo(a,mod-2); }

#define maxn 500050

struct Bit {
	int c[550];
	void add(int x,int val) {
		for(;x<550;x+=lb(x)) c[x]=max(c[x],val);
	}
	int query(int x) {
		int res=0;
		for(;x;x-=lb(x)) res=max(res,c[x]);
		return res;
	}
}Tr;

int n,m;
int a[maxn],sum[maxn];

void work() {
	in2(n,m);
	For(i,1,n) in1(a[i]),sum[i]=sum[i-1]+a[i];
	For(i,1,m) {
		int l=read(),r=read(),p=read();
		if(l-r+1>p) { cout<<0<<'\n'; continue; }
		For(i,0,p) Tr.c[i]=0;
		int ans=1e18;
		For(j,l,r) {
			int res=(sum[j]-sum[l-1])%p;
			ans=min(ans,res); 
			ans=min(ans,res-Tr.query(res));
			if(ans==0) break;
			Tr.add(res,res);
		}
		cout<<ans<<'\n';
	}
}

signed main() {
//	ios::sync_with_stdio(false); 
//	cin.tie(0); cout.tie(0);
	int _=1;
//	_=read();
//	cin>>_;
	For(i,1,_) {
		work();
	}
	return 0;
}
posted @   coding_goat_qwq  阅读(3)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 如何调用 DeepSeek 的自然语言处理 API 接口并集成到在线客服系统
· 【译】Visual Studio 中新的强大生产力特性
· 2025年我用 Compose 写了一个 Todo App
点击右上角即可分享
微信分享提示