cch杯 day 7 总结

· · 个人记录

A

思路

首先发现,无论你如何翻转,逆序对的奇偶性是不变的,并且当且仅当其逆序对数量为偶数时才是有可能的,所以可以直接算逆序对。

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

const int N = 1e6 + 10;
int T, n, a[N], vis[N], t[N];

int lowbit(int x) {
    return x & -x;
}

void add(int x, int w) {
    while(x <= n) {
        t[x] += w;
        x += lowbit(x);
    }
}

int query(int x) {
    int ans = 0;
    while(x) {
        ans += t[x];
        x -= lowbit(x);
    }
    return ans;
}

signed main() {

    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    cin >> T;
    while(T--) {
        cin >> n;
        for(int i = 1; i <= n; i++) {
            cin >> a[i];
        }
        int ans = 0;
        for(int i = 1; i <= n; i++) {
            ans += i - query(a[i]) - 1;
            add(a[i], 1);
        }
        for(int i = 1; i <= n; i++) {
            add(a[i], -1);
        }
        cout << (ans & 1 ? "No" : "Yes") << "\n";
    }

    return 0;
}

B

思路

可以想到一种策略:能除就除,不能除就减。那么我们可以把 n 转换进制,然后算就行了。

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

int C, T, n, x1, c1, x2, c2;

signed main() {

    cin >> C >> T;
    while(T--) {
        cin >> n >> x1 >> c1 >> x2 >> c2;
        if(x2 == 1) {
            cout << ((n - 1) % x1 ? -1 : (n - 1) / x1 * c1) << "\n";
            continue;
        }
        int i = 0, mul = 1, ans = 1e18;
        while(1) {
            if((n - mul) % x1 == 0) {
                int u = (n - mul) / x1, now = i * c2;
                for(int j = 1; j <= i; j++) {
                    now += (u % x2) * c1;
                    u /= x2;
                }
                now += u * c1;
                ans = min(ans, now);
            }
            if(mul > n / x2) {
                break;
            }
            i++;
            mul *= x2;
        }
        cout << (ans == 1e18 ? -1 : ans) << "\n";
    }

    return 0;
}

C

思路

直接 O(n^2) DP会T,所以考虑优化。因为对于一个区间,如果其两个端点都不为区间的最大/最小值,则不如把这两个端点丢给其他区间算,所以可以单调栈,找出每个点右边的第一个大于/小于当前值的位置,不断往后跳着转移,虽然依然是 O(n^2),但已经可以拿到 96 分的高分了,然后看到 T 的那个点是单调递减,那么直接把这种情况特判掉就行了。

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

const int N = 1e6 + 10;
int n, a[N], nxt1[N], nxt2[N], dp[N];

signed main() {

    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    cin >> n;
    bool flag = 1;
    for(int i = 1; i <= n; i++) {
        cin >> a[i];
        if(i > 1 && a[i] < a[i - 1]) {
            flag = 0;
        }
    }
    if(flag) {
        cout << (a[n] - a[1]) * (a[n] - a[1]) << "\n";
        return 0;
    }
    stack<int> stk;
    for(int i = 1; i <= n; i++) {
        while(stk.size() && a[stk.top()] < a[i]) {
            nxt1[stk.top()] = i;
            stk.pop();
        }
        stk.push(i);
    }
    while(stk.size()) {
        stk.pop();
    }
    for(int i = 1; i <= n; i++) {
        while(stk.size() && a[stk.top()] > a[i]) {
            nxt2[stk.top()] = i;
            stk.pop();
        }
        stk.push(i);
    }
    for(int i = 1; i <= n; i++) {
        if(!nxt1[i]) {
            nxt1[i] = n + 1;
        }
        if(!nxt2[i]) {
            nxt2[i] = n + 1;
        }
    }
    for(int i = 1; i <= n; i++) {
        dp[i] = max(dp[i], dp[i - 1]);
        int minn = a[i], maxx = a[i], k1 = nxt1[i], k2 = nxt2[i];
        for(int j = i + 1; j <= n;) {
            minn = min(minn, a[j]);
            maxx = max(maxx, a[j]);
            dp[j] = max(dp[j], dp[i - 1] + (maxx - minn) * (maxx - minn));
            if(k1 < k2) {
                j = k1;
                k1 = nxt1[k1];
            }
            else {
                j = k2;
                k2 = nxt2[k2];
            }
        }
    }
    cout << dp[n] << "\n";

    return 0;
}