Codeforces Round #719 (Div. 3)

Codeforces Round #719 (Div. 3)

A - Do Not Be Distracted!

int main() {
    IOS;
    for (cin >> _; _; --_) {
        cin >> n; set<int> st; m = 0;
        string s; cin >> s; bool f = 0;
        for (auto &i : s)
            if (i != m && st.count(i)) f = 1;
            else if (i != m) st.insert(i), m = i;
        cout << (!f ? "YES
" : "NO
");
    }
    return 0;
}

B - Ordinary Numbers

int main() {
    IOS;
    for (cin >> _; _; --_) {
        cin >> n; m = 0; k = 9;
        for (ll i = 1; i <= n; i = i * 10 + 1)
            for (ll j = i, k = 1; j <= n && k <= 9; ++k, j += i) m += j <= n;
        cout << m << '
';
    }
    return 0;
}

C - Not Adjacent Matrix

直接先排列好

1 2 3
4 5 6
7 8 9

再把奇数列轮转一下即可

4 2 6
7 5 9
1 8 3

除了(n = 2) 其他都有解

int main() {
    IOS;
    for (cin >> _; _; --_) {
        cin >> n;
        if (n == 2) { cout << "-1
"; continue; }
        rep (i, 0, n - 1) rep (j, 0, n - 1) {
            int id = i * n + j;
            if (j + 1 & 1) id = (id + n) % (n * n);
            cout << id + 1 << char(" 
"[j == n - 1]);
        }
    }
    return 0;
}

D - Same Differences

(a_j - a_i = j - i) 等价 $a_j - j = a_i - i $ 直接统计 (a_i - i) 即可

int main() {
    IOS;
    for (cin >> _; _; --_) {
        cin >> n; map<int, int> st;
        rep (i, 1, n) cin >> a[i], ++st[a[i] - i];
        ll ans = 0;
        for (auto &i : st) ans += (i.se - 1ll) * i.se >> 1;
        cout << ans << '
';
    }
    return 0;
}

E - Arranging The Sheep

(*)共出现(n)次, 第(i)(*)出现在原串位置(a_i),

则变成了移动所有(*)使得所有(*)并列, 按序让每个(a_i -= i)

则变成了让所有(*)到同一个位置, 直接找中位数

char s[N];
int a[N];
 
int main() {
    IOS;
    for (cin >> _; _; --_) {
        cin >> n >> s + 1; m = 0; ll ans = 0;
        rep (i, 1, n) if (s[i] == '*') ++m, a[m] = i - m;
        rep (i, 1, m) ans += abs(a[m + 1 >> 1] - a[i]);
        cout << ans << '
';
    }
    return 0;
}

F - Guess the K-th Zero

线段树维护一下已经猜过的(0)对区间的影响即可

const int N = 2e5 + 5;

struct BIT {
    struct node { int l, r, val; } tr[N * 20];
    void build(int rt, int l, int r) {
        tr[rt].l = l, tr[rt].r = r; tr[rt].val = -1;
        if (l == r) return;
        int mid = l + r >> 1;
        build(rt << 1, l, mid); build(rt << 1 | 1, mid + 1, r);
    }
    void change(int rt, int k) {
        if (~tr[rt].val) --tr[rt].val;
        if (tr[rt].l == tr[rt].r) return;
        int mid = tr[rt].l + tr[rt].r >> 1;
        change(rt << 1 | (mid < k), k);
    }
    int ask(int rt) {
        if (!~tr[rt].val) {
            cout << "? " << tr[rt].l << ' ' << tr[rt].r << endl;
            cin >> tr[rt].val; tr[rt].val = tr[rt].r - tr[rt].l + 1 - tr[rt].val;
        }
        return tr[rt].val;
    }
    void ask(int rt, int k) {
        if (tr[rt].l == tr[rt].r) {
            cout << "! " << tr[rt].l << endl;
            change(1, tr[rt].l);
            return;
        }
        int mid = tr[rt].l + tr[rt].r >> 1, res = ask(rt << 1);
        if (res >= k) ask(rt << 1, k);
        else ask(rt << 1 | 1, k - res);
    }
} T;

int n, m, _, k, cas;

int main() {
    IOS; cin >> n >> k; T.build(1, 1, n);
    rep (i, 1, k) cin >> m, T.ask(1, m);
    return 0;
}

G - To Go Or Not To Go?

最短路

const int N = 4e6 + 6;

int n, m, _, k, cas;
int a[N];
ll d[N];
bool v[N];

int get(int x, int y) { return (x - 1) * m + y; }

int main() {
    IOS; cin >> n >> m >> k; VI c;
    rep (i, 1, n) rep (j, 1, m) {
        cin >> a[get(i, j)];
        if (a[get(i, j)] > 0) c.pb(get(i, j));
    }
    memset(d, 0x3f, sizeof d);
    priority_queue<pair<ll, int>> q; q.emplace(d[1] = 0, 1);
    while (!q.empty()) {
        int x = q.top().se; q.pop();
        if (v[x]) continue; v[x] = 1;
        if (x == 0) {
            for (auto &i : c) if (umin(d[i], d[0] + a[i])) q.emplace(-d[i], i);
        } else {
            if (x > m && ~a[x - m] && umin(d[x - m], d[x] + k)) q.emplace(-d[x - m], x - m);
            if (x % m != 1 && ~a[x - 1] && umin(d[x - 1], d[x] + k)) q.emplace(-d[x - 1], x - 1);
            if (x + m <= n * m && ~a[x + m] && umin(d[x + m], d[x] + k)) q.emplace(-d[x + m], x + m);
            if (x % m && ~a[x + 1] && umin(d[x + 1], d[x] + k)) q.emplace(-d[x + 1], x + 1);
            if (a[x] > 0) if (umin(d[0], d[x] + a[x])) q.emplace(-d[0], 0);
        }
    }
    cout << (d[n * m] == d[N - 1] ? -1 : d[n * m]);
    return 0;
}
原文地址:https://www.cnblogs.com/2aptx4869/p/14735949.html