Loading

Codeforces Round #811 (Div. 3) A - G 个人题解

传送门

A - Everyone Loves to Sleep

判断一下哪个时间离得最近,如果时间比当前时间还前的话,就加上一天的时间

#include <iostream>
#include <vector>
#include <array>
using namespace std;

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t;
    cin >> t;
    while(t--)
    {
        int n, h, w;
        cin >> n >> h >> w;
        int ans = 1e9;
        for(int i=0; i<n; i++)
        {
            int a, b;
            cin >> a >> b;
            if(a < h || (a == h && b < w))
                a += 24;
            ans = min(ans, (a - h) * 60 + b - w);
        }
        cout << ans / 60 << " " << ans % 60 << "\n";
    }
    return 0;
}

B - Remove Prefix

放个桶,从后面往前找一下,直到重复

#include <iostream>
#include <vector>
#include <array>
using namespace std;
const int maxn = 2e5 + 10;
int a[maxn], alp[maxn];

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t;
    cin >> t;
    while(t--)
    {
        int n;
        cin >> n;
        for(int i=0; i<=n; i++) alp[i] = 0;
        for(int i=0; i<n; i++) cin >> a[i];
        for(int i=n-1; i>=0; i--)
        {
            if(alp[a[i]]) break;
            n--;
            alp[a[i]]++;
        }
        cout << n << "\n";
    }
    // cout << endl;
    return 0;
}

C - Minimum Varied Number

这题可以直接暴力枚举,但是看到了集训队有人拿贪心做的

暴力:

#include <iostream>
#include <vector>
#include <array>
using namespace std;
const int maxn = 2e5 + 10;
int alp[maxn], len = 99;
int last[maxn];

void dfs(int now, int sum, int s, int tot)
{
    if(now == 10)
    {
        if(s == sum)
        {
            if(tot > len) return;
            if(tot == len)
            {
                int tp = 0;
                for(int i=1; i<=9; i++)
                {
                    if(alp[now] == 0) continue;
                    if(i > last[tp]) return;
                    else if(i < last[tp]) break;
                    tp++;
                }
            }
            int tp = 0;
            for(int i=1; i<=9; i++)
            {
                if(alp[i] == 0) continue;
                last[tp++] = i;
            }
            len = tot;
        }
        return;
    }
    if(sum > s) return;
    dfs(now + 1, sum, s, tot);
    alp[now] = 1;
    dfs(now + 1, sum + now, s, tot + 1);
    alp[now] = 0;
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t;
    cin >> t;
    while(t--)
    {
        int s;
        cin >> s;
        len = 99;
        for(int i=0; i<=10; i++) last[i] = 99;
        dfs(1, 0, s, 0);
        for(int i=0; i<len; i++)
            cout << last[i];
        cout << "\n";
    }
    // cout << endl;
    return 0;
}

贪心:从数字大的枚举,如果能取则取,取了大的会很大程度上减小最后答案的长度

#include <iostream>
using namespace std;
const int maxn = 110;
int alp[maxn];

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t;
    cin >> t;
    while(t--)
    {
        int n;
        cin >> n;
        for(int i=9; i>=1; i--)
        {
            if(n >= i)
            {
                alp[i] = 1;
                n -= i;    
            }
            else alp[i] = 0;
        }
        for(int i=1; i<=9; i++)
            if(alp[i]) cout << i;
        cout << endl;
    }
    return 0;
}

D - Color with Occurrences

贪心

从左边开始寻找子串匹配,如果出现若干个子串能匹配上,则选择能匹配的最远的,这样就会出现一段红色的区域

在这段新出现的红色区域之中,枚举所有的起点,枚举所有的子串,判断一下能不能让红色区域延伸,如果能延伸,则选取能延伸的最远的,重复这个过程

#include <iostream>
#include <vector>
#include <string>
#include <array>
using namespace std;
const int maxn = 2e5 + 10;

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t;
    cin >> t;
    while(t--)
    {
        string s;
        cin >> s;
        int n;
        cin >> n;
        vector<string>ss(n);
        for(int i=0; i<n; i++) cin >> ss[i];
        int l = 0, r = 0;
        vector<array<int, 2> >last;
        int cnt = 0;
        while(r < s.length())
        {
            int x = r;
            int a, b;
            while(l <= r)
            {
                for(int i=0; i<n; i++)
                {
                    if(l + ss[i].length() > s.length()) continue;
                    int f = 1;
                    for(int j=0; j<ss[i].length() && f; j++)
                    {
                        if(ss[i][j] != s[l + j]) f = 0;
                    }
                    if(f && l + (int)ss[i].length() > x)
                    {
                        a = i + 1;
                        b = l + 1;
                        x = l + (int)ss[i].length();
                    }
                }
                l++;
            }
            if(x == r) break;
            r = x;
            cnt++;
            last.push_back({a, b});
        }
        if(r != s.length()) cout << -1 << "\n";
        else
        {
            cout << cnt << "\n";
            for(auto [a, b] : last)
                cout << a << " " << b << "\n";
        }
    }
    // cout << endl;
    return 0;
}

