QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#649627#7960. 排序大师hos_lyricAC ✓57ms4380kbC++146.2kb2024-10-18 06:21:002024-10-18 06:21:01

Judging History

你现在查看的是最新测评结果

  • [2024-10-18 06:21:01]
  • 评测
  • 测评结果:AC
  • 用时:57ms
  • 内存:4380kb
  • [2024-10-18 06:21:00]
  • 提交

answer

#include <cassert>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

using Int = long long;

template <class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &a) { return os << "(" << a.first << ", " << a.second << ")"; };
template <class T> ostream &operator<<(ostream &os, const vector<T> &as) { const int sz = as.size(); os << "["; for (int i = 0; i < sz; ++i) { if (i >= 256) { os << ", ..."; break; } if (i > 0) { os << ", "; } os << as[i]; } return os << "]"; }
template <class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cerr << *i << " "; cerr << endl; }
template <class T> bool chmin(T &t, const T &f) { if (t > f) { t = f; return true; } return false; }
template <class T> bool chmax(T &t, const T &f) { if (t < f) { t = f; return true; } return false; }
#define COLOR(s) ("\x1b[" s "m")


int root(vector<int> &uf, int u) {
  return (uf[u] < 0) ? u : (uf[u] = root(uf, uf[u]));
}
bool connect(vector<int> &uf, int u, int v) {
  u = root(uf, u);
  v = root(uf, v);
  if (u == v) return false;
  if (uf[u] > uf[v]) swap(u, v);
  uf[u] += uf[v];
  uf[v] = u;
  return true;
}


void exper() {
  for (int N = 1; N <= 9; ++N) {
    queue<string> que;
    map<string, int> dist;
    string ini(N, '?');
    for (int i = 0; i < N; ++i) ini[i] = '0' + i;
    dist[ini] = 0;
    que.push(ini);
    int maxC = 0;
    for (; que.size(); ) {
      const string s = que.front();
      que.pop();
      const int c = dist[s];
      // printf("%d %s\n", c, s.c_str());
      chmax(maxC, c);
      auto sub = [&](int l, int r) -> string {
        return s.substr(l, r - l);
      };
      for (int i = 0; i <= N; ++i) for (int j = i + 1; j <= N; ++j) for (int k = j; k <= N; ++k) for (int l = k + 1; l <= N; ++l) {
        const string t = sub(0, i) + sub(k, l) + sub(j, k) + sub(i, j) + sub(l, N);
        if (!dist.count(t)) {
          dist[t] = dist[s] + 1;
          que.push(t);
        }
      }
    }
    fflush(stdout);
    for (const auto &sc : dist) {
      const string &s = sc.first;
      const int c = sc.second;
      auto at = [&](int i) -> int {
        return (0 <= i && i < N) ? (s[i] - '0') : i;
      };
      vector<int> uf(N + 1, -1);
      for (int i = -1; i < N; ++i) {
        connect(uf, at(i) + 1, at(i + 1));
      }
      int k = 0;
      for (int u = 0; u <= N; ++u) if (uf[u] < 0) ++k;
      assert((N + 1 - k) % 2 == 0);
      assert(c == (N + 1 - k) / 2);
bool ok=true;for(int u=0;u<=N;++u)if(uf[u]<0)ok=ok&&(-uf[u]<=2);if(c>=2&&ok){cerr<<c<<" "<<s<<" "<<k<<endl;assert(false);}
    }
    cerr << "DONE N = " << N << ": maxC = " << maxC << endl;
  }
}

/*
  p: perm of [1, N]
  q: perm of [1, N+1]: p[i] + 1 -> p[i+1]  (0 <= i <= N)
  
  ... a [b ... c] [d ... e] f ...
  ... a [d ... e] [b ... c] f ...
    a+1 -> b,  c+1 -> d,  e+1 -> f
    a+1 -> d,  c+1 -> f,  e+1 -> b
    3
  
  ... a [b ... c] d ... e [f ... g] h ...
  ... a [f ... g] d ... e [b ... c] h ...
    a+1 -> b,  c+1 -> d,  e+1 -> f,  g+1 -> h
    a+1 -> f,  c+1 -> h,  e+1 -> b,  g+1 -> d
    2,2
  
  target: q only has 1-cycles
  always possible (by adj. swaps) ~~> q: even
  
  use (3) to break cycle
    a|b ... c|d ... e|f
    f,d,b in this order on the cycle
    bad:
      u[0] -> u[1] -> ... -> u[l-1] -> u[0]
      (u[l-1]-1)|u[0] ... (u[0]-1)|u[1] ... ... ... (u[l-2]-1)|u[l-1]
  
  use (2,2) to break 2 2-cycles
    u <-> v
    (v-1)|u ... (u-1)|v
  
  take minimal interval ~~> (u ... u-1) must contain some edge, (2,2) usable
*/

