이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#include "sequence.h"
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define MASK(i) (1LL << (i))
#define GETBIT(mask, i) (((mask) >> (i)) & 1)
#define ALL(v) (v).begin(), (v).end()
ll max(ll a, ll b){return (a > b) ? a : b;}
ll min(ll a, ll b){return (a < b) ? a : b;}
ll LASTBIT(ll mask){return (mask) & (-mask);}
int pop_cnt(ll mask){return __builtin_popcountll(mask);}
int ctz(ll mask){return __builtin_ctzll(mask);}
int logOf(ll mask){return __lg(mask);}
mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
ll rngesus(ll l, ll r){return l + (ull) rng() % (r - l + 1);}
template <class T1, class T2>
bool maximize(T1 &a, T2 b){
if (a < b) {a = b; return true;}
return false;
}
template <class T1, class T2>
bool minimize(T1 &a, T2 b){
if (a > b) {a = b; return true;}
return false;
}
template <class T>
void printArr(T& container, char separator = ' ', char finish = '\n'){
for(auto item: container) cout << item << separator;
cout << finish;
}
template <class T>
void remove_dup(vector<T> &a){
sort(ALL(a));
a.resize(unique(ALL(a)) - a.begin());
}
namespace Sub2{
struct FenwickTree{
int n;
vector<int> a;
FenwickTree(int _n){
n = _n;
a.resize(n+1);
}
void update(int i, int v){
while(i <= n){
a[i] += v;
i += LASTBIT(i);
}
}
int get(int i){
int ans = 0;
while(i > 0){
ans += a[i];
i -= LASTBIT(i);
}
return ans;
}
int binaryLift(int v){
int idx = 0, p = MASK(logOf(n));
while(p > 0){
if (idx + p <= n && v - a[idx + p] > 0){
idx += p;
v -= a[idx];
}
p >>= 1;
}
return idx + 1;
}
};
int solve(int n, vector<int> a){
int ans = 0;
for(int i = 0; i<n; ++i){
FenwickTree bit(n);
vector<int> cnt(n+1);
for(int j = i; j<n; ++j){
bit.update(a[j], 1);
cnt[a[j]]++;
int mid = (j - i) / 2 + 1;
maximize(ans, cnt[bit.binaryLift(mid)]);
if (mid * 2 == (j - i + 1)){
maximize(ans, cnt[bit.binaryLift(mid + 1)]);
}
}
}
return ans;
}
}
namespace Sub3{
struct FenwickTree{
int n;
vector<int> a;
FenwickTree(int _n){
n = _n;
a.resize(n+1);
}
void update(int i, int v){
while(i <= n){
a[i] += v;
i += LASTBIT(i);
}
}
int get(int i){
int ans = 0;
while(i > 0){
ans += a[i];
i -= LASTBIT(i);
}
return ans;
}
int binaryLift(int v){
int idx = 0, p = MASK(logOf(n));
while(p > 0){
if (idx + p <= n && v - a[idx + p] > 0){
idx += p;
v -= a[idx];
}
p >>= 1;
}
return idx + 1;
}
};
bool check(vector<int> a){
for(int i: a) if (i > 3) return 0;
return 1;
}
int edging(int n, vector<int> a){
int ans = 0;
vector<array<int, 3>> pref(n+1, {{0, 0, 0}});
a.insert(a.begin(), -1);
for(int i = 1; i<=n; ++i){
pref[i] = pref[i-1];
if (a[i] == 1) pref[i][0]++;
if (a[i] < 1) pref[i][1]++;
else pref[i][1]--;
if (a[i] <= 1) pref[i][2]++;
else pref[i][2]--;
}
for(int i = 0; i<=n; ++i)
for(int j = i + 1; j<=n; ++j){
if (pref[j][2] >= pref[i][2] && pref[j][1] < pref[i][1])
maximize(ans, pref[j][0] - pref[i][0]);
}
return ans;
}
int solve(int n, vector<int> a){
int ans = 0;
for(int i = 1; i<=3; ++i){
vector<int> b = a;
for(int &j: b){
if (j < i) j = 0;
else if (j == i) j = 1;
else j = 2;
}
maximize(ans, edging(n, b));
}
return ans;
}
}
int sequence(int n, vector<int> a) {
if (Sub3::check(a)) return Sub3::solve(n, a);
if (n <= 5000) return Sub2::solve(n, a);
return 0;
}
// int main(void){
// ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
// int n; cin >> n;
// vector<int> a(n);
// for(int i = 0; i<n; ++i) cin >> a[i];
// cout << sequence(n, a) << "\n";
// return 0;
// }
# | 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... |