CodeForces - 1526D Kill Anton(模拟)
生活随笔
收集整理的這篇文章主要介紹了
CodeForces - 1526D Kill Anton(模拟)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題目鏈接:https://vjudge.net/problem/CodeForces-1526D
題目大意:給出一個只有四種字母組成的字符串 AAA,要求將其重排列 BBB,使得貢獻最大。貢獻指的是,每次可以交換相鄰的兩個字母,問從 AAA 變成 BBB 的最小操作次數
題目分析:猜的結論,就是相同的字母一定連續,具體證明可以參考官方題解
然后思維上就沒什么難度了,剩下的就是如何 O(n)O(n)O(n) 或者 O(nlogn)O(nlogn)O(nlogn) 實現 calcalcal 函數用來計算兩個字符串的貢獻了
因為字符集比較小,而且還是連續的,所以我直接枚舉 BBB 串,每次去 AAA 串從前往后掃,當一個 BBB 串中一個字符用完之后,將 AAA 串的游標置零再重新開始就可以了,常數是 444
就是實現的時候感覺蠻難調的
代碼:
// Problem: D. Kill Anton // Contest: Codeforces - Codeforces Round #723 (Div. 2) // URL: https://codeforces.com/contest/1526/problem/D // Memory Limit: 512 MB // Time Limit: 2000 ms // // Powered by CP Editor (https://cpeditor.org)// #pragma GCC optimize(2) // #pragma GCC optimize("Ofast","inline","-ffast-math") // #pragma GCC target("avx,sse2,sse3,sse4,mmx") #include<iostream> #include<cstdio> #include<string> #include<ctime> #include<cmath> #include<cstring> #include<algorithm> #include<stack> #include<climits> #include<queue> #include<map> #include<set> #include<sstream> #include<cassert> #include<bitset> #define lowbit(x) x&-x using namespace std; typedef long long LL; typedef unsigned long long ull; template<typename T> inline void read(T &x) {T f=1;x=0;char ch=getchar();while(0==isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}while(0!=isdigit(ch)) x=(x<<1)+(x<<3)+ch-'0',ch=getchar();x*=f; } template<typename T> inline void write(T x) {if(x<0){x=~(x-1);putchar('-');}if(x>9)write(x/10);putchar(x%10+'0'); } const int inf=0x3f3f3f3f; const int N=1e6+100; int cnt[4],a[N]; char str[]="AOTN"; int get_id(char ch) {if(ch=='A') {return 0;} else if(ch=='O') {return 1;} else if(ch=='T') {return 2;} else {return 3;} } string s; bool ban[N]; LL check(string a) {LL ans=0;int pos=0;int cnt=1;for(int i=0;i<(int)a.size();i++) {if(i>0&&a[i]!=a[i-1]) {pos=0;cnt=!ban[pos];}while(s[pos]!=a[i]) {pos++;if(!ban[pos]) {cnt++;}}ban[pos]=true;ans+=cnt-1; while(ban[pos]) {pos++;}}return ans; } int main() { #ifndef ONLINE_JUDGE // freopen("data.in.txt","r",stdin); // freopen("data.out.txt","w",stdout); #endif // ios::sync_with_stdio(false);int w;cin>>w;while(w--) {memset(cnt,0,sizeof(cnt));cin>>s;for(auto it:s) {cnt[get_id(it)]++;}for(int i=0;i<4;i++) {a[i]=i;}LL mmax=-1;string ans;do {string ss;for(int i=0;i<4;i++) {for(int j=0;j<cnt[a[i]];j++) {ss+=str[a[i]];}}memset(ban,0,s.size()+5);LL tmp=check(ss);if(tmp>mmax) {mmax=tmp;ans=ss;}}while(next_permutation(a,a+4));cout<<ans<<endl;}return 0; } 超強干貨來襲 云風專訪:近40年碼齡,通宵達旦的技術人生總結
以上是生活随笔為你收集整理的CodeForces - 1526D Kill Anton(模拟)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: CodeForces - 1494E A
- 下一篇: CodeForces - 1430E S