[ARC088C] Papple Sort
首先,回文假如只考虑 2 个字符,那么它是相对的。
即 A...A,我们只需要考虑 A 是否能匹配到 A。
考虑一个字符的移动代价,显然假如在 $pos$,那么全局移动代价就是 $n-pos$。
对于 A1...A2...A3...A4 这种情况,显然 A1,A4匹配,A2,A3匹配最优。即,能包含不交叉。
考虑扫描线的形式,将每个对应字符都移动到相对位置上的 n,由于当 $i <x$ 扫描线经过 i 时将 i 的对应字符移动到 n ,并消除 i,n 的贡献。那么接下来,x 的对应字符 $y,y>i'$ 的移动代价就是 n-y-1。即每次的 n 只是为了查询方便,但实际上这个代价可能移动不到 n 只是对于当前扫描线来说是 n (边界)。
那么考虑长度为奇数的回文串,一定有且仅有一个奇数出现次数的字符,那么这个字符一定是在中点。
但全局中点是 $\dfrac{n+1}{2}$,显然在扫描线的过程中如果将这个字符移动后的位置看成全局中点是不行的。需要考虑相对中点的位置或者移动代价。我们假如现在有 $[l,r]$ 那么中点位置就是 $\dfrac{l+r}{2}$ ,那么实际上代价也相当于这样,即 $\dfrac{query(n)-query(x)}{2}$,就是移动代价。
至于动态维护相对位置的,可以使用树状数组。
实际上,我认为这是一种从相对上进行考虑的想法。
#include <cstdio> #include <algorithm> #include <iostream> #include <cstring> #include <vector> #include <cmath> #include <queue> #include <map> #define ll long long using namespace std; int rd() { int f=1,sum=0; char ch=getchar(); while(!isdigit(ch)) {if(ch=='-') f=-1;ch=getchar();} while(isdigit(ch)) {sum=(sum<<3)+(sum<<1)+ch-'0';ch=getchar();} return sum*f; } ll lrd() { ll f=1,sum=0; char ch=getchar(); while(!isdigit(ch)) {if(ch=='-') f=-1;ch=getchar();} while(isdigit(ch)) {sum=(sum<<3)+(sum<<1)+ch-'0';ch=getchar();} return sum*f; } const int N=(int)(2e5+5); vector<int>p[26]; char s[N]; bool vis[N]; int n,sum[N]; int lowbit(int x) { return x&(-x); } void upt(int x,int v) { while(x<=n) sum[x]+=v,x+=lowbit(x); } int qry(int x) { int res=0; while(x) res+=sum[x],x-=lowbit(x); return res; } int main() { scanf("%s",s+1); n=strlen(s+1); for(int i=1;i<=n;i++) p[s[i]-'a'].push_back(i); bool fl=0; for(int i=0;i<26;i++) if(p[i].size()%2) { if(fl) return puts("-1"),0; fl=1; } for(int i=1;i<=n;i++) upt(i,1); ll ans=0; for(int i=1;i<=n;i++) { if(vis[i]) continue; int j=p[s[i]-'a'].back(); if(i==j) ans+=1ll*(qry(n)-qry(j))/2; else ans+=1ll*qry(n)-qry(j); vis[i]=vis[j]=1; upt(i,-1); if(i!=j) upt(j,-1); p[s[i]-'a'].erase(p[s[i]-'a'].end()-1); } printf("%lld",ans); return 0; }