Codeforces Round 811 (Div. 3)

A. Everyone Loves to Sleep

#include<bits/stdc++.h>

using namespace std;

void solve() {
    int n, h, m, t;
    cin >> n >> h >> m;
    t = h * 60 + m;
    vector<int> a;
    for (int i = 1, x, y; i <= n; i++)
        cin >> x >> y, a.push_back(x * 60 + y);
    sort( a.begin(), a.end() ) , a.push_back( a.front() + 24 * 60 );
    int p = *lower_bound( a.begin(), a.end() , t );
    p -= t;
    cout << p / 60 << " " << p % 60 << "\n";
    return;
}

int32_t main() {
    ios::sync_with_stdio(0), cin.tie(0);
    int t;
    for (cin >> t; t; t--)
        solve();
    return 0;
}

B. Remove Prefix

#include <bits/stdc++.h>

using namespace std;

#define int long long

const int inf = 1e18;

using vi = vector<int>;

const int N = 1e3;
vi val(N + 1, inf);

void solve() {
    int n;
    cin >> n;
    vi a(n);
    for( auto & i : a ) cin >> i;
    set<int> cnt;
    reverse(a.begin(), a.end());
    for( auto & i : a )
        if( cnt.insert(i).second == false ) break;
    cout << n - cnt.size() << "\n";
    return;
}

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

C. Minimum Varied Number

贪心暴搜似乎过不去,但是可以打表

#include<bits/stdc++.h>

using namespace std;

//int res, n = 0;
//vector<int> q, vis(10);
//
//void dfs(int x) {
//    int cnt = 0;
//    auto p = q;
//    sort(p.begin(), p.end());
//    for (auto i: p) cnt = cnt * 10 + i;
//    if( cnt > res ) return ;
//    if (x == 0) {
//        res = min(res, cnt);
//        return;
//    }
//    for (int i = 1; i <= 9; i++) {
//        if (i > x) break;
//        if (vis[i]) continue;
//        vis[i] = 1, q.push_back(i);
//        dfs(x - i);
//        vis[i] = 0, q.pop_back();
//    }
//}

//void solve() {
//    ++ n, res = INT_MAX;
//    dfs(n);
//    cout << res << "\n";
//}

vector<string> res = {"1",
                      "2",
                      "3",
                      "4",
                      "5",
                      "6",
                      "7",
                      "8",
                      "9",
                      "19",
                      "29",
                      "39",
                      "49",
                      "59",
                      "69",
                      "79",
                      "89",
                      "189",
                      "289",
                      "389",
                      "489",
                      "589",
                      "689",
                      "789",
                      "1789",
                      "2789",
                      "3789",
                      "4789",
                      "5789",
                      "6789",
                      "16789",
                      "26789",
                      "36789",
                      "46789",
                      "56789",
                      "156789",
                      "256789",
                      "356789",
                      "456789",
                      "1456789",
                      "2456789",
                      "3456789",
                      "13456789",
                      "23456789",
                      "123456789"};

void solve(){
    int n;
    cin >> n;
    cout << res[n-1]<< "\n";
}

int32_t main() {
    ios::sync_with_stdio(0), cin.tie(0);
    int t;
    for (cin >> t; t; t--)
        solve();
    return 0;
}

D. Color with Occurrences

当我们确定之前覆盖的部分后,可以直接贪心的选择可以之前覆盖区间能够相连且,覆盖距离最远的。

#include <bits/stdc++.h>

using namespace std;

#define int long long

const int inf = 1e18;

using db = long double;
using vi = vector<int>;
using pii = pair<int, int>;

void solve() {
    string t, s;
    int n;
    cin >> t >> n;
    int m = t.size();
    vector<array<int, 3>> e;
    for (int i = 1; i <= n; i++) {
        cin >> s;
        for (int l = 0, r = s.size()-1; r < m; l++, r++) {
            if (t.substr(l, s.size()) == s)
                e.push_back({l + 1, r + 1, i});
        }
    }
    vector<int> res;

    for (int pos = 0, left = 1, right = 0; left <= m; pos = 0) {
        for (int i = 0; i < e.size(); i++)
            if (e[i][0] <= left and e[i][1] > right)
                right = e[i][1], pos = i;
        res.push_back(pos);
        left = right + 1;
        if (res.size() > e.size() + 100) {
            cout << "-1\n";
            return;
        }
    }
    cout << res.size() << "\n";
    for (auto i: res)
        cout << e[i][2] << " " << e[i][0] << "\n";
}

int32_t main() {
    ios::sync_with_stdio(false), cin.tie(nullptr);
    int t;
    cin >> t;
    for (; t; t--)
        solve();
}

E. Add Modulo 10

可以发现除了结尾是5和 0 的情况,其他的都会进入到2 4 8 6 的循环中,所以可以先把各位全部移动到循环上,然后判断差值是否都是20 的倍数

#include<bits/stdc++.h>

using namespace std;

#define int long long
using vi = vector<int>;

