首页 > 其他分享 >Codeforces Round #723 (Div. 2)D. Kill Anton 贪心,逆序对个数

Codeforces Round #723 (Div. 2)D. Kill Anton 贪心,逆序对个数

时间:2023-02-08 16:03:59浏览次数:50  
标签:body vc string int Anton Codeforces Kill id


problem

D. Kill Anton
time limit per test2 seconds
memory limit per test512 megabytes
inputstandard input
outputstandard output
After rejecting 10100 data structure problems, Errorgorn is very angry at Anton and decided to kill him.

Anton’s DNA can be represented as a string a which only contains the characters “ANTON” (there are only 4 distinct characters).

Errorgorn can change Anton’s DNA into string b which must be a permutation of a. However, Anton’s body can defend against this attack. In 1 second, his body can swap 2 adjacent characters of his DNA to transform it back to a. Anton’s body is smart and will use the minimum number of moves.

To maximize the chance of Anton dying, Errorgorn wants to change Anton’s DNA the string that maximizes the time for Anton’s body to revert his DNA. But since Errorgorn is busy making more data structure problems, he needs your help to find the best string B. Can you help him?

Input
The first line of input contains a single integer t (1≤t≤100000) — the number of testcases.

The first and only line of each testcase contains 1 string a (1≤|a|≤100000). a consists of only the characters “A”, “N”, “O” and “T”.

It is guaranteed that the sum of |a| over all testcases does not exceed 100000.

Output
For each testcase, print a single string, b. If there are multiple answers, you can output any one of them. b must be a permutation of the string a.

Example
inputCopy
4
ANTON
NAAN
AAAAAA
OAANTTON
outputCopy
NNOTA
AANN
AAAAAA
TNNTAOOA
Note
For the first testcase, it takes 7 seconds for Anton’s body to transform NNOTA to ANTON:

NNOTA → NNOAT → NNAOT → NANOT → NANTO → ANNTO → ANTNO → ANTON.

Note that you cannot output strings such as AANTON, ANTONTRYGUB, AAAAA and anton as it is not a permutation of ANTON.

For the second testcase, it takes 2 seconds for Anton’s body to transform AANN to NAAN. Note that other strings such as NNAA and ANNA will also be accepted.

solution

//相同字母一定是连着的
//顺序的话,4!枚举,操作次数是编号后逆序对进行比较
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
int z[50];
vector<int>id[50], vc;
LL nxd = 0;
void MergeSort(int l, int r){
if(l >= r)return ;
int m = l+r>>1;
MergeSort(l,m);
MergeSort(m+1,r);
int i = l, j = m+1;
int t[r-l+1], k = 0;
while(i<=m && j<=r){
if(vc[i]<=vc[j])t[k++]=vc[i++];
else{
t[k++] = vc[j++];
nxd += m-i+1;//加上剩余元素个数
}
}
while(i<=m)t[k++]=vc[i++];
while(j<=r)t[k++]=vc[j++];
for(i=l, k=0; i <= r; i++,k++)vc[i]=t[k];
}
int main(){
ios::sync_with_stdio(false);
int T; cin>>T;
while(T--){
memset(z,0,sizeof(z));
id['A'-'A'].clear();
id['N'-'A'].clear();
id['O'-'A'].clear();
id['T'-'A'].clear();
string s; cin>>s;
for(int i = 0; i < s.size(); i++){
z[s[i]-'A']++;
id[s[i]-'A'].push_back(i+1);
}
int r[5] = {0,'A','N','O','T'};
LL ans = 0; string as;
do{
//vector<int>vc;
vc.clear();
nxd = 0;
for(int i = 1; i <= 4; i++){
vc.insert(vc.end(),id[r[i]-'A'].begin(),id[r[i]-'A'].end());
}
MergeSort(0,vc.size()-1);
if(nxd>ans){
ans = nxd;
as = "";
for(int i = 1; i <= 4; i++)
as += string(z[r[i]-'A'],(char)r[i]);
}
}while(next_permutation(r+1,r+5));
//cout<<ans<<"\n";
if(ans!=0)cout<<as<<"\n";
else cout<<s<<"\n";
}
return 0;
}


标签:body,vc,string,int,Anton,Codeforces,Kill,id
From: https://blog.51cto.com/gwj1314/6044575

相关文章

  • Codeforces Round #833 (Div. 2)-D. ConstructOR -构造、位运算
    题目:https://codeforces.com/problemset/problem/1748/D当时自己做的时候完全没什么想法,可能一个原因就是当时总想找充要条件首先题解说了一个很显然的性质,即\((a,b,d)......
  • Codeforces Round #846 (Div. 2)
    链接:https://codeforces.com/contest/1780\(B\)一个显然但是不会证明的结论是分成段数尽可能少时\(gcd\)会取到极大值,也就是说这道题里面分成两段一定可以取到最大,所以......
  • Codeforces Round #850 (Div. 2, based on VK Cup 2022 - Final Round)
    题目链接\(A_1\)核心思路不要傻乎乎的模拟先观察性质。(2,3)(4,5)(6,7)(8,9).我们可以发现a的模4都是大于1的,b的都是小于。发现这个性质之后就可以写代码了。//P......
  • Codeforces Round #848 (Div. 2)(B,C,D)
    CodeforcesRound#848(Div.2)(B,C,D)哎呀,最近不知道怎么回事,题目老是没看懂,还是心不在此,太烦了,好几天都只是一道题了BB这道题到看答案才发现我理解错了,我怎么说越想越......
  • Codeforces Round #850 Div. 2,
    《B.CakeAssemblyLine》思维题目大意:给定n个蛋糕,其在x轴上占有范围[cl,cr]还有n个巧克力喷管,其在x轴上也有范围[dl,dr]现在就是问:是否可以通......
  • Educational Codeforces Round 142 E. Divisors and Table
    链接:https://codeforces.com/problemset/problem/1792/E题意:给定n*n乘法表,第a[i][j]=i*j,给定m=m1*m2,求m的所有因子中出现在表中的最小行的xor和。n<=1e9,m......
  • CodeForces - 240D. Merging Two Decks 贪心+暴力
    Therearetwodecksofcardslyingonthetableinfrontofyou,somecardsinthesedeckslayfaceup,someofthemlayfacedown.Youwanttomergetheminto......
  • CodeForces - 240 C. Practice 模拟或者二分或者规律
    LittletimeisleftbeforeBerlandannualfootballchampionship.Thereforethecoachofteam"LosewilleRangers"decidedtoresumethepractice,thatwereindef......
  • CodeForces - 234 F. Fence DP
    F.Fencetimelimitpertest2secondsmemorylimitpertest256megabytesinputinput.txtoutputoutput.txtVasyashouldpaintafenceinfrontofhisowncottage.The......
  • CodeForces - 237D T-decomposition 构造
    D.T-decompositiontimelimitpertest2secondsmemorylimitpertest256megabytesinputstandardinputoutputstandardoutputYou'vegotaundirectedtree s,consist......