【2020省选模拟】题解
T1:
傻逼般的把暴力写挂了
考虑最左右的两个对称点一定在以内
暴力枚举后即可确定对称中心
双指针判断有多少对对称点满足即可
#include<bits/stdc++.h>
using namespace std;
#define cs const
#define re register
#define pb push_back
#define pii pair<int,int>
#define ll long long
#define fi first
#define se second
#define bg begin
cs int RLEN=1<<20|1;
inline char gc(){
static char ibuf[RLEN],*ib,*ob;
(ib==ob)&&(ob=(ib=ibuf)+fread(ibuf,1,RLEN,stdin));
return (ib==ob)?EOF:*ib++;
}
inline int read(){
char ch=gc();
int res=0;bool f=1;
while(!isdigit(ch))f^=ch=='-',ch=gc();
while(isdigit(ch))res=(res+(res<<2)<<1)+(ch^48),ch=gc();
return f?res:-res;
}
template<class tp>inline void chemx(tp &a,tp b){a<b?a=b:0;}
template<class tp>inline void chemn(tp &a,tp b){a>b?a=b:0;}
cs int N=100005;
char xxx;
int cnt,n,k,ans;
pii p[N];
inline pii operator +(cs pii &a,cs pii &b){
return pii(a.fi+b.fi,a.se+b.se);
}
map<pii,int>vt;
inline void calc(pii now){
if(vt[now])return;
vt[now]=1;
int l=1,r=n,cnt=0;
while(l<=r){
if(p[l]+p[r]==now)l++,r--;
else{
cnt++;
if(p[l]+p[r]<now)l++;
else r--;
}
}
if(cnt<=k)ans++;
}
char yyy;
int main(){
n=read(),k=read();
if(k>=n)return puts("-1"),0;
for(int i=1;i<=n;i++)p[i].fi=read(),p[i].se=read();
sort(p+1,p+n+1);
for(int i=1;i<=k+1;i++)
for(int j=1;j<=k+1;j++)
calc(p[i]+p[n-j+1]);
cout<<ans<<'\n';
return 0;
}
T2:
先求得次后某一边被吹掉块的概率
显然有一个用前后缀和优化的
即记表示当前行保留第个块的概率
考虑如果用
发现把贡献拆开可以维护等一堆前缀和得到
复杂度
#include<bits/stdc++.h>
using namespace std;
#define cs const
#define re register
#define pb push_back
#define pii pair<int,int>
#define ll long long
#define fi first
#define se second
#define bg begin
cs int RLEN=1<<20|1;
inline char gc(){
static char ibuf[RLEN],*ib,*ob;
(ib==ob)&&(ob=(ib=ibuf)+fread(ibuf,1,RLEN,stdin));
return (ib==ob)?EOF:*ib++;
}
inline int read(){
char ch=gc();
int res=0;bool f=1;
while(!isdigit(ch))f^=ch=='-',ch=gc();
while(isdigit(ch))res=(res+(res<<2)<<1)+(ch^48),ch=gc();
return f?res:-res;
}
template<class tp>inline void chemx(tp &a,tp b){a<b?a=b:0;}
template<class tp>inline void chemn(tp &a,tp b){a>b?a=b:0;}
cs int mod=1e9+7;
inline int add(int a,int b){return (a+=b)>=mod?(a-mod):a;}
inline int dec(int a,int b){a-=b;return a+(a>>31&mod);}
inline int mul(int a,int b){static ll r;r=1ll*a*b;return (r>=mod)?(r%mod):r;}
inline void Add(int &a,int b){(a+=b)>=mod?(a-=mod):0;}
inline void Dec(int &a,int b){a-=b,a+=a>>31&mod;}
inline void Mul(int &a,int b){static ll r;r=1ll*a*b;a=(r>=mod)?(r%mod):r;}
inline int ksm(int a,int b,int res=1){for(;b;b>>=1,Mul(a,a))(b&1)&&(Mul(res,a),1);return res;}
inline int Inv(int x){return ksm(x,mod-2);}
inline int fix(int x){return (x<0)?x+mod:x;}
cs int N=1505,M=100005;
int fac[M],ifac[M];
inline void init_inv(cs int len=M-5){
fac[0]=ifac[0]=1;
for(int i=1;i<=len;i++)fac[i]=mul(fac[i-1],i);
ifac[len]=Inv(fac[len]);
for(int i=len-1;i;i--)ifac[i]=mul(ifac[i+1],i+1);
}
inline int C(int n,int m){return n<m?0:mul(fac[n],mul(ifac[m],ifac[n-m]));}
char xx;
int f[N],pf[N],n,m,p,q,k,pw1[M],pw2[M];
int s1[N],s2[N],pre[N],suf[N],spre[N],ssuf[N];
char yy;
int main(){
n=read(),m=read(),p=read(),q=read();
p=mul(p,Inv(q)),q=dec(1,p);
k=read(),pw1[0]=pw2[0]=1;
init_inv();
for(int i=1;i<=k;i++)pw1[i]=mul(pw1[i-1],p),pw2[i]=mul(pw2[i-1],q);
for(int i=0;i<=k;i++)Add(f[min(i,m)],mul(C(k,i),mul(pw1[i],pw2[k-i])));
pf[0]=f[0];
for(int i=1;i<=m;i++)pf[i]=add(pf[i-1],f[i]);
s1[m]=1,s2[1]=1;
int ss=0;
for(int i=1;i<=n;i++){
for(int k=1;k<=m;k++)
pre[k]=s1[k];
for(int j=1;j<=m;j++)
suf[j]=s2[j];
for(int j=1;j<=m;j++)Add(pre[j],pre[j-1]),spre[j]=add(spre[j-1],mul(pre[j],f[j]));
for(int j=m;j>=1;j--)Add(suf[j],suf[j+1]),ssuf[j]=add(ssuf[j+1],mul(suf[j],f[m-j+1]));
ss=pre[m];
for(int k=1;k<=m;k++){
int ret=mul(ss,pf[k-1]);Dec(ret,mul(suf[k+1],pf[k-1]));Dec(ret,spre[k-1]);
s1[k]=mul(ret,f[m-k]);
}
for(int k=1;k<=m;k++){
int now=mul(ss,pf[m-k]);Dec(now,mul(pre[k-1],pf[m-k])),Dec(now,ssuf[k+1]);
s2[k]=mul(now,f[k-1]);
}
}
int ret=0;
for(int k=1;k<=m;k++)Add(ret,s1[k]);
cout<<ret<<'\n';
return 0;
}
T3:
考虑先对第一种边求出最小生成树
从小到大枚举第二种边
找到换上最大的第一种边即可得到用哪种边的分界点
用维护即可
#include<bits/stdc++.h>
using namespace std;
#define cs const
#define re register
#define pb push_back
#define pii pair<int,int>
#define pil pair<int,ll>
#define ll long long
#define fi first
#define se second
#define bg begin
cs int RLEN=1<<20|1;
inline char gc(){
static char ibuf[RLEN],*ib,*ob;
(ib==ob)&&(ob=(ib=ibuf)+fread(ibuf,1,RLEN,stdin));
return (ib==ob)?EOF:*ib++;
}
inline int read(){
char ch=gc();
int res=0;bool f=1;
while(!isdigit(ch))f^=ch=='-',ch=gc();
while(isdigit(ch))res=(res+(res<<2)<<1)+(ch^48),ch=gc();
return f?res:-res;
}
template<class tp>inline void chemx(tp &a,tp b){a<b?a=b:0;}
template<class tp>inline void chemn(tp &a,tp b){a>b?a=b:0;}
cs int N=100005;
cs int inf=1e9+7;
namespace Lct{
cs int N=::N*5;
int son[N][2],fa[N],mx[N],mxpos[N],val[N],pos[N],rev[N];
#define lc(u) son[u][0]
#define rc(u) son[u][1]
inline void init(int u,int v,int p){
mxpos[u]=pos[u]=p,val[u]=mx[u]=v;
}
inline void pushup(int u){
mx[u]=val[u],mxpos[u]=pos[u];
if(lc(u)&&mx[lc(u)]>mx[u])mx[u]=mx[lc(u)],mxpos[u]=mxpos[lc(u)];
if(rc(u)&&mx[rc(u)]>mx[u])mx[u]=mx[rc(u)],mxpos[u]=mxpos[rc(u)];
}
inline void pushnow(int u){
swap(lc(u),rc(u)),rev[u]^=1;
}
inline void pushdown(int u){
if(!rev[u])return;
pushnow(lc(u)),pushnow(rc(u)),rev[u]=0;
}
inline bool isrc(int u){
return rc(fa[u])==u;
}
inline bool isrt(int u){
return !fa[u]||(lc(fa[u])!=u&&rc(fa[u])!=u);
}
inline void rotate(int v){
int u=fa[v],z=fa[u];
int t=isrc(v);
if(!isrt(u))son[z][isrc(u)]=v;
fa[v]=z;
son[u][t]=son[v][t^1];
fa[son[v][t^1]]=u;
son[v][t^1]=u,fa[u]=v;
pushup(u),pushup(v);
}
int stk[N],top;
inline void splay(int u){
stk[top=1]=u;
for(int v=u;!isrt(v);v=fa[v])stk[++top]=fa[v];
for(int i=top;i;i--)pushdown(stk[i]);
while(!isrt(u)){
if(!isrt(fa[u]))
isrc(fa[u])==isrc(u)?rotate(fa[u]):rotate(u);
rotate(u);
}
}
inline void access(int u){
for(int v=0;u;v=u,u=fa[u]){
splay(u);
rc(u)=v,pushup(u);
}
}
inline void makert(int u){
access(u),splay(u),pushnow(u);
}
inline void link(int u,int v){
access(u),splay(u),makert(v),fa[v]=u;
}
inline void cut(int u,int v){
makert(u),access(v),splay(v);
fa[lc(v)]=0,lc(v)=0,pushup(v);
}
inline int query(int u,int v){
makert(u),access(v),splay(v);
return mxpos[v];
}
}
int n,a,b,q;
struct edge{
int u,v,w;
friend inline bool operator <(cs edge &a,cs edge &b){
return a.w<b.w;
}
}e1[N*2],e2[N*2];
int fa[N],cnt;
ll upd[N];
ll s[N];
inline int find(int x){return fa[x]==x?x:fa[x]=find(fa[x]);}
inline void solve(){
for(int i=1;i<=n;i++)fa[i]=i;
for(int i=1;i<=a;i++){
int f1=find(e1[i].u),f2=find(e1[i].v);
if(f1!=f2){
fa[f1]=f2,s[0]+=e1[i].w;
Lct::link(e1[i].u,i+n),Lct::link(i+n,e1[i].v);
}
}
for(int i=1;i<=n;i++)fa[i]=i;
for(int i=1;i<=b;i++){
int u=e2[i].u,v=e2[i].v,f1=find(u),f2=find(v);
if(f1!=f2){
fa[f1]=f2;int pos=Lct::query(u,v);
upd[++cnt]=e2[i].w-e1[pos].w;
Lct::cut(e1[pos].u,pos+n),Lct::cut(e1[pos].v,pos+n);
Lct::link(u,a+n+i),Lct::link(v,a+n+i);
}
}
sort(upd+1,upd+cnt+1);
for(int i=1;i<=cnt;i++)s[i]=s[i-1]+upd[i];
while(q--){
int x=read(),pos=upper_bound(upd+1,upd+cnt+1,x*2)-upd-1;
cout<<s[pos]+1ll*(n-1-pos*2)*x<<'\n';
}
}
int main(){
#ifdef Stargazer
freopen("lx.in","r",stdin);
#endif
n=read(),a=read(),b=read(),q=read();
for(int i=1;i<=a;i++)e1[i].u=read(),e1[i].v=read(),e1[i].w=read();
for(int i=1;i<=b;i++)e2[i].u=read(),e2[i].v=read(),e2[i].w=read();
sort(e1+1,e1+a+1),sort(e2+1,e2+b+1);
for(int i=1;i<=n;i++)Lct::init(i,-inf,0);
for(int i=1;i<=a;i++)Lct::init(i+n,e1[i].w,i);
for(int i=1;i<=b;i++)Lct::init(n+a+i,-inf,0);
solve();return 0;
}