제출 #798754

#제출 시각아이디문제언어결과실행 시간메모리
798754fanwen서열 (APIO23_sequence)C++17
7 / 100
147 ms51036 KiB
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace std; using namespace __gnu_pbds; #define MASK(x) (1LL << (x)) #define BIT(x, i) (((x) >> (i)) & 1) #define ALL(x) (x).begin(), (x).end() #define REP(i, n) for (int i = 0, _n = n; i < _n; ++i) #define FOR(i, a, b) for (int i = (a), _b = (b); i <= _b; ++i) #define FORD(i, a, b) for (int i = (a), _b = (b); i >= _b; --i) #define FORE(i, a, b) for (int i = (a), _b = (b); i < _b; ++i) #define debug(...) "[" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] " #define file(name) if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); } template <class A, class B> bool minimize(A &a, B b) { if (a > b) { a = b; return true; } return false; } template <class A, class B> bool maximize(A &a, B b) { if (a < b) { a = b; return true; } return false; } typedef tree <int, null_type, less_equal <int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set; int sub2 (int n, vector <int> a) { int res = 0; REP(l, n) { unordered_map <int, int> cnt; ordered_set values; FORE(i, l, n) { cnt[a[i]]++; values.insert(a[i]); int k = i - l + 1; if(k % 2 == 0) { res = max(res, cnt[*values.find_by_order(k / 2 - 1)]); res = max(res, cnt[*values.find_by_order(k / 2)]); } else { res = max(res, cnt[*values.find_by_order(k / 2)]); } } } return res; } int sub3 (int n, vector <int> a) { unordered_map <int, vector <pair <int, int>>> pos; for (int l = 0; l < n;) { int r = l; while(r < n and a[r] == a[l]) ++r; pos[a[l]].emplace_back(l, r - 1); l = r; } auto is_median = [&] (int less, int greater, int equal) -> bool { return (equal >= greater ? true : (equal + less >= greater)); }; int ans = 0; for (auto [_, p] : pos) { for (auto [l, r] : p) ans = max(ans, r - l + 1); if((int) p.size() < 2) continue; assert((int) p.size() == 2); int equal = p[0].second - p[0].first + 1 + p[1].second - p[1].first + 1; int less = p[0].first + n - 1 - p[1].second; int greater = p[1].first - p[0].second - 1; if(is_median(less, greater, equal)) ans = max(ans, equal); } return ans; } int sequence(int n, vector <int> a) { return sub3(n, a); if(n <= 2000) return sub2(n, a); } #ifdef LOCAL #include <cassert> #include <cstdio> #include <vector> int main() { int N; freopen("TASK.inp", "r", stdin); freopen("TASK.out", "w", stdout); cin >> N; std::vector<int> A(N); for (int i = 0; i < N; ++i) { cin >> A[i]; } int result = sequence(N, A); cout << result; return 0; } #endif // Dream it. Wish it. Do it.
#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...