Codeforces Round #781 (Div. 2)

· · 个人记录

Codeforces Round #781 (Div. 2)

A. GCD vs LCM

$1\le t\le 10^4$,$4\le n\le 10^9$。

取 a=n-3,b=1,c=1,d=1 即可满足要求。

时间复杂度:O(t)。

#include<bits/stdc++.h>
using namespace std;
int t,n;
int main()
{
    cin>>t;
    while(t--)
    {
        cin>>n;
        cout<<n-3<<" 1 1 1"<<endl;
    }
    return 0;
}

B. Array Cloning Technique

一次操作中,可以新增任意一个已存在的序列,或者交换所有元素中的任意两个元素。 $1\le t\le 10^4$,$1\le \sum n\le 10^5$,$-10^9\le a_i\le 10^9$。

显然只需考虑出现次数最多的元素,分别计算两种操作的次数再相加即可。

时间复杂度:O(\sum n)。

#include<bits/stdc++.h>
using namespace std;
const int N=1e5+5;
int t,n;
int a[N];
map<int,int> mp;
int main()
{
    cin>>t;
    while(t--)
    {
        int mx=0,cnt=0;
        long long mul;
        cin>>n;
        for(int i=1;i<=n;i++) cin>>a[i],mp[a[i]]++;
        for(int i=1;i<=n;i++) mx=max(mx,mp[a[i]]),mp[a[i]]=0;
        mul=mx;
        while(mul<n) mul*=2,cnt++;
        cout<<cnt+n-mx<<endl;
    }
    return 0;
}

C. Tree Infection

每一秒内,会先后进行两个操作: - 对于至少有一个子结点为红色的结点,可染红它的任意一个子结点。 - 任选一个结点将其染红。 $1\le t\le 10^4$,$2\le \sum n\le 2\times 10^5$,$1\le p_i\le n$。

显然每个结点的子结点至少要染一个,应该贪心地从子结点数量多的往少的染,若还有剩余,再用优先队列维护剩余子结点个数,每一秒贪心地染最多的,直到全部染完为止。

时间复杂度:O(\sum n\log n)。

#include<bits/stdc++.h>
using namespace std;
const int N=2e5+5;
int t,n;
int deg[N];
priority_queue<int> q;
bool cmp(int x,int y)
{
    return y<x;
}
int main()
{
    cin>>t;
    while(t--)
    {
        int sum=0,m=0;
        cin>>n;
        for(int i=2;i<=n;i++)
        {
            int x;
            cin>>x;
            deg[x]++;
        }
        sort(deg+1,deg+n+1,cmp);
        for(m=n;!deg[m];m--);
        deg[++m]=1;
        for(int i=1;i<=m;i++)
        {
            deg[i]-=m-i+1;
            if(deg[i]) q.push(deg[i]);
        }
        if(!q.empty())
        {
            while(q.top()>sum)
            {
                int x=q.top();
                q.pop();
                q.push(x-1);
                sum++;
            }
        }
        cout<<m+sum<<endl;
        while(q.size()) q.pop();
        for(int i=1;i<=n;i++) deg[i]=0;
    }
    return 0;
}

D. GCD Guess

每次询问中,询问 $(a,b)$,则会返回 $\gcd(x+a,x+b)$ 的值。 $1\le t\le 1000$,$1\le x\le 10^9$,$1\le a,b\le 2\times 10^9$,$a\neq b$。

容易发现 2^{30}>10^9,因此考虑二进制下每一位的值。

设 x\bmod 2^{k}=s,只需判断 \gcd(x-s+2^k,2^{k+1}) 是否为 2^{k+1} 即可确定 x\bmod 2^{k+1} 的值,直至 k=29,便能知道 x 的值。

进行变形,\gcd(x-s+2^k,2^{k+1})=\gcd(x+2^k-s,x+2^{k+1}+2^k-s),取 a=2^k-s,b=2^{k+1}+2^k-s 最大值为 2^{29+1}+2^{29}-0<2\times 10^9,因此符合要求。

