首页 > 其他分享 >牛客小白月赛87

牛客小白月赛87

时间:2024-02-17 18:00:37浏览次数:20  
标签:cur int long 牛客 solve 小白月赛 using 87 define

牛客小白月赛87

小苯的石子游戏

代码:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<ll, ll>;
#define fi first
#define se second
using i128 = __int128_t;
using piii = pair<ll, pair<ll, ll>>;
void solve()
{
    int n;
    cin >> n;
    vector<int> a(n + 1);
    int x = 0;
    int y = 0;
    for (int i = 1; i <= n; i++)
    {
        cin >> a[i];
    }
    int cur = 1;
    for (int i = n; i; i--)
    {
        if (cur & 1)
        {
            x += a[i];
        }
        else
        {
            y += a[i];
        }
        cur++;
    }
    if (x > y)
    {
        puts("Alice");
    }
    else
    {
        puts("Bob");
    }
}

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

小苯的排序疑惑

解题思路:

最小在首位或最大在尾部即可。

代码:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<ll, ll>;
#define fi first
#define se second
using i128 = __int128_t;
using piii = pair<ll, pair<ll, ll>>;
void solve()
{
    int n;
    cin >> n;
    vector<int> a(n + 1);
    int maxa = -1e9 - 1;
    int mina = 1e9 + 1;
    for (int i = 1; i <= n; i++)
    {
        cin >> a[i];
        maxa = max(maxa, a[i]);
        mina = min(mina, a[i]);
    }
    if (a[1] == mina || a[n] == maxa)
    {
        puts("YES");
    }
    else
    {
        puts("NO");
    }
}

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

小苯的IDE括号问题(easy)

解题思路:

双向链表删改即可。

代码:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<ll, ll>;
#define fi first
#define se second
using i128 = __int128_t;
using piii = pair<ll, pair<ll, ll>>;
void solve()
{
    int n, q;
    cin >> n >> q;
    string s;
    cin >> s;
    int cur = 0;
    n = s.size();
    s = ' ' + s;
    vector<int> l(n + 1, 0), r(n + 1, n + 1);
    for (int i = 1; i <= n; i++)
    {
        if (s[i] == 'I')
        {
            cur = i;
        }
        l[i] = i - 1;
        r[i] = i + 1;
    }
    auto del = [&](int u)
    {
        if (l[u] > 0)
        {
            r[l[u]] = r[u];
        }
        if (r[u] <= n)
        {
            l[r[u]] = l[u];
        }
    };
    while (q--)
    {
        string t;
        cin >> t;
        if (t[0] == 'b')
        {
            array<bool, 2> vis;
            if (r[cur] <= n)
            {
                vis[0] = true;
            }
            if (l[cur] > 0)
            {
                vis[1] = true;
            }
            if (!vis[1])
            {
                continue;
            }
            else
            {
                if (vis[0] && s[l[cur]] == '(' && s[r[cur]] == ')')
                {
                    del(l[cur]);
                    del(r[cur]);
                }
                else
                {
                    del(l[cur]);
                }
            }
        }
        else
        {
            array<bool, 2> vis;
            if (r[cur] <= n)
            {
                vis[0] = true;
            }
            if (vis[0])
            {
                del(r[cur]);
            }
        }
    }
    string ls, rs;
    int t = cur;
    while (true)
    {
        ls += s[t];
        t = l[t];
        if (t == 0)
        {
            break;
        }
    }
    reverse(ls.begin(), ls.end());
    ls.pop_back();
    t = cur;
    while (true)
    {
        rs += s[t];
        t = r[t];
        if (t == n + 1)
        {
            break;
        }
    }
    ls += rs;
    cout << ls << endl;
}

int main()
{
    int t = 1;
    // cin >> t;
    while (t--)
    {
        solve();
    }
    return 0;
}

小苯的IDE括号问题(hard)

