結果
問題 | No.919 You Are A Project Manager |
ユーザー | ningenMe |
提出日時 | 2019-09-24 03:36:25 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
TLE
|
実行時間 | - |
コード長 | 5,128 bytes |
コンパイル時間 | 2,349 ms |
コンパイル使用メモリ | 195,884 KB |
実行使用メモリ | 25,680 KB |
最終ジャッジ日時 | 2024-09-19 04:55:20 |
合計ジャッジ時間 | 10,814 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | TLE | - |
testcase_01 | -- | - |
testcase_02 | -- | - |
testcase_03 | -- | - |
testcase_04 | -- | - |
testcase_05 | -- | - |
testcase_06 | -- | - |
testcase_07 | -- | - |
testcase_08 | -- | - |
testcase_09 | -- | - |
testcase_10 | -- | - |
testcase_11 | -- | - |
testcase_12 | -- | - |
testcase_13 | -- | - |
testcase_14 | -- | - |
testcase_15 | -- | - |
testcase_16 | -- | - |
testcase_17 | -- | - |
testcase_18 | -- | - |
testcase_19 | -- | - |
testcase_20 | -- | - |
testcase_21 | -- | - |
testcase_22 | -- | - |
testcase_23 | -- | - |
testcase_24 | -- | - |
testcase_25 | -- | - |
testcase_26 | -- | - |
testcase_27 | -- | - |
testcase_28 | -- | - |
testcase_29 | -- | - |
testcase_30 | -- | - |
testcase_31 | -- | - |
testcase_32 | -- | - |
testcase_33 | -- | - |
testcase_34 | -- | - |
testcase_35 | -- | - |
testcase_36 | -- | - |
testcase_37 | -- | - |
testcase_38 | -- | - |
testcase_39 | -- | - |
testcase_40 | -- | - |
testcase_41 | -- | - |
testcase_42 | -- | - |
testcase_43 | -- | - |
testcase_44 | -- | - |
testcase_45 | -- | - |
testcase_46 | -- | - |
testcase_47 | -- | - |
testcase_48 | -- | - |
testcase_49 | -- | - |
testcase_50 | -- | - |
testcase_51 | -- | - |
testcase_52 | -- | - |
testcase_53 | -- | - |
testcase_54 | -- | - |
testcase_55 | -- | - |
testcase_56 | -- | - |
testcase_57 | -- | - |
ソースコード
#include <bits/stdc++.h> using namespace std; template <class T> inline void chmax(T& a, const T b){a=max(a,b);} template <class T, class U>ostream &operator<<(ostream &o, const map<T, U>&obj) {o << "{"; for (auto &x : obj) o << " {" << x.first << " : " << x.second << "}" << ","; o << " }"; return o;} template <class T>ostream &operator<<(ostream &o, const set<T>&obj) {o << "{"; for (auto itr = obj.begin(); itr != obj.end(); ++itr) o << (itr != obj.begin() ? ", " : "") << *itr; o << "}"; return o;} template <class T>ostream &operator<<(ostream &o, const multiset<T>&obj) {o << "{"; for (auto itr = obj.begin(); itr != obj.end(); ++itr) o << (itr != obj.begin() ? ", " : "") << *itr; o << "}"; return o;} template <class T>ostream &operator<<(ostream &o, const vector<T>&obj) {o << "{"; for (int i = 0; i < (int)obj.size(); ++i)o << (i > 0 ? ", " : "") << obj[i]; o << "}"; return o;} template <class T, class U>ostream &operator<<(ostream &o, const pair<T, U>&obj) {o << "{" << obj.first << ", " << obj.second << "}"; return o;} template <template <class tmp> class T, class U> ostream &operator<<(ostream &o, const T<U> &obj) {o << "{"; for (auto itr = obj.begin(); itr != obj.end(); ++itr)o << (itr != obj.begin() ? ", " : "") << *itr; o << "}"; return o;} void print(void) {cout << endl;} template <class Head> void print(Head&& head) {cout << head;print();} template <class Head, class... Tail> void print(Head&& head, Tail&&... tail) {cout << head << " ";print(forward<Tail>(tail)...);} inline void balance(multiset<long long>& stl,multiset<long long>& str) { int nl = stl.size(), nr = str.size(); while( (nl!=nr) && (nl != nr+1)) { if(nl>nr) { long long tmp = *stl.rbegin(); str.insert(tmp); stl.erase(stl.find(tmp)); } else { stl.insert(*str.begin()); str.erase(str.begin()); } nl = stl.size(); nr = str.size(); } } inline void exchange(multiset<long long>& stl,multiset<long long>& str){ if(!str.size()) return; long long tmpl = *stl.rbegin(); long long tmpr = *str.begin(); while(tmpl>tmpr){ stl.erase(stl.find(tmpl)); stl.insert(tmpr); str.erase(str.find(tmpr)); str.insert(tmpl); tmpl = *stl.rbegin(); tmpr = *str.begin(); } } int main() { cin.tie(0);ios::sync_with_stdio(false); int N; cin >> N; assert(1<=N && N <= 10000); vector<long long> A(N); for(int i = 0; i < N; ++i) cin >> A[i]; for(int i = 0; i < N; ++i) assert(-1000000000<=A[i] && A[i] <= 1000000000); //クエリ区間を列挙、調和級数なのでO(N*logN) vector<pair<long long,long long>> range; for(int n = 1; n <= N; ++n) { int M = N/n; for(int i = 0; i+n <= N; i+=n) range.push_back({i,i+n-1}); for(int i = N-M*n; i+n <= N; i+=n) range.push_back({i,i+n-1}); } //Mo順ソート O(N*logN*log(NlogN)) int bucket = max(1,(int)sqrt(N)-10); vector<int> idx(range.size()); iota(idx.begin(),idx.end(),0); sort(idx.begin(),idx.end(),[&](int a, int b){ auto al = range[a].first/bucket; auto& ar = range[a].second; auto bl = range[b].first/bucket; auto& br = range[b].second; return (al != bl) ? (al < bl) : ((al%2)?(ar > br):(ar < br)); }); //Moで中央値列挙 O(N*sqrt(N)*(logN)^2) int l = 0, r = 0; multiset<long long> stl,str; stl.insert(A[0]); unordered_map<long long,long long> mp; for(int& i:idx){ auto& xl = range[i].first; auto& xr = range[i].second; int cnt = 0; //左端を広げる while(xl < l){ l--; stl.insert(A[l]); balance(stl,str); exchange(stl,str); } //右端を広げる while(r < xr){ r++; stl.insert(A[r]); balance(stl,str); exchange(stl,str); } //左端を狭める while(l < xl){ if(cnt++ > 20000) { cout << -1 << endl; return 0; } if(stl.count(A[l])) stl.erase(stl.find(A[l])); else str.erase(str.find(A[l])); l++; balance(stl,str); exchange(stl,str); } //右端を狭める while(xr < r){ if(stl.count(A[r])) stl.erase(stl.find(A[r])); else str.erase(str.find(A[r])); r--; balance(stl,str); exchange(stl,str); } mp[xl*N+xr] = *stl.rbegin(); } long long ans = 0; int cnt = 0; //区間長決め打ち全探索O(N*logN) for(long long n = 1; n <= N; ++n) { int M = N/n; vector<long long> lSum(M,0),rSum(M,0); vector<pair<long long,long long>> lRange(M),rRange(M); //区間取得 for(int i = 0; i < M; ++i) { lRange[i] = range[cnt + i]; lSum[i] = n*mp[lRange[i].first*N+lRange[i].second]; rRange[i] = range[cnt + i + M]; rSum[i] = n*mp[rRange[i].first*N+rRange[i].second]; } //累積和 for(int i = 1; i < M; ++i) lSum[i] += lSum[i-1]; for(int i = M-2; 0 <= i; --i) rSum[i] += rSum[i+1]; //累積max for(int i = 1; i < M; ++i) chmax(lSum[i],lSum[i-1]); for(int i = M-2; 0 <= i; --i) chmax(rSum[i],rSum[i+1]); chmax(ans,lSum[M-1]); chmax(ans,rSum[0]); //尺取りしながら左右決め打ち全探索 int j = 0; for(int i = 0; i < M; ++i) { while(j < M && lRange[i].second >= rRange[j].first) j++; if(j<M && lRange[i].second < rRange[j].first) { chmax(ans,lSum[i]+rSum[j]); } } cnt += 2*M; } cout << ans << endl; return 0; }