結果

問題 No.919 You Are A Project Manager
ユーザー ningenMeningenMe
提出日時 2019-09-25 09:41:25
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 862 ms / 3,000 ms
コード長 3,705 bytes
コンパイル時間 2,379 ms
コンパイル使用メモリ 192,104 KB
実行使用メモリ 17,136 KB
最終ジャッジ日時 2023-09-10 00:36:33
合計ジャッジ時間 22,173 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 831 ms
13,840 KB
testcase_01 AC 2 ms
4,384 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 74 ms
5,380 KB
testcase_05 AC 81 ms
5,648 KB
testcase_06 AC 5 ms
4,384 KB
testcase_07 AC 225 ms
7,616 KB
testcase_08 AC 34 ms
4,388 KB
testcase_09 AC 22 ms
4,384 KB
testcase_10 AC 47 ms
4,380 KB
testcase_11 AC 46 ms
4,456 KB
testcase_12 AC 2 ms
4,384 KB
testcase_13 AC 60 ms
4,988 KB
testcase_14 AC 4 ms
4,380 KB
testcase_15 AC 14 ms
4,380 KB
testcase_16 AC 97 ms
5,388 KB
testcase_17 AC 648 ms
16,416 KB
testcase_18 AC 627 ms
14,252 KB
testcase_19 AC 782 ms
14,100 KB
testcase_20 AC 758 ms
13,428 KB
testcase_21 AC 833 ms
13,860 KB
testcase_22 AC 253 ms
7,880 KB
testcase_23 AC 243 ms
7,952 KB
testcase_24 AC 269 ms
8,152 KB
testcase_25 AC 254 ms
7,788 KB
testcase_26 AC 716 ms
13,232 KB
testcase_27 AC 617 ms
13,680 KB
testcase_28 AC 812 ms
14,352 KB
testcase_29 AC 643 ms
16,516 KB
testcase_30 AC 632 ms
14,572 KB
testcase_31 AC 631 ms
17,136 KB
testcase_32 AC 646 ms
16,492 KB
testcase_33 AC 280 ms
8,104 KB
testcase_34 AC 284 ms
8,072 KB
testcase_35 AC 848 ms
13,636 KB
testcase_36 AC 852 ms
13,780 KB
testcase_37 AC 862 ms
13,724 KB
testcase_38 AC 850 ms
13,924 KB
testcase_39 AC 2 ms
4,380 KB
testcase_40 AC 14 ms
4,384 KB
testcase_41 AC 4 ms
4,380 KB
testcase_42 AC 4 ms
4,380 KB
testcase_43 AC 8 ms
4,380 KB
testcase_44 AC 20 ms
4,380 KB
testcase_45 AC 6 ms
4,380 KB
testcase_46 AC 17 ms
4,384 KB
testcase_47 AC 251 ms
7,808 KB
testcase_48 AC 240 ms
7,808 KB
testcase_49 AC 271 ms
8,060 KB
testcase_50 AC 253 ms
7,736 KB
testcase_51 AC 225 ms
7,620 KB
testcase_52 AC 160 ms
6,828 KB
testcase_53 AC 223 ms
7,608 KB
testcase_54 AC 14 ms
4,380 KB
testcase_55 AC 2 ms
4,380 KB
testcase_56 AC 2 ms
4,380 KB
testcase_57 AC 1 ms
4,384 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
template <class T> inline void chmax(T& a, const T b){a=max(a,b);}

inline void balanceLtoR(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));
	}
}

inline void balanceRtoL(multiset<long long>& stl,multiset<long long>& str) {
	if(stl.size()<str.size()) {
		stl.insert(*str.begin());
		str.erase(str.begin());
	}
}

inline void exchange(multiset<long long>& stl,multiset<long long>& str){
	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);
	}
}

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;
		
		//左端を広げる
		while(xl < l){
			l--;
			stl.insert(A[l]);
			if(str.size()) exchange(stl,str);
			balanceLtoR(stl,str);
		}
		//右端を広げる
		while(r < xr){
			r++;
			stl.insert(A[r]);
			if(str.size()) exchange(stl,str);
			balanceLtoR(stl,str);
		}
		//左端を狭める
		while(l < xl){
			if(stl.find(A[l])!=stl.end()) {
				stl.erase(stl.find(A[l]));
				balanceRtoL(stl,str);
			}
			else {
				str.erase(str.find(A[l]));
				balanceLtoR(stl,str);
			}
			l++;
		}
		//右端を狭める
		while(xr < r){
			if(stl.find(A[r])!=stl.end()) {
				stl.erase(stl.find(A[r]));
				balanceRtoL(stl,str);
			}
			else {
				str.erase(str.find(A[r]));
				balanceLtoR(stl,str);
			}
			r--;
		}
		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;
}
0