首页 > 其他分享 >Codeforces Round 968 (Div. 2)

Codeforces Round 968 (Div. 2)

时间:2024-08-26 14:38:29浏览次数:4  
标签:cnt cout int ios 968 Codeforces long Div define

A. Turtle and Good Strings

思路:题意大致为把一个字符串分成若干段,要求每两段,前一段的首字符不能等于后的一段的尾字符,给你一个字符串,能不能构造出合法方案。观察到,分的段数越小,越有助于我们判断。所以,不妨分成两段,问题转化为判断首尾字符是否相等。

代码:

#include <bits/stdc++.h>
#define int long long
#define ull unsigned long long
#define ios ios::sync_with_stdio(0)
#define endl '\n'
#define pii pair<int, int>
#define debug(x) cout << "x = " << x << endl;
#define lowbit(x) (x & (-x))
using namespace std;
const int N = 5e5 + 5, P = 13331;
void solve()
{
    int T = 1;
    cin >> T;
    while (T--)
    {
        string s;
        int n;
        cin >> n >> s;
        if (s[0] != s[n - 1])
            cout << "yes" << endl;
        else
            cout << "no" << endl;
    }
}
signed main()
{
    ios;
    solve();
    return 0;
}

B. Turtle and Piggy Are Playing a Game 2

思路:题意可以转化成,为了最大化最后剩下的数,每次先手能删除当前最大的数,每次后手能删除当前最小的数。 所以答案是原序列的第 $$\lfloor \frac {n}{2} \rfloor + 1$$ 小的数。

代码:

#include<bits/stdc++.h>
#define int long long
#define ull unsigned long long
#define ios ios::sync_with_stdio(0)
#define endl '\n' 
#define pii pair<int,int>
#define debug(x) cout<<"x = "<<x<<endl;
#define lowbit(x) (x&(-x))
using namespace std;
const int N = 5e5 + 5,P = 13331;
int a[N],n;
void solve() {
    int T = 1;
    cin>>T;
    while(T --) {
        cin >> n;
        for (int i = 1; i <= n; i ++)
            cin >> a[i];
        sort(a + 1, a + 1 + n);
        int cnt = n - 1,l;
        if(cnt&1)
        {
            l = cnt / 2 + 1;
        }
        else 
        {
            l = r = cnt / 2;
        }
        cout << a[l + 1] << endl;
   }
}
signed main() {
    ios;
    solve();
    return 0;
}

C. Turtle and Good Pairs

思路:既然题目让我们最大化好对的数量,那我们不让去分类讨论一下那些 $$(i,j)$$ 是好对。

  1. 若 $$S_i=S_j$$,那么 $$(i,j)$$ 是一个好对。
  2. 若 $$S_i \neq S_j$$ ,且 $$\exists k \in [i,j)$$ 使得 $$S_k \neq S_i \and S_k \neq S_j$$,那么 $$(i,j)$$ 是一个好对。

不妨把 $$S$$ 划分成若干个由相同字符组成的连续段,那么如果 $$(i,j)$$ 为好对,则 $$i$$ 所在的连续段和 $$j$$ 所在的连续段必然不相邻。令 $$m$$ 为字符串 $$S$$ 连续段的数量,$$a_i$$ 为第 $$i$$ 段的长度,那么好对数量应为 $$\frac{n(n-1)}{2}-\sum_{i=1}^{m-1}a_ia_{i+1}$$,问题转化为令 $$\sum_{i=1}^{m-1}a_i*a_{i+1}$$ 最小。我们只需这样构造,令每一个字符不等于上一个字符,当只剩余一个字符时,全放在字符串最后即可。

代码:

#include <bits/stdc++.h>
#define int long long
#define ull unsigned long long
#define ios ios::sync_with_stdio(0)
#define endl '\n'
#define pii pair<int, int>
#define debug(x) cout << "x = " << x << endl;
#define lowbit(x) (x & (-x))
using namespace std;
const int N = 5e5 + 5, P = 13331;
struct node
{
    int id, num;
    bool operator<(const node &u) const
    {
        return num < u.num;
    }
};
int n;
void solve()
{
    int T = 1;
    cin >> T;
    while (T--)
    {
        node a[26];
        cin >> n;
        char c;
        for (int i = 0; i < 26; i++)
            a[i].id = i, a[i].num = 0;
        for (int i = 0; i < n; i++)
        {
            cin >> c;
            a[c - 'a'].num++;
        }
        sort(a, a + 26);
        int st = 0, ed = 25;
        while (a[st].num == 0)
            st++;
        int cnt = 0;
        while (cnt < n)
        {
            if (cnt % 2 == 0)
            {
                if (a[ed].num == 0)
                {
                    ed--;
                }
                cout << (char)('a' + a[ed].id);
                a[ed].num--;
                cnt++;
            }
            else
            {
                if (a[st].num == 0)
                {
                    st++;
                }
                cout << (char)('a' + a[st].id);
                a[st].num--;
                cnt++;
            }
        }
        cout << endl;
    }
}
signed main()
{
    ios;
    solve();
    return 0;
}

D1. Turtle and a MEX Problem (Easy Version)

思路:思路很好想到,设 $$u_i$$ 为序列 $$i$$ 最小的没有出现的非负整数,$$v_i$$ 为序列 $$i$$ 次小的没有出现的非负整数,容易发现 $$x$$ 经过若干次操作,最大值为 $$\max(\sum^n_{i=1}v_i,x)$$。不让设 $$\sum^n_{i=1}v_i$$ 为 $$k$$,则最后的答案为 $$\sum_{i=0}kk+\sum_{i=k+1}mi$$,因为 $$m \leq 10^9$$,所以后面那一段需要用等差数列求和公式。(赛时脑抽了,没注意到...)

