跳到正文

个人记录

「考试总结」2021.11.17 多校联测

Resurgammm约 15 分钟
原文
文章信息与版本

文章信息

发布
2021年11月17日
收录
2025年12月12日
更新
2026年11月11日
篇幅
约 15 分钟

版本

  1. v1当前

开题,发现 T1T1 不会,T2T2 不会,T3T3 原题,T4T4 不会

然后先用了 1h1h 把 T3T3 写了,调了 3h T13h\ T1 贪心,到结束都不知道那假了

36+15+100+036+15+100+0 垫底了

A.饥饿的狐狸

不难想到最小值就是顺着吃,可以 O(1)O(1) 出解

而最大值贪心的策略是以 ww 为分界线左右横跳,最后剩都在一边的时候就分类讨论取最大值就行了

时间复杂度 O(nlog⁡n)O(n\log n)

Code:\texttt{Code:}

C++
#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define fir first
#define sec second
#define int long long 
using namespace std;
namespace IO{
	#define File(x,y) freopen(#x,"r",stdin),freopen(#y,"w",stdout)
	#define fill(a,b,c,d) memset(a,b,sizeof(c)*(d+1))
	#define fillall(x,y) memset(x,y,sizeof(x))
	inline int read(){
		int w=0,f=1; char ch=getchar();
		while(ch<'0'||ch>'9'){if(ch=='-') f=-1; ch=getchar();}
		while(ch>='0'&&ch<='9'){w=(w<<1)+(w<<3)+(ch^48); ch=getchar();}
		return w*f;
	}
}
using namespace IO;
namespace CL{

	const int maxn=1e5+5;

	int n,w;
	int a[maxn];
	multiset<int> s;
	inline int solve(bool flag){
		int lst=w,res=0; s.clear();
		for(int i=1;i<=n;i++) s.insert(a[i]);
		while(s.size()){
			int now=flag?*--s.end():*s.begin();
			if((flag && now<w) || (!flag && now>w)) break;
			s.erase(s.find(now));
			res+=abs(lst-now);
			lst=now,flag^=1;
		}
		while(s.size()){
			int now=flag?*--s.end():*s.begin();
			s.erase(s.find(now));
			res+=max(abs(w-now),abs(lst-now));
			lst=now,flag^=1;
		}
		return res;
	}

	inline int main(){
		File(a.in,a.out);
		n=read(),w=read();
		for(int i=1;i<=n;i++) a[i]=read();
		sort(a+1,a+1+n);
		printf("%lld %lld\n",max(0ll,w-a[1])+max(0ll,a[n]-w),max(solve(0),solve(1)));
		return 0;
	}
}
signed main(){return CL::main();}

B.保险箱

若密码为 c1…wc_{1\dots w},则 ∀t,q0…w∈Z\forall t,q_{0\dots w}\in Z 有:

tn+q0mk+q1c1+q2c2+…qwcw≠mitn+q_0m_k+q_1c_1+q_2c_2+\dots q_wc_w\ne m_i

由裴蜀定理可知,左式的最小整数值为 gcd⁡(n,mk,c1,c2,…,cw)\gcd(n,m_k,c_1,c_2,\dots,c_w),设其为 gg

也就是说要有 g∤mi(i<k)g\nmid m_i(i<k)

又因为 n,mkn,m_k 为定值,那么可得 g∣gcd⁡(n,mk)g\mid\gcd(n,m_k) 且若 gg 一定那么 w=ngw=\frac{n}{g}

那么直接枚举 gcd⁡(n,mk)\gcd(n,m_k) 的因子,判断是否整除 mim_i 即可

时间复杂度 O(d(gcd⁡(n,mk))×k)O(d(\gcd(n,m_k))\times k)

Code:\texttt{Code:}

C++
#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define fir first
#define sec second
#define int long long 
using namespace std;
namespace IO{
	#define File(x,y) freopen(#x,"r",stdin),freopen(#y,"w",stdout)
	#define fill(a,b,c,d) memset(a,b,sizeof(c)*(d+1))
	#define fillall(x,y) memset(x,y,sizeof(x))
	inline int read(){
		int w=0,f=1; char ch=getchar();
		while(ch<'0'||ch>'9'){if(ch=='-') f=-1; ch=getchar();}
		while(ch>='0'&&ch<='9'){w=(w<<1)+(w<<3)+(ch^48); ch=getchar();}
		return w*f;
	}
}
using namespace IO;
namespace CL{

	const int maxn=2.5e5+5;

	int n,K,tot;
	int a[maxn];
	inline int gcd(int a,int b){return !b?a:gcd(b,a%b);}
	inline bool check(int x){
		for(int i=tot;i>=1;i--){
			if(a[i]%x==0) return 0;
			if(a[i]<x) return 1;
		}
		return 1;
	}

	inline int main(){
		File(b.in,b.out);
		n=read(),K=read();
		for(int i=1;i<=K;i++) a[i]=read();
		int g=gcd(n,a[K]);
		sort(a+1,a+K);
		tot=unique(a+1,a+K)-a-1;
		for(int i=1;i*i<=g;i++) if(g%i==0 && check(i)) return printf("%lld\n",n/i),0;
		for(int i=sqrt(g);i>=1;i--) if(g%i==0 && check(g/i)) return printf("%lld\n",n/(g/i)),0;
		return 0;
	}
}
signed main(){return CL::main();}

C.追逐

原题,见 「考试总结」2021.7.20

Code:\texttt{Code:}

