传送门
文章目录
- 题意:
- 思路:
题意:
给你一个只有ANTOANTOANTO四个字母的字符串,你每次可以交换相邻两个,花费为111,让后让你打乱字符串,使得将打乱的字符串还原为原来的字符串的花费最小。
n≤1e5n\le1e5n≤1e5
思路:
打了这么长时间acmacmacm了,还能因为没开LLLLLL挂题,赛后又交了将近一页,真是越来越菜了。
首先这个题通过打表以及第六感可以发现,将所有相同的字母放在一起是最优的,所以可以4!4!4!枚举所有位置,让后checkcheckcheck一下取最大的花费即可,现在瓶颈就是在怎么快速求出花费来。
我们假设原来的串为aaa,现在的串为bbb,首先我们可以从头遍历aaa,让后对于aaa的每一个字符我们肯定都找其在bbb中第一次出现的位置,用完之后删掉这个位置。可是我们将某一个删除的时候,他们的某些位置可能会变化,比如aaa从111开始,初始的时候bbb的位置是1,2,3,4,51,2,3,4,51,2,3,4,5,现在将444这个位置删掉,花费为333,那么现在bbb变为1,2,3,51,2,3,51,2,3,5,下次需要选333这里删掉的时候就会出现问题了,因为按照上面的话花费应该是3−2=13-2=13−2=1,但是实际花费是222,问题也很明显了,我们将444删掉后,应该让444前面的所有位置都+1+1+1,即变成2,3,4,52,3,4,52,3,4,5即可,这个就很好维护了,用线段树实现区间加即可。
代码写的丑的一批。
// 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("Ofast,no-stack-protector,unroll-loops,fast-math")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4.1,sse4.2,avx,avx2,popcnt,tune=native")
//#pragma GCC optimize(2)
#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
#include<map>
#include<cmath>
#include<cctype>
#include<vector>
#include<set>
#include<queue>
#include<algorithm>
#include<sstream>
#include<ctime>
#include<cstdlib>
#define X first
#define Y second
#define L (u<<1)
#define R (u<<1|1)
#define pb push_back
#define mk make_pair
#define Mid (tr[u].l+tr[u].r>>1)
#define Len(u) (tr[u].r-tr[u].l+1)
#define random(a,b) ((a)+rand()%((b)-(a)+1))
#define db puts("---")
#define int long long
using namespace std;//void rd_cre() { freopen("d://dp//data.txt","w",stdout); srand(time(NULL)); }
//void rd_ac() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//AC.txt","w",stdout); }
//void rd_wa() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//WA.txt","w",stdout); }typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> PII;const int N=1000010,mod=1e9+7,INF=0x3f3f3f3f;
const double eps=1e-6;string s,ps;
string mp[10];
int a[20];
vector<int>v[20];
struct Node {int l,r;int cnt,lazy;
}tr[N<<2];void pushup(int u) {tr[u].cnt=tr[L].cnt+tr[R].cnt;
}void pushdown(int u) {if(!tr[u].lazy) return;int lazy=tr[u].lazy; tr[u].lazy=0;tr[L].lazy+=lazy; tr[R].lazy+=lazy;tr[L].cnt+=lazy; tr[R].cnt+=lazy;
}void build(int u,int l,int r) {tr[u]={l,r,1,0};if(l==r) {tr[u].cnt=l;return;}build(L,l,Mid); build(R,Mid+1,r);pushup(u);
} void change(int u,int l,int r,int c) {if(tr[u].l>=l&&tr[u].r<=r) {tr[u].cnt+=c;tr[u].lazy+=c;return;}if(l<=Mid) change(L,l,r,c);if(r>Mid) change(R,l,r,c);pushup(u);
}int query(int u,int l,int r) {if(tr[u].l>=l&&tr[u].r<=r) return tr[u].cnt;int ans=0;pushdown(u);if(l<=Mid) ans+=query(L,l,r);if(r>Mid) ans+=query(R,l,r);return ans;
}int check(string ps) {int ans=0;build(1,1,ps.length());for(int i=0;i<4;i++) v[i].clear();for(int i=s.length()-1;i>=0;i--) {if(ps[i]=='A') v[0].pb(i+1);else if(ps[i]=='N') v[1].pb(i+1);else if(ps[i]=='O') v[2].pb(i+1);else v[3].pb(i+1);}for(int i=0;i<s.length();i++) {int pos;if(s[i]=='A') pos=v[0].back(),v[0].pop_back();else if(s[i]=='N') pos=v[1].back(),v[1].pop_back();else if(s[i]=='O') pos=v[2].back(),v[2].pop_back();else pos=v[3].back(),v[3].pop_back();int now=query(1,pos,pos);//cout<<i+1<<' '<<now<<' '<<pos<<endl;ans+=abs(now-(i+1));change(1,1,pos-1,1);}return ans;
}signed main()
{ios::sync_with_stdio(false);cin.tie(0);int _; cin>>_;while(_--) {for(int i=0;i<4;i++) mp[i]="",a[i]=i;cin>>ps; s=ps;for(int i=0;i<ps.length();i++) {if(ps[i]=='A') mp[0]+='A';else if(ps[i]=='N') mp[1]+='N';else if(ps[i]=='O') mp[2]+='O';else mp[3]+='T';}int ans=-1;string res;do {string now="";for(int i=0;i<4;i++) now+=mp[a[i]];int x=check(now);if(x>ans) ans=x,res=now;}while(next_permutation(a,a+4));cout<<res<<endl;}return 0;
}
/**/