結果
| 問題 |
No.919 You Are A Project Manager
|
| ユーザー |
|
| 提出日時 | 2019-09-12 22:25:08 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 4,463 bytes |
| コンパイル時間 | 2,205 ms |
| コンパイル使用メモリ | 197,732 KB |
| 実行使用メモリ | 26,704 KB |
| 最終ジャッジ日時 | 2024-09-19 04:15:19 |
| 合計ジャッジ時間 | 10,933 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | -- * 3 |
| other | TLE * 1 -- * 54 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
template <class T> 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)...);}
void balance(multiset<long long>& stl,multiset<long long>& str) {
while(stl.size()>str.size()+1) {
long long tmp = *stl.rbegin();
str.insert(tmp);
stl.erase(stl.find(tmp));
}
while(stl.size()<str.size()) {
stl.insert(*str.begin());
str.erase(str.begin());
}
}
int main() {
cin.tie(0);ios::sync_with_stdio(false);
int N; cin >> N;
vector<long long> A(N);
for(int i = 0; i < N; ++i) cin >> A[i];
vector<vector<pair<int,int>>> rAsc(N+1),rDsc(N+1);
vector<pair<int,int>> range;
for(int i = 1; i <= N; ++i) {
for(int j = 0; j+i <= N; j+=i) {
rAsc[i].push_back({j,j+i-1});
range.push_back({j,j+i-1});
}
for(int j = N; 0 <= j-i; j-=i) {
rDsc[i].push_back({j-i,j-1});
range.push_back({j-i,j-1});
}
reverse(rDsc[i].begin(),rDsc[i].end());
}
int Q = range.size();
int bucket = sqrt(N);
vector<int> idx(Q);
iota(idx.begin(),idx.end(),0);
sort(idx.begin(),idx.end(),[&](int a, int b){
int& al = range[a].first;
int& ar = range[a].second;
int& bl = range[b].first;
int& br = range[b].second;
return (al/bucket != bl/bucket) ? (al/bucket < bl/bucket) : (ar < br);
});
int l = 0, r = 0;
multiset<long long> stl,str;
stl.insert(A[0]);
map<pair<int,int>,long long> mp;
for(int& i:idx){
int& xl = range[i].first;
int& xr = range[i].second;
while(l < xl){
if(stl.count(A[l])) stl.erase(stl.find(A[l]));
else str.erase(str.find(A[l]));
l++;
balance(stl,str);
}
while(xl < l){
l--;
stl.insert(A[l]);
if(str.size()){
long long tmpl = *stl.rbegin();
long long tmpr = *str.begin();
if(tmpl>tmpr) {
stl.erase(stl.find(tmpl));
stl.insert(tmpr);
str.erase(str.find(tmpr));
str.insert(tmpl);
}
}
balance(stl,str);
}
while(r < xr){
r++;
stl.insert(A[r]);
if(str.size()){
long long tmpl = *stl.rbegin();
long long tmpr = *str.begin();
if(tmpl>tmpr) {
stl.erase(stl.find(tmpl));
stl.insert(tmpr);
str.erase(str.find(tmpr));
str.insert(tmpl);
}
}
balance(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);
}
if(l==xl && r==xr) mp[{xl,xr}] = *stl.rbegin();
}
long long ans = 0;
for(long long i = 1; i <= N; ++i) {
int M = N/i;
vector<long long> sAsc(M,0),sDsc(M,0);
for(int j = 0; j < M; ++j) sAsc[j] = i*mp[rAsc[i][j]];
for(int j = M-1; 0 <= j; --j) sDsc[j] = i*mp[rDsc[i][j]];
for(int j = 1; j < M; ++j) sAsc[j] += sAsc[j-1];
for(int j = M-2; 0 <= j; --j) sDsc[j] += sDsc[j+1];
for(int j = 1; j < M; ++j) chmax(sAsc[j],sAsc[j-1]);
for(int j = M-2; 0 <= j; --j) chmax(sDsc[j],sDsc[j+1]);
chmax(ans,sAsc[M-1]);
chmax(ans,sDsc[0]);
int k = 0;
for(int j = 0; j < M; ++j) {
while(k < M && rAsc[i][j].second >= rDsc[i][k].first) k++;
if(k<M && rAsc[i][j].second < rDsc[i][k].first) {
chmax(ans,sAsc[j]+sDsc[k]);
}
}
}
cout << ans << endl;
return 0;
}