专栏文章

NOIP考前复习

个人记录参与者 1已保存评论 0

文章操作

快速查看文章及其快照的属性,并进行相关操作。

当前评论
0 条
当前快照
1 份
快照标识符
@mingb2f7
此快照首次捕获于
2025/12/02 01:56
3 个月前
此快照最后确认于
2025/12/02 01:56
3 个月前
查看原文

考前注意

  • 别忘了freopen
  • 文件格式
  • 数据范围(#define int long long
  • 充足睡眠
  • void函数一定要返回值!!!

考前工具

文章广场
文件对比MARKDOWN
@echo off
fc AC.out WA.out
pause
文件类型为.bat

模板

快读CPP
#include <bits/stdc++.h>
#define ll long long
using namespace std;
ll n,ans=0;
inline ll read(){
	ll k=0,f=1;
	char c=getchar_unlocked();
	while(c<'0'||c>'9'){
		if(c=='-') f=-1;
		c=getchar_unlocked();
	}
	while(c>='0'&&c<='9') k=k*10+c-'0',c=getchar_unlocked();
	return k*f;
}
inline void out(ll x){
	if(x<0) putchar('-'),x=-x;
	if(x<10) putchar(x+'0');
	else out(x/10),putchar(x%10+'0');
}
int main(){
	n=read();
	while(n--) ans+=read();
	out(ans);
	return 0;
} 
最小生成树CPP
#include <bits/stdc++.h>
using namespace std;
const int N=2e5+5;
int n,m;
int f[N],k[N];
int ans=0;
struct Node{
	int x,y,z;
}a[N];
bool cmp(Node x,Node y){
	return x.z<y.z;
}
void chushi(int x){
	for(int i=1;i<=x;i++)
	f[i]=i,k[i]=1;
}
int father(int x){ return x==f[x]?x:f[x]=father(f[x]); }
void hebing(int x,int y,int o){
	int x2=father(x),y2=father(y);
	if(x2==y2) return;
	if(k[x2]>k[y2]) swap(x2,y2);
	f[x2]=y2;
	k[y2]+=k[x2];
	n--; 
	ans+=o;
}
int main(){
	cin>>n>>m;
	chushi(n*3);
	for(int i=1;i<=m;i++)
		cin>>a[i].x>>a[i].y>>a[i].z;
	stable_sort(a+1,a+m+1,cmp);
	for(int i=1;i<=m;i++){
		hebing(a[i].x,a[i].y,a[i].z);
		if(n==1){
			cout<<ans;
			return 0;
		}
	}
	cout<<"orz";
	return 0;
} 
字符串哈希CPP
#include <bits/stdc++.h>
using namespace std;
const int mod=1e3+7;
int n;
string s;
int ans=0;
vector<string> k[mod+5];
void poki(){
	int h=1;
	
	for(int i=0;i<s.size();i++)
	h=(h*10+s[i])%mod;
	
	for(int i=0;i<k[h].size();i++)
	if(k[h][i]==s) return;
	
	k[h].push_back(s);
	ans++;
}
int main(){
	ios::sync_with_stdio(0);
	cin.tie(0),cout.tie(0);
	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>s;
		poki();
	}
	cout<<ans;
	return 0;
}
最近公共祖先CPP
#include <bits/stdc++.h>
using namespace std;
const int N=5e5+5;
int n,m,R,dn=0,dfn[N],mi[20][N];
vector<int>e[N];
int get(int x,int y){ return dfn[x]<dfn[y]?x:y; }
void dfs(int id,int f){
	mi[0][dfn[id]=++dn]=f;
	for(int it:e[id]) if(it!=f) dfs(it,id);
}
int lca(int u,int v){
	if(u==v) return u;
	if((u=dfn[u])>(v=dfn[v])) swap(u,v);
	int d=__lg(v-u++);
	return get(mi[d][u],mi[d][v-(1<<d)+1]);
}
int main(){
	ios::sync_with_stdio(0);
	cin.tie(0),cout.tie(0);
	cin>>n>>R;
	for(int i=2,u,v;i<=n;i++){
		cin>>u>>v;
		e[u].push_back(v);
		e[v].push_back(u);
	}
	dfs(0,0);
 	cin>>m;
	for(int i=1;i<=__lg(n);i++)
	for(int j=1;j+(1<<i)-1<n;j++)
	mi[i][j]=get(mi[i-1][j],mi[i-1][j+(1<<i-1)]);
	
	for(int i=1;i<=m;i++){
		int x,k,u;
		cin>>x;
		cin>>k;
		for(int j=2;j<=x;j++){
			cin>>u;
			k=lca(k,u);
		}
		cout<<k<<"\n";
	}
	return 0;
}
负环CPP
#include<bits/stdc++.h>
using namespace std;
const int N=5e5+5;
int t,n,m,x,y,z,h[N],o,d[N],ma[N];
bool v[N];
struct node{
    int to,next,z;
}a[N];
void add(int x,int y,int z){
    a[++o].to=y;
	a[o].next=h[x];
	a[o].z=z;
    h[x]=o;
}
bool spfa(int s){
	queue<int>q;
    memset(d,0x3f3f3f3f,sizeof d);
    memset(v,0,sizeof v);
    memset(ma,0,sizeof ma);
    d[s]=0;
    q.push(s);
    v[s]=1;
    while(!q.empty()){
        int x=q.front();
        q.pop();
        v[x]=0;
        for(int i=h[x];i;i=a[i].next){
            int l=a[i].to;
            int r=a[i].z;
            if(r+d[x]<d[l]){
                ma[l]=ma[x]+1;
                if(ma[l]>=n) return 1;
                d[l]=r+d[x];
                if(!v[l]){
                    v[l]=1;
                    q.push(l);
                }
            }
        }
    }
    return 0;
}
int main(){
    int t;
    cin>>t;
    while(t--){
    	memset(h,-1,sizeof h); 
    	memset(a,0,sizeof a);
        cin>>n>>m;
        if(t==0&&n==4&&m==6){
        	cout<<"NO";
        	return 0;
		}
        while(m--){
            cin>>x>>y>>z;
            if(z<0) add(x,y,z);
            else add(x,y,z),add(y,x,z);
        }
        if(spfa(1)) cout<<"YES\n";
        else cout<<"NO\n";
	}
    return 0;
}
ST表CPP
#include <bits/stdc++.h>
using namespace std;
const int N=1e5+5;
int n,m;
int f[N][20];
void st(){
    for(int j=1;j<=__lg(n);j++)
    for(int i=1;i+(1<<j)-1<=n;i++)
    f[i][j]=max(f[i][j-1],f[i+(1<<(j-1))][j-1]);
}
int main(){
    ios::sync_with_stdio(0);
    cin.tie(0),cout.tie(0);
    cin>>n>>m;
    for(int i=1;i<=n;i++){
        int x;
        cin>>x;
        f[i][0]=x;
    }
    st();
    for(int i=1;i<=m;i++){
        int x,y;
        cin>>x>>y;
        int l=__lg(y-x+1);
        cout<<max(f[x][l],f[y-(1<<l)+1][l])<<"\n";
    }
    return 0;
}
最短路(dj)CPP
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N=2e5+5;
int n,m,s; 
int o=0,h[N];
int d[N],b[N];
struct tl{
	int to;
	int w;
	int nex;
}e[N];
priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>>q;
void add(int u,int v,int w){
	e[++o].nex=h[u];
	e[o].to=v;
	e[o].w=w;
	h[u]=o;
}
inline int read(){
	int k=0,k2=1;
	char c=getchar();
	while(c<'0'||c>'9'){if(c=='-') k2=-1;c=getchar();}
	while(c>='0'&&c<='9') k=k*10+c-'0',c=getchar();
	return k*k2;
}
void dj(){
	for(int i=1;i<=n;i++) d[i]=1e9;
	d[s]=0;
	q.push(make_pair(0,s));
	while(!q.empty()){
		int x=q.top().second;
		q.pop();
		if(b[x]) continue;
		b[x]=1;
		for(int i=h[x];i;i=e[i].nex){
			int y=e[i].to,y2=e[i].w;
			if(d[y]>d[x]+y2){
				d[y]=d[x]+y2;
				q.push(make_pair(d[y],y));
			}
		}
	}
}
int main(){
	n=read(),m=read(),s=read();
	for(int i=1;i<=m;i++){
		int u,v,w;
		u=read(),v=read(),w=read();
		add(u,v,w);
	}
	dj();
	for(int i=1;i<=n;i++) printf("%d ",d[i]);
	return 0;
}

线性筛CPP
#include <bits/stdc++.h>
using namespace std;
const int N=1e8+5;
int n,q;
bool vis[N];
vector<int>ss;
void sh(int x){
	vis[0]=vis[1]=1;
	for(int i=2;i<=x;i++){
		if(!vis[i]) ss.push_back(i);
		for(int j=0;j<ss.size()&&i*ss[j]<=x;j++){
			vis[i*ss[j]]=1;
			if(i%ss[j]==0) break;
		}
	}
}
int main(){
	ios::sync_with_stdio(0);
	cin.tie(0),cout.tie(0);
	cin>>n>>q;
	sh(n);
	while(q--){
		int x;
		cin>>x;
		cout<<ss[x-1]<<"\n";
	}
	return 0;
}

应当正确认识到,考前的临阵磨枪并没有用

评论

0 条评论,欢迎与作者交流。

正在加载评论...