时间复杂度:O(t)。

#include<bits/stdc++.h>
using namespace std;
int t,x,ans;
int main()
{
    cin>>t;
    while(t--)
    {
        ans=0;
        for(int i=0;i<=29;i++)
        {
            cout<<"? "<<(1<<i)-ans<<" "<<(1<<(i+1))+(1<<i)-ans<<endl;
            cout.flush();
            cin>>x;
            if(x>>(i+1)) ans+=(1<<i);
        }
        cout<<"! "<<ans<<endl;
    }
    return 0;
}

E. MinimizOR

定义 $f(l,r)=\min_{l\le i<j\le r}a_i|a_j$。 $1\le t\le 10^4$,$2\le \sum n\le 10^5$,$1\le \sum q\le 10^5$,$0\le a_i<2^{30}$,$1\le l_j<r_j\le n$。

结论:若 \{a\} 是长度为 m,值域为 [0,2^k) 的单调不降序列, 则 f(1,m)=f(1,\min\{m,k+1\})。

证明:使用数学归纳法。

当 k=1 时,显然成立。

当 k\ge 2 时,设 s=\max_{a_i<2^{k-1}}i,分情况讨论。

用线段树维护区间最小值,求出最小的 \min\{r_j-l_j+1,31\} 个数两两计算答案,更新答案即可。

设 V=\max_{1\le i\le n}a_i。

时间复杂度:O(\sum (n+q\log V)\log n+q\log^2V)。

#include<bits/stdc++.h>
using namespace std;
const int N=1e5+5;
int t,n,q;
int a[N],stk[32],p,ans;
struct Seg
{
    int l,r;
    int mn,id;
}tree[N*4];
void pushup(int p)
{
    if(tree[p*2].mn<tree[p*2+1].mn) tree[p].id=tree[p*2].id;
    else tree[p].id=tree[p*2+1].id;
    tree[p].mn=min(tree[p*2].mn,tree[p*2+1].mn);
}
void build(int p,int l,int r)
{
    tree[p].l=l;
    tree[p].r=r;
    if(l==r)
    {
        tree[p].mn=a[l];
        tree[p].id=l;
        return;
    }
    int mid=(l+r)/2;
    build(p*2,l,mid);
    build(p*2+1,mid+1,r);
    pushup(p);
}
pair<int,int> ask(int p,int l,int r)
{
    if(tree[p].l>=l&&tree[p].r<=r) return make_pair(tree[p].id,tree[p].mn);
    int mid=(tree[p].l+tree[p].r)/2;
    pair<int,int> res=make_pair(0,INT_MAX),x;
    if(l<=mid)
    {
        x=ask(p*2,l,r);
        if(res.second>x.second) res=x;
    }
    if(r>=mid+1)
    {
        x=ask(p*2+1,l,r);
        if(res.second>x.second) res=x;
    }
    return res;
}
void modify(int p,int x,int d)
{
    if(tree[p].l==tree[p].r)
    {
        tree[p].mn=d;
        return;
    }
    int mid=(tree[p].l+tree[p].r)/2;
    if(x<=mid) modify(p*2,x,d);
    if(x>=mid+1) modify(p*2+1,x,d);
    pushup(p);
    return;
}
int main()
{
    cin>>t;
    while(t--)
    {
        cin>>n;
        for(int i=1;i<=n;i++) cin>>a[i];
        build(1,1,n);
        cin>>q;
        while(q--)
        {
            ans=INT_MAX;
            int l,r,cnt=31;
            pair<int,int> x;
            cin>>l>>r;
            cnt=min(cnt,r-l+1);
            for(int i=1;i<=cnt;i++)
            {
                x=ask(1,l,r);
                stk[++p]=x.first;
                modify(1,x.first,INT_MAX);
                for(int j=1;j<=i-1;j++) ans=min(ans,a[stk[i]]|a[stk[j]]);
            }
            while(p) modify(1,stk[p],a[stk[p]]),p--;
            cout<<ans<<endl;
        }
    }
    return 0;
}