Codeforces Round #627 (Div. 3)

Topic links: https://codeforces.com/contest/1324

A - Yet Another Tetris Problem

Tetris This question can not actually rotate?

Does not know the algorithm, if that is the subject can place 1 * 2 * 2, or two kinds of block 1, then, always first put series 1 * 2 so that the last remaining full ai <= 1. If this time are all 0 or all 1 is YES, or if there is an odd number between two adjacent 0 1 (1 is the wall), it must be NO. Because either all the grid has been eliminated, either eliminate the last time this line must be 00111 or 10011 and the like, an even number of consecutive zeros of the situation. However, all attempts to position 0 1 2 on end, so that 0 and 1 will be inverted state, there may be a continuum of parity inverting. For example: 0110, 1001 Flip become.

Or for such problems with the old routine checkered dyeing, defined the lower left corner of the grid is white, when there are an even number of columns, each time to eliminate the same number of black and white plaid, so only when the number of black and white plaid all the same, only eliminate all possible (since the introduction of the new grid will not change the number of poor black and white). At the same time easy to know the number of black and white plaid all the same, all will be able to eliminate, first with the above ideas to all the low grid +2, then all disappear, the last remaining sporadic grid row, then the row adjacent black and white grid, can not even fill up the middle by lying grid, and finally there may be two remaining corner of the grid. Then make all the +2 state flip, do it all at once eliminated. When there is an odd columns alternately a plurality delete or multiple delete a white cell Haig. Prerequisite therefore a total elimination of the difference is the number of checkered <= 1. If the number of black and white squares are equal, all may be in the form of +2 such that the number of black and white squares of a difference. Then with the adjacent lying between black and white lattice grid connected together.

If it can be vertically or sideways, it is necessary and sufficient condition is removed: n = even number: equal black and white, n-odd, black and white difference of <= 1.

But if the subject is the meaning of words to determine what parity on it.

B - Yet Another Palindrome Problem

C - Frog Jumps

The meaning of problems: there is a frog, a jump start right only, [1, n] have some character 'L' or 'R'. A frog can begin to determine a maximum jump distance d, then he can only go in this direction jump [0, d] step (not cross-border) grid in what the characters. Seeking the minimum skip d n + 1 need.

Solution: you can find those 'L' is not useful (do not need to enter 'L' grid, you can go to its left 'R' grid, while the left 'R' grid nothing more useful), every time to go 'R' above to move forward, so look for the longest distance between two 'R'. The default is the first and last 'R'.

Why do these bells and whistles.

int n;
char s[200005];

void test_case() {
    scanf("%s", s + 1);
    n = strlen(s + 1);
    int ans = 0, prev = 0;
    for(int i = 1; i <= n; ++i) {
        if(s[i] == 'R') {
            ans = max(ans, i - prev);
            prev = i;
        }
    }
    ans = max(ans, n + 1 - prev);
    printf("%d\n", ans);
}

*D - Pair of Topics

The meaning of problems: How many of the requirements (i, j) (i <j), satisfy ai + aj> bi + bj.

Solution: transposing obtained AI-BI> BJ-AJ , i.e. BI-AI <AJ-BJ , note that the form is not the same on both sides, is not a problem of seeking to reverse, but the processing method is the same. That is, queries are ai-bi-1, inserted into the Fenwick tree is bi-ai. Because it is less than the number, so the tree with the array fails to build.

int n;
int a[200005];
int b[200005];
int x[400005], xtop;
int bit[400005];
 
void add(int x, int v) {
    for(; x <= xtop; x += x & (-x))
        bit[x] += v;
}
 
ll sum(int x) {
    ll res = 0;
    for(; x; x -= x & (-x))
        res += bit[x];
    return res;
}
 
void test_case() {
    scanf("%d", &n);
    for(int i = 1; i <= n; ++i)
        scanf("%d", &a[i]);
    xtop = 0;
    for(int i = 1; i <= n; ++i) {
        scanf("%d", &b[i]);
        x[++xtop] = a[i] - b[i] - 1;
        x[++xtop] = b[i] - a[i];
    }
    sort(x + 1, x + 1 + xtop);
    xtop = unique(x + 1, x + 1 + xtop) - (x + 1);
    ll ans = 0;
    for(int i = 1; i <= n; ++i) {
        int pos1 = lower_bound(x + 1, x + 1 + xtop, a[i] - b[i] - 1) - x;
        ans += sum(pos1);
        int pos2 = lower_bound(x + 1, x + 1 + xtop, b[i] - a[i]) - x;
        add(pos2, 1);
    }
    printf("%lld\n", ans);
}

Harvest: To move items carefully, do not take it for granted.

*E - Sleeping Schedule

Meaning of the questions: To sleep n times, before the i-th sleep, when the first time i-1 can be controlled just woke up after sleeping or after hours ai ai-1 hour sleep. H hours a day. Start time to sleep is in a normal rest [l, r] in. Seeking to get to sleep n times how many times the normal daily routine.

To see that this is a silly b of dp. Set times before i go to bed, sleep for the time after the end of j, then every time just to sleep because the day just do not need to add go. It seems as ai> = 1 + h so the fact that anti-negative is not necessary.

int a[2005];
int dp[2005][2005];

