Codeforces Round #609

Long Beautiful Integer

Topic Link http://codeforces.com/contest/1268/problem/A

The meaning of problems

A given n-bit digital x, seeking> = beauty its minimum numbers.
Beautiful numbers defined as:
For a given k, i-th digit has \ (b_i = b_ {i + k} \).

answer

Be seen, the required number is repeated every k bits, equal to the high-bit high-k x k bits, it is determined whether or greater to meet the requirements and can be increased if k-th bit.

Code

#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
#define Nmax 200010
int N;
int A[Nmax];
int K;
int main() {
    scanf("%d %d", &N, &K);
    for (int i = 1; i <= N; i++)
        scanf("%1d", &A[i]);
    bool F = false;
    for (int i = K+1; i <= N; i++) {
        if (A[i] > A[(i-1)%K+1]) {
            F = true;
            break;
        }
        else if (A[i] < A[(i-1)%K+1])
            break;
    }
    if (F) {
        for (int i = K; i >= 1; i--) {
            if (F) {
                if (A[i] != 9) {
                    A[i]++;
                    F = false;
                }
                else {
                    A[i] = 0;
                }
            }
            else
                break;
        }
    }
    printf("%d\n", N);
    for (int i = 1; i <= N; i++)
        printf("%01d", A[(i-1)%K+1]);
    return 0;
}

Domino for Young

Topic Link http://codeforces.com/contest/1268/problem/B

The meaning of problems

Given a chessboard, the height of the i-th column is \ (a_i \), seeking to cover with 1x2 dominoes, the maximum number of dominoes can be put.

answer

Black and white stain on the board, the minimum number of Haig and white grid is the answer.
Proof:
For a length equal to two rows or two, may be covered with domino apparent transverse part are equal number of black and white cells.
Such a cover is repeated, and finally only the sequence of monotonically decreasing.
Apparently in this simplest case the minimum number of cells as black and white answer.

Code

#include <cstdio>
#include <algorithm>
using namespace std;
#define Nmax 300010
int N;
int ls[Nmax];
long long B[2];
int main() {
    scanf("%d", &N);
    int a;
    for (int i = 1; i <= N; i++) {
        scanf("%d", &a);
        B[i%2] += a/2;
        B[(i+1)%2] += a - a/2;
    }
    printf("%lld\n", min(B[0], B[1]));
    return 0;
}

K Integers

Topic Link http://codeforces.com/contest/1268/problem/C

The meaning of problems

Arrangement of a given number n, each exchange two adjacent numbers.
For each k belonging to 1 ~ n, the required switching 1 ~ k k This number is incremented to the number of exchanges neighboring required.

answer

Decomposition problem, to let the k number of adjacent, then their correct relative positions.
Let this number k adjacent, apparently moved to a position where the median number of k near the optimum use of two priority queues to maintain the median.
Problems switching times relative position, into the ordinary sort of adjustment request, for n different interrogation, has its number (the number of new reverse order) to how many decimal number k after each new obtained.

Code

#include <cstdio>
#include <queue>
using namespace std;
#define Nmax 200010
int N;
int ls[Nmax];
priority_queue<int> qb, qs;
long long bsm, ssm;
int sm[Nmax];
inline int lowbit(int x) {
    return x&-x;
}
void adin(int x) {
    for (int i = x; i <= N; i += lowbit(i))
        sm[i] += 1;
}
int qin(int x) {
    int ans = 0;
    for (int i = x; i; i -= lowbit(i))
        ans += sm[i];
    return ans;
}
int main() {
    scanf("%d", &N);
    int a;
    for (int i = 1; i <= N; i++) {
        scanf("%d", &a);
        ls[a] = i;
    }
    long long ans = 0;
    long long ans2;
    for (int k = 1; k <= N; k++) {
        if (qb.size() && ls[k] < qb.top()) {
            qb.push(ls[k]);
            bsm += ls[k];
        }
        else {
            qs.push(-ls[k]);
            ssm += ls[k];
        }
        while (qb.size() < qs.size()) {
            a = -qs.top();
            qs.pop();
            ssm -= a;
            qb.push(a);
            bsm += a;
        }
        while (qb.size() > qs.size()+1) {
            a = qb.top();
            qb.pop();
            bsm -= a;
            qs.push(-a);
            ssm += a;
        }
        adin(ls[k]);
        ans += k - qin(ls[k]);
        a = qb.top();
        ans2 = ans
                + (long long)qb.size()*a-bsm - (0LL+qb.size()-1)*qb.size()/2
                + ssm-(long long)qs.size()*a - (1LL+qs.size())*qs.size()/2;
        printf("%lld ", ans2);
    }
    return 0;
}

Guess you like

Origin www.cnblogs.com/KZNS/p/codeforces-round-609.html