比赛密码:waccjyt2024
多校A层冲刺NOIP2024模拟赛01
构造字符串
咕咕咕
点击查看代码
#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5+9;
int n,m,die[maxn],ans[maxn];
int x[maxn],y[maxn],z[maxn];
vector<int> vec[maxn];bool vis[maxn];
int findie(int x){return die[x]==x?x:die[x]=findie(die[x]);}
int main(){
freopen("str.in","r",stdin);
freopen("str.out","w",stdout);
scanf("%d%d",&n,&m);
memset(ans,-1,sizeof ans);
for(int i=1;i<=n;i++) die[i]=i;
for(int i=1;i<=m;i++){
scanf("%d%d%d",&x[i],&y[i],&z[i]);
for(int j=0;j<z[i];j++) die[findie(x[i]+j)]=findie(y[i]+j);
}
for(int i=1;i<=m;i++){
int u=findie(x[i]+z[i]),v=findie(y[i]+z[i]);
if(u==v) return puts("-1"),0;
vec[u].push_back(v),vec[v].push_back(u);
}
for(int i=1;i<=n;i++){
if(ans[findie(i)]==-1){
for(int j=0;j<=n;j++) vis[j]=false;
for(auto j:vec[findie(i)]) if(~ans[j]) vis[ans[j]]=true;
for(int j=0;j<=n;j++) if(!vis[j]){ans[findie(i)]=j;break;}
}
printf("%d ",ans[findie(i)]);
}
return 0;
}
寻宝
咕咕咕
点击查看代码
#include<bits/stdc++.h>
using namespace std;
const int maxn=50009;
int n,m,k,q,tot,cnt,vis[32767];
int a[4]={1,-1,0,0};
int b[4]={0,0,-1,1};
map<int,short> mp[maxn];
queue<pair<int,int>> que;
vector<int> vec[32767];
void bfs(int sx,int sy){
mp[sx][sy]=++cnt;
que.push(make_pair(sx,sy));
while(que.size()){
int x=que.front().first;
int y=que.front().second;
que.pop();
for(int i=0;i<4;i++){
int tx=x+a[i],ty=y+b[i];
if(mp[tx][ty]) continue;
mp[tx][ty]=cnt;
que.push(make_pair(tx,ty));
}
}
}
bool dfs(int x,int goal,int rnd){
if(x==goal) return true;
if(!vec[x].size()) return false;
vis[x]=rnd;bool res=false;
for(auto to:vec[x]) if(vis[to]^rnd) res|=dfs(to,goal,rnd);
return res;
}
int main(){
freopen("treasure.in","r",stdin);
freopen("treasure.out","w",stdout);
scanf("%d%d%d%d",&n,&m,&k,&q);
int x1,y1,x2,y2;char c;mp[n+1][m+1]=-1;
for(int i=0;i<=n;i++) mp[i][0]=mp[i][m+1]=-1;
for(int j=0;j<=m;j++) mp[0][j]=mp[n+1][j]=-1;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
scanf("\n%c",&c);
if(c=='#') mp[i][j]=-1;
}
}
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
if(mp[i][j]) continue;
bfs(i,j);
}
}
while(k--){
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
vec[mp[x1][y1]].push_back(mp[x2][y2]);
}
for(int i=1;i<=q;i++){
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
printf("%d\n",dfs(mp[x1][y1],mp[x2][y2],i));
}
return 0;
}
序列
咕咕咕
构树
咕咕咕
多校A层冲刺NOIP2024模拟赛02
法阵
咕咕咕
连通块
咕咕咕
点击查看代码
#include<bits/stdc++.h>
using namespace std;
const int maxn=2e5+9;
int n,m,tot,cnt,dim[maxn][2];
int u[maxn],v[maxn],fa[maxn];
int h[maxn],nxt[maxn<<1],to[maxn<<1];
int data[maxn],die[maxn],dep[maxn],siz[maxn];
int son[maxn],dfn[maxn],rnk[maxn],top[maxn];
bool del[maxn];
struct OP{
int opt,id,ans;
}op[maxn];
void add(int x,int y){
tot++;
nxt[tot]=h[x];
to[tot]=y;
h[x]=tot;
}
void dfs1(int x){
siz[x]=1;
for(int i=h[x];i;i=nxt[i]){
int y=to[i];
if(die[y]||dep[y]) continue;
die[y]=x,dep[y]=dep[x]+1,dfs1(y),siz[x]+=siz[y];
if(!son[x]||siz[y]>siz[son[x]]) son[x]=y;
}
}
void dfs2(int x,int tp){
top[x]=tp,dfn[x]=++cnt,rnk[dfn[x]]=x;
if(!son[x]) return;
dfs2(son[x],tp);
for(int i=h[x];i;i=nxt[i]){
int y=to[i];
if(y^die[x]&&y^son[x]) dfs2(y,y);
}
}
int lca(int x,int y){
while(top[x]^top[y]){
if(dep[top[x]]<dep[top[y]]) swap(x,y);
x=die[top[x]];
}
return dep[x]<dep[y]?x:y;
}
inline int calc(int x,int y){return dep[x]+dep[y]-(dep[lca(x,y)]<<1);}
inline int findie(int x){return x==fa[x]?x:fa[x]=findie(fa[x]);}
void merge(int x,int y){
x=findie(x),y=findie(y);fa[y]=x;
int mx=0,tmp[4]={dim[x][0],dim[x][1],dim[y][0],dim[y][1]};
for(int i=0;i<4;i++){
for(int j=i+1;j<4;j++){
int cp=calc(tmp[i],tmp[j]);
if(cp>mx){
mx=cp;
dim[x][0]=tmp[i];
dim[x][1]=tmp[j];
}
}
}
}
int query(int x){
int y=findie(x);
return max(calc(x,dim[y][0]),calc(x,dim[y][1]));
}
int main(){
freopen("block.in","r",stdin);
freopen("block.out","w",stdout);
scanf("%d%d",&n,&m);
for(int i=1;i<n;i++){
scanf("%d%d",&u[i],&v[i]);
add(u[i],v[i]),add(v[i],u[i]);
}
dep[1]=1,dfs1(1),dfs2(1,1);
for(int i=1;i<=n;i++) dim[i][0]=dim[i][1]=fa[i]=i;
for(int i=1;i<=m;i++){
scanf("%d%d",&op[i].opt,&op[i].id);
if(op[i].opt==1) del[op[i].id]=true;
}
for(int i=1;i<n;i++) if(!del[i]) merge(u[i],v[i]);
for(int i=m;i;i--){
if(op[i].opt==1) merge(u[op[i].id],v[op[i].id]);
else op[i].ans=query(op[i].id);
}
for(int i=1;i<=m;i++) if(op[i].opt==2) printf("%d\n",op[i].ans);
return puts(""),0;
}
军队
咕咕咕
棋盘
咕咕咕
多校A层冲刺NOIP2024模拟赛03
五彩斑斓
咕咕咕
点击查看代码
#include<bits/stdc++.h>
using namespace std;
const int maxn=409;
short n,m,sum[1000009];
int mp[maxn][maxn];
int main(){
freopen("colorful.in","r",stdin);
freopen("colorful.out","w",stdout);
scanf("%hd%hd",&n,&m);
long long ans=1ll*(n*n+n)*(m*m+m)>>2;
for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) scanf("%d",&mp[i][j]);
for(int i=1;i<=n;i++){
for(int j=i;j<=n;j++){
for(int k=1;k<=m;k++){
if(mp[i][k]^mp[j][k]) continue;
ans-=++sum[mp[i][k]];
}
for(int k=1;k<=m;k++) if(mp[i][k]==mp[j][k]) sum[mp[i][k]]=0;
}
}
return printf("%lld\n",ans),0;
}
错峰旅行
咕咕咕
点击查看代码
#include<bits/stdc++.h>
using namespace std;
const int mod=1e9+7;
int n,m,s,t,x,l,r,cnt,ans;
pair<int,int> pr[2000009];
inline int ksm(int a,int b){
int res=1;
for(;b;b>>=1,a=1ll*a*a%mod) if(b&1) res=1ll*res*a%mod;
return res;
}
int main(){
freopen("travel.in","r",stdin);
freopen("travel.out","w",stdout);
scanf("%d%d%d%d",&n,&m,&s,&t);
while(m--){
scanf("%d%d%d",&x,&l,&r);
pr[++cnt]=make_pair(l,-1);
pr[++cnt]=make_pair(r+1,1);
}
pr[++cnt]=make_pair(t+1,0);
sort(pr+1,pr+cnt+1);ans++;
for(int i=1,las=s;i<=cnt;){
while(pr[i].first==las) n+=pr[i++].second;
ans=1ll*ans*ksm(n,pr[i].first-las)%mod;
if(pr[i].first==t+1) break;
las=pr[i].first;
}
return printf("%d\n",ans),0;
}
线段树
咕咕咕
点击查看代码
#include <bits/stdc++.h>
using namespace std;
const int maxn=509;
int n,q,l,r,sum[maxn][maxn],dp[maxn][maxn];
int main(){
freopen("segment.in","r",stdin);
freopen("segment.out","w",stdout);
scanf("%d%d",&n,&q);
memset(dp,0x3f,sizeof dp);
while(q--) scanf("%d%d",&l,&r),sum[l][r]++;
for(int len=n;len;len--){
for(int l=1;l+len-1<=n;l++){
int r=l+len-1;
sum[l][r]+=sum[l-1][r]+sum[l][r+1]-sum[l-1][r+1];
}
}
for(int i=1;i<=n;i++) dp[i][i]=sum[i][i];
for(int len=2;len<=n;len++){
for(int l=1;l+len-1<=n;l++){
int r=l+len-1;
for(int mid=l;mid<r;mid++) dp[l][r]=min(dp[l][r],dp[l][mid]+dp[mid+1][r]);
dp[l][r]-=sum[l][r];
}
}
return printf("%d\n",dp[1][n]),0;
}
量子隧穿问题
咕咕咕
多校A层冲刺NOIP2024模拟赛04
02表示法
咕咕咕
点击查看代码
#include<bits/stdc++.h>
using namespace std;
void change(char *s){
char cp[189];
for(int i=1,las=0;s[i];i++){
int now=s[i]-'0'+las;
las=now%2?10:0;s[i]=0;
cp[i]=(now>>1)+'0';
cp[i+1]=0;
}
int now=1,len=0;
if(cp[now+1]&&cp[now]=='0') now++;
for(int i=now;cp[i];i++) s[++len]=cp[i];
}
bool judge(char *s){
int end=strlen(s+1);
return s[end]=='1'||s[end]=='3'||s[end]=='5'||s[end]=='7'||s[end]=='9';
}
void solve(int n){
if(!n) return putchar('0'),void(0);
stack<int> st;int cnt=0;
while(n){
if(n&1) st.push(cnt);
n>>=1,cnt++;
}
while(st.size()){
putchar('2');
if(st.top()^1){
putchar('(');
solve(st.top());
putchar(')');
}
st.pop();
if(st.size()) putchar('+');
}
}
int main(){
freopen("pow.in","r",stdin);
freopen("pow.out","w",stdout);
char s[189];scanf("%s",s+1);
if(strlen(s+1)==1&&s[1]=='0') return puts("0"),0;
stack<int> st;int cnt=0;
while(s[1]^'0'||s[2]){
if(judge(s)) st.push(cnt);
change(s),cnt++;
}
while(st.size()){
putchar('2');
if(st.top()^1){
putchar('(');
solve(st.top());
putchar(')');
}
st.pop();
if(st.size()) putchar('+');
}
return 0;
}
子串的子串
咕咕咕
点击查看代码
#include <bits/stdc++.h>
using namespace std;
using ull=unsigned long long;
const int maxn=3009;
const int base=233333;
int n,q,ans[maxn][maxn];
ull ha[maxn],mi[maxn];char s[maxn];
unordered_map<ull,int> mp;
inline ull H(int l,int r){return ha[r]-ha[l-1]*mi[r-l+1];}
int main(){
freopen("substring.in","r",stdin);
freopen("substring.out","w",stdout);
scanf("%d%d%s",&n,&q,s+1);mi[0]=1;
for(int i=1;i<=n;i++) ha[i]=ha[i-1]*base+s[i],mi[i]=mi[i-1]*base;
for(int len=1;len<=n;len++){
mp.clear();
for(int l=1;l+len-1<=n;l++){
int r=l+len-1;
ull tmp=H(l,r);
ans[mp[tmp]][r]--;
mp[tmp]=l;
}
}
for(int len=1;len<=n;len++){
for(int l=1;l+len-1<=n;l++){
int r=l+len-1;
ans[l][r]+=ans[l+1][r]+ans[l][r-1]-ans[l+1][r-1]+1;
}
}
for(int i=1,l,r;i<=q;i++){
scanf("%d%d",&l,&r);
printf("%d\n",ans[l][r]);
}
return 0;
}
魔法咒语
咕咕咕
点击查看代码
#include <bits/stdc++.h>
using namespace std;
const int maxn=10009;
int n;long long ans;
bool tag[26],one[26];
char s[maxn][49];
struct trie{
int cnt,tail[26],trie[maxn*29][26];
void insert(char *s,bool rev){
int now=0;
if(!rev){
for(int i=0;s[i];i++){
int c=s[i]-'a';
if(!trie[now][c]) trie[now][c]=++cnt;
now=trie[now][c];
}
}
else{
for(int i=strlen(s)-1;~i;i--){
int c=s[i]-'a';
if(!trie[now][c]) trie[now][c]=++cnt,tail[c]++;
now=trie[now][c];
}
}
}
}tr1,tr2;
int main(){
freopen("magic.in","r",stdin);
freopen("magic.out","w",stdout);
scanf("%d",&n);
for(int i=1;i<=n;i++){
scanf("%s",s[i]);
tr1.insert(s[i],false);
tr2.insert(s[i],true);
int len=strlen(s[i]);
tag[s[i][len-1]-'a']=true;
if(len==1) one[s[i][0]-'a']=true;
}
for(int c=0;c<26;c++) ans+=one[c];
for(int now=1;now<=tr1.cnt;now++){
for(int c=0;c<26;c++){
ans+=tr1.trie[now][c]?tag[c]:tr2.tail[c];
}
}
return printf("%lld\n",ans),0;
}
表达式
咕咕咕
多校A层冲刺NOIP2024模拟赛05
好数
咕咕咕
点击查看代码
#include<bits/stdc++.h>
using namespace std;
const int maxn=5009;
const int inf=3e5+9;
int n,ans,a[maxn];bool b[inf<<1];
int main(){
freopen("number.in","r",stdin);
freopen("number.out","w",stdout);
scanf("%d",&n);
for(int i=1;i<=n;i++){
scanf("%d",&a[i]);
for(int j=1;j<i;j++){
if(b[a[i]-a[j]+inf]){
ans++;
break;
}
}
for(int j=1;j<=i;j++) b[a[i]+a[j]+inf]=true;
}
return printf("%d\n",ans),0;
}
SOS字符串
咕咕咕
点击查看代码
#include<bits/stdc++.h>
using namespace std;
const int mod=1e9+7;
int n,las,now;long long f[2][3][4];
int main(){
freopen("sos.in","r",stdin);
freopen("sos.out","w",stdout);
scanf("%d",&n);f[0][2][0]=1;
for(int i=1;i<=n;i++){
las=now,now^=1;
for(int j=0;j<4;j++){
f[now][0][j]=(f[las][0][j]+f[las][2][j])%mod;
f[now][1][j]=f[las][0][j];
f[now][2][j]=(f[las][0][j]*24+f[las][1][j]*25+f[las][2][j]*25)%mod;
if(j) (f[now][2][j]+=f[las][1][j-1])%=mod;
if(j==3) (f[now][2][j]+=f[las][1][j])%=mod;
}
}
return printf("%lld\n",(f[now][0][3]+f[now][1][3]+f[now][2][3])%mod),0;
}
集训营的气球
咕咕咕
连通子树与树的重心
咕咕咕
多校A层冲刺NOIP2024模拟赛06
小Z的手套
咕咕咕
点击查看代码
#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5+9;
int n,m,l[maxn],r[maxn];
bool check(int x){
int res=0,L=1,R=1;
while(L<=n&&R<=m){
if(abs(l[L]-r[R])<=x) res++,L++,R++;
else if(n<m) R++;
else if(n>m) L++;
else return false;
}
return res==min(n,m);
}
int main(){
freopen("gloves.in","r",stdin);
freopen("gloves.out","w",stdout);
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++) scanf("%d",&l[i]);
for(int i=1;i<=m;i++) scanf("%d",&r[i]);
sort(l+1,l+n+1),sort(r+1,r+m+1);
int L=0,R=max(r[m]-l[1],l[m]-r[1]);
while(L<R){
int mid=(L+R)>>1;
if(check(mid)) R=mid;
else L=mid+1;
}
return printf("%d\n",L),0;
}
小Z的字符串
咕咕咕
点击查看代码
#include<bits/stdc++.h>
using namespace std;
const int maxn=409;
int t[3],pos[3][maxn];char s[maxn];
int dp[maxn>>1][maxn>>1][maxn>>1][3];
int main(){
freopen("string.in","r",stdin);
freopen("string.out","w",stdout);
scanf("%s",s+1);
for(int i=1;s[i];i++) pos[s[i]-'0'][++t[s[i]-'0']]=i;
int n=strlen(s+1);
int mx=max({t[0],t[1],t[2]});
if(mx>(n+1)>>1) return puts("-1"),0;
memset(dp,0x3f,sizeof dp);
dp[0][0][0][0]=dp[0][0][0][1]=dp[0][0][0][2]=0;
for(int i=0;i<=t[0];i++){
for(int j=0;j<=t[1];j++){
for(int k=0;k<=t[2];k++){
int now=i+j+k;
if(i) dp[i][j][k][0]=min(dp[i-1][j][k][1],dp[i-1][j][k][2])+abs(now-pos[0][i]);
if(j) dp[i][j][k][1]=min(dp[i][j-1][k][0],dp[i][j-1][k][2])+abs(now-pos[1][j]);
if(k) dp[i][j][k][2]=min(dp[i][j][k-1][0],dp[i][j][k-1][1])+abs(now-pos[2][k]);
}
}
}
int ans=0x3f3f3f3f;
for(int i=0;i<3;i++) ans=min(ans,dp[t[0]][t[1]][t[2]][i]>>1);
return printf("%d\n",ans),0;
}
一个真实的故事
咕咕咕
异或区间
咕咕咕
多校A层冲刺NOIP2024模拟赛07
限速
咕咕咕
点击查看代码
#include<bits/stdc++.h>
using namespace std;
const int maxn=2e5+9;
int n,m,k,mx,die[maxn];long long ans;
int findie(int x){return x==die[x]?x:die[x]=findie(die[x]);}
struct edge{
int x,y,s;
bool operator <(const edge &cp)const{
return s<cp.s;
}
}e[maxn];
void kruskal(){
int tmp=n-1;sort(e+1,e+m+1);
for(int i=1;i<=m;i++){
int x=findie(e[i].x),y=findie(e[i].y);
if(x==y) continue;
die[x]=y;mx=max(mx,e[i].s);
if(e[i].s>k) ans+=e[i].s-k;
if(!(--tmp)){
if(e[i].s<k){
for(int j=i;j<=m;j++){
if(j<m&&e[j].s<k) continue;
ans=min(abs(e[j-1].s-k),abs(e[j].s-k));
}
}
return;
}
}
}
int main(){
freopen("speed.in","r",stdin);
freopen("speed.out","w",stdout);
scanf("%d%d%d",&n,&m,&k);
for(int i=1;i<=m;i++) scanf("%d%d%d",&e[i].x,&e[i].y,&e[i].s);
for(int i=1;i<=n;i++) die[i]=i;
return kruskal(),printf("%lld\n",ans),0;
}
酒鬼
咕咕咕
距离
咕咕咕
团队选拔
咕咕咕
多校A层冲刺NOIP2024模拟赛08
传送
咕咕咕
点击查看代码
#include<bits/stdc++.h>
using namespace std;
const int maxn=2e5+9;
int n,m,tot;long long dis[maxn];bool vis[maxn];
int h[maxn],nxt[maxn*6],to[maxn*6],w[maxn*6];
priority_queue<pair<int,int>> q;
struct point{
int id,x,y;
}p[maxn];
bool cmp1(point a,point b){return a.x<b.x;}
bool cmp2(point a,point b){return a.y<b.y;}
void add(int x,int y,int z){
tot++;
nxt[tot]=h[x];
to[tot]=y;
w[tot]=z;
h[x]=tot;
}
void dij(){
memset(dis,0x3f,sizeof dis);dis[1]=0;
q.push(make_pair(-dis[1],1));
while(q.size()){
int x=q.top().second;q.pop();
if(vis[x]) continue;vis[x]=true;
for(int i=h[x];i;i=nxt[i]){
int y=to[i];
if(dis[y]>dis[x]+w[i]){
dis[y]=dis[x]+w[i];
if(!vis[y]) q.push(make_pair(-dis[y],y));
}
}
}
}
int main(){
freopen("teleport.in","r",stdin);
freopen("teleport.out","w",stdout);
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++) scanf("%d%d",&p[i].x,&p[i].y),p[i].id=i;
for(int i=1,u,v,w;i<=m;i++){
scanf("%d%d%d",&u,&v,&w);
add(u,v,w),add(v,u,w);
}
sort(p+1,p+n+1,cmp1);
for(int i=1;i<n;i++){
int _=min(abs(p[i].x-p[i+1].x),abs(p[i].y-p[i+1].y));
add(p[i].id,p[i+1].id,_),add(p[i+1].id,p[i].id,_);
}
sort(p+1,p+n+1,cmp2);
for(int i=1;i<n;i++){
int _=min(abs(p[i].x-p[i+1].x),abs(p[i].y-p[i+1].y));
add(p[i].id,p[i+1].id,_),add(p[i+1].id,p[i].id,_);
}
dij();
for(int i=2;i<=n;i++) printf("%lld ",dis[i]);
return 0;
}
排列
咕咕咕
点击查看代码
#include<bits/stdc++.h>
using namespace std;
const int maxn=3009;
const int mod=998244353;
int n,m,k,ans;bool vis[11];
int f[11],fac[maxn],ny[maxn];
inline int ksm(int a,int b){
int res=1;
for(;b;b>>=1,a=1ll*a*a%mod) if(b&1) res=1ll*res*a%mod;
return res;
}
int C(int n,int m){return 1ll*fac[n]*ny[m]%mod*ny[n-m]%mod;}
void dfs(int pos,int cnt){
if(pos>m) return (ans+=C(n-cnt,m))%=mod,void(0);
for(int i=1;i<=m;i++){
if(vis[i]) continue;
vis[i]=true;f[pos]=i;
dfs(pos+1,cnt+(pos^1&&__gcd(f[pos-1],f[pos])==1));
vis[i]=false;
}
}
int main(){
freopen("permutation.in","r",stdin);
freopen("permutation.out","w",stdout);
scanf("%d%d",&n,&k);m=n/k,fac[0]=f[0]=1;
for(int i=1;i<=n;i++){
fac[i]=1ll*fac[i-1]*i%mod;
ny[i]=ksm(fac[i],mod-2);
}
return dfs(1,0),printf("%lld\n",1ll*ans*fac[n-m]%mod),0;
}
战场模拟器
咕咕咕
点亮
咕咕咕
多校A层冲刺NOIP2024模拟赛09
排列最小生成树
咕咕咕
点击查看代码
#include<bits/stdc++.h>
using namespace std;
const int maxn=50009;
int n,w,tot,a[maxn],pos[maxn],die[maxn];
vector<pair<int,int>> vec[maxn];
int findie(int x){return x==die[x]?x:die[x]=findie(die[x]);}
inline int sol(int i,int j){return abs(i-j)*abs(a[i]-a[j]);}
int main(){
freopen("pmst.in","r",stdin);
freopen("pmst.out","w",stdout);
scanf("%d",&n);
for(int i=1;i<=n;i++){
scanf("%d",&a[i]);
pos[a[i]]=die[i]=i;
}
for(int i=1;i<=n;i++){
for(int j=max(1,i-(int)sqrt(n));j<i;j++){
w=sol(i,j);
if(w<=n) vec[w].push_back(make_pair(i,j));
w=sol(pos[i],pos[j]);
if(w<=n) vec[w].push_back(make_pair(pos[i],pos[j]));
}
}
long long ans=0;
for(int i=1;i<=n;i++){
for(auto u:vec[i]){
int x=findie(u.first),y=findie(u.second);
if(x==y) continue;
die[x]=y,ans+=i;
if((++tot)==n-1) break;
}
}
return printf("%lld\n",ans),0;
}
卡牌游戏
咕咕咕
点击查看代码
#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5+9;
int n,m,a[maxn],b[maxn];
long long aw,bw,cw;
vector<int> v[maxn];
int main(){
freopen("cardgame.in","r",stdin);
freopen("cardgame.out","w",stdout);
scanf("%d%d",&n,&m);
int gcd=__gcd(n,m);
for(int i=1;i<=n;i++){
scanf("%d",&a[i]);
v[i%gcd].push_back(a[i]);
}
for(int i=0;i<gcd;i++) sort(v[i].begin(),v[i].end());
for(int i=1;i<=m;i++){
scanf("%d",&b[i]);
int l=lower_bound(v[i%gcd].begin(),v[i%gcd].end(),b[i])-v[i%gcd].begin();
int r=v[i%gcd].size()-(upper_bound(v[i%gcd].begin(),v[i%gcd].end(),b[i])-v[i%gcd].begin());
aw+=r,bw+=l,cw+=v[i%gcd].size()-l-r;
}
return printf("%lld\n%lld\n%lld\n",aw*gcd,bw*gcd,cw*gcd),0;
}
比特跳跃
咕咕咕
点击查看代码
#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5+9;
int n,m,s,k,tot;bool vis[maxn*3];
int h[maxn*3],nxt[maxn<<7],to[maxn<<7];
long long w[maxn<<7],dis[maxn*3];
priority_queue<pair<long long,int>> q;
void add(int x,int y,long long z){
tot++;
nxt[tot]=h[x];
to[tot]=y;
w[tot]=z;
h[x]=tot;
}
void dij(){
memset(dis,0x3f,sizeof dis),dis[1]=0;
q.push(make_pair(-dis[1],1));
while(q.size()){
int x=q.top().second;q.pop();
if(vis[x]) continue;vis[x]=true;
for(int i=h[x];i;i=nxt[i]){
int y=to[i];
if(dis[y]>dis[x]+w[i]){
dis[y]=dis[x]+w[i];
if(!vis[y]) q.push(make_pair(-dis[y],y));
}
}
}
for(int i=2;i<=n;i++) printf("%lld ",dis[i]);
}
int main(){
freopen("jump.in","r",stdin);
freopen("jump.out","w",stdout);
scanf("%d%d%d%d",&n,&m,&s,&k);
int x,y;long long z;
for(int i=1;i<=m;i++){
scanf("%d%d%lld",&x,&y,&z);
add(x,y,z),add(y,x,z);
}
for(int i=0;i<=n;i++){
for(int j=0;(1<<j)<=n;j++){
if(s==1) x=i,y=1<<j,z=1ll*k*(x&y);
if(s==2) x=i,y=1<<j^i,z=1ll*k*(x^y);
if(s==3) x=i+n+1,y=(i^(1<<j))+n+1,z=((i>>j)&1)?0:1ll*k*(1<<j);
add(x,y,z);if(s^3) add(y,x,z);
}
if(s==3) add(i,i+n+1,1ll*k*i),add(i+n+1,i,0);
}
return dij(),0;
}
区间
咕咕咕
多校A层冲刺NOIP2024模拟赛10
岛屿
咕咕咕
点击查看代码
#include<bits/stdc++.h>
using namespace std;
int x,y;double ans;
int main(){
freopen("island.in","r",stdin);
freopen("island.out","w",stdout);
scanf("%d%d",&x,&y);
for(int i=1;i<=x;i++) ans+=1.0/((i<<1)-1);
for(int i=1;i<=y;i++) ans+=1.0/((x<<1)+i);
return printf("%.9lf\n",ans),0;
}
最短路
咕咕咕
点击查看代码
#include<bits/stdc++.h>
#define lcs (rt<<1)
#define rcs (rt<<1|1)
using namespace std;
const int maxn=1e5+9;
const int maxm=2e5+9;
int n,m,tot_,tot,cnt,pre[maxn];
bool vis[maxn],use[maxm<<1];
int h_[maxn],nxt_[maxm<<1],to_[maxm<<1];
long long dis[maxn],w[maxm<<1];
int h[maxn],nxt[maxn],to[maxn];
int dep[maxn],die[maxn],siz[maxn];
int son[maxn],dfn[maxn],top[maxn];
priority_queue<pair<long long,int>> q;
struct segtree{
long long ans,lz;int l,r;
segtree(){ans=lz=0x3f3f3f3f3f3f3f3f;}
}tree[maxn<<2];
void add_(int x,int y,long long z){
tot_++;
nxt_[tot_]=h_[x];
to_[tot_]=y;
w[tot_]=z;
h_[x]=tot_;
}
void add(int x,int y){
tot++;
nxt[tot]=h[x];
to[tot]=y;
h[x]=tot;
}
void dij(){
memset(dis,0x3f,sizeof dis),dis[1]=0;
q.push(make_pair(-dis[1],1));
while(q.size()){
int x=q.top().second;q.pop();
if(vis[x]) continue;vis[x]=true;
for(int i=h_[x];i;i=nxt_[i]){
int y=to_[i];
if(dis[y]>dis[x]+w[i]){
dis[y]=dis[x]+w[i];pre[y]=x;
use[i]=use[((i+1)^1)-1]=true;
if(!vis[y]) q.push(make_pair(-dis[y],y));
}
}
}
}
void dfs1(int x,int fa){
siz[x]=1,die[x]=fa,dep[x]=dep[fa]+1;
for(int i=h[x];i;i=nxt[i]){
int y=to[i];
dfs1(y,x),siz[x]+=siz[y];
if(siz[y]>siz[son[x]]) son[x]=y;
}
}
void dfs2(int x,int tp){
top[x]=tp,dfn[x]=++cnt;
if(!son[x]) return;
dfs2(son[x],tp);
for(int i=h[x];i;i=nxt[i]){
int y=to[i];
if(y^die[x]&&y^son[x]) dfs2(y,y);
}
}
inline void pushup(int rt){tree[rt].ans=min(tree[lcs].ans,tree[rcs].ans);}
void pushdown(int rt){
if(tree[rt].lz==0x3f3f3f3f) return;
tree[lcs].ans=min(tree[lcs].ans,tree[rt].lz);
tree[rcs].ans=min(tree[rcs].ans,tree[rt].lz);
tree[lcs].lz=min(tree[lcs].lz,tree[rt].lz);
tree[rcs].lz=min(tree[rcs].lz,tree[rt].lz);
tree[rt].lz=0x3f3f3f3f;
}
void build(int rt,int l,int r){
tree[rt].l=l,tree[rt].r=r;
if(l==r) return;
int mid=(l+r)>>1;
build(lcs,l,mid),build(rcs,mid+1,r);
}
void update(int rt,int l,int r,long long val){
if(l>tree[rt].r||r<tree[rt].l) return;
if(l<=tree[rt].l&&tree[rt].r<=r){
tree[rt].ans=min(tree[rt].ans,val);
tree[rt].lz=min(tree[rt].lz,val);
return;
}
pushdown(rt),update(lcs,l,r,val),update(rcs,l,r,val),pushup(rt);
}
long long query(int rt,int pos){
if(tree[rt].l==tree[rt].r) return tree[rt].ans;
int mid=(tree[rt].l+tree[rt].r)>>1;pushdown(rt);
if(pos<=mid) return query(lcs,pos);
else return query(rcs,pos);
}
void update(int x,int y,long long z){
while(top[x]^top[y]){
if(dep[top[x]]<dep[top[y]]) swap(x,y);
update(1,dfn[top[x]],dfn[x],z);
x=die[top[x]];
}
if(dep[x]>dep[y]) swap(x,y);
update(1,dfn[x]+1,dfn[y],z);
}
int main(){
freopen("path.in","r",stdin);
freopen("path.out","w",stdout);
scanf("%d%d",&n,&m);
for(int i=1,x,y,z;i<=m;i++){
scanf("%d%d%d",&x,&y,&z);
add_(x,y,z),add_(y,x,z);
}
dij();
for(int i=1;i<=n;i++) add(pre[i],i);
dfs1(1,0),dfs2(1,1),build(1,1,n);
for(int i=1;i<=n;i++){
for(int j=h_[i];j;j=nxt_[j]){
int x=i,y=to_[j];
long long z=dis[x]+dis[y]+w[j];
if((pre[y]==x||pre[x]==y)&&use[j]) continue;
update(x,y,z);
}
}
for(int i=2;i<=n;i++){
long long ans=query(1,dfn[i]);
printf("%lld\n",ans^dis[0]?ans-dis[i]:-1);
}
return 0;
}
列表
咕咕咕
种植
咕咕咕
多校A层冲刺NOIP2024模拟赛11
冒泡排序
咕咕咕
点击查看代码
#include<bits/stdc++.h>
using namespace std;
const int maxn=1e6+9;
int n,k,a[maxn];
vector<int> v;
int main(){
freopen("bubble.in","r",stdin);
freopen("bubble.out","w",stdout);
scanf("%d%d",&n,&k);
for(int i=1;i<=n;i++) scanf("%d",&a[i]);
for(int i=1;i<=k;i++){
for(int j=i;j<=n;j+=k) v.push_back(a[j]);
sort(v.begin(),v.end());
int now=i;
for(auto j:v) a[now]=j,now+=k;
v.clear();
}
for(int i=1;i<=n;i++) printf("%d ",a[i]);
return 0;
}
染色
咕咕咕
图
咕咕咕
山峦
咕咕咕
多校A层冲刺NOIP2024模拟赛12
Alice 和璀璨花
咕咕咕
点击查看代码
#include<bits/stdc++.h>
using namespace std;
const int maxn=1e6+9;
int n,ans,b[maxn];long long a[maxn],cp[maxn];
int main(){
freopen("alice.in","r",stdin);
freopen("alice.out","w",stdout);
scanf("%d",&n);
for(int i=1;i<=n;i++) scanf("%lld",&a[i]);
for(int i=1;i<=n;i++) scanf("%d",&b[i]);
for(int i=1;i<=n;i++){
if(a[i]>cp[ans]){cp[ans]=a[i]*b[++ans];continue;}
int pos=lower_bound(cp+1,cp+ans+1,a[i])-cp;
if(a[i]*b[pos]<cp[pos]) cp[pos]=a[i]*b[pos];
}
return printf("%d\n",ans),0;
}
Bob 与幸运日
咕咕咕
Charlie 的运输网
咕咕咕
David 与和谐号
咕咕咕
点击查看代码
#include<bits/stdc++.h>
using namespace std;
const int maxn=29;
int T,n,ans,a[maxn];
bool dfs(int stp){
int delta=0;
for(int i=1;i<=n;i++) delta+=abs(a[i]-a[i+1])!=1;
if(stp+delta>ans) return false;
if(stp==ans) return !delta;
for(int i=2;i<=n;i++){
reverse(a+1,a+i+1);
if(dfs(stp+1)) return true;
reverse(a+1,a+i+1);
}
return false;
}
int main(){
freopen("david.in","r",stdin);
freopen("david.out","w",stdout);
scanf("%d",&T);
while(T--){
scanf("%d",&n);
for(int i=1;i<=n;i++) scanf("%d",&a[i]);
ans=0,a[n+1]=n+1;
while(!dfs(0)) ans++;
printf("%d\n",ans);
}
return 0;
}
多校A层冲刺NOIP2024模拟赛13
子集和
咕咕咕
异或
咕咕咕
异或2
咕咕咕
卡牌游戏
咕咕咕
多校A层冲刺NOIP2024模拟赛14
数位
咕咕咕
乘法
咕咕咕
周期
咕咕咕
轰炸
咕咕咕
多校A层冲刺NOIP2024模拟赛15
追逐游戏
咕咕咕
点击查看代码
#include<bits/stdc++.h>
using namespace std;
const int maxn=2e5+9;
int n,q,s,t,s_,tot,cnt;
int h[maxn],nxt[maxn<<1],to[maxn<<1];
int die[maxn][19],dep[maxn],siz[maxn];
int son[maxn],top[maxn],dfn[maxn];
void add(int x,int y){
tot++;
nxt[tot]=h[x];
to[tot]=y;
h[x]=tot;
}
void dfs1(int x,int fa){
siz[x]=1,die[x][0]=fa,dep[x]=dep[fa]+1;
for(int i=1;i<=18;i++) die[x][i]=die[die[x][i-1]][i-1];
for(int i=h[x];i;i=nxt[i]){
int y=to[i];
if(y==fa) continue;
dfs1(y,x),siz[x]+=siz[y];
if(siz[y]>siz[son[x]]) son[x]=y;
}
}
void dfs2(int x,int tp){
top[x]=tp,dfn[x]=++cnt;
if(!son[x]) return;
dfs2(son[x],tp);
for(int i=h[x];i;i=nxt[i]){
int y=to[i];
if(y^son[x]&&y^die[x][0]) dfs2(y,y);
}
}
int query(int x,int y){
int res=0;
while(top[x]^top[y]){
if(dep[top[x]]<dep[top[y]]) swap(x,y);
res+=dfn[x]-dfn[top[x]]+1;
x=die[top[x]][0];
}
if(dep[x]>dep[y]) swap(x,y);
return res+=dfn[y]-dfn[x];
}
int lca(int x,int y){
while(top[x]^top[y]){
if(dep[top[x]]<dep[top[y]]) swap(x,y);
x=die[top[x]][0];
}
if(dep[x]>dep[y]) swap(x,y);
return x;
}
int main(){
freopen("chase.in","r",stdin);
freopen("chase.out","w",stdout);
scanf("%d%d",&n,&q);
for(int i=1,x,y;i<n;i++){
scanf("%d%d",&x,&y);
add(x,y),add(y,x);
}
dfs1(1,0),dfs2(1,1);
while(q--){
scanf("%d%d%d",&s,&t,&s_);
int l=query(s,s_),r=query(t,s_),dis=query(s,t);
if(r<=dis){
int tim=((l+r-dis)>>1)+((dis-r-1)>>1)+1,pos,d;
tim<=query(s,lca(s,t))?(d=tim,pos=s):(d=dis-tim,pos=t);
for(int i=18;~i;i--) if(d>=1<<i) d-=1<<i,pos=die[pos][i];
printf("%d %d\n",tim,pos);
}
else printf("%d %d\n",r,t);
}
return 0;
}
统计
咕咕咕
点击查看代码
#include<bits/stdc++.h>
using namespace std;
const int maxn=1e6+9;
int T,n,m,a[maxn];
long long ans,v[maxn],s[maxn];
mt19937 rd(time(0));
int main(){
freopen("st.in","r",stdin);
freopen("st.out","w",stdout);
scanf("%d",&T);
while(T--){
scanf("%d%d",&n,&m);
ans=v[m]=s[0]=0;
for(int i=1;i<m;i++) v[i]=rd(),v[m]-=v[i];
for(int i=1;i<=n;i++) scanf("%d",&a[i]),s[i]=s[i-1]+v[a[i]];
sort(s,s+n+1);
for(int i=1,cp=1;i<=n;i++){
if(s[i]^s[i-1]) cp=0;
ans+=cp++;
}
printf("%lld\n",ans);
}
return 0;
}
软件工程
咕咕咕
命运的X
咕咕咕
多校A层冲刺NOIP2024模拟赛16
四舍五入
咕咕咕
点击查看代码
#include<bits/stdc++.h>
using namespace std;
const int maxn=3e6+9;
int n,l,r,cf[maxn];
int main(){
freopen("count.in","r",stdin);
freopen("count.out","w",stdout);
scanf("%d",&n);
for(int j=1;j<=n;j++){
for(int i=0;i<=n;i+=j){
l=i,r=i+((j-1)>>1);
cf[l]++,cf[r+1]--;
}
}
for(int i=1;i<=n;i++) printf("%d ",cf[i]+=cf[i-1]);
return 0;
}
填算符
咕咕咕
道路修建
咕咕咕
逆序图
咕咕咕
多校A层冲刺NOIP2024模拟赛17
网格
咕咕咕
点击查看代码
#include <bits/stdc++.h>
using namespace std;
const int maxn=2009;
const int mod=998244353;
int n,m,dp[maxn][maxn];char mp[maxn][maxn];
int x[maxn][maxn],z[maxn][maxn],y[maxn][maxn];
int main(){
freopen("grid.in","r",stdin);
freopen("grid.out","w",stdout);
scanf("%d%d",&n,&m);dp[1][1]=x[1][1]=1;
for(int i=1;i<=n;i++) scanf("%s",mp[i]+1);
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
if(i>1||j>1){
dp[i][j]=(dp[i-1][j]+dp[i][j-1])%mod;
x[i][j]=(x[i-1][j]+x[i][j-1])%mod;
}
y[i][j]=(y[i-1][j]+y[i][j-1])%mod;
z[i][j]=(z[i-1][j]+z[i][j-1])%mod;
if(mp[i][j]>='1'&&mp[i][j]<='9') y[i][j]=(10ll*y[i][j]+1ll*x[i][j]*(mp[i][j]^48))%mod;
else if(mp[i][j]=='+'){
(z[i][j]+=y[i][j])%=mod;
x[i][j]=dp[i][j],y[i][j]=0;
}
else if(mp[i][j]=='*') x[i][j]=y[i][j],y[i][j]=0;
}
}
return printf("%d",(z[n][m]+y[n][m])%mod),0;
}
矩形
咕咕咕
点击查看代码
#include<bits/stdc++.h>
using namespace std;
const int maxn=2e5+9;
int n,deg[maxn<<1],cnt[maxn<<1];
vector<int> ude[maxn<<1],de[maxn<<1];
int main(){
freopen("rect.in","r",stdin);
freopen("rect.out","w",stdout);
scanf("%d",&n);
for(int i=1,x,y;i<=n;i++){
scanf("%d%d",&x,&y),y+=n;
ude[x].push_back(y);
ude[y].push_back(x);
deg[x]++,deg[y]++;
}
long long ans=0;n<<=1;
for(int i=1;i<=n;i++)
for(auto j:ude[i])
if(deg[i]<deg[j]||(deg[i]==deg[j]&&i<j)) de[i].push_back(j);
for(int i=1;i<=n;i++){
for(auto j:ude[i]){
for(auto k:de[j]){
if(deg[i]<deg[k]||(deg[i]==deg[k]&&i<k)) ans+=cnt[k]++;
}
}
for(auto j:ude[i]) for(auto k:de[j]) cnt[k]=0;
}
return printf("%lld\n",ans),0;
}
集合
咕咕咕
倒水
咕咕咕
多校A层冲刺NOIP2024模拟赛18
选彩笔
咕咕咕
点击查看代码
#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5+9;
const int inf=1<<8|1;
int n,k,sum[inf][inf][inf];
int calc(int v,int x,int y,int z){
int res=sum[x+v][y+v][z+v];
res-=sum[x+v][y+v][z-1]+sum[x+v][y-1][z+v]+sum[x-1][y+v][z+v];
res+=sum[x+v][y-1][z-1]+sum[x-1][y+v][z-1]+sum[x-1][y-1][z+v];
res-=sum[x-1][y-1][z-1];
return res;
}
bool check(int v){
for(int x=1;x+v<inf;x++){
for(int y=1;y+v<inf;y++){
for(int z=1;z+v<inf;z++){
if(calc(v,x,y,z)>=k) return true;
}
}
}
return false;
}
int main(){
freopen("rgb.in","r",stdin);
freopen("rgb.out","w",stdout);
scanf("%d%d",&n,&k);
for(int i=1,r,g,b;i<=n;i++){
scanf("%d%d%d",&r,&g,&b);
sum[r+1][g+1][b+1]++;
}
for(int x=1;x<inf;x++){
for(int y=1;y<inf;y++)
for(int z=1;z<inf;z++)
sum[x][y][z]+=sum[x][y][z-1];
for(int z=1;z<inf;z++)
for(int y=1;y<inf;y++)
sum[x][y][z]+=sum[x][y-1][z];
for(int y=1;y<inf;y++)
for(int z=1;z<inf;z++)
sum[x][y][z]+=sum[x-1][y][z];
}
int l=0,r=inf;
while(l<r){
int mid=(l+r)>>1;
if(check(mid)) r=mid;
else l=mid+1;
}
return printf("%d\n",l),0;
}
兵蚁排序
咕咕咕
点击查看代码
#include<bits/stdc++.h>
using namespace std;
const int maxn=1009;
int T,n,a[maxn],b[maxn],st[maxn*maxn];
int main(){
freopen("sort.in","r",stdin);
freopen("sort.out","w",stdout);
scanf("%d",&T);
while(T--){
scanf("%d",&n);st[0]=0;bool flag=false;
for(int i=1;i<=n;i++) scanf("%d",&a[i]);
for(int i=1;i<=n;i++) scanf("%d",&b[i]);
for(int i=n;i;i--){
for(int j=i;j;j--){
if(a[j]>b[i]) flag=true;
if(a[j]<b[i]) continue;
for(int k=j;k<i;k++){
st[++st[0]]=k;
swap(a[k],a[k+1]);
}
break;
}
}
if(flag){puts("-1");continue;}
printf("0\n%d\n",st[0]);
for(int i=1;i<=st[0];i++) printf("%d %d\n",st[i],st[i]+1);
}
return 0;
}
人口局DBA
咕咕咕
点击查看代码
#include <bits/stdc++.h>
using namespace std;
const int maxn=2009;
const int mod=1e9+7;
int m,L,s,ans,a[maxn];
int fac[maxn*maxn],inv[maxn*maxn];
inline int ksm(int a,int b){
int res=1;
for(;b;b>>=1,a=1ll*a*a%mod) if(b&1) res=1ll*res*a%mod;
return res;
}
inline int C(int n,int m){return n<m?0:1ll*fac[n]*inv[m]%mod*inv[n-m]%mod;}
int calc(int n,int p){
int res=0;
for(int i=0;i<=n&&i*m<=s;i++){
int sum=1ll*C(n,i)*(C(s-i*m+n,n)-C(s-i*m+n-p,n)+mod)%mod;
(res+=i&1?mod-sum:sum)%=mod;
}
return res;
}
int main(){
freopen("dba.in","r",stdin);
freopen("dba.out","w",stdout);
scanf("%d%d",&m,&L);fac[0]=1;
for(int i=1;i<=L;i++) scanf("%d",&a[i]),s+=a[i];
for(int i=1;i<=L*m;i++) fac[i]=1ll*fac[i-1]*i%mod;
inv[L*m]=ksm(fac[L*m],mod-2);
for(int i=L*m;i;i--) inv[i-1]=1ll*inv[i]*i%mod;
for(int i=1;i<L;i++) (ans+=calc(L-i,a[i]))%=mod,s-=a[i];
return printf("%d",ans),0;
}
银行的源起
咕咕咕
多校A层冲刺NOIP2024模拟赛19
图书管理
咕咕咕
点击查看代码
#include<bits/stdc++.h>
using namespace std;
const int maxn=10009;
int n,p[maxn],a[maxn],t[maxn<<1];
int main(){
freopen("book.in","r",stdin);
freopen("book.out","w",stdout);
scanf("%d",&n);long long ans=0;
for(int i=1;i<=n;i++) scanf("%d",&p[i]);
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
if(i==j) a[j]=0;
else a[j]=p[j]<p[i]?-1:1;
}
for(int j=i,s=n;j;j--) t[s+=a[j]]+=j;
for(int j=i,s=n;j<=n;j++) ans+=1ll*t[(n<<1)-(s+=a[j])]*j*p[i];
memset(t,0,sizeof t);
}
return printf("%lld\n",ans),0;
}
两棵树
咕咕咕
点击查看代码
#include<bits/stdc++.h>
using namespace std;
const int maxn=2e5+9;
const int mod=998244353;
const int _2=499122177;
const int _16=935854081;
int n,ans,deg[maxn];
map<int,bool> vis[maxn];
int main(){
freopen("tree.in","r",stdin);
freopen("tree.out","w",stdout);
scanf("%d",&n);
for(int i=1,x,y;i<n;i++){
scanf("%d%d",&x,&y);
if(x>y) swap(x,y);
deg[x]++,deg[y]++;
vis[x][y]=true;
}
ans=1ll*(n-1)*_2%mod;
for(int i=1,x,y;i<n;i++){
scanf("%d%d",&x,&y);
if(x>y) swap(x,y);
int cnt=n-1-deg[x]-deg[y]+vis[x][y];
(ans+=1ll*cnt*_16%mod)%=mod;
}
return printf("%d\n",ans),0;
}
函数
咕咕咕
点击查看代码
#include<bits/stdc++.h>
using namespace std;
const int maxn=1e6+9;
int n,q,a,b,cnt,x[maxn];
int trie[maxn*29][2],tail[maxn*29];
void insert(int x,int id){
int now=0;
for(int i=29;~i;i--){
bool v=x>>i&1;
if(!trie[now][v]) trie[now][v]=++cnt;
now=trie[now][v];
}
tail[now]=id;
}
int query(int a,bool _){
int now=0;
for(int i=29;~i;i--){
bool v=(a>>i&1)^_;
now=trie[now][v]?trie[now][v]:trie[now][!v];
}
return tail[now];
}
inline int f(int x){return (x^a)-b;}
int main(){
freopen("fun.in","r",stdin);
freopen("fun.out","w",stdout);
scanf("%d%d",&n,&q);
for(int i=1;i<=n;i++){
scanf("%d",&x[i]);
insert(x[i],i);
}
while(q--){
scanf("%d%d",&a,&b);
int l=query(a,0),r=query(a,1);
if(l>r) swap(l,r);
if(1ll*f(x[l])*f(x[r])>0){puts("-1");continue;}
while(l+1<r){
int mid=(l+r)>>1;
1ll*f(x[l])*f(x[mid])<=0?r=mid:l=mid;
}
printf("%d\n",l);
}
return 0;
}
编辑
咕咕咕
多校A层冲刺NOIP2024模拟赛20
星际联邦
咕咕咕
点击查看代码
#include<bits/stdc++.h>
using namespace std;
const int maxn=3e5+9;
int n,mn;long long ans;
int a[maxn],mx[maxn];
int main(){
freopen("star.in","r",stdin);
freopen("star.out","w",stdout);
scanf("%d",&n);
mx[0]=-0x3f3f3f3f,mn=0x3f3f3f3f;
for(int i=1;i<=n;i++){
scanf("%d",&a[i]);
mx[i]=max(mx[i-1],a[i]);
}
for(int i=n;i>1;i--){
mn=min(mn,a[i]);
ans+=(a[i]^mx[i])?min(a[i]-mx[i-1],mn-a[i]):mn-mx[i-1];
}
return printf("%lld\n",ans),0;
}
和平精英
咕咕咕
摆烂合唱
咕咕咕
对称旅行者
咕咕咕
多校A层冲刺NOIP2024模拟赛21
送信卒
咕咕咕
点击查看代码
#include<bits/stdc++.h>
using namespace std;
const int maxn=109;
const double eps=1e-9;
int n,m,sx,sy,tx,ty;
int mp[maxn][maxn];
int a[4]={1,0,-1,0};
int b[4]={0,1,0,-1};
double dis[maxn][maxn];
double s,l,r;bool vis[maxn][maxn];
priority_queue<pair<double,pair<int,int>>> q;
bool check(double k){
for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) dis[i][j]=114514;
memset(vis,false,sizeof vis);dis[sx][sy]=0;
q.push(make_pair(-dis[sx][sy],make_pair(sx,sy)));
while(q.size()){
int x=q.top().second.first,y=q.top().second.second;
vis[x][y]=true;q.pop();
for(int i=0;i<4;i++){
int _x=x+a[i],_y=y+b[i];
double w=a[i]?k:1;
if(mp[_x][_y]||_x<1||_x>n||_y<1||_y>m) continue;
if(dis[_x][_y]>dis[x][y]+w){
dis[_x][_y]=dis[x][y]+w;
if(!vis[_x][_y]) q.push(make_pair(-dis[_x][_y],make_pair(_x,_y)));
}
}
}
return dis[tx][ty]-s<sqrt(eps);
}
int main(){
freopen("msg.in","r",stdin);
freopen("msg.out","w",stdout);
scanf("%d%d%d%d%d%d",&n,&m,&sx,&sy,&tx,&ty);
for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) scanf("%d",&mp[i][j]);
scanf("%lf",&s);
l=0,r=s;
while(r-l>eps){
double mid=(l+r)/2;
if(check(mid)) l=mid;
else r=mid;
}
return printf("%.3lf\n",l),0;
}
共轭树图
咕咕咕
摸鱼军训
咕咕咕
点击查看代码
#include<bits/stdc++.h>
using namespace std;
const int maxn=5e5+9;
int n,q,pos[maxn],tree[maxn],ans[maxn];
vector<pair<int,int>> v[maxn];
void update(int x){for(;x<=n;x+=-x&x) tree[x]++;}
int query(int x){
int res=0;
for(;x;x-=-x&x) res+=tree[x];
return res;
}
int lowb(int rnk){
int l=1,r=n;
while(l<r){
int mid=(l+r)>>1;
if(query(mid)>=rnk) r=mid;
else l=mid+1;
}
return r;
}
int main(){
freopen("bubble.in","r",stdin);
freopen("bubble.out","w",stdout);
scanf("%d",&n);
for(int i=1,x;i<=n;i++) scanf("%d",&x),pos[x]=i;
scanf("%d",&q);
for(int i=1,k,x;i<=q;i++) scanf("%d%d",&k,&x),v[x].push_back(make_pair(k,i));
for(int i=n;i;i--){
for(auto pr:v[i]){
if(pr.first>n-i) ans[pr.second]=i;
else ans[pr.second]=max(lowb(pr.first),pos[i])-pr.first;
}
update(pos[i]);
}
for(int i=1;i<=q;i++) printf("%d\n",ans[i]);
return 0;
}
神奇园艺师
咕咕咕
多校A层冲刺NOIP2024模拟赛22
内容暂未上传,请稍后查看!
多校A层冲刺NOIP2024模拟赛23
字符串构造机
咕咕咕
点击查看代码
#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5+9;
int n,m,die[maxn],ans[maxn];
int x[maxn],y[maxn],z[maxn];
vector<int> vec[maxn];bool vis[maxn];
int findie(int x){return die[x]==x?x:die[x]=findie(die[x]);}
int main(){
freopen("str.in","r",stdin);
freopen("str.out","w",stdout);
scanf("%d%d",&n,&m);
memset(ans,-1,sizeof ans);
for(int i=1;i<=n;i++) die[i]=i;
for(int i=1;i<=m;i++){
scanf("%d%d%d",&x[i],&y[i],&z[i]);
for(int j=0;j<z[i];j++) die[findie(x[i]+j)]=findie(y[i]+j);
}
for(int i=1;i<=m;i++){
int u=findie(x[i]+z[i]),v=findie(y[i]+z[i]);
if(u==v) return puts("-1"),0;
vec[u].push_back(v),vec[v].push_back(u);
}
for(int i=1;i<=n;i++){
if(ans[findie(i)]==-1){
for(int j=0;j<=n;j++) vis[j]=false;
for(auto j:vec[findie(i)]) if(~ans[j]) vis[ans[j]]=true;
for(int j=0;j<=n;j++) if(!vis[j]){ans[findie(i)]=j;break;}
}
printf("%d ",ans[findie(i)]);
}
return 0;
}
忍者小队
咕咕咕
狗卡
咕咕咕
怪盗德基
咕咕咕
多校A层冲刺NOIP2024模拟赛24
选取字符串
咕咕咕
点击查看代码
#include <bits/stdc++.h>
using namespace std;
const int maxn=1e6+9;
const int mod=998244353;
int n,k,ans;char s[maxn];
int kmp[maxn],dep[maxn];
int jc[maxn],ny[maxn],sz[maxn];
int ksm(int a,int b){
int res=1;
for(;b;b>>=1,a=1ll*a*a%mod) if(b&1) res=1ll*res*a%mod;
return res;
}
int C(int n,int m){return n<m?0:1ll*jc[n]*ny[m]%mod*ny[n-m]%mod;}
int main(){
freopen("string.in","r",stdin);
freopen("string.out","w",stdout);
scanf("%d\n%s",&k,s+1);
n=strlen(s+1);jc[0]=1;
for(int i=1;i<=n+1;i++) jc[i]=1ll*jc[i-1]*i%mod;
ny[n+1]=ksm(jc[n+1],mod-2);
for(int i=n+1;i;i--) ny[i-1]=1ll*ny[i]*i%mod;
for(int i=2,j=0;i<=n;i++){
while(j&&s[i]^s[j+1]) j=kmp[j];
kmp[i]=j+=s[i]==s[j+1];
}
for(int i=n;i;i--) sz[kmp[i]]+=++sz[i];
dep[0]=1,ans=C(++sz[0],k);
for(int i=1;i<=n;i++){
dep[i]=dep[kmp[i]]+1;
(ans+=1ll*C(sz[i],k)*((dep[i]<<1)-1)%mod)%=mod;
}
return printf("%d\n",ans),0;
}
取石子
咕咕咕
均衡区间
咕咕咕
点击查看代码
#include<bits/stdc++.h>
using namespace std;
const int maxn=1e6+9;
int n,id,tp1,tp2;
int a[maxn],b[maxn],ans[maxn][2];
int mx[maxn],mn[maxn],st1[maxn],st2[maxn];
void solve(){
for(int i=1;i<=n;i++) scanf("%d",&a[i]);
for(int i=n;i;i--){
while(tp1&&a[st1[tp1]]<a[i]) mx[st1[tp1--]]=i;
while(tp2&&a[st2[tp2]]>a[i]) mn[st2[tp2--]]=i;
st1[++tp1]=st2[++tp2]=i;ans[i][0]=n-i+1,ans[i][1]=i;
}
for(int i=1,j,k;i<=n;i++){
j=min(mx[i],mn[i]);j+=!j;
ans[i][1]-=i-j+1;b[j]--,b[i+1]++;
for(k=mx[i];k>=j;k=mx[k]);
for(;k;k=mx[k]) ans[i][1]--,ans[k][0]--;
for(k=mn[i];k>=j;k=mn[k]);
for(;k;k=mn[k]) ans[i][1]--,ans[k][0]--;
}
for(int i=1;i<=n;i++) ans[i][0]+=b[i]+=b[i-1];
}
int main(){
freopen("interval.in","r",stdin);
freopen("interval.out","w",stdout);
scanf("%d%d",&n,&id);
if(id^2) solve();
for(int i=1;i<=n;i++) printf("%d ",ans[i][0]);
puts("");
for(int i=1;i<=n;i++) printf("%d ",ans[i][1]);
return 0;
}
禁止套娃
咕咕咕
多校A层冲刺NOIP2024模拟赛25
图
咕咕咕
点击查看代码
#include<bits/stdc++.h>
using namespace std;
const int maxn=10009;
int n,m,ans,s[maxn],t[maxn];
bitset<maxn> e[maxn],vis[maxn],cp;
int main(){
freopen("a.in","r",stdin);
freopen("a.out","w",stdout);
scanf("%d%d",&n,&m);
while(m--){
char op;
s[0]=t[0]=0;
for(int i=1;i<=n;i++){
scanf("\n%c",&op);vis[i]=0;
if(op=='1') t[++t[0]]=i;
if(op=='2') s[++s[0]]=i;
if(op=='3') t[++t[0]]=s[++s[0]]=i;
}
cp=0;
for(int i=1;i<=s[0];i++) cp[s[i]]=true;
for(int i=1;i<=t[0];i++) vis[t[i]]=vis[t[i]]|cp;
cp=0;
for(int i=1;i<=t[0];i++) cp[t[i]]=true;
for(int i=1;i<=s[0];i++) vis[s[i]]=vis[s[i]]|cp;
for(int i=1;i<=n;i++) e[i]=e[i]^vis[i];
}
for(int i=1;i<=n;i++) for(int j=i+1;j<=n;j++) ans+=e[i][j];
return printf("%d\n",ans),0;
}
序列
咕咕咕
点击查看代码
#include <bits/stdc++.h>
using namespace std;
const int maxn=1e5+9;
const int mod=1e9+7;
int n,m,ans;
int a[maxn],b[maxn];
vector<int> v[maxn];
struct node{
int x,y;
bool operator <(const node &cp)const{
return 1ll*x*cp.y<1ll*cp.x*y;
}
};
priority_queue<node> q;
int ksm(int a,int b){
int res=1;
for(;b;b>>=1,a=1ll*a*a%mod) if(b&1) res=1ll*res*a%mod;
return res;
}
int main(){
freopen("b.in","r",stdin);
freopen("b.out","w",stdout);
scanf("%d%d",&n,&m);ans++;
for(int i=1;i<=n;i++) scanf("%d",&a[i]),ans=1ll*ans*a[i]%mod;
for(int i=1,t,x,y;i<=m;i++){
scanf("%d%d%d",&t,&x,&y);
if(t==1) b[x]=max(b[x],y);
if(t==2) v[x].push_back(-y);
if(t==3) q.push({y,1});
}
for(int i=1;i<=n;i++){
if(a[i]<b[i]) v[i].push_back(a[i]-b[i]);
sort(v[i].begin(),v[i].end());
for(auto delta:v[i]) q.push({a[i]-delta,a[i]}),a[i]-=delta;
}
printf("%d ",ans);
while(q.size()){
int x=q.top().x,y=q.top().y;q.pop();
printf("%d ",ans=1ll*ans*x%mod*ksm(y,mod-2)%mod),m--;
}
while(m--) printf("%d ",ans);
return 0;
}
树
咕咕咕
字符串
咕咕咕