E - Add Modulo 10

其实在 \(20\) 的模意义下都是一样的,会陷入一个循环,因此判断一下模 \(20\) 的意义下有没有一个数字出现 \(n\) 次就可以了

#include <iostream>
#include <map>
using namespace std;
typedef long long ll;
const int maxn = 2e5 + 10;

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t;
    cin >> t;
    while(t--)
    {
        int n;
        cin >> n;
        int cnt = 0, num = -1;
        int f = 1, cnt_a = 0, cnt_b = 0;
        for(int i=0; i<n; i++)
        {
            int x;
            cin >> x;
            if(x % 10 == 5) x += 5;
            if(x % 10 == 0)
            {
                cnt++;
                if(cnt > 1) {if(num != x) {num = -1;}}
                else num = x;
                continue;
            }
            int y = (x / 10) & 1;
            x %= 10;
            if(x == 1 | x == 2 || x == 4 || x == 8) y++;
            if(y & 1) cnt_a++;
            else cnt_b++;
        }
        if(cnt_a == n || cnt_b == n || (cnt == n && num != -1))
            cout << "yes\n";
        else cout << "no\n";
    }
    cout << endl;
    return 0;
}

F - Build a Tree and That Is It

构造

  1. 其中一个点位于其他两个点的最短路径上:要求两个小的距离之和等于最长的那个距离,并且不超过 \(n - 1\)

  2. 任意一个点都不在其他两个点的最短路径上,类似于菊花图:要求三个路径之和是个偶数(解方程),并且最长的那个路径小于其他两个路径长之和

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
typedef long long ll;
const int maxn = 2e5 + 10;
int tp = 0;

void solve(int pre, int k, int last)
{
    for(int i=0; i<k; i++)
    {
        cout << pre << " " << tp << "\n";
        pre = tp;
        tp++;
    }
    if(last != -1)
        cout << pre << " " << last << "\n";
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t;
    cin >> t;
    while(t--)
    {
        int n, d12, d23, d31;
        cin >> n >> d12 >> d23 >> d31;
        int sum = d12 + d23 + d31;
        int maxx = max({d12, d23, d31});
        int minn = min({d12, d23, d31});
        int f = 0;
        tp = 4;
        if(d12 * 2 == sum || d23 * 2 == sum || d31 * 2 == sum) f = 1;
        else if(sum / 2 < n && (sum % 2 == 0) && sum / 2 > maxx) f = 2;
        if(f == 0) {cout << "NO\n"; continue;}
        cout << "YES\n";
        if(f == 1)
        {
            vector<int>a(4);
            for(int i=1; i<=3; i++) a[i] = i;
            if(d23 == maxx) {swap(a[3], a[1]); swap(d23, d12);}
            if(d31 == maxx) {swap(a[2], a[3]); swap(d31, d12);}
            solve(a[1], d31 - 1, a[3]);
            solve(a[2], d23 - 1, a[3]);
            solve(1, n - d31 - d23 - 1, -1);
        }
        else
        {
            sum /= 2;
            tp = 5;
            solve(4, sum - d23 - 1, 1);
            solve(4, sum - d31 - 1, 2);
            solve(4, sum - d12 - 1, 3);
            solve(1, n - (sum * 3 - d12 - d31 - d23) - 1, -1);
        }
    }
    cout << endl;
    return 0;
}

G - Path Prefixes

二分

考虑一下树上在 \(dfs\) 的时候记录一下 \(b\) 数组的前缀和,然后再用当前的 \(a\) 数组前缀和进行二分求解答案

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
typedef long long ll;
const int maxn = 2e5 + 10;
ll fa[maxn], a[maxn], b[maxn];
vector<int>gra[maxn];
ll last[maxn];
ll sum[maxn], tp = 0;

void dfs(int now, int pre, ll tot)
{
    if(now != 1) sum[tp] = sum[tp-1] + b[now];
    tp++;
    if(now != 1)
    {
        last[now] = lower_bound(sum, sum + tp, tot + a[now]) - sum;
        if(sum[last[now]] > tot + a[now] || tp == last[now]) last[now]--;
    }
    for(ll nex : gra[now])
    {
        if(nex == pre) continue;
        dfs(nex, now, tot + a[now]);
    }
    sum[--tp] = 0;
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t;
    cin >> t;
    while(t--)
    {
        int n;
        cin >> n;
        for(int i=0; i<=n; i++) gra[i].clear();
        tp = 0;
        for(int i=2; i<=n; i++)
        {
            cin >> fa[i] >> a[i] >> b[i];
            gra[fa[i]].push_back(i);
        }
        dfs(1, 1, 0);
        for(int i=2; i<=n; i++) cout << last[i] << " ";
        cout << "\n";
    }
    // cout << endl;
    return 0;
}
posted @ 2022-08-02 10:31  dgsvygd  阅读(232)  评论(0编辑  收藏  举报