void solve() {
    int n, f1 = 1, f2 = 1;
    cin >> n;
    vi a(n);
    for (auto &i: a) {
        cin >> i;
        if (i % 10 == 0 or i % 10 == 5) {
            f1 = 0;
            if (i % 10 == 5) i += 5;
        } else f2 = 0;
    }
    if (f1 + f2 == 0) {
        cout << "No\n";
        return;
    } else if (f2) {
        for (auto i: a)
            if (i != a.front()) {
                cout << "No\n";
                return;
            }
        cout << "Yes\n";
        return;
    }
    for (auto &i: a)
        while (i % 10 != 2) i += i % 10;
    int top = *max_element(a.begin(), a.end());
    for( auto i : a )
        if( (top - i) % 20 != 0 ){
            cout << "No\n";
            return ;
        }
    cout << "Yes\n";
    return ;
}

int32_t main() {
    ios::sync_with_stdio(0), cin.tie(0);
    int t;
    for (cin >> t; t; t--)
        solve();
    return 0;
}

F. Build a Tree and That Is It

除去不合法的情况,答案只有两种情况,一种是三个点在一条链上,此时选择中间的点作为根就好了。还有一种情况是一个 y 形的树,计算出三个点到分叉点的距离,分叉点做根就好了

#include <bits/stdc++.h>

using namespace std;

using vi = vector<int>;

void solve() {
    int n, d1, d2, d3;
    cin >> n >> d1 >> d2 >> d3;
    if (max({d1, d2, d3}) * 2 > d1 + d2 + d3) {
        cout << "NO\n";
        return;
    }
    if ((d1 + d2 + d3) & 1) {
        cout << "NO\n";
        return;
    }
    if ((d1 + d2 + d3) / 2 + 1 > n) {
        cout << "NO\n";
        return;
    }
    cout << "YES\n";

    vi p(n + 1);
    auto solver = [&](int root, int p1, int p2, int dep1, int dep2) {
        set<int> s;
        for (int i = 4; i <= n; i++) s.insert(i);
        int fa, dep, t;
        for (fa = root, dep = 1, t; dep < dep1; dep++) {
            t = *s.begin(), s.erase(s.begin());
            p[t] = fa, fa = t;
        }
        p[p1] = fa;
        for (fa = root, dep = 1, t; dep < dep2; dep++) {
            t = *s.begin(), s.erase(s.begin());
            p[t] = fa, fa = t;
        }
        p[p2] = fa;
        while (!s.empty())
            p[*s.begin()] = root, s.erase(s.begin());
        for (int i = 1; i <= n; i++)
            if (i != root) cout << i << " " << p[i] << "\n";
    };
    if (d1 + d2 == d3)
        solver(2, 1, 3, d1, d2);
    else if (d1 + d3 == d2)
        solver(1, 2, 3, d1, d3);
    else if (d2 + d3 == d1)
        solver(3, 1, 2, d3, d2);
    else {
        set<int> s;
        for (int i = 5; i <= n; i++) s.insert(i);
        int dep1 = (d1 + d3 - d2) / 2;
        int dep2 = (d1 + d2 - d3) / 2;
        int dep3 = (d2 + d3 - d1) / 2;
        int root = 4, fa, dep, t;
        for (fa = root, dep = 1, t; dep < dep1; dep++) {
            t = *s.begin(), s.erase(s.begin());
            p[t] = fa, fa = t;
        }
        p[1] = fa;
        for (fa = root, dep = 1, t; dep < dep2; dep++) {
            t = *s.begin(), s.erase(s.begin());
            p[t] = fa, fa = t;
        }
        p[2] = fa;
        for (fa = root, dep = 1, t; dep < dep3; dep++) {
            t = *s.begin(), s.erase(s.begin());
            p[t] = fa, fa = t;
        }
        p[3] = fa;
        while (!s.empty())
            p[*s.begin()] = root, s.erase(s.begin());
        for (int i = 1; i <= n; i++)
            if (i != root) cout << i << " " << p[i] << "\n";
    }
    return;
}

int32_t main() {
    ios::sync_with_stdio(false), cin.tie(nullptr);
    int TC;
    for (cin >> TC; TC; TC--)
        solve();
    return 0;
}

G. Path Prefixes

dfs 的过程记录一下到根的路径,在路径上二分一下就好

#include <bits/stdc++.h>

using namespace std;

#define int long long

const int inf = 1e18;

using db = long double;
using vi = vector<int>;
using pii = pair<int, int>;

void solve() {
    int n;
    cin >> n;
    vi a(n + 1), b(n + 1), q, f(n + 1);
    vector<vi> e(n + 1);
    for (int i = 2, p; i <= n; i++)
        cin >> p >> a[i] >> b[i], e[p].push_back(i);

    auto dfs = [e, a, b, &f, &q](auto &&self, int x, int cntA, int cntB, int dep) -> void {
        q.push_back(cntB);
        int res = -1;
        for (int l = 0, r = dep, mid; l <= r;) {
            mid = (l + r) / 2;
            if (q[mid] <= cntA) res = mid, l = mid + 1;
            else r = mid - 1;
        }
        f[x] = res;
        for (auto y: e[x])
            self(self, y, cntA + a[y], cntB + b[y], dep + 1);
        q.pop_back();
    };

    dfs(dfs, 1, 0, 0, 0);
    for( int i = 2 ; i <= n ; i ++ )
        cout << f[i] << " ";
    cout << "\n";
    return ;
}

int32_t main() {
    ios::sync_with_stdio(false), cin.tie(nullptr);
    int t;
    cin >> t;
    for (; t; t--)
        solve();
}
posted @ 2023-10-01 18:15  PHarr  阅读(12)  评论(0编辑  收藏  举报