C++
#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define fir first
#define sec second
#define int long long 
using namespace std;
namespace IO{
	#define File(x,y) freopen(#x,"r",stdin),freopen(#y,"w",stdout)
	#define fill(a,b,c,d) memset(a,b,sizeof(c)*(d+1))
	#define fillall(x,y) memset(x,y,sizeof(x))
	inline int read(){
		int w=0,f=1; char ch=getchar();
		while(ch<'0'||ch>'9'){if(ch=='-') f=-1; ch=getchar();}
		while(ch>='0'&&ch<='9'){w=(w<<1)+(w<<3)+(ch^48); ch=getchar();}
		return w*f;
	}
}
using namespace IO;
namespace CL{
	
	const int maxn=1e5+5,maxm=105;
	
	int n,m,ans;
	int val[maxn],sum[maxn],stk[maxn],f[maxn][maxm],g[maxn][maxm];
	namespace Graph{
		int head[maxn],id;
		struct e{int v,next;}edge[maxn<<1];
		inline void add(int u,int v){
			edge[++id]=(e){v,head[u]};
			head[u]=id;
		}
	}using namespace Graph;
	void dfs(int u,int fa){
		for(int i=1;i<=m;i++) f[u][i]=sum[u],g[u][i]=sum[u]-val[fa];
		for(int i=head[u];i;i=edge[i].next){
			int v=edge[i].v;
			if(v==fa) continue;
			dfs(v,u);
			for(int j=0;j<=m;j++) ans=max(ans,f[u][j]+g[v][m-j]);
			for(int j=1;j<=m;j++){
				f[u][j]=max(f[u][j],max(f[v][j],f[v][j-1]+sum[u]-val[v]));
				g[u][j]=max(g[u][j],max(g[v][j],g[v][j-1]+sum[u]-val[fa]));
			}
		}
		for(int i=1;i<=m;i++) f[u][i]=sum[u],g[u][i]=sum[u]-val[fa];
		int top=0;
		for(int i=head[u];i;i=edge[i].next){
			int v=edge[i].v;
			if(v==fa) continue;
			stk[++top]=v;
		}
		while(top){
			int v=stk[top--];
			for(int j=0;j<=m;j++) ans=max(ans,f[u][j]+g[v][m-j]);
			for(int j=1;j<=m;j++){
				f[u][j]=max(f[u][j],max(f[v][j],f[v][j-1]+sum[u]-val[v]));
				g[u][j]=max(g[u][j],max(g[v][j],g[v][j-1]+sum[u]-val[fa]));
			}
		}
	}	
	
	inline int main(){
		File(c.in,c.out);
		n=read(); m=read();
		for(int i=1;i<=n;i++) val[i]=read();
		for(int i=1;i<n;i++){
			int x=read(),y=read();
			add(x,y),add(y,x);
			sum[x]+=val[y],sum[y]+=val[x];
		}
		dfs(1,0);
		printf("%lld\n",ans);														
		return 0;
	}
}
signed main(){return CL::main();}

D.字符串

Code:\texttt{Code:}

C++
#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define fir first
#define sec second
using namespace std;
namespace IO{
	#define File(x,y) freopen(#x,"r",stdin),freopen(#y,"w",stdout)
	#define fill(a,b,c,d) memset(a,b,sizeof(c)*(d+1))
	#define fillall(x,y) memset(x,y,sizeof(x))
	inline int read(){
		int w=0,f=1; char ch=getchar();
		while(ch<'0'||ch>'9'){if(ch=='-') f=-1; ch=getchar();}
		while(ch>='0'&&ch<='9'){w=(w<<1)+(w<<3)+(ch^48); ch=getchar();}
		return w*f;
	}
}
using namespace IO;
namespace CL{

	const int maxn=5e5+5;

	int n,q;
	int a[maxn];
	char s[maxn];
	namespace SegmentTree{
		#define lson p<<1
   		#define rson p<<1|1
		struct node{
			int sum,maxx,pre,suf;
			node(){sum=maxx=pre=suf=0;}
			node(int _sum,int _maxx,int _pre,int _suf){sum=_sum,maxx=_maxx,pre=_pre,suf=_suf;}
			inline node operator +(const node &x)const{
				node res;
				res.sum=sum+x.sum;
				res.maxx=max(max(maxx,x.maxx),suf+x.pre);
				res.pre=max(pre,sum+x.pre);
				res.suf=max(x.suf,suf+x.sum);
				return res;
			}
		}t[maxn<<2];
		inline void pushup(int p){t[p]=t[lson]+t[rson];}
		inline void build(int p,int l,int r){
			if(l==r) return t[p]=node(a[l],a[l],a[l],a[l]),void();
			int mid=(l+r)>>1;
			build(lson,l,mid); build(rson,mid+1,r);
			pushup(p);
		}
		inline node query(int p,int l,int r,int tl,int tr){
			if(tl<=l && r<=tr) return t[p];
			int mid=(l+r)>>1;
			if(tl<=mid && tr>mid) return query(lson,l,mid,tl,tr)+query(rson,mid+1,r,tl,tr);
			if(tl<=mid) return query(lson,l,mid,tl,tr);
			if(tr>mid) return query(rson,mid+1,r,tl,tr);
		}
	}using namespace SegmentTree;

	inline int main(){
		File(d.in,d.out);
		n=read(),scanf("%s",s+1),q=read();
		for(int i=1;i<=n;i++) a[i]=s[i]=='C'?1:-1;
		build(1,1,n);
		while(q--){
			int l=read(),r=read();
			node res=query(1,1,n,l,r);
			printf("%d\n",max(0,res.maxx)-res.sum);
		}
		return 0;
	}
}
signed main(){return CL::main();}
原文