Codeforces Round #847 (Div. 3) A - G

Codeforces Round #847 (Div. 3) A - G

https://codeforces.com/contest/1790
(5/7):ABCDE,写的特别慢
我写的代码又臭又长

A. Polycarp and the Day of Pi

按题意匹配,注意不是传统的 \(\pi\)

#include <bits/stdc++.h>
#define pi acos(-1)

using namespace std;
//string t = "314159265358979311599796346854418516";
string t = "314159265358979323846264338327";

void solve () {
    string s;
    cin >> s;
    for (int i = 0; i < s.size (); i++) {
        if (s[i] != t[i]) {
            cout << i << endl;
            return ;
        }
    }
    cout << s.size () << endl;
}

int main () {
    int t;
    cin >> t;
    while (t --)    solve ();
    //cout << fixed << setprecision (35) << pi;
}

B. Taisia and Dice

平均分配,多的匀一点

#include <bits/stdc++.h>

using namespace std;

void solve () {
    int n, r, s;
    cin >> n >> r >> s;
    int m = r - s;
    int cnt = s / (n - 1), res = s % (n - 1);
    for (int i = 0; i < res; i++)   cout << cnt + 1 << ' ';
    for (int i = 0; i < n - 1 - res; i++)   cout << cnt << ' ';
    cout << m << endl;
    //cout << cnt << ' ' << res << endl;
}

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

C. Premutation

同一位置上,数量上占据绝对优势的就是该数字;
否则奇数长度下就是唯一被剩下来的数字放中间;
偶数长度下则需记录剩下的这俩数字的出现的位置之和。
(好拗口)

#include <bits/stdc++.h>

using namespace std;
const int N = 105;
int n, a[N][N], sum[N], vis[N];

void solve () {
    cin >> n;
    map<int, int> mp[n+1];
    for (int i = 1; i <= n; i++)    sum[i] = vis[i] = 0;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j < n; j++) {
            cin >> a[i][j];
            mp[j][a[i][j]] ++, sum[a[i][j]] += j;
        }
    }
    vector <int> st, ed;
    for (int i = 1; i < n; i++) {
        int mx = 0, num = 0, num2 = 0;
        for (auto j : mp[i]) {
            if (j.second > mx) {
                mx = j.second, num = j.first;
            }
            else if (j.second == mx)    num2 = j.first;
        }
        if (mx > n / 2) {
            if (i <= n / 2)     st.push_back (num);
            else    ed.push_back (num);
            vis[num] = 1;
        }
        else {
            if (sum[num] > sum[num2])   swap (num, num2);
            //cout << num << ' ' << num2 << ' ';
            st.push_back (num), st.push_back (num2);
        }
        //cout << mx << ' ' << num << endl;
    }
    for (auto i : st)   cout << i << ' ';
    if (n & 1) {
        for (int i = 1; i <= n; i++) {
            if (!vis[i])    cout << i << ' ';
        }
    }
    for (auto i : ed)   cout << i << ' ';
    cout << endl;
}

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

D. Matryoshkas

按题意贪心划分连续序列。
map记录队头即可,若不存在队头就新开一个,否则接上去后更新队头。
感觉比C简单

#include <bits/stdc++.h>

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

void solve () {
    cin >> n;
    map<int, int> mp;
    for (int i = 1; i <= n; i++)    cin >> a[i];
    sort (a + 1, a + n + 1);
    //for (int i = 1; i <= n; i++)    cout << a[i] << ' ';cout << endl;
    int ans = 0;
    for (int i = 1; i <= n; i++) {
        if (!mp[a[i]-1])    ans ++;
        else    mp[a[i]-1] --;

        mp[a[i]] ++;
    }

    cout << ans << endl;
}

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

E. Vlad and a Pair of Numbers

赛时打表(打表代码见注释)发现结论——可能解为:\(a=\frac x2, b=\frac{3x}{2}\)\(check\) 这组解是否合法即可。
证明见:https://zhuanlan.zhihu.com/p/601359475
== 位运算小结论:加法=异或+进位 \(a+b=(a\bigotimes b)+2(a\&b)\) ==

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

using namespace std;

void solve () {
    ll x, a, b;
    cin >> x;
    if (x & 1)  cout << -1 << endl;
    else {
        a = x / 2, b = a * 3;
        if ((a + b) / 2 != x || ((a ^ b) != x)) cout << -1 << endl;
        else    cout << a << ' ' << b << endl;
    }
    // ll sum1 = x * 2, sum2 = sum1 + 1;
    // for (int i = 1; i < sum1; i++) {
    //     if ((i ^ (sum1 - i)) == x)    cout << i << ' ' << sum1 - i << endl;
    // }
    // for (int i = 1; i < sum2; i++) {
    //     if ((i ^ (sum2 - i)) == x)    cout << i << ' ' << sum2 - i << endl;
    // }
    // cout << endl;
}

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

F. Timofey and Black-White Tree

我的评价是要大胆的做,相信自己的猜想bushi
证明见:https://zhuanlan.zhihu.com/p/601359475

#include <bits/stdc++.h>

using namespace std;
const int N = 2e5 + 5, M = N * 2;
int dis[N], c[N], n;

void solve () {
    cin >> n;
    int ans = n - 1;
    vector<int> vi[n+1];
      
    for (int i = 1; i <= n; i++)     cin >> c[i], dis[i] = n + 1;
    for (int i = 1; i < n; i++) {
        int a, b;   cin >> a >> b;
        vi[a].push_back (b), vi[b].push_back (a);
    }
    for (int i = 1; i <= n; i++) {
        ans = min (ans, dis[c[i]]);
        if (i > 1)  cout << ans << ' ';
        queue <int> q;
        q.push (c[i]);
        dis[c[i]] = 0;
        while (!q.empty ()) {
            int u = q.front ();
            q.pop ();
            for (auto v : vi[u]) {
                if (dis[v] > dis[u] + 1 && dis[u] + 1 < ans) {
                    dis[v] = dis[u] + 1;
                    q.push (v);
                }
            }
        }
    }
    cout << endl;
}

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

//根号

G. Tokens on Graph

又臭又长,看都不想看。
算了,明天看看

posted @ 2023-01-28 23:46  Sakana~  阅读(57)  评论(0编辑  收藏  举报