이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <cstring>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <queue>
#include <bitset>
#include <stack>
#include <cassert>
#include <iterator>
#include <random>
#include <chrono>
#include <fstream>
#define fastIO \
ios::sync_with_stdio(false); \
cin.tie(0); \
cout.tie(0);
#define mod 1000000007ll
#define LL long long
#define LD long double
#define MP make_pair
#define PB push_back
#define all(v) v.begin(), v.end()
using namespace std;
const int N = 500005;
int n, a[N], ans;
vector<int> pos[N];
pair<int, int> t[N * 4];
int d[N * 4];
pair<int, int> merge(pair<int, int> a, pair<int, int> b)
{
return MP(min(a.first, b.first), max(a.second, b.second));
}
void push(int v, int l, int r)
{
if (d[v] == 0)
return;
if (l != r)
{
d[v * 2] += d[v];
d[v * 2 + 1] += d[v];
}
t[v] = MP(t[v].first + d[v], t[v].second + d[v]);
d[v] = 0;
}
void add(int v, int l, int r, int i, int j, int val)
{
push(v, l, r);
if (i > j)
return;
if (i == l && j == r)
{
d[v] += val;
push(v, l, r);
return;
}
int m = (l + r) / 2;
add(v * 2, l, m, i, min(m, j), val);
add(v * 2 + 1, m + 1, r, max(m + 1, i), j, val);
t[v] = merge(t[v * 2], t[v * 2 + 1]);
}
pair<int, int> get(int v, int l, int r, int i, int j)
{
push(v, l, r);
if (i > j)
return MP(mod, -mod);
if (i == l && j == r)
return t[v];
int m = (l + r) / 2;
return merge(
get(v * 2, l, m, i, min(m, j)),
get(v * 2 + 1, m + 1, r, max(m + 1, i), j)
);
}
int sequence(int nn, vector<int> aa)
{
n = nn;
for (int i = 1; i <= n; i++)
a[i] = aa[i - 1];
for (int i = 1; i <= n; i++)
pos[a[i]].push_back(i);
for (int i = 1; i <= n; i++)
add(1, 0, n, i, n, 1);
for (int x = 1; x <= n; x++)
{
for (int p = 0; p < pos[x].size(); p++)
{
int i = pos[x][p];
int l = 0;
int r = p;
while (l <= r)
{
int m = (l + r) / 2;
int j = pos[x][m];
int len = p - m + 1;
pair<int, int> dr = get(1, 0, n, i, n);
dr.first -= len;
dr.second -= len;
pair<int, int> dl = get(1, 0, n, 0, j - 1);
pair<int, int> d = MP(dr.first - dl.second, dr.second - dl.first);
if ((d.second < 0 && len >= -d.second) || (d.first > 0 && len >= d.first) || (d.first <= 0 && d.second >= 0))
{
ans = max(ans, len);
r = m - 1;
}
else
l = m + 1;
}
}
for (int p = 0; p < pos[x].size(); p++)
{
int i = pos[x][p];
add(1, 0, n, i, n, -1);
}
for (int p = 0; p < pos[x].size(); p++)
{
int i = pos[x][p];
add(1, 0, n, i, n, -1);
}
for (int p = 0; p < pos[x].size(); p++)
{
int i = pos[x][p];
int l = 0;
int r = p;
while (l <= r)
{
int m = (l + r) / 2;
int j = pos[x][m];
int len = p - m + 1;
pair<int, int> dr = get(1, 0, n, i, n);
dr.first -= len;
dr.second -= len;
pair<int, int> dl = get(1, 0, n, 0, j - 1);
pair<int, int> d = MP(dr.first - dl.second, dr.second - dl.first);
if ((d.second < 0 && len >= -d.second) || (d.first > 0 && len >= d.first) || (d.first <= 0 && d.second >= 0))
{
ans = max(ans, len);
r = m - 1;
}
else
l = m + 1;
}
}
}
return ans;
}
#ifdef death
void solve()
{
int nn;
vector<int> aa;
cin >> nn;
for (int i = 0; i < nn; i++)
{
int x;
cin >> x;
aa.push_back(x);
}
cout << sequence(nn, aa) << endl;
}
int main()
{
fastIO;
int T = 1;
//cin >> T;
while (T--)
solve();
return 0;
}
#endif // death
/*
7
1 2 3 1 2 1 3
9
1 1 2 3 4 3 2 1 1
14
2 6 2 5 3 4 2 1 4 3 5 6 3 2
*/
컴파일 시 표준 에러 (stderr) 메시지
sequence.cpp: In function 'int sequence(int, std::vector<int>)':
sequence.cpp:109:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
109 | for (int p = 0; p < pos[x].size(); p++)
| ~~^~~~~~~~~~~~~~~
sequence.cpp:141:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
141 | for (int p = 0; p < pos[x].size(); p++)
| ~~^~~~~~~~~~~~~~~
sequence.cpp:148:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
148 | for (int p = 0; p < pos[x].size(); p++)
| ~~^~~~~~~~~~~~~~~
sequence.cpp:155:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
155 | for (int p = 0; p < pos[x].size(); p++)
| ~~^~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |