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

Codeforces Round 915 (Div. 2)

时间:2023-12-17 21:34:54浏览次数:298  
标签:std 字符 end int Codeforces pos 序列 915 Div

基本情况

A题还没进入状态,卡了快10分钟。

B题一开始想复杂了,以为是树的直径,后面推出来发现针对叶子数目讨论就行了,正确思路出来太慢了(一个半小时)。

C题留了半个多小时,随便口胡了一个LIS思路,但是判断无解没思路。

C. Largest Subsequence

Problem - C - Codeforces

首先我们把字典序最大的子序列找出,方法就是从大到小枚举每个字母,然后能加就加入.

然后我们可以发现我们能做的只有对这个子序列排序,每次循环位移之后一定是把子序列最后的字符(最小的字符)放到最前面,然后相当于把这个字符从子序列里删掉了.

所以我们只需要判断把这个序列排序后能否使得原序列有序即可.

操作次数是子序列除了最大的字符以外的其他字符的数量.

void solve()
{
	int n; std::string s;
    std::cin >> n >> s;
    std::vector<std::vector<int> > pos(26);
    for(int i = 0; i < n; i++)
        pos[s[i] - 'a'].push_back(i);
    std::vector<int> p;
    int mx = -1;
    int cnt = 0;
    for(int i = 25; i >= 0; i--)
	{
        auto it = upper_bound(pos[i].begin(), pos[i].end(), mx);
        p.insert(p.end(), it, pos[i].end());
        if (cnt == 0) cnt = p.size();
        if (!p.empty()) mx = p.back();
    }
    auto q = p;
    std::sort(q.begin(), q.end(), [&](int x, int y){return s[x] < s[y];});
    std::string t = s;
    for(int i = 0; i < p.size(); i++)
        t[p[i]] = s[q[i]];
    if (!std::is_sorted(t.begin(), t.end()))
        std::cout << -1 << '\n';
    else
        std::cout << p.size() - cnt << '\n';
}

标签:std,字符,end,int,Codeforces,pos,序列,915,Div
From: https://www.cnblogs.com/kdlyh/p/17909870.html

相关文章

  • Educational Codeforces Round 134 (Rated for Div. 2)
    基本情况AB秒了。C搞了一个错的二分答案,虽然过样例了。C.Min-MaxArrayTransformation错误分析没有进一步推导性质,而是觉得数据单调递增估计是二分,然后就无脑写,实际上check的正确性没有保证。boolcheck(intind,intnow){ if(ind==now)returntrue; if(b[ind]......
  • Codeforces Round 867 (Div. 3)
    CodeforcesRound867(Div.3)A:ABCD(E差一点点,最后把那种特殊情况想出来然后没写上去就结束了)A.TubeTubeFeed题意:给两个数组分别是时间和价值,要价值最大但是只能选一个思路:最开始以为是01背包,结果只选一个,一个一个枚举就行#include<bits/stdc++.h>usingnamespace......
  • Codeforces Round 855 (Div. 3)
    CodeforcesRound855(Div.3)A.IsItaCat?题意:要求:字符串必须以只包含字母"m"或"M"的非空序列开始必须紧跟由'e'或'E'字符组成的非空序列必须紧接着仅由字符'o'或'O'组成的非空序列必须紧接着是仅由字符'w'或'W'组成的非空序列思路:一个一个来(WA了三发注意这几......
  • Codeforces Round 913 (Div. 3)
    CodeforcesRound913(Div.3)A:ABCA.Rook简单题,就两个循环搞定(直接上码)#include<bits/stdc++.h>usingnamespacestd;voidsolve(){chara;intb;cin>>a>>b;for(inti=1;i<=8;i++){if(i!=b){......
  • Codeforces Round 863 (Div. 3)
    CodeforcesRound863(Div.3)A.InsertDigit题意:插入一个字母使得这个数字最大化思路:只要从前往后便利就行#include<iostream>usingnamespacestd;voidsolve(){intn,k;cin>>n>>k;boolx=true,y=false;for(inti=0;i<n;i++){......
  • Codeforces Round 913 (Div. 3)
    CF1907总结A.Rook题面翻译给出车在国际象棋棋盘中的位置,输出其可到达的坐标(不必在意顺序)。车可以横着或竖着走任意格数。分析题意明了,输出车所在行和列所有格子的序号(除车所在位置外)。code#include<bits/stdc++.h>usingnamespacestd;intt;voidsolve(){ string......
  • Codeforces Round 891 (Div3)
    CodeforcesRound891(Div.3)A.ArrayColoring这个我vp的时候写复杂了,想不到答案的思路这么清晰,将两部分分别看,将偶数加进去其奇偶性不变,只有奇数加进去才会改变奇偶性,so只有改变偶数次奇偶性才能使其奇偶性相同,所以cnt%2==0.#include<bits/stdc++.h>usingnamespacestd;......
  • Codeforces Round 816 (Div. 2) VP
    基本情況A秒了,B错一次之后也过了,C没思路。B.BeautifulArrayProblem-B-Codeforcesvoidsolve(){ longlongn,k,b,s; memset(ans,0,sizeof(ans)); std::cin>>n>>k>>b>>s; if(s<b*k){std::cout<<"-1\n";ret......
  • Codeforces Round 815 (Div. 2)
    基本情况脑子太不清楚了。A题有思路,但是各种细节问题(数论题太不熟练了),错了好几次才过。B题直接分析数据愣猜一个解,猜对了。A.BurenkaPlayswithFractionsProblem-A-Codeforces难点在分析输出\(1\)的情况。我的想法是通分,然后直接比分子是否互相整除,想法很对,但是......
  • Codeforces Round 915 (Div. 2)
    A.ConstructiveProblems看了一眼数据,猜测可能是n,m里的最大#include<bits/stdc++.h>usingnamespacestd;voidsolve(){ inta,b; cin>>a>>b; intans=max(a,b); cout<<ans<<"\n";}intmain(){ ios::sync_with_stdio(false);cin.tie......