Codeforces Global Round 13 ABCDE题解

A K-th Largest Value

思路:记录一下当前数组中有多少个1,求第k大的数显然比较k与1的个数的大小

B Minimal Cost

思路:如果起点和终点不是互通的,我们只需要对相邻的两排进行操作

C Pekora and Trampoline

我们可以从前往后模拟。
题目给出的n<=5000,所以当s[i]+i>n的时候,s[i]多余的那一部分可以直接拿掉。我们这么做的时间复杂度是o(n^3),对于5000的范围会超时,所以需要进行优化。
优化:当从前往后模拟时,数组是不断趋近于全部都是1的状态的,我们可以记录:如果这个数是1,那么这个数后面第一个非1的位置。

#include <vector>
#include <iostream>
using namespace std;
typedef long long LL;
const int N = 1e5 + 10;
int a[N], nxt[N];
int n, t;
int ans = 0;
int gnxt(int u)
{
    
    
    if (u == nxt[u])
        return u;
    return nxt[u] = gnxt(nxt[u]);
}
int main()
{
    
    
    cin >> t;
    while (t--)
    {
    
    
        cin >> n;
        for (int i = 1; i <= n; ++i)
            cin >> a[i];
        for (int i = 1; i <= n + 1; ++i)
            nxt[i] = i + (a[i] == 1);
        LL res = 0;
        for (int i = 1; i <= n; ++i)
        {
    
    
            if (a[i] + i > n)
            {
    
    
                if (i == n)
                {
    
    
                    res += a[i] - 1;
                    a[i] = 1;
                }
                else
                {
    
    
                    res += a[i] - (n - i);
                    a[i] = n - i;
                }
            }
            while (a[i] != 1)
            {
    
    
                a[i]--;
                res++;
                int t = i + a[i] + 1;
                while (t <= n)
                {
    
    
                    if (a[t] == 1)
                        t = gnxt(t);
                    else
                    {
    
    
                        a[t]--;
                        nxt[t] = t + (a[t] == 1);
                        t = t + a[t] + 1;
                    }
                }
            }
        }
        cout << res << endl;
    }
    return 0;
}

D Zookeeper and The Infinite Zoo

当 u&v=v 时 u+v的2进制 一定是由 u的2进制 进位而来
例子:
u = 001010 时
v 可以为 001010 , 001000 , 000010 , 000000
我们发现 u+v 一定会发生2进制的进位(v=0除外)
所以我们只需要从低位往高位遍历一遍,看对于第i位后面两个数1的大小关系 。

#include <vector>
#include <iostream>
using namespace std;
typedef long long LL;
const int N = 1e5 + 10;
int main()
{
    
    
    int q;
    cin >> q;
    for (int i = 1; i <= q; ++i)
    {
    
    
        int a, b;
        cin >> a >> b;
        int x = 0, y = 0;
        if (a > b)
        {
    
    
            puts("NO");
            continue;
        }
        int flag = 1;
        while (a | b)
        {
    
    
            if (a & 1)
                x++;
            if (b & 1)
                y++;
            if (y > x)
                flag = 0;
            a >>= 1, b >>= 1;
        }
        if (flag)
            puts("YES");
        else
            puts("NO");
    }
    return 0;
}

E Fib-tree

每次找到将fi分成fi-1与fi-2的边,递归处理fi-1与fi-2是否为fib-tree

#include <vector>
#include <cstring>
#include <iostream>
using namespace std;
const int N = 5e5 + 10, M = 2 * N;
int h[N], e[M], ne[M], idx = 0;
int del[M];
void add(int a, int b)
{
    
    
    e[idx] = b, ne[idx] = h[a], h[a] = idx++;
}
int sz[N], father[N];
vector<int> point;
void dfs(int u, int fa)
{
    
    
    father[u] = fa;
    point.push_back(u);
    sz[u] = 1;
    for (int i = h[u]; i != -1; i = ne[i])
    {
    
    
        if (del[i] == true)
            continue;
        int j = e[i];
        if (j == fa)
            continue;
        dfs(j, u);
        sz[u] += sz[j];
    }
}
bool check(int u, int n)
{
    
    
    if (n <= 3)
        return true;
    int A = 1, B = 1;
    while (A + B < n)
    {
    
    
        int tmp = A;
        A = B;
        B += tmp;
    }
    if (A + B != n)
        return false;
    point.clear();
    dfs(u, 0);
    int x = 0, y = 0;
    for (auto p : point)
    {
    
    
        if (sz[p] == A || sz[p] == B)
        {
    
    
            x = p;
            y = father[p];
        }
    }
    if (x == 0 || y == 0)
        return false;
    for (int i = h[x]; i != -1; i = ne[i])
        if (e[i] == y)
            del[i] = del[i ^ 1] = true;
    if (sz[x] == B)
        return check(x, B) && check(y, A);
    return check(x, A) && check(y, B);
}
int n;
int main()
{
    
    
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    memset(h, -1, sizeof h);
    cin >> n;
    for (int i = 1; i < n; ++i)
    {
    
    
        int a, b;
        cin >> a >> b;
        add(a, b);
        add(b, a);
    }
    if (check(1, n))
        puts("YES");
    else
        puts("NO");
    return 0;
}

猜你喜欢

转载自blog.csdn.net/wenyisir/article/details/114271293