代码:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<ll, ll>;
#define fi first
#define se second
using i128 = __int128_t;
using piii = pair<ll, pair<ll, ll>>;
void solve()
{
    int n, q;
    cin >> n >> q;
    string s;
    cin >> s;
    int cur = 0;
    n = s.size();
    s = ' ' + s;
    vector<int> l(n + 1, 0), r(n + 1, n + 1);
    for (int i = 1; i <= n; i++)
    {
        if (s[i] == 'I')
        {
            cur = i;
        }
        l[i] = i - 1;
        r[i] = i + 1;
    }
    auto del = [&](int u)
    {
        if (l[u] > 0)
        {
            r[l[u]] = r[u];
        }
        if (r[u] <= n)
        {
            l[r[u]] = l[u];
        }
    };
    while (q--)
    {
        string t;
        cin >> t;
        if (t[0] == 'b')
        {
            array<bool, 2> vis;
            if (r[cur] <= n)
            {
                vis[0] = true;
            }
            if (l[cur] > 0)
            {
                vis[1] = true;
            }
            if (!vis[1])
            {
                continue;
            }
            else
            {
                if (vis[0] && s[l[cur]] == '(' && s[r[cur]] == ')')
                {
                    del(l[cur]);
                    del(r[cur]);
                }
                else
                {
                    del(l[cur]);
                }
            }
        }
        else if (t[0] == 'd')
        {
            array<bool, 2> vis;
            if (r[cur] <= n)
            {
                vis[0] = true;
            }
            if (vis[0])
            {
                del(r[cur]);
            }
        }
        else if (t[0] == '<')
        {
            if (l[cur] > 0)
            {
                swap(s[cur], s[l[cur]]);
                cur = l[cur];
            }
        }
        else
        {
            if (r[cur] <= n)
            {
                swap(s[cur], s[r[cur]]);
                cur = r[cur];
            }
        }
    }
    string ls, rs;
    int t = cur;
    while (true)
    {
        ls += s[t];
        t = l[t];
        if (t == 0)
        {
            break;
        }
    }
    reverse(ls.begin(), ls.end());
    ls.pop_back();
    t = cur;
    while (true)
    {
        rs += s[t];
        t = r[t];
        if (t == n + 1)
        {
            break;
        }
    }
    ls += rs;
    cout << ls << endl;
}

int main()
{
    int t = 1;
    // cin >> t;
    while (t--)
    {
        solve();
    }
    return 0;
}

小苯的数组构造

解题思路:

整个\(b\)数组同时加减一个数不影响答案正确性,我们先默认\(b_1 = 0\)。

若\(a_i > a_j,(i < j)\),\(dif = abs(a_i - a_j)\),我们有两种方法\(a_i - dif,a_j + dif\)。两种操作对于首位\(0\)带来的极差影响是一样的,我们往后统一只加不减。

所以,每个元素只需要加到和前缀最大值一样即可。

代码:

#include<bits/stdc++.h>
using namespace std;

void solve()
{
    int n;
    cin >> n;
    vector<long long> a(n + 1);
    for (int i = 1; i <= n; i++)
    {
        cin >> a[i];
    }
    long long pre = a[1];
    long long ans = 0;
    vector<long long> b(n + 1, 0);
    for (int i = 2; i <= n; i++)
    {
        if (a[i] < pre)
        {
            b[i] = pre - a[i];
        }
        pre = max(pre, a[i]);
    }
    for (int i = 1; i <= n; i++)
    {
        cout << b[i] << ' ';
    }
    cout << endl;
}


int main()
{
    int t = 1;
//     cin >> t;
    while (t--)
    {
        solve();
    }
    return 0;
}

小苯的数组切分

解题思路:

\(\&\):由于是最后一段数组,该操作包含元素越多,区间总权值只可能越小,所以,最后一段只有\(a_n\)。

接下来就是两段区间和\((1,i) + (i + 1, n - 1)\),预处理前后缀和后,枚举分界点即可。

代码:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<ll, ll>;
#define fi first
#define se second
using i128 = __int128_t;
using piii = pair<ll, pair<ll, ll>>;

void solve()
{
    int n;
    cin >> n;
    vector<int> a(n + 1);
    for (int i = 1; i <= n; i++)
    {
        cin >> a[i];
    }
    vector<int> pre(n + 1), suf(n + 1);
    for (int i = 1; i <= n; i++)
    {
        pre[i] = pre[i - 1] ^ a[i];
    }
    for (int i = n - 1; i > 0; i--)
    {
        suf[i] = suf[i + 1] | a[i];
    }
    ll ans = 0;
    for (int i = 1; i <= n - 2; i++)
    {
        ans = max(ans, (ll)pre[i] + suf[i + 1] + a[n]);
    }
    cout << ans << endl;
}

int main()
{
    int t = 1;
    // cin >> t;
    while (t--)
    {
        solve();
    }
    return 0;
}

小苯的逆序对

解题思路:

\(g[x]:gcd 为x的倍数的数组对数\)

\(f[x]:gcd恰好为x的数组对数\)

\(f[x] = g[x] - g[2x] - g[3x] -...-g[kx],ps:(k+1)x > n\)。

\(\lfloor\frac{n}{i}\rfloor:1\sim n 内i的倍数有多少个\)

代码:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<ll, ll>;
#define fi first
#define se second
using i128 = __int128_t;
using piii = pair<ll, pair<ll, ll>>;
const int N = 2e5 + 10;
ll f[N];
ll tr[N];
ll n;

void add(int x, int v)
{
    for (int i = x; i <= n; i += (i & -i))
    {
        tr[i] += v;
    }
}

ll query(int x)
{
    ll res = 0;
    for (int i = x; i; i -= (i & -i))
    {
        res += tr[i];
    }
    return res;
}

