108272024-04-16 08:24:08TomaSajtSzimmetrikus sorozatcpp17Accepted 100/100194ms34320 KiB
#include <bits/stdc++.h>
#define int long long
using namespace std;

vector<vector<int>> g;
vector<int> color, parent, depth;

int get_root(int u) {
  if (u == parent[u]) return u;
  return parent[u] = get_root(parent[u]);
}

vector<array<int, 2>> sol;

void merge(int u, int v, bool init) {
  u = get_root(u), v = get_root(v);
  if (u == v) return;
  if (!init) assert(color[u] != color[v]);
  if (depth[u] < depth[v]) swap(u, v);
  if (depth[u] == depth[v]) depth[u]++;
  int c1 = min(color[u], color[v]);
  int c2 = max(color[u], color[v]);
  color[u] = c1;
  parent[v] = u;
  if (init) return;
  sol.push_back({c2, c1});
}

signed main() {
  ios::sync_with_stdio(0), cin.tie(0);
  int n;
  cin >> n;
  parent.resize(n + 1);
  iota(parent.begin(), parent.end(), 0);
  depth.assign(n + 1, 1);
  color.resize(n + 1);
  map<int, int> col_map;
  for (int i = 1; i <= n; i++) {
    cin >> color[i];
    if (col_map.count(color[i])) {
      merge(col_map[color[i]], i, true);
    }
    else {
      col_map[color[i]] = i;
    }
  }

  for (int i = 1; i <= n; i++) {
    int j = n - i + 1;
    if (j <= i) break;
    merge(i, j, false);
  }

  cout << sol.size() << endl;
  for (auto [a, b] : sol) cout << a << ' ' << b << '\n';
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted3ms1700 KiB
2Accepted75ms14608 KiB
subtask29/9
3Accepted3ms2396 KiB
4Accepted3ms2492 KiB
5Accepted3ms2768 KiB
6Accepted3ms2652 KiB
7Accepted3ms2940 KiB
subtask314/14
8Accepted3ms2924 KiB
9Accepted2ms3004 KiB
10Accepted3ms3132 KiB
11Accepted3ms3368 KiB
12Accepted3ms3572 KiB
13Accepted3ms3660 KiB
subtask425/25
14Accepted3ms3660 KiB
15Accepted3ms3988 KiB
16Accepted4ms4436 KiB
17Accepted4ms4692 KiB
18Accepted4ms4472 KiB
19Accepted4ms4616 KiB
20Accepted4ms4568 KiB
21Accepted4ms4648 KiB
subtask522/22
22Accepted123ms28324 KiB
23Accepted129ms30376 KiB
24Accepted134ms30320 KiB
25Accepted119ms28252 KiB
26Accepted122ms28292 KiB
27Accepted125ms30476 KiB
28Accepted128ms30556 KiB
29Accepted115ms28488 KiB
30Accepted103ms28656 KiB
31Accepted109ms30744 KiB
32Accepted114ms30752 KiB
33Accepted97ms28704 KiB
34Accepted83ms28704 KiB
35Accepted98ms30904 KiB
36Accepted100ms30900 KiB
37Accepted86ms28696 KiB
subtask630/30
38Accepted3ms4824 KiB
39Accepted82ms17568 KiB
40Accepted3ms2396 KiB
41Accepted3ms2492 KiB
42Accepted3ms2768 KiB
43Accepted3ms2652 KiB
44Accepted3ms2940 KiB
45Accepted3ms2924 KiB
46Accepted2ms3004 KiB
47Accepted3ms3132 KiB
48Accepted3ms3368 KiB
49Accepted3ms3572 KiB
50Accepted3ms3660 KiB
51Accepted3ms3660 KiB
52Accepted3ms3988 KiB
53Accepted4ms4436 KiB
54Accepted4ms4692 KiB
55Accepted4ms4472 KiB
56Accepted4ms4616 KiB
57Accepted4ms4568 KiB
58Accepted4ms4648 KiB
59Accepted123ms28324 KiB
60Accepted129ms30376 KiB
61Accepted134ms30320 KiB
62Accepted119ms28252 KiB
63Accepted122ms28292 KiB
64Accepted125ms30476 KiB
65Accepted128ms30556 KiB
66Accepted115ms28488 KiB
67Accepted103ms28656 KiB
68Accepted109ms30744 KiB
69Accepted114ms30752 KiB
70Accepted97ms28704 KiB
71Accepted83ms28704 KiB
72Accepted98ms30904 KiB
73Accepted100ms30900 KiB
74Accepted86ms28696 KiB
75Accepted192ms34068 KiB
76Accepted144ms22648 KiB
77Accepted57ms17056 KiB
78Accepted137ms22600 KiB
79Accepted101ms17576 KiB
80Accepted131ms26284 KiB
81Accepted133ms26132 KiB
82Accepted142ms26188 KiB
83Accepted194ms34320 KiB
84Accepted63ms17120 KiB
85Accepted111ms17588 KiB
86Accepted144ms26668 KiB
87Accepted144ms26664 KiB
88Accepted143ms26792 KiB
89Accepted140ms26792 KiB