#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10;
int n, a[N], ind[N], pref[N];
vector<pair<int, int>> vec;
vector<int> at[N];
void solve(){
cin >> n;
for (int i = 1; i <= 2 * n; i += 2)
cin >> a[i], a[i] = 2 * a[i] - 1, ind[a[i]] = i;
for (int i = 1; i <= 2 * n; i += 2){
int l = i, r = ind[i];
if (l > r) swap(l, r);
vec.push_back({l, r});
}
sort(vec.begin(), vec.end());
for (int i = 0; i < vec.size(); i ++){
auto [l, r] = vec[i];
at[(l + r) / 2].push_back(i);
}
for (int i = 1; i <= 2 * n; i ++)
pref[i] = pref[i - 1] + (a[i] == i);
int mx = -1;
pair<int, int> output;
for (int i = 1; i <= 2 * n; i ++){
for (int j = 0; j < at[i].size(); j ++){
auto [l, r] = vec[at[i][j]];
int cnt = pref[l - 1] + (pref[2 * n] - pref[r]) + (at[i].size() - j);
// cout << i << ", " << a[l] / 2 + 1 << " - " << a[r] / 2 + 1 << " : " << cnt << endl;
if (mx < cnt){
mx = cnt;
output = {a[l] / 2 + 1, a[r] / 2 + 1};
}
}
}
cout << output.first << " " << output.second << endl;
}
int main(){
int t = 1;
// cin >> t;
while (t--)
solve();
return 0;
}
Compilation message
doktor.cpp: In function 'void solve()':
doktor.cpp:21:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
21 | for (int i = 0; i < vec.size(); i ++){
| ~~^~~~~~~~~~~~
doktor.cpp:32:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
32 | for (int j = 0; j < at[i].size(); j ++){
| ~~^~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
25176 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
25176 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
25180 KB |
Output is correct |
2 |
Correct |
5 ms |
25180 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
25180 KB |
Output is correct |
2 |
Correct |
5 ms |
25244 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
25436 KB |
Output is correct |
2 |
Correct |
6 ms |
25180 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
25180 KB |
Output is correct |
2 |
Correct |
5 ms |
25184 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
25692 KB |
Output is correct |
2 |
Correct |
89 ms |
38448 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
35 ms |
29380 KB |
Output is correct |
2 |
Correct |
32 ms |
28252 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
192 ms |
50356 KB |
Output is correct |
2 |
Correct |
140 ms |
46772 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
103 ms |
39548 KB |
Output is correct |
2 |
Correct |
117 ms |
54204 KB |
Output is correct |