Codeforces Round # 627 (Div. 3) summary

nonsense

Life first real-time CF, felt his hands slow brain more slowly ......

E questions face looked a long time before understand the question, sub-theme of the tree can not see the definition of F

Summary

Question number Title Description Thinking
A slightly If and only if all elements of the same parity
B Given a sequence, and asked if the length \ (\ geq 3 \) a palindromic sequence As long as there are two intermediate elements have the same element length \ (\ geq 3 \) a palindromic sequence, then \ (O (n ^ 2) \) sweep it again
C slightly First, we can destroy all the lattice L, R only go lattice, then the answer is that all R grid (including \ (0, the n-1 + \) ) minimum spacing
D There \ (n-\) elements and each element has \ (a, b \) two kinds of weights. Seeking \ ((i, j) \ ) logarithm (disorder), such that \ (a_i + a_j> b_i + b_j \) Set \ (= a_i-C_i B_i \) , this order of sequence, and then scan it again with the double pointer. Specifically, for \ (I \) Maintenance \ (POS \) represents a \ - (> c [i] \) position of the element, contribute to answer \ (\ max (0, pos -i) \ ) when \ (i \) and \ (POS \) Meet when he quit
E Day \ (H \) hours to sleep \ (n-\) th, \ (I \) times can be determined in the last wake up \ (a_i \) h, or \ (a_i-1 \) h after falling asleep, waking up in time for the first time the origin \ (0 \) . Every time sleep takes \ (h \) hours. Maximize sleep start time \ ([l, r] \ ) number in the range of sleep. Set \ (f [i] [j ] \) represents the previous sleep \ (I \) th, \ (I \) when the time to sleep onset as \ (J \) , transferred to violence, complexity \ (O (n ^ 2) \)
F Given a \ (n-\) unrooted trees points, each node is black or white. For each point \ (I \) , you need to select a containing \ (I \) subtree, white difference between the maximum number of nodes and the nodes to maximize the black subtree, find the maximum value. Unicom subtree defines a tree block. \ (n \ leq 2 \ times 10 ^ 5 \) Select Point \ (1 \) is defined as a rooted tree root, provided \ (f [i] \) represents the tree rooted at \ (I \) subtree rooted in, comprising selecting a \ (I \ ) maximum difference Unicom blocks can be obtained, provided \ (g [i] \) represents removed in \ (I \) other parts of the subtree rooted in, and select a \ (I \) communication the communication block, the maximum difference that can be obtained, then obviously \ (RET [I] = F [I] + G [I] \) . Consider how to obtain \ (f [i] \) , we first add it \ (i \) own contribution, then the decision \ (i \) each rooted tree select whether the son can, of course, selecting a son means may select a communication block containing it, i.e. plus \ (F [J] \) . Consider how to obtain \ (G [I] \) , first of all there is the boundary condition \ (G [. 1] = 0 \) , consider the transfer of a top-down, every point on its parent calculation, such a father it all points to calculate the son of nodes, each node is either his son not to renew his father, or the continuation of his father, and may additionally choose a number of his father's other children subtrees

answer

A

slightly

Solution

If and only if all elements of the same parity

#include <bits/stdc++.h>
using namespace std;

#define int long long
const int N = 1000005;

void sh(int &x,int y) {x=max(x,y);}

int t,n,a[105];

signed main() {
    cin>>t;
    while(t--) {
        cin>>n;
        for(int i=1;i<=n;i++) cin>>a[i],a[i]&=1;
        int flag=1;
        for(int i=2;i<=n;i++) if(a[i]!=a[1]) flag=0;
        cout<<(flag?"YES":"NO")<<endl;
    }
}

B

Given a sequence, and asked if the length \ (\ geq 3 \) a palindromic sequence

Solution

As long as there are two intermediate elements have the same element length \ (\ geq 3 \) a palindromic sequence, then \ (O (n ^ 2) \) sweep it again

#include <bits/stdc++.h>
using namespace std;

#define int long long
const int N = 1000005;

int t,n,a[N];

signed main() {
    ios::sync_with_stdio(false);
    cin>>t;
    while(t--) {
        cin>>n;
        for(int i=1;i<=n;i++) cin>>a[i];
        int flag=0;
        for(int i=1;i<=n;i++) {
            for(int j=i+2;j<=n;j++) {
                if(a[i]==a[j]) flag=1;
            }
        }
        cout<<(flag?"YES":"NO")<<endl;
    }
}

C

slightly

Solution

First, we can destroy all the lattice L, R only go lattice, then the answer is that all R grid (including \ (0, the n-1 + \) ) minimum spacing

#include <bits/stdc++.h>
using namespace std;

const int N = 1000005;
int t,n,ans;
char str[N];

signed main() {
    ios::sync_with_stdio(false);
    cin>>t;
    while(t--) {
        cin>>str+1;
        n=strlen(str+1);
        int pos=0;
        ans=0;
        for(int i=1;i<=n;i++) {
            if(str[i]=='R') {
                ans=max(ans,i-pos);
                pos=i;
            }
        }
        ans=max(ans,n+1-pos);
        cout<<ans<<endl;
    }
}

D

There \ (n-\) elements and each element has \ (a, b \) two kinds of weights. Seeking \ ((i, j) \ ) logarithm (disorder), such that \ (a_i + a_j> b_i + b_j \)

Solution

Set \ (= a_i-C_i B_i \) , this order of sequence, and then scan it again with the double pointer. Specifically, for \ (I \) Maintenance \ (POS \) represents a \ - (> c [i] \) position of the element, contribute to answer \ (\ max (0, pos -i) \ ) when \ (i \) and \ (POS \) Meet when he quit

#include <bits/stdc++.h>
using namespace std;

#define int long long
const int N = 1000005;

int a[N],b[N],n,ans;

signed main() {
    cin>>n;
    for(int i=1;i<=n;i++) cin>>a[i];
    for(int i=1;i<=n;i++) cin>>b[i];
    for(int i=1;i<=n;i++) a[i]-=b[i];
    int pin=n;
    sort(a+1,a+n+1);
    reverse(a+1,a+n+1);
    for(int i=1;i<=n;i++) {
        while(a[i]+a[pin]<=0) --pin;
        ans+=max(0ll,pin-i);
    }
    cout<<ans;
}

E

Day \ (H \) hours to sleep \ (n-\) th, \ (I \) times can be determined in the last wake up \ (a_i \) h, or \ (a_i-1 \) h after falling asleep, waking up in time for the first time the origin \ (0 \) . Every time sleep takes \ (h \) hours. Maximize sleep start time \ ([l, r] \ ) number in the range of sleep.

Solution

Set \ (f [i] [j ] \) represents the previous sleep \ (I \) th, \ (I \) when the time to sleep onset as \ (J \) , transferred to violence, complexity \ (O (n ^ 2) \)

#include <bits/stdc++.h>
using namespace std;

int n,h,l,r,a[2005],f[2005][2005];

void sh(int &x,int y) {
    x=max(x,y);
}

signed main() {
    ios::sync_with_stdio(false);
    cin>>n>>h>>l>>r;
    for(int i=1;i<=n;i++) cin>>a[i];
    memset(f,-0x3f,sizeof f);
    f[0][0]=0;
    for(int i=1;i<=n;i++) {
        for(int j=0;j<h;j++) {
            sh(f[i][(j+a[i])%h], f[i-1][j]+((j+a[i])%h>=l&&((j+a[i])%h<=r)));
            sh(f[i][(j+a[i]-1)%h], f[i-1][j]+((j+a[i]-1)%h>=l&&((j+a[i]-1)%h<=r)));
        }
    }
    cout<<*max_element(f[n],f[n]+h);
}

F

Given a \ (n-\) unrooted trees points, each node is black or white. For each point \ (I \) , you need to select a containing \ (I \) subtree, white difference between the maximum number of nodes and the nodes to maximize the black subtree, find the maximum value. Unicom subtree defines a tree block. \ (n \ leq 2 \ times 10 ^ 5 \)

Solution

Select Point \ (1 \) is defined as a rooted tree root, provided \ (f [i] \) represents the tree rooted at \ (I \) subtree rooted in, comprising selecting a \ (I \ ) maximum difference Unicom blocks can be obtained, provided \ (g [i] \) represents removed in \ (I \) other parts of the subtree rooted in, and select a \ (I \) communication the communication block, the maximum difference that can be obtained, then obviously \ (ret [i] = f [i] + g [i] \)

Consider how to obtain \ (f [i] \) , we first add it \ (i \) own contribution, then the decision \ (i \) each rooted tree select whether the son can, of course, selecting a son means may select a communication block containing it, i.e. plus \ (f [j] \)

Consider how to obtain \ (G [I] \) , first of all there is the boundary condition \ (G [. 1] = 0 \) , consider the transfer of a top-down, every point on its parent calculation, such a father it all points to calculate the son of nodes, each node is either his son not to renew his father, or the continuation of his father, and may additionally choose a number of his father's other children subtrees

#include <bits/stdc++.h>
using namespace std;

const int N = 200005;

int n,f[N],h[N],a[N],b[N],w[N],fa[N],vis[N],sb,sw;
vector <int> g[N];

void dfs(int p) {
    vis[p]=1;
    if(a[p]==1) f[p]=1;
    else f[p]=-1;
    for(int q:g[p]) {
        if(vis[q]) continue;
        fa[q]=p;
        dfs(q);
        b[p]+=b[q];
        w[p]+=w[q];
        f[p]+=max(f[q],0);
    }
}

void dfs2(int p) {
    int sum=max(0,h[p]);
    if(a[p]) sum++;
    else sum--;
    for(int q:g[p]) if(q!=fa[p]) {
        sum+=max(0,f[q]);
    }
    for(int q:g[p]) if(q!=fa[p]) {
        h[q]=max(0,sum-max(f[q],0));
    }
    for(int q:g[p]) if(q!=fa[p]) dfs2(q);
}
signed main() {
    ios::sync_with_stdio(false);
    cin>>n;
    for(int i=1;i<=n;i++) cin>>a[i];
    for(int i=1;i<n;i++) {
        int t1,t2;
        cin>>t1>>t2;
        g[t1].push_back(t2);
        g[t2].push_back(t1);
    }
    dfs(1);
    dfs2(1);
    for(int i=1;i<=n;i++) {
        cout<<f[i]+h[i]<<" ";
    }
}

Guess you like

Origin www.cnblogs.com/mollnn/p/12484242.html