25/1/18 模拟赛做题记录
B
题意
给定一个序列
,你需要回答 个询问,每个询问给定 ,你需要回答满足 的最小的 ,即区间内最近的两个相同数的距离,若不存在,输出 -1
。
思路
我们设
因为答案要求
时间复杂度
点击查看代码
#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
如果有一个
到 的排列 满足对于所有 都有 ,则我们称排列 是「几乎有序」的。
给定,你需要求出字典序第 小的「几乎有序」的长度为 的排列或报告其不存在。
思路
首先,题目给出的
所以题目变成给序列化分成若干段,每一段反转后得到的第
首先,我们知道最小肯定是
另:因为
点击查看代码
#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
题意
给定序列
以及 个询问,每次询问给定 ,求: 即询问区间 的所有子区间和在模意义下的最小值,注意先取模再求最小值。
思路
我们设
点击查看代码
#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;
}
本文来自博客园,作者:coding_goat_qwq,转载请注明原文链接:https://www.cnblogs.com/CodingGoat/p/18679566
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 如何调用 DeepSeek 的自然语言处理 API 接口并集成到在线客服系统
· 【译】Visual Studio 中新的强大生产力特性
· 2025年我用 Compose 写了一个 Todo App