結果

問題 No.919 You Are A Project Manager
ユーザー ningenMeningenMe
提出日時 2019-09-26 06:51:41
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 3,581 bytes
コンパイル時間 2,326 ms
コンパイル使用メモリ 195,484 KB
実行使用メモリ 15,372 KB
最終ジャッジ日時 2023-10-24 15:08:03
合計ジャッジ時間 18,473 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 700 ms
12,468 KB
testcase_01 WA -
testcase_02 AC 2 ms
4,348 KB
testcase_03 WA -
testcase_04 AC 68 ms
5,336 KB
testcase_05 AC 77 ms
5,604 KB
testcase_06 WA -
testcase_07 AC 191 ms
7,184 KB
testcase_08 AC 29 ms
4,348 KB
testcase_09 AC 20 ms
4,348 KB
testcase_10 WA -
testcase_11 WA -
testcase_12 AC 2 ms
4,348 KB
testcase_13 WA -
testcase_14 WA -
testcase_15 AC 12 ms
4,348 KB
testcase_16 AC 84 ms
5,336 KB
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 AC 698 ms
12,468 KB
testcase_22 WA -
testcase_23 AC 204 ms
7,184 KB
testcase_24 WA -
testcase_25 WA -
testcase_26 AC 600 ms
11,408 KB
testcase_27 AC 507 ms
10,880 KB
testcase_28 AC 686 ms
12,204 KB
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 WA -
testcase_37 AC 708 ms
12,204 KB
testcase_38 WA -
testcase_39 WA -
testcase_40 WA -
testcase_41 AC 4 ms
4,348 KB
testcase_42 AC 5 ms
4,348 KB
testcase_43 WA -
testcase_44 AC 18 ms
4,348 KB
testcase_45 WA -
testcase_46 WA -
testcase_47 WA -
testcase_48 WA -
testcase_49 WA -
testcase_50 AC 211 ms
7,448 KB
testcase_51 WA -
testcase_52 WA -
testcase_53 WA -
testcase_54 WA -
testcase_55 AC 2 ms
4,348 KB
testcase_56 AC 2 ms
4,348 KB
testcase_57 WA -
権限があれば一括ダウンロードができます

ソースコード

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 balance(multiset<int, greater<>>& stl,multiset<int>& str) {
	while(stl.size() < str.size()) {
		stl.insert(*str.begin());
		str.erase(str.begin());
	}
	while(stl.size() > str.size()+1) {
		str.insert(*stl.begin());
		stl.erase(stl.begin());
	}
}

inline void exchange(multiset<int, greater<>>& stl,multiset<int>& str){
	if(!str.size()) return;
	for(int tmpl = *stl.begin(),tmpr = *str.begin(); tmpl>tmpr; tmpl = *stl.begin(), tmpr = *str.begin()) {
		stl.erase(stl.begin());
		str.erase(str.begin());
		stl.insert(tmpr);
		str.insert(tmpl);
	}
}

int main() {

	cin.tie(0);ios::sync_with_stdio(false);
	int N; cin >> N;
	assert(1<=N && N <= 10000);
	vector<int> 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<int,int>> 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 = (int)sqrt(N);
	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<int, greater<>> stl;
	multiset<int> str;
	stl.insert(A[0]);
	unordered_map<int,int> mp;

	for(int& i:idx){
		auto& xl = range[i].first;
		auto& xr = range[i].second;
		
		//左端を広げる
		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){
            auto itr = stl.find(A[l]);
			if(itr != stl.end()) stl.erase(itr);
			else str.erase(str.find(A[l]));
			l++;
			balance(stl,str);
			exchange(stl,str);
		}
		//右端を狭める
		while(xr < r){
            auto itr = stl.find(A[r]);
			if(itr != stl.end()) stl.erase(itr);
			else str.erase(str.find(A[r]));
			r--;
			balance(stl,str);
			exchange(stl,str);
		}
		mp[xl*N+xr] = *stl.begin();
	}

	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<int, int>> lRange(M),rRange(M);
		//区間取得 O(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];
		}
		//累積和 O(M)
		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 O(M)
		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]);

		//尺取りしながら左右決め打ち全探索 O(M)
		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