結果

問題 No.919 You Are A Project Manager
ユーザー ningenMeningenMe
提出日時 2019-11-09 00:55:41
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 724 ms / 3,000 ms
コード長 6,658 bytes
コンパイル時間 2,354 ms
コンパイル使用メモリ 193,856 KB
実行使用メモリ 15,284 KB
最終ジャッジ日時 2024-06-27 19:45:41
合計ジャッジ時間 19,311 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 697 ms
12,432 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 2 ms
6,944 KB
testcase_04 AC 71 ms
6,944 KB
testcase_05 AC 79 ms
6,940 KB
testcase_06 AC 5 ms
6,944 KB
testcase_07 AC 193 ms
7,028 KB
testcase_08 AC 29 ms
6,940 KB
testcase_09 AC 19 ms
6,940 KB
testcase_10 AC 42 ms
6,940 KB
testcase_11 AC 37 ms
6,940 KB
testcase_12 AC 2 ms
6,940 KB
testcase_13 AC 52 ms
6,940 KB
testcase_14 AC 3 ms
6,940 KB
testcase_15 AC 12 ms
6,940 KB
testcase_16 AC 84 ms
6,940 KB
testcase_17 AC 601 ms
15,244 KB
testcase_18 AC 586 ms
12,808 KB
testcase_19 AC 694 ms
12,424 KB
testcase_20 AC 642 ms
11,908 KB
testcase_21 AC 718 ms
12,300 KB
testcase_22 AC 218 ms
7,292 KB
testcase_23 AC 210 ms
7,172 KB
testcase_24 AC 225 ms
7,428 KB
testcase_25 AC 215 ms
7,208 KB
testcase_26 AC 608 ms
11,524 KB
testcase_27 AC 509 ms
10,884 KB
testcase_28 AC 697 ms
12,176 KB
testcase_29 AC 599 ms
15,072 KB
testcase_30 AC 583 ms
12,552 KB
testcase_31 AC 601 ms
15,284 KB
testcase_32 AC 604 ms
15,048 KB
testcase_33 AC 241 ms
7,424 KB
testcase_34 AC 240 ms
7,548 KB
testcase_35 AC 723 ms
12,296 KB
testcase_36 AC 724 ms
12,300 KB
testcase_37 AC 708 ms
12,296 KB
testcase_38 AC 723 ms
12,296 KB
testcase_39 AC 2 ms
6,940 KB
testcase_40 AC 13 ms
6,940 KB
testcase_41 AC 4 ms
6,940 KB
testcase_42 AC 5 ms
6,944 KB
testcase_43 AC 7 ms
6,940 KB
testcase_44 AC 18 ms
6,940 KB
testcase_45 AC 5 ms
6,940 KB
testcase_46 AC 15 ms
6,944 KB
testcase_47 AC 216 ms
7,288 KB
testcase_48 AC 212 ms
7,156 KB
testcase_49 AC 229 ms
7,416 KB
testcase_50 AC 215 ms
7,416 KB
testcase_51 AC 192 ms
7,028 KB
testcase_52 AC 139 ms
6,940 KB
testcase_53 AC 184 ms
7,032 KB
testcase_54 AC 13 ms
6,948 KB
testcase_55 AC 1 ms
6,940 KB
testcase_56 AC 2 ms
6,940 KB
testcase_57 AC 2 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;

#define REP(i,n) for(long long i = 0; i < (n); i++)
#define FOR(i, m, n) for(long long i = (m);i < (n); ++i)
#define ALL(obj) (obj).begin(),(obj).end()
#define SPEED cin.tie(0);ios::sync_with_stdio(false);

template<class T> using V = vector<T>;
template<class T, class U> using P = pair<T, U>;
template<class T> using PQ = priority_queue<T>;
template<class T> using PQR = priority_queue<T,vector<T>,greater<T>>;

constexpr long long MOD = (long long)1e9 + 7;
constexpr long long MOD2 = 998244353;
constexpr long long HIGHINF = (long long)1e18;
constexpr long long LOWINF = (long long)1e15;
constexpr long double PI = 3.1415926535897932384626433;

template <class T> vector<T> multivector(size_t N,T init){return vector<T>(N,init);}
template <class... T> auto multivector(size_t N,T... t){return vector<decltype(multivector(t...))>(N,multivector(t...));}
template <class T> void corner(bool flg, T hoge) {if (flg) {cout << hoge << endl; exit(0);}}
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)...);}
template <class T> void chmax(T& a, const T b){a=max(a,b);}
template <class T> void chmin(T& a, const T b){a=min(a,b);}
void YN(bool flg) {cout << (flg ? "YES" : "NO") << endl;}
void Yn(bool flg) {cout << (flg ? "Yes" : "No") << endl;}
void yn(bool flg) {cout << (flg ? "yes" : "no") << endl;}

template<class T> class Median{
	multiset<T, greater<>> stl;
	multiset<T> str;
    T init;

    // 要素の均衡を保つ関数
    inline void balance() {
        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());
        }

        if(!str.size()) return;

        for(T 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);
        }
    }
public:
    Median(T init):init(init){
        //do nothing
    }

    void insert(T val){
        stl.insert(val);
        balance();
    }

    int find(T val){
        return (stl.find(val)!=stl.end() || str.find(val)!=str.end());
    }

    void eraseItr(T val){
        auto itr = stl.find(val);
        if(itr != stl.end()) {
            stl.erase(itr);
            balance();
            return;
        }
        itr = str.find(val);
        if(itr != str.end()) {
            str.erase(itr);
            balance();
            return;
        }
    }

    void eraseVal(T val){
        stl.erase(val);
        str.erase(val);
        balance();
    }

    T get(){
        return (stl.empty()?init:*stl.begin());
    }

    size_t size(){
        return stl.size()+str.size();
    }
};


int main() {
    cin.tie(0);ios::sync_with_stdio(false);
	int N; cin >> N;
	vector<int> A(N);
	for(int i = 0; i < N; ++i) cin >> A[i];

	//クエリ区間を列挙、調和級数なので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)^2)
    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;
    Median<int> med(0);
    med.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--;
			med.insert(A[l]);
		}
		//右端を広げる
		while(r < xr){
			r++;
			med.insert(A[r]);
		}
		//左端を狭める
		while(l < xl){
            med.eraseItr(A[l]);
			l++;
		}
		//右端を狭める
		while(xr < r){
            med.eraseItr(A[r]);
			r--;
		}
		mp[xl*N+xr] = med.get();
	}

	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