제출 #1163524

#제출 시각아이디문제언어결과실행 시간메모리
1163524kxdEkoeko (COCI21_ekoeko)C++20
110 / 110
62 ms12876 KiB
#include <bits/stdc++.h>
//#define DEBUG 1106
#define int long long
#define ll long long
#define ld long double
#define pb emplace_back
#define p_q priority_queue
#define m_p make_pair
#define pii pair<int,int>
#define endl '\n'
#define INIT ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define FOR(i,a,b) for(int i = a; i <= b; i++)
#define forn(i,n) for (int i = 0; i < n; i++)
#define forn1(i,n) for (int i = 1; i <= n; i++)
#define all(x) x.begin(),x.end()
#define ft first
#define sd second
#define lowbit(x) (x&(-x))
#define chmax(x,y) x=max(x,y)
#define chmin(x,y) x=min(x,y)
#ifdef DEBUG
#define debug(x) cout << #x << ": " << x << endl;
#else
#define debug(x) 1106;
#endif

using namespace std;
const int N = 1e6+5;
const int M = 2005;
const int inf = 1e9;
const int INF = 1e18;
const int MOD = 1e9+7;

int a[N], b[N];
int cnt[27], c[27];
int s1[N], s2[N];

int BIT[N], n;
int num[N];
void update(int x, int y) {
	for(; x <= n; x += lowbit(x)) {
		BIT[x] += y;
	}
}

int query(int x) {
	int sum = 0;
	for(; x; x -= lowbit(x))
		sum += BIT[x];
	return sum;
}
int tot = 0;
int r(int x) {
	return lower_bound(num+1,num+1+tot,x)-num;
}


signed main() {
	INIT
	#ifdef DEBUG
	freopen("input.txt", "r", stdin);
	#endif
	///////////
	cin >> n;
	char ch;
	forn(i,2*n) {
		cin >> ch;
		int t = ch-'a';
		a[i] = t;
		b[i] = cnt[t];
		cnt[t]++;
	}
	forn(i,26) cnt[i]/=2;
	int x = 0, y = 0, ret = 0;
	map<pii,int> mp;
	forn(i,2*n) {
		if(b[i] < cnt[a[i]]) {
			ret += (i-x);
			s1[x] = a[i];
			mp[m_p(a[i],b[i])] = x;
			x++;
		} 	}
	forn(i,2*n) {
		if(b[i] >= cnt[a[i]]) {
			s2[y] = mp[m_p(a[i],c[a[i]])];
			c[a[i]]++;
			y++;
		}
	}
	tot = 0;
	int ans = 0;
	for(int i = 1; i <= n; i++) {
		a[i] = s2[i-1];
		num[i]=a[i];
	}
	sort(num+1,num+1+n);
	for (int i = 1; i <= n; i++) {
		if(i==1||num[i]!=num[i-1]) num[++tot]=num[i];
	}
	for (int i = 1; i <= n; i++) {
		a[i] = r(a[i]);
	}
	
	for (int i = 1; i <= n; i++) {
		ans += i-1-query(a[i]);
		update(a[i],1);
	}
	cout << ans + ret;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...