Submission #3231292
Source Code Expand
#include <iostream> #include <vector> using namespace std; typedef long long ll; string S; vector<vector<int>> v(26); int A[26] = {0},id[26] = {0}; ll merge_count(vector<ll> &a){ int n = a.size(); if(n<=1) return 0; ll cnt = 0; vector<ll> b(a.begin(),a.begin() + n/2); vector<ll> c(a.begin() + n/2, a.end()); cnt += merge_count(b); cnt += merge_count(c); int ai = 0,bi = 0,ci = 0; while(ai<n){ if(bi<b.size() && (ci == c.size() || b[bi] <= c[ci])){ a[ai++] = b[bi++]; }else{ cnt += n/2 - bi; a[ai++] = c[ci++]; } } return cnt; } int main(){ cin >> S; string T = ""; int N = S.size(); for(int i=0;i<N;i++){ int t = S[i]-'a'; T = T + S[N-i-1]; A[t]++; v[t].push_back(-1); } int count = 0; for(int i=0;i<26;i++){ if(A[i]%2==1) count++; } if(count>=2){ cout << -1 << endl; return 0; } vector<ll> L,R; int l = 0; for(int i=0;i<N;i++){ int c = S[i]-'a'; if(A[c]%2==1 && 2*id[c]+1==v[c].size()){ L.push_back(N/2); id[c]++; } else{ if(id[c]*2<v[c].size()){ L.push_back(l); v[c][id[c]] = l; l++; id[c]++; }else{ int t = v[c].size()-id[c]-1; L.push_back(N-v[c][t]-1); id[c]++; } } } l = 0; for(int i=0;i<26;i++) id[i] = 0; for(int i=0;i<N;i++){ int c = T[i]-'a'; if(A[c]%2==1&& 2*id[c]+1==v[c].size()){ R.push_back(N/2); id[c]++; } else{ if(id[c]*2<v[c].size()){ R.push_back(l); v[c][id[c]] = l; l++; id[c]++; }else{ int t = v[c].size()-id[c]-1; R.push_back(N-v[c][t]-1); id[c]++; } } } cout << min(merge_count(L),merge_count(R)) << endl; }
Submission Info
Submission Time | |
---|---|
Task | E - Papple Sort |
User | idsigma |
Language | C++14 (GCC 5.4.1) |
Score | 800 |
Code Size | 1688 Byte |
Status | AC |
Exec Time | 1298 ms |
Memory | 8024 KB |
Judge Result
Set Name | Sample | All | ||||
---|---|---|---|---|---|---|
Score / Max Score | 0 / 0 | 800 / 800 | ||||
Status |
|
|
Set Name | Test Cases |
---|---|
Sample | s1.txt, s2.txt, s3.txt |
All | 01.txt, 02.txt, 03.txt, 04.txt, 05.txt, 06.txt, 07.txt, 08.txt, 09.txt, 10.txt, 11.txt, 12.txt, 13.txt, 14.txt, 15.txt, 16.txt, 17.txt, 18.txt, 19.txt, 20.txt, 21.txt, 22.txt, 23.txt, 24.txt, 25.txt, 26.txt, 27.txt, 28.txt, 29.txt, 30.txt, 31.txt, 32.txt, 33.txt, 34.txt, 35.txt, 36.txt, 37.txt, 38.txt, 39.txt, 40.txt, 41.txt, 42.txt, 43.txt, 44.txt, 45.txt, 46.txt, 47.txt, s1.txt, s2.txt, s3.txt |
Case Name | Status | Exec Time | Memory |
---|---|---|---|
01.txt | AC | 1227 ms | 7932 KB |
02.txt | AC | 1250 ms | 7812 KB |
03.txt | AC | 1229 ms | 7996 KB |
04.txt | AC | 1240 ms | 7808 KB |
05.txt | AC | 1221 ms | 7964 KB |
06.txt | AC | 1238 ms | 7756 KB |
07.txt | AC | 1235 ms | 8024 KB |
08.txt | AC | 1265 ms | 7832 KB |
09.txt | AC | 1243 ms | 7956 KB |
10.txt | AC | 1226 ms | 7856 KB |
11.txt | AC | 1168 ms | 2444 KB |
12.txt | AC | 1152 ms | 2000 KB |
13.txt | AC | 1152 ms | 2088 KB |
14.txt | AC | 1157 ms | 2612 KB |
15.txt | AC | 1224 ms | 7844 KB |
16.txt | AC | 1236 ms | 7716 KB |
17.txt | AC | 1298 ms | 7904 KB |
18.txt | AC | 1246 ms | 7684 KB |
19.txt | AC | 1252 ms | 7900 KB |
20.txt | AC | 1189 ms | 2200 KB |
21.txt | AC | 1249 ms | 7904 KB |
22.txt | AC | 1242 ms | 7688 KB |
23.txt | AC | 1249 ms | 7872 KB |
24.txt | AC | 1170 ms | 1964 KB |
25.txt | AC | 1251 ms | 7860 KB |
26.txt | AC | 1243 ms | 7732 KB |
27.txt | AC | 1241 ms | 7888 KB |
28.txt | AC | 1197 ms | 7704 KB |
29.txt | AC | 1204 ms | 7908 KB |
30.txt | AC | 1200 ms | 2216 KB |
31.txt | AC | 1252 ms | 7896 KB |
32.txt | AC | 1160 ms | 1868 KB |
33.txt | AC | 1222 ms | 7712 KB |
34.txt | AC | 1165 ms | 1964 KB |
35.txt | AC | 1184 ms | 7808 KB |
36.txt | AC | 1141 ms | 2424 KB |
37.txt | AC | 1166 ms | 2320 KB |
38.txt | AC | 1195 ms | 2164 KB |
39.txt | AC | 1245 ms | 7920 KB |
40.txt | AC | 1171 ms | 2060 KB |
41.txt | AC | 1 ms | 256 KB |
42.txt | AC | 1 ms | 256 KB |
43.txt | AC | 1 ms | 256 KB |
44.txt | AC | 1 ms | 256 KB |
45.txt | AC | 1 ms | 256 KB |
46.txt | AC | 1 ms | 256 KB |
47.txt | AC | 1 ms | 256 KB |
s1.txt | AC | 1 ms | 256 KB |
s2.txt | AC | 1 ms | 256 KB |
s3.txt | AC | 1 ms | 256 KB |