이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "sequence.h"
#include <bits/stdc++.h>
using namespace std;
#define f(i,a,b) for(int i = a; i < b; i++)
#define fa(i,a,b) for(int i = a; i >= b; i--)
const int N = 5e5 + 5;
int n;
int bit[N], c[N];
vector <pair<int,int>> pos[N];
void upd(int x, int val){
for(; x < N; x = (x|(x+1))) bit[x] += val;
}
int sum(int x){
int res = 0;
for(; x >= 0; x = (x&(x+1)) - 1) res += bit[x];
return res;
}
int get(int x){
int l = 1, r = n;
while(l < r){
int m = (l+r)>>1;
if(sum(m) >= x) r = m;
else l = m+1;
}
return c[l];
}
int subtask_2(vector <int> A){
int ans = 1;
f(i,0,n){
c[A[i]]++;
upd(A[i], 1);
f(j,i,n){
int l = j - i + 1;
if(l&1){
ans = max(ans, get((l+1)/2));
}
else{
ans = max(ans, get(l/2));
ans = max(ans, get(l/2 + 1));
}
if(j+1 < n) {
c[A[j+1]]++;
upd(A[j+1], 1);
}
}
f(j,i,n){
c[A[j]]--;
upd(A[j], -1);
}
}
return ans;
}
int id(int pos){
if(pos == 0) return 0;
if(c[1] >= pos) return 1;
if(c[1] + c[2] >= pos) return 2;
return 3;
}
int res(vector <int> v){
vector <int> aux, mini;
int len = v.size(), ans = 1;
f(i,0,len) {
aux.push_back(v[i] - 2*i);
mini.push_back(v[i] - 2*i);
}
fa(i,len-2,0) mini[i] = min(mini[i+1], mini[i]);
f(i,0,len-1){
int ult = aux[i] + 1; // <= ult
if(mini[i+1] > ult) continue;
int l = i+1, r = len-1;
while(l < r){
int m = (l+r+1)>>1;
if(mini[m] > ult) r = m-1;
else l = m;
}
ans = max(ans, l-i+1);
}
return ans;
}
int subtask_4(vector <int> A){
for(int x: A) c[x]++;
int p1 = 0, p2 = 0;
if(n&1) p1 = (n+1)/2;
else p1 = n/2, p2 = p1 + 1;
if(id(p1) == 1 or id(p1) == 3) return c[id(p1)];
if(id(p2) == 1 or id(p2) == 3) return c[id(p2)];
if(c[2] > c[1] and c[2] > c[3]) return c[2];
int ans = c[2];
vector <int> P1, P3;
f(i,0,n){
if(A[i] == 1) P1.push_back(i);
if(A[i] == 3) P3.push_back(i);
}
ans = max(ans, res(P1));
ans = max(ans, res(P3));
return ans;
}
bool is(int l, int r, int ty){
if(l != r) return 1;
return l%2 == ty;
}
int subtask_3(vector <int> A){
int ans = 0, id = 0;
while(id < n){
int c = 0, ini = id+1, comp = A[id];
while(A[id] == comp and id < n) c++;
ans = max(ans, c);
pos[comp].push_back({ini, ini + c - 1});
}
f(i,1,n+1){
if(pos[i].size() != 2) continue;
int l1 = pos[i][0].first, r1 = pos[i][1].second;
int l2 = pos[i][1].first, r2 = pos[i][1].second;
int C = r1 - l1 + r2 - l2 + 2;
int B = -(r1 + 1) + (l2 - 1) + 1;
int A = n - C - B, ty = (B+C)%2;
bool fe = 0;
if(is(max(0, B-C), min(A, B+C), ty)) fe = 1;
if(is(max(0, B-C-1), min(A, B+C-1), 1-ty)) fe = 1;
if(fe) ans = max(ans, C);
}
return ans;
}
int sequence(int Ni, vector<int> A) {
n = Ni;
if(n <= 2000) return subtask_2(A);
int maxi = 0;
for(int x: A) maxi = max(maxi, x);
if(maxi <= 3) return subtask_4(A);
return subtask_3(A);
}
# | 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... |