void test_case() {
    int n, h, l, r;
    scanf("%d%d%d%d", &n, &h, &l, &r);
    for(int i = 1; i <= n; ++i)
        scanf("%d", &a[i]);
    memset(dp, -INF, sizeof(dp));
    dp[0][0] = 0;
    for(int i = 1; i <= n; ++i) {
        for(int j = 0, tj; j < h; ++j) {
            tj = (j + a[i]) % h;
            dp[i][tj] = max(dp[i][tj], dp[i - 1][j] + (l <= tj && tj <= r));
            tj = (j + a[i] - 1 + h) % h;
            dp[i][tj] = max(dp[i][tj], dp[i - 1][j] + (l <= tj && tj <= r));
        }
    }
    int ans = 0;
    for(int j = 0; j < h; ++j)
        ans = max(ans, dp[n][j]);
    printf("%d\n", ans);
}

F - Maximum White Subtree

Question is intended: to a tree, the tree has a number of white dots a black dot, for each point, the communication block comprises a selected point, such that the number of the white point than the maximum value of the number of black dots. Find the best value.

Solution: looks like a tree in the form of dp, dp set [u] [v] as the "current node u to the root of the whole tree outside of the other side direction v is removed (i.e. removal of the outer edge of subtrees v in), the largest cntw-cntb value. "

Obviously this thing can change the root method scans the secondary transfer, in particular:

1 is first referred to as white, black denoted -1.

First choose a root, such as 1, once dfs, dfs in this, the transfer from the leaves to the roots, array processing denoted by dp.

After only v while removing leaves no other side, so that only a \ (dp [U] [the p-] = Color [U] \) .

Otherwise, there must subtree must \ (DP [U] [P] = Color [U] + \ sum_ {V \} NEQ P max (0, DP [V] [U]) \) , to remove a child information tree to get information on his father's side, temporarily can not be transferred.

Then once dfs, dfs in this, the transfer from the roots to the leaves, array processing is still dp.

For the root, its father does not exist, then do not go to a subtree of v0 entire subtree containing the root of u is equivalent to \ (dp [u] [v0 ] = color [u] + \ sum_ {v \ neq max} V0 (0, DP [V] [U]) \) , this latter equation can be deformed, becomes \ (dp [u] [v0 ] = color [u] + \ sum_ {v} max (0 , DP [V] [U]) - max (0, DP [V0] [U]) \) .

For non-root is, there \ (dp [u] [v0 ] = color [u] + \ sum_ {v \ neq p} max (0, dp [v] [u]) - max (0, dp [v0 ] [U]) + max (0, DP [P] [U]) \) .

Then for a point u answer is \ (dp [u] [the p-] + max (0, dp [the p-] [u]) \) .

A closer look at the above formula can also merge? It does not require special handling leaves and roots.

int color[200005];
vector<int> G[200005];
map<int, int> dp[200005];

void dfs1(int u, int p) {
    int sum = color[u];
    for(auto &v : G[u]) {
        if(v == p)
            continue;
        dfs1(v, u);
        sum += max(0, dp[v][u]);
    }
    dp[u][p] = sum;
}

int ans[200005];

void dfs2(int u, int p) {
    int sum = color[u];
    for(auto &v : G[u])
        sum += max(0, dp[v][u]);
    for(auto &v : G[u])
        dp[u][v] = sum - max(0, dp[v][u]);
    for(auto &v : G[u]) {
        if(v == p)
            continue;
        dfs2(v, u);
    }
    ans[u] = dp[u][p] + max(0, dp[p][u]);
}

void test_case() {
    int n;
    scanf("%d", &n);
    for(int i = 1; i <= n; ++i) {
        scanf("%d", &color[i]);
        if(color[i] == 0)
            color[i] = -1;
    }
    for(int i = 1; i <= n - 1; ++i) {
        int u, v;
        scanf("%d%d", &u, &v);
        G[u].push_back(v);
        G[v].push_back(u);
    }
    dfs1(1, 0);
    dfs2(1, 0);
    for(int i = 1; i <= n; ++i)
        printf("%d%c", ans[i], " \n"[i == n]);
}

The above is a version of the map, mainly in dfs2 time to create dp [u] [v] and the use of his father's dp [p] [u], a sudden whim this fact can be directly downstream.

int color[200005];
vector<int> G[200005];
int dp[200005];

void dfs1(int u, int p) {
    int sum = color[u];
    for(auto &v : G[u]) {
        if(v == p)
            continue;
        dfs1(v, u);
        sum += max(0, dp[v]);
    }
    dp[u] = sum;
}

int ans[200005];

void dfs2(int u, int p, int dppu) {
    int sum = color[u] + max(0, dppu);
    for(auto &v : G[u]) {
        if(v == p)
            continue;
        sum += max(0, dp[v]);
    }
    for(auto &v : G[u]) {
        if(v == p)
            continue;
        dfs2(v, u, sum - max(0, dp[v]));
    }
    ans[u] = dp[u] + max(0, dppu);
}

void test_case() {
    int n;
    scanf("%d", &n);
    for(int i = 1; i <= n; ++i) {
        scanf("%d", &color[i]);
        if(color[i] == 0)
            color[i] = -1;
    }
    for(int i = 1; i <= n - 1; ++i) {
        int u, v;
        scanf("%d%d", &u, &v);
        G[u].push_back(v);
        G[v].push_back(u);
    }
    dfs1(1, 0);
    dfs2(1, 0, 0);
    for(int i = 1; i <= n; ++i)
        printf("%d%c", ans[i], " \n"[i == n]);
}

How many did not fast, but becomes complicated.

Tip: Requirements for each point in the tree to find a root information, most likely the secondary scanning transducer tree root method dp.

Guess you like

Origin www.cnblogs.com/KisekiPurin2019/p/12484164.html