代码:

#include <bits/stdc++.h>
#define int long long
#define ull unsigned long long
#define ios ios::sync_with_stdio(0)
#define endl '\n'
#define pii pair<int, int>
#define debug(x) cout << "x = " << x << endl;
#define lowbit(x) (x & (-x))
using namespace std;
const int N = 5e5 + 5, P = 13331;
map<int, int> mp;
void solve()
{
    int T = 1;
    cin >> T;
    while (T--)
    {
        int mx = 0, n, m;
        cin >> n >> m;
        for (int i = 1; i <= n; i++)
        {
            int l;
            cin >> l;
            mp.clear();
            for (int j = 1, x; j <= l; j++)
            {
                cin >> x;
                mp[x] = 1;
            }
            bool f = false;
            for (int j = 0; j <= l + 2; j++)
            {
                if (mp[j] == 0)
                {
                    if (!f)
                        f = true;
                    else
                    {
                        mx = max(mx, j);
                        break;
                    }
                }
            }
        }
        int cnt1 = min(mx, m) + 1, cnt2 = max(m - mx, (int)0);
        int ans = cnt1 * mx + (mx + 1 + m) * cnt2 / 2;
        cout << ans << endl;
    }
}
signed main()
{
    ios;
    solve();
    return 0;
}

D2. Turtle and a MEX Problem (Hard Version)

待补......

标签:cnt,cout,int,ios,968,Codeforces,long,Div,define
From: https://www.cnblogs.com/zc-study-xcu/p/18380997

相关文章

  • [ARC182C] Sum of Number of Divisors of Product 题解
    题目链接点击打开链接题目解法我怎么不会分治/fn首先把\(x\)分解成\(\prodp_i^{x_i}(0\lei\le5)\)的形式,正因数个数为\(\prod(x_i+1)\)有一个很牛的想法是:合并两个\(x_i\)序列(假设一个叫\(x_0,...,x_5\),另一个叫\(y_0,...,y_5\))先不考虑后面的\(+1\)(可以最后......
  • Codeforces Round 968 (Div. 2)
    良心出题人给了中文题解!!!A.TurtleandGoodStrings长度为\(n\)的字符串至少分成两段,使\(\foralli<j\),第\(i\)段的首字符不等于第\(j\)段的尾字符第一个字符一定作为首字符,最后一个字符一定作为尾字符,只要判断这两个字符是否相等即可相等的话一定无解,不相等一定有......
  • EPIC Institute of Technology Round August 2024 (Div. 1 + Div. 2)
    Preface两个礼拜前打的比赛拖到现在才写博客,我只能说也是个神人了这场其实D2很快就想到做法了,但自己把自己给否了,后面不管了实现了一发交上去发现过了然后这天由于12点左右室友就关灯睡觉了,我写完D2后看了眼E没仔细想就睡觉去了,后面发现E其实很trivialA.Distance......
  • Codeforces Round #900 (Div. 3)
    三年之后第一次打比赛,用小号打了场\(Div.3\),居然没有AK,感觉实力退步到小学了。A.HowMuchDoesDaytonaCost?若只判断题,只要判断\(\{a_n\}\)中是否存在\(k\)即可。B.AleksaandStack构造方法不唯一,我直接输出奇数列,显然正确。C.VasilijeinCacak若只判断题......
  • Codeforces Round 967 (Div. 2) - C
    题意概述这是一个交互题。有一棵有\(n\)个节点的树,节点编号从\(1\)到\(n\),并要求你使用以下类型的查询来猜测它:"?ab"会告诉你哪个节点\(x\)在点\(a\)和\(b\)之间(\(|d(a,x)-d(b,x)|\)的值最小),其中\(d(x,y)\)是节点\(x\)和\(y\)之间的距离。如果存在不......
  • CodeForces - 1353D Constructing the Array
    CodeForces-1353D这道题也可能比较简单,主要是要想到优先队列要怎么使用,这一点如果用递归会写不了但是因为对优先队列不太熟悉,只有被提示可以用优先队列才想到要怎么用,还是很重要的STL注意运算符的重构应该反着来写其他的思维很朴素,运算符的重构就是,先比较长度,优先用长度长......
  • CodeForces-431C k-Tree
    感觉这个题的dp还是有点思维的,可能就是我现在能做到的题目的天花板级别的了,dp还是要点灵感感觉,以下是代码,可能要写题解要过好久,先水着include<bits/stdc++.h>defineintlonglongdefineendl'\n'usingnamespacestd;constintmod=1000000007;intn,k,d;intdp[200][2]=......
  • CodeForces - 1336A Linova and Kingdom
    CodeForces-1336A就差一点点,很可惜,少发现个很显而易见的结论就是一个点的价值,实际上就是(这个点的深度-之后的点的数目)就是\(depth_i-size_i\)然后只要用dfs维护就好了然后把一个点的价值用STL优先队列放在一起,贪心完成。但是可能也算不上什么贪心,因为是很朴素的东西......
  • Codeforces Round 967 (Div. 2)
    A.MakeAllEqual签到题,先确定最终答案,然后把其他数删去,转化为统计众数点击查看代码#include<bits/stdc++.h>usingnamespacestd;#definelllonglong#defineullunsignedlonglongintread(){intx=0;boolf=false;charc=getchar();while(c<......
  • Codeforces Round 967(Div.2)之赛后总结
    CodeforcesRound967(Div.2)传送锚点A.MakeAllEqual1.题面分析废话这么多,说白了就是求总数减去最多出现数的个数的值。2.解法直接用桶装一下,然后扫一遍维护最大值。3.code#include<bits/stdc++.h>usingnamespacestd;typedeflonglongll;constintN=1e6+520;......