제출 #763898

#제출 시각아이디문제언어결과실행 시간메모리
763898gg123_pe서열 (APIO23_sequence)C++17
40 / 100
285 ms12416 KiB
#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]; 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; } 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 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...