CodeForces - 1430E String Reversal(线段树+模拟)
題目鏈接:點(diǎn)擊查看
題目大意:給出一個(gè)字符串 sss ,令其反轉(zhuǎn)的串為 ttt ,每次操作可以將 ttt 中的兩個(gè)相鄰位置的字符交換,問(wèn)最少需要進(jìn)行多少次操作才能使得 ttt 變成 sss
題目分析:假設(shè)字符 chchch 在 sss 中的出現(xiàn)位置為 p1,p2...pkp_1,p_2...p_kp1?,p2?...pk?,在 ttt 中出現(xiàn)的位置為 q1,q2,...qkq_1,q_2,...q_kq1?,q2?,...qk?,那么顯然讓 pip_ipi? 和 qiq_iqi? 匹配一定是最優(yōu)的
如此一來(lái)可以初始時(shí)將 sss 中每個(gè)字符出現(xiàn)的位置開(kāi)個(gè)桶記錄一下,然后掃一遍字符串 ttt,對(duì)于 tit_iti? 來(lái)說(shuō),找到 sss 中相應(yīng)的位置 pospospos,將 sposs_{pos}spos? 一直操作至 sposs_{pos}spos? 到達(dá)字符串 sss 的開(kāi)頭即可,貢獻(xiàn)就是 pos?1pos-1pos?1。因?yàn)槊看纹ヅ渲蟮淖址伎梢灾苯訌?sss 和 ttt 中刪除(對(duì)后續(xù)貢獻(xiàn)無(wú)影響),所以 tit_iti? 在刪除之后的字符串中下標(biāo)總是為 111,我們只需要用線(xiàn)段樹(shù)維護(hù)一下 sposs_{pos}spos? 的下標(biāo)用于記錄貢獻(xiàn)即可
代碼:
// Problem: E. String Reversal // Contest: Codeforces - Educational Codeforces Round 96 (Rated for Div. 2) // URL: https://codeforces.com/contest/1430/problem/E // Memory Limit: 256 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> #include<list> #include<unordered_map> #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; vector<int>node[30]; int cnt[30]; char s[N],t[N]; struct Node {int l,r,lazy; }tree[N<<2]; void build(int k,int l,int r) {tree[k]={l,r,0};if(l==r) {return;}int mid=(l+r)>>1;build(k<<1,l,mid);build(k<<1|1,mid+1,r); } void update(int k,int l,int r,int val) {if(l>r) {return;}if(tree[k].l>r||tree[k].r<l) {return;}if(tree[k].l>=l&&tree[k].r<=r) {tree[k].lazy+=val;return;}update(k<<1,l,r,val);update(k<<1|1,l,r,val); } int query(int k,int pos) {if(tree[k].l==tree[k].r) {return pos+tree[k].lazy;}int mid=(tree[k].l+tree[k].r)>>1;if(pos<=mid) {return query(k<<1,pos)+tree[k].lazy;} else {return query(k<<1|1,pos)+tree[k].lazy;} } int main() { #ifndef ONLINE_JUDGE // freopen("data.in.txt","r",stdin); // freopen("data.out.txt","w",stdout); #endif // ios::sync_with_stdio(false);int n;read(n);scanf("%s",s+1);strcpy(t+1,s+1);reverse(t+1,t+1+n);build(1,1,n);for(int i=1;i<=n;i++) {node[s[i]-'a'].push_back(i);}LL ans=0;for(int i=1;i<=n;i++) {int to=t[i]-'a';int pos=node[to][cnt[to]];ans+=query(1,pos)-1;update(1,pos+1,n,-1);cnt[to]++;}cout<<ans<<endl;return 0; }總結(jié)
以上是生活随笔為你收集整理的CodeForces - 1430E String Reversal(线段树+模拟)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: CodeForces - 1526D K
- 下一篇: AtCoder - arc120_c S