void solve()
{
    cin >> n;
    vector<int> pos(n + 1);
    for (int i = 1; i <= n; i++)
    {
        int x;
        cin >> x;
        pos[x] = i;
    }
    for (int i = n; i; i--)
    {
        for (int j = i; j <= n; j += i)
        {
            f[i] += (j / i) - 1 - query(pos[j]);
            add(pos[j], 1);
        }
        for (int j = i; j <= n; j += i)
        {
            add(pos[j], -1);
            if (j != i)
            {
                f[i] -= f[j];
            }
        }
    }
    cout << f[1] << endl;
}

int main()
{
    int t = 1;
    // cin >> t;
    while (t--)
    {
        solve();
    }
    return 0;
}

标签:cur,int,long,牛客,solve,小白月赛,using,87,define
From: https://www.cnblogs.com/value0/p/18018184

相关文章

  • 牛客小白月赛87(A-F)
    A题题意:给定一个长度为n的升序序列a,Alice和Bob轮流操作,每次取走序列中的一个数,直到所有的数都被拿完,游戏结束。若游戏结束时,如果Alice拿到的数的总数严格大于Bob所拿到的数的总数,则Alice获胜,否则Bob获胜。思路:Alice每一次都拿最大的数,直到游戏结束。当序列长度为奇数,Alice......
  • P8784 [蓝桥杯 2022 省 B] 积木画
    原题链接太妙了,请移步题解区,有用数学归纳法做的,也有用找规律做的L型积木一定是成对出现的code#include<bits/stdc++.h>usingnamespacestd;constintmod=1e9+7;longlongdp[10000005]={0};intmain(){intn;cin>>n;dp[0]=1;dp[1]=1;dp[2]=2;......
  • 牛客小白87题解A-G
    A小苯的石子游戏本题贪心模拟即可B小苯的排序疑惑考虑到最简单的操作把n-1个数排好,最后一个看能否有序。即:a[1]<=min(a[2],a[3],a[4]..,a[n])||a[n]>=max(a[1],a[2],a[3]....,a[n-1])满足条件,反之易得不可能C&D小苯的IDE括号问题本题考察题意理解,简单版本我们可以只关注逻......
  • 牛客小白月赛87(非常菜的小白)
    这场被B坑了很长时间,导致没有看下面的题哈哈哈,还得练,赛后1分钟写出C,加训。A.小苯的石子游戏思路:Alice和Bob玩石子游戏,这里的石头谁多谁赢,不存在平局。由于本身就是升序,所以从后往前取即可。解法:由于升序,为了方便变成降序,最优解法就是最大的一个个轮流过去......
  • VP-CF1879 总结
    VP-CF1879总结Url:https://codeforces.com/contest/1879Score:A+B+C+DD做出来了,使用了一个复杂的方法。拆位肯定没错,但是有异或前缀和的方法,可以大大简化码量。E做出来了,贪心搞出来性质,即按深度染色。但是没读题,没看到\(k\)要最小。那就分三类讨论:k=1,k=2,k=3k=1或k=3......
  • P8725 [蓝桥杯 2020 省 AB3] 画中漂流
    原题链接题解1.总共有t秒,每一秒不是上升就是下降2.要在救援队赶来之前把体力全部花光code#include<bits/stdc++.h>usingnamespacestd;intdp[3005][1505]={0};//代表第i秒使用j点体力的方案数intmain(){intd,t,m;cin>>d>>t>>m;dp[0][0]=1;for(i......
  • P8732 [蓝桥杯 2020 国 ABC] 答疑
    原题链接题解存在某种问问题顺序使得答案最小,可是我们不知道排序的规律,遂试探给定一种排序,交换任意相邻同学问问题顺序,对答案的改变如下:code#include<bits/stdc++.h>usingnamespacestd;structunit{ints,a,e,sum;}stu[1005];boolcmp(unita,unitb){ret......
  • P8786 [蓝桥杯 2022 省 B] 李白打酒加强版
    原题链接题解根据样例,观察到李白总共走\(n+m\)次,每一次不是遇到酒馆就是遇到花故我们可以设\(dp[i][0/1]\)代表第\(i\)次遇到酒馆或花的方案数但是我们发现这样的状态不好转移故我们可以设\(dp[i][0/1][k]\)代表第\(i\)次遇到酒馆或花,还剩下\(k\)斗酒的方案数但......
  • P1873 [COCI 2011/2012 #5] EKO / 砍树
    题目链接:一、本题为什么能想到利用二分解决?\(1.\)有单调性提高伐木机的高度,显然地,得到的木头会减少。同样地,放低得到的木头会增多。而正因为答案有单调性,所以我们可以使用二分。\(2.\)数据范围大如果采用暴力枚举,时间复杂度为\(O(n\cdotm)\)会超时。用二分优化后时间......
  • P8783 [蓝桥杯 2022 省 B] 统计子矩阵
    原题链接题解1.当存在某个矩阵符合题意时,所有小于该矩阵的矩阵都符合题意这是我们就可以想到用双指针code#include<bits/stdc++.h>usingnamespacestd;inta[505][505]={0},sum[505][505]={0};intn,m,k;intcheck(intdown,intright,intup,intleft){returnsu......