int N;
vector<int> P;

int main() {
  // exper();
  
  for (; ~scanf("%d", &N); ) {
    P.resize(N + 2);
    for (int i = 1; i <= N; ++i) {
      scanf("%d", &P[i]);
    }
    P[0] = 0;
    P[N + 1] = N + 1;
    
    for (int iter = 0; ; ++iter) {
      vector<int> Q(N + 2, 0);
      for (int i = 0; i <= N; ++i) Q[P[i] + 1] = P[i + 1];
      vector<vector<int>> cycles;
      {
        vector<int> vis(N + 2, 0);
        for (int u = 1; u <= N + 1; ++u) if (!vis[u]) {
          vector<int> cyc;
          for (int v = u; !vis[v]; v = Q[v]) {
            vis[v] = 1;
            cyc.push_back(v);
          }
          cycles.push_back(cyc);
        }
      }
      const int K = cycles.size();
      assert((N + 1 - K) % 2 == 0);
      if (iter == 0) {
        printf("%d\n", (N + 1 - K) / 2);
      }
// cerr<<"P = "<<P<<", cycles = "<<cycles<<endl;
      if (K == N + 1) break;
      
      vector<int> invP(N + 2, 0);
      for (int i = 0; i <= N + 1; ++i) invP[P[i]] = i;
      auto oper = [&](int b, int d, int f, int h) -> void {
        const int i = invP[b];
        const int j = invP[d];
        const int k = invP[f];
        const int l = invP[h];
        assert(i < j); assert(j <= k); assert(k < l);
        printf("%d %d %d %d\n", i, j - 1, k, l - 1);
        vector<int> PP;
        auto add = [&](int lef, int rig) -> void {
          PP.insert(PP.end(), P.begin() + lef, P.begin() + rig);
        };
        add(0, i); add(k, l); add(j, k); add(i, j); add(l, N + 2);
        P.swap(PP);
      };
      
      int dm = N + 1;
      int um = -1;
      for (int u = 1; u <= N + 1; ++u) {
        const int v = Q[u];
        const int i = invP[u], j = invP[v];
        if (i < j) {
          if (chmin(dm, j - i)) um = u;
        }
      }
      const int u = um;
      const int v = Q[u];
      int w = -1, x = -1;
      for (int i = invP[u]; i < invP[u - 1]; ++i) if (P[i] + 1 != P[i + 1]) {
        w = P[i + 1];
        x = Q[w];
        break;
      }
// cerr<<__LINE__<<": "<<u<<" "<<v<<" "<<w<<" "<<x<<endl;
      assert(~w);
      if (u == x) {
        oper(u, w, w, v);
      } else if (invP[w] < invP[x]) {
        oper(u, w, v, x);
      } else {
        oper(x, u, w, v);
      }
    }
  }
  return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3836kb

input:

1
1

output:

0

result:

ok orz U R the sorting master!

Test #2:

score: 0
Accepted
time: 57ms
memory: 4136kb

input:

1970
1452 1799 174 371 132 637 23 1510 1819 1794 1665 450 1183 564 1305 548 554 1310 701 1454 1843 1498 1040 1678 77 614 1928 1761 1718 1637 1853 1026 1804 1062 805 864 1859 586 663 346 335 681 152 1768 1639 1713 856 1401 1833 1350 1842 558 241 1829 802 581 1958 845 722 239 1793 1118 1251 1892 1949 ...

output:

981
568 568 570 1017
358 358 360 366
408 1897 1899 1900
1224 1224 1227 1785
1014 1014 1018 1835
656 1551 1553 1557
179 179 185 201
523 1152 1154 1158
703 1450 1452 1457
409 410 417 1309
295 295 304 1746
180 659 661 668
72 582 584 592
968 1379 1381 1389
641 641 653 1220
845 845 858 1274
222 222 237 9...

result:

ok orz U R the sorting master!

Test #3:

score: 0
Accepted
time: 57ms
memory: 4132kb

input:

1983
1002 653 275 1324 1618 1893 1085 342 1858 355 1084 753 910 1417 841 1640 1620 613 311 456 225 51 1316 279 61 1261 68 870 320 147 148 959 49 1862 347 1765 880 293 746 849 488 233 1500 1825 22 547 1363 1034 136 151 1378 1756 1579 1957 647 336 1621 1543 637 214 1717 1871 1339 1732 975 565 919 424 ...

output:

988
721 721 726 810
386 1200 1202 1205
7 7 12 1766
457 517 519 523
76 76 83 697
1533 1533 1540 1777
1315 1504 1506 1512
746 746 756 800
789 789 793 1173
126 126 138 1629
576 1251 1253 1263
505 818 820 831
597 597 614 945
942 942 952 1816
291 1811 1813 1818
1518 1518 1536 1676
303 303 321 848
325 107...

result:

ok orz U R the sorting master!

Test #4:

score: 0
Accepted
time: 48ms
memory: 4340kb

input:

1967
172 148 690 1375 213 280 435 1727 1442 1579 1876 1455 1341 563 523 456 1712 312 1425 1890 616 1353 391 1957 1755 1155 1481 1402 532 1677 1328 1195 1940 156 188 965 937 1589 1429 1051 1025 1631 983 400 1029 1157 839 1594 1816 1911 46 609 1 1446 709 310 1706 410 1800 1725 124 199 529 131 1628 154...

output:

981
321 615 617 617
626 1125 1127 1127
291 291 295 1197
780 780 784 1623
79 79 84 955
1275 1275 1282 1794
460 1491 1493 1498
110 110 118 269
49 1337 1339 1345
1235 1235 1244 1831
112 112 122 960
455 455 465 828
1315 1315 1326 1488
1671 1879 1881 1894
459 1667 1669 1675
8 222 224 238
552 785 787 801
...

result:

ok orz U R the sorting master!

Test #5:

score: 0
Accepted
time: 0ms
memory: 3992kb

input:

1961
1 2 3 4 5 6 7 8 9 10 11 12 13 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 ...

output:

1
14 283 506 1204

result:

ok orz U R the sorting master!

Test #6:

score: 0
Accepted
time: 0ms
memory: 4292kb

input:

1979
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101...

output:

5
330 340 468 738
338 444 514 1361
169 248 373 1815
266 1187 1518 1649
376 1243 1364 1976

result:

ok orz U R the sorting master!

Test #7:

score: 0
Accepted
time: 2ms
memory: 4048kb

input:

1971
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 162 163 164 165 166 167 168 169 170 171 172 17...

output:

20
921 1929 1935 1935
87 592 633 667
445 1874 1878 1965
523 561 622 680
787 792 889 1232
396 1431 1454 1567
188 188 357 1679
858 1470 1524 1525
1075 1615 1623 1834
443 1284 1324 1428
482 515 614 1387
979 995 1150 1345
1314 1351 1462 1620
315 1260 1344 1381
350 569 673 871
860 915 934 1621
178 1253 1...

result:

ok orz U R the sorting master!

Test #8:

score: 0
Accepted
time: 10ms
memory: 4040kb

input:

1971
1 2 3 4 5 6 7 8 9 712 1843 131 132 1556 1557 1558 1559 1742 1743 1744 1745 1746 1747 1748 613 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 202 203 204 1767...

output:

100
175 177 189 716
333 338 347 525
321 332 354 1061
990 1134 1138 1171
371 1003 1006 1041
156 159 198 678
96 97 161 1613
1591 1592 1631 1643
95 106 139 807
792 807 812 921
427 427 496 1384
1230 1237 1307 1717
125 760 765 852
122 125 175 543
797 805 887 1663
11 11 106 1231
1130 1130 1219 1396
814 13...

result:

ok orz U R the sorting master!

Test #9:

score: 0
Accepted
time: 43ms
memory: 4364kb

input:

1973
1470 1471 1472 1473 1266 1959 1648 1649 1337 498 1080 875 1810 775 1448 1111 1112 1260 212 588 589 1128 51 1381 1442 700 701 660 1339 1850 1892 1893 905 1287 628 629 630 650 1262 1263 835 505 506 1773 173 98 1758 627 1677 1808 1809 1585 236 237 1458 1459 1460 1461 1907 1908 1909 1125 1474 37 19...

output:

500
739 1453 1455 1456
1492 1493 1499 1845
177 178 185 501
35 37 49 762
742 743 758 1678
325 454 456 471
937 1601 1603 1619
573 573 594 1137
763 763 785 947
193 193 215 1409
894 1858 1860 1880
966 966 991 1736
13 13 38 1908
207 1892 1894 1916
139 199 201 216
58 148 150 166
801 1849 1853 1876
304 304...

result:

ok orz U R the sorting master!

Test #10:

score: 0
Accepted
time: 54ms
memory: 4080kb

input:

1959
1 1009 1010 1461 171 1629 1761 1507 1508 1509 1621 992 1901 1154 841 736 737 738 333 1274 1652 1653 452 881 882 541 1247 1177 1662 965 966 1192 1193 1339 1831 1832 1031 1032 1928 105 1634 820 423 1103 1893 1894 163 1759 552 805 555 166 1199 1725 1463 23 1696 464 1787 1254 1655 1656 745 746 1388...

output:

824
967 967 972 1390
923 923 929 1092
621 621 629 687
1438 1438 1447 1555
371 371 381 1911
872 894 896 907
86 86 100 379
1055 1699 1701 1714
4 101 103 116
1234 1664 1667 1681
1624 1780 1783 1797
226 1469 1471 1488
173 174 194 841
741 844 846 867
816 816 842 1188
713 1335 1337 1364
917 917 951 1798
4...

result:

ok orz U R the sorting master!

Test #11:

score: 0
Accepted
time: 57ms
memory: 4372kb

input:

1966
1070 1605 424 264 1500 626 999 836 794 972 233 136 1256 1591 1271 417 678 235 854 1632 152 1895 257 258 469 1796 1811 1962 1207 934 1857 1089 1090 642 414 242 279 280 145 435 774 1351 1366 403 948 28 260 656 1467 1635 818 1061 1033 736 1706 619 228 1408 739 124 1554 688 1619 917 1332 1435 1015 ...

output:

936
1080 1080 1083 1951
1230 1307 1309 1313
70 70 76 203
735 735 741 1021
1561 1561 1567 1821
125 1837 1839 1846
1304 1304 1314 1855
53 1833 1835 1846
49 49 55 1903
206 206 225 1845
120 120 141 741
561 561 588 632
899 1077 1079 1104
1596 1911 1913 1940
612 1294 1297 1324
510 1322 1324 1340
68 512 51...

result:

ok orz U R the sorting master!

Test #12:

score: 0
Accepted
time: 0ms
memory: 3836kb

input:

2
2 1

output:

1
1 1 2 2

result:

ok orz U R the sorting master!

Test #13:

score: 0
Accepted
time: 0ms
memory: 3884kb

input:

3
1 3 2

output:

1
2 2 3 3

result:

ok orz U R the sorting master!

Test #14:

score: 0
Accepted
time: 0ms
memory: 4128kb

input:

10
10 1 8 5 7 9 4 3 2 6

output:

5
3 7 9 9
5 5 8 9
8 9 10 10
1 1 2 5
4 5 6 10

result:

ok orz U R the sorting master!

Test #15:

score: 0
Accepted
time: 1ms
memory: 3904kb

input:

1967
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101...

output:

0

result:

ok orz U R the sorting master!

Test #16:

score: 0
Accepted
time: 47ms
memory: 4180kb

input:

1999
1999 1998 1997 1996 1995 1994 1993 1992 1991 1990 1989 1988 1987 1986 1985 1984 1983 1982 1981 1980 1979 1978 1977 1976 1975 1974 1973 1972 1971 1970 1969 1968 1967 1966 1965 1964 1963 1962 1961 1960 1959 1958 1957 1956 1955 1954 1953 1952 1951 1950 1949 1948 1947 1946 1945 1944 1943 1942 1941 ...

output:

999
1 1997 1999 1999
3 1997 1999 1999
5 1997 1999 1999
7 1997 1999 1999
9 1997 1999 1999
11 1997 1999 1999
13 1997 1999 1999
15 1997 1999 1999
17 1997 1999 1999
19 1997 1999 1999
21 1997 1999 1999
23 1997 1999 1999
25 1997 1999 1999
27 1997 1999 1999
29 1997 1999 1999
31 1997 1999 1999
33 1997 1999 ...

result:

ok orz U R the sorting master!

Test #17:

score: 0
Accepted
time: 48ms
memory: 4376kb

input:

1961
2 1 4 3 6 5 8 7 10 9 12 11 14 13 16 15 18 17 20 19 22 21 24 23 26 25 28 27 30 29 32 31 34 33 36 35 38 37 40 39 42 41 44 43 46 45 48 47 50 49 52 51 54 53 56 55 58 57 60 59 62 61 64 63 66 65 68 67 70 69 72 71 74 73 76 75 78 77 80 79 82 81 84 83 86 85 88 87 90 89 92 91 94 93 96 95 98 97 100 99 102...

output:

980
1 1 2 2
3 3 4 4
5 5 6 6
7 7 8 8
9 9 10 10
11 11 12 12
13 13 14 14
15 15 16 16
17 17 18 18
19 19 20 20
21 21 22 22
23 23 24 24
25 25 26 26
27 27 28 28
29 29 30 30
31 31 32 32
33 33 34 34
35 35 36 36
37 37 38 38
39 39 40 40
41 41 42 42
43 43 44 44
45 45 46 46
47 47 48 48
49 49 50 50
51 51 52 52
53...

result:

ok orz U R the sorting master!

Test #18:

score: 0
Accepted
time: 50ms
memory: 4380kb

input:

1996
3 2 1 6 5 4 9 8 7 12 11 10 15 14 13 18 17 16 21 20 19 24 23 22 27 26 25 30 29 28 33 32 31 36 35 34 39 38 37 42 41 40 45 44 43 48 47 46 51 50 49 54 53 52 57 56 55 60 59 58 63 62 61 66 65 64 69 68 67 72 71 70 75 74 73 78 77 76 81 80 79 84 83 82 87 86 85 90 89 88 93 92 91 96 95 94 99 98 97 102 101...

output:

665
1 1 3 3
4 4 6 6
7 7 9 9
10 10 12 12
13 13 15 15
16 16 18 18
19 19 21 21
22 22 24 24
25 25 27 27
28 28 30 30
31 31 33 33
34 34 36 36
37 37 39 39
40 40 42 42
43 43 45 45
46 46 48 48
49 49 51 51
52 52 54 54
55 55 57 57
58 58 60 60
61 61 63 63
64 64 66 66
67 67 69 69
70 70 72 72
73 73 75 75
76 76 78...

result:

ok orz U R the sorting master!

Test #19:

score: 0
Accepted
time: 40ms
memory: 4004kb

input:

1973
4 2 3 1 8 6 7 5 12 10 11 9 16 14 15 13 20 18 19 17 24 22 23 21 28 26 27 25 32 30 31 29 36 34 35 33 40 38 39 37 44 42 43 41 48 46 47 45 52 50 51 49 56 54 55 53 60 58 59 57 64 62 63 61 68 66 67 65 72 70 71 69 76 74 75 73 80 78 79 77 84 82 83 81 88 86 87 85 92 90 91 89 96 94 95 93 100 98 99 97 104...

output:

493
1 1 4 4
5 5 8 8
9 9 12 12
13 13 16 16
17 17 20 20
21 21 24 24
25 25 28 28
29 29 32 32
33 33 36 36
37 37 40 40
41 41 44 44
45 45 48 48
49 49 52 52
53 53 56 56
57 57 60 60
61 61 64 64
65 65 68 68
69 69 72 72
73 73 76 76
77 77 80 80
81 81 84 84
85 85 88 88
89 89 92 92
93 93 96 96
97 97 100 100
101 ...

result:

ok orz U R the sorting master!