「考试总结」2021.11.17 多校联测

· · 个人记录

开题,发现 T1 不会,T2 不会,T3 原题,T4 不会

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

## [A.饥饿的狐狸](http://47.92.197.167:5283/contest/128/problem/1) 不难想到最小值就是顺着吃,可以 $O(1)$ 出解 而最大值贪心的策略是以 $w$ 为分界线左右横跳,最后剩都在一边的时候就分类讨论取最大值就行了 时间复杂度 $O(n\log n) \texttt{Code:}
#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.保险箱

若密码为 c_{1\dots w},则 \forall t,q_{0\dots w}\in Z 有:

tn+q_0m_k+q_1c_1+q_2c_2+\dots q_wc_w\ne m_i

由裴蜀定理可知,左式的最小整数值为 \gcd(n,m_k,c_1,c_2,\dots,c_w),设其为 g

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

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

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

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

\texttt{Code:}
#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

\texttt{Code:}
#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.字符串

\texttt{Code:}
#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();}