이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <iomanip>
#include <array>
#include <string>
#include <algorithm>
#include <cmath>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <bitset>
#include <list>
#include <iterator>
#include <numeric>
#include <complex>
#include <utility>
#include <random>
#include <cassert>
#include <fstream>
#include "sequence.h"
using namespace std;
mt19937 rnd(7069);
typedef int itn;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef float fl;
typedef long double ld;
using vi = vector<int>;
using vll = vector<ll>;
using mii = map<int, int>;
using mll = map<ll, ll>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
#define ff first
#define ss second
#define pub push_back
#define pob pop_back
#define puf push_front
#define pof pop_front
#define mpr make_pair
#define yes cout<<"Yes\n"
#define no cout<<"No\n"
#define all(x) (x).begin(), (x).end()
const int MAX = int(2e9 + 5);
const ll MAXL = ll(1e18) + 5ll;
const int N = 500005;
int a[N], pref[N], dif[N], tp[N * 4], tn[N * 4], par[N];
int n, ans = 1;
vector<pii> v;
void bld(int l = 0, int r = n, int pos = 1) {
tn[pos] = tp[pos] = MAX;
if (l == r) {
par[l] = pos;
return;
}
int mid = (l + r) / 2;
bld(l, mid, 2 * pos);
bld(mid + 1, r, 2 * pos + 1);
}
int qry(int l, int r, int type, int tl = 0, int tr = n, int pos = 1) {
if (r < l) return MAX;
if (l == tl && r == tr) {
return (type == 1 ? tn[pos] : tp[pos]);
}
int mid = (tl + tr) / 2;
if (mid >= r) {
return qry(l, r, type, tl, mid, 2 * pos);
}
if (mid < l) {
return qry(l, r, type, mid + 1, tr, 2 * pos + 1);
}
return min(qry(l, mid, type, tl, mid, 2 * pos), qry(mid + 1, r, type, mid + 1, tr, 2 * pos + 1));
}
void upd(int pos, int val, int type) {
pos = par[pos];
if (type == 1) tn[pos] = min(tn[pos], val);
else tp[pos] = min(tp[pos], val);
pos /= 2;
while (pos) {
if (type == 1) tn[pos] = min(tn[2 * pos], tn[2 * pos + 1]);
else tp[pos] = min(tp[2 * pos], tp[2 * pos + 1]);
pos /= 2;
}
}
void func() {
sort(all(v));
bld();
for (auto elem : v) {
int d = elem.ff, p = elem.ss, node = p - d;
if (node <= 0) {
node *= -1;
upd(node, p, 1);
ans = max(ans, p - qry(node, n, 1));
}
else {
upd(node, p, 2);
ans = max(ans, p - min(qry(0, n, 1), qry(0, node, 2)));
}
}
bld();
reverse(all(v));
for (auto elem : v) {
int d = elem.ff, p = elem.ss, node = p + d;
if (node <= 0) {
node *= -1;
upd(node, p, 1);
ans = max(ans, p - qry(node, n, 1));
}
else {
upd(node, p, 2);
ans = max(ans, p - min(qry(0, n, 1), qry(0, node, 2)));
}
}
v.clear();
}
void solv1() {
for (int i = 1; i <= n; ++i) {
pref[i] = pref[i - 1];
dif[i] = dif[i - 1];
if (a[i] == 1) {
++pref[i];
}
else {
++dif[i];
}
v.pub({ dif[i], pref[i] });
}
v.pub({ 0,0 });
func();
}
void solv2() {
for (int i = 1; i <= n; ++i) {
pref[i] = pref[i - 1];
dif[i] = dif[i - 1];
if (a[i] == 2) {
++pref[i];
}
else if (a[i] == 1) {
--dif[i];
}
else {
++dif[i];
}
v.pub({ dif[i], pref[i] });
}
func();
}
void solv3() {
for (int i = 1; i <= n; ++i) {
pref[i] = pref[i - 1];
dif[i] = dif[i - 1];
if (a[i] == 3) {
++pref[i];
}
else {
--dif[i];
}
v.pub({ dif[i], pref[i] });
}
func();
}
int sequence(int N, std::vector<int> A) {
n = N;
for (int i = 1; i <= n; ++i) {
a[i] = A[i - 1];
}
solv1();
solv2();
solv3();
return ans;
}
/*
*/
# | 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... |