Codeforces Round 858 (Div. 2) ABCE

Codeforces Round 858 (Div. 2)

https://codeforces.com/contest/1806
现放一下ABC,然后明天补(大概)
坐牢的时候看了一下F1,感觉应该是一个蛮有意思的题?但是我不会做。
B卡了有一阵子,C想了好久快结束了才做出了(是我很不确定的分类讨论,总感觉是假的,比赛快结束不抱希望的交了一发结果过了,喜)
赛后看见大家都在讨论E,然而我还没看

A. Walking Master

很浅显的签到!

#include <bits/stdc++.h>

using namespace std;

void solve () {
    int a, b, c, d;
    cin >> a >> b >> c >> d;
    int dx = d - b;
    if (dx < 0) {
        cout << "-1\n";
        return ;
    }
    a += dx;
    int dy = a - c;
    if (dy < 0) {
        cout << "-1\n";
        return ;
    }
    cout << dx + dy << endl;
}

int main () {
    int t;
    cin >> t;
    while (t--) solve ();
}

B. Mex Master

WA了三发,主要是太容易想当然了,没有多举几个例子,往下验证下去。
其实手玩多几个样例就会发现,本质是分类讨论题。
0的个数不超过一半的时候,可以通过 '用非零数字把他隔开' 这种做法来保证所有相邻两数字之和 \(>0\),这样 \(mex\) 就是0了;
接着考虑能否让 \(mex\) 为1,此时可以把0全部拿掉,转化为长度为 \(n-cnt0\) 的非零序列来讨论(因为0对和造不成影响,所以可以丢一边):
如果剩下的数字全为1,那么就会出现01交界处和为1的情况,即 \(mex=2\);否则为 \(mex=1\)(注意特判没有1)

#include <bits/stdc++.h>
#define ll long long

using namespace std;
const int N = 2e5 + 5;
int a[N], n;

void solve () {
    cin >> n;
    int cnt0 = 0, cnt1 = 0;
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
        if (a[i] == 0)  cnt0++;
        else if (a[i] == 1) cnt1 ++;
    }
    if (cnt0 <= (n + 1) / 2)    cout << "0\n";
    else if (cnt1 == 0)     cout << "1\n";
    else if (cnt1 == n - cnt0)  cout << "2\n";
    else    cout << "1\n";
}

int main () {
    int t;
    cin >> t;
    while (t--) solve ();
}

C. Sequence Master

找规律。
先把数组升序排列,然后:
首先全0是一种,适用于全部。
偶数长度还可以按照 -1,-1,-1,...-1,-1,n 来构造。
然后特判长度为1,2:
1:全为其中某个数字
2:2 2 2 2也是一种方案
在符合条件的方案中选取代价最小即可。

数组开小WA了一发,寄

#include <bits/stdc++.h>
#define ll long long

using namespace std;
const int N = 4e5 + 5;
int a[N], n;

void solve () {
    cin >> n;
    int m = n;
    n *= 2;
    for (int i = 1; i <= n; i++)    cin >> a[i];
    sort (a + 1, a + n + 1);
    ll ans = 0;
    for (int i = 1; i <= n; i++)    ans += abs (a[i]);
    ll dx = 0;
    for (int i = 1; i < n; i++)     dx += abs(a[i] + 1);
    if (m & 1) {
        if (m == 1)    ans = min (ans, 1ll * abs (a[1] - a[2]));
    }
    else {
        dx += abs(n / 2 - a[n]);
        ans = min (ans, dx);
        if (m == 2) {
            dx = 0;
            for (int i = 1; i <= n; i++)    dx += abs (a[i] - 2);
            ans = min (ans, dx);
        }
    }
    cout << ans << endl;
}

int main () {
    int t;
    cin >> t;
    while (t--) solve ();
}

E. Tree Master

(既卡map,又卡umap)
非常神必的根号分治题目,感觉对于这种带根号的是不是就大胆暴力,小心卡常啊(逃
看看ygg:https://zhuanlan.zhihu.com/p/615164267

#include <bits/stdc++.h>
#define ll long long

using namespace std;
const int N = 1e5 + 5, M = 300;
ll a[N];
int fa[N], dep[N], s[N], id[N]; //该层点数,离散化
vector<int> v[N];
int n, q;
ll ans[N][M + 5];

void init (int u, int fa) {
    dep[u] = dep[fa] + 1;
    id[u] = ++s[dep[u]];
    for (auto j : v[u]) {
        if (j == fa)    continue;
        init (j, u);
    }
}

ll dfs (int x, int y) {
    if (x > y)  swap (x, y); //极大崩溃
    if (x == 0) return 0;
    if (s[dep[x]] < M && ans[x][id[y]])  return ans[x][id[y]];
    ll sum = dfs (fa[x], fa[y]) + a[x] * a[y];
    if (s[dep[x]] < M)  ans[x][id[y]] = sum;
    return sum;
}

int main () {
    cin >> n >> q;
    for (int i = 1; i <= n; i++)    cin >> a[i];
    for (int i = 2; i <= n; i++)    cin >> fa[i], v[fa[i]].push_back (i);
    init (1, -1);
    while (q--) {
        int x, y;
        cin >> x >> y;
        cout << dfs (x, y) << endl;
    }
}

//暴力+记搜
//非常的神奇
//根号分治,小于根号的记忆化,大的直接跳
posted @ 2023-03-18 23:55  Sakana~  阅读(130)  评论(2编辑  收藏  举报