結果

問題 No.956 Number of Unbalanced
ユーザー mtsdmtsd
提出日時 2019-12-19 03:00:51
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 3,726 bytes
コンパイル時間 1,420 ms
コンパイル使用メモリ 126,004 KB
実行使用メモリ 9,764 KB
最終ジャッジ日時 2023-09-21 04:50:27
合計ジャッジ時間 4,127 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
5,812 KB
testcase_01 AC 4 ms
5,616 KB
testcase_02 AC 3 ms
5,700 KB
testcase_03 AC 3 ms
5,984 KB
testcase_04 AC 3 ms
5,696 KB
testcase_05 AC 4 ms
5,664 KB
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 AC 21 ms
7,460 KB
testcase_14 AC 86 ms
9,360 KB
testcase_15 AC 21 ms
7,200 KB
testcase_16 AC 70 ms
9,004 KB
testcase_17 AC 19 ms
7,188 KB
testcase_18 WA -
testcase_19 AC 22 ms
7,244 KB
testcase_20 AC 87 ms
9,568 KB
testcase_21 AC 49 ms
7,584 KB
testcase_22 AC 20 ms
7,052 KB
testcase_23 AC 77 ms
9,028 KB
testcase_24 AC 25 ms
7,540 KB
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <climits>
#include <cmath>
#include <complex>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#include <cstdint>
using namespace std;
typedef long long ll;
#define MP make_pair
#define PB push_back
#define inf 1000000007
#define mod 1000000007
#define rep(i,n) for(int i = 0; i < (int)(n); ++i)

template<typename T> class BIT {
private:
	int n;
	vector<T> bit;
public:
	// 0_indexed で i 番目の要素に x を加える
	void add(int i, T x){
		i++;
		while(i < n){
			bit[i] += x, i += i & -i;
		}
	}
	// 0_indexed で [0,i] の要素の和(両閉区間!!)
	T sum(int i){
		i++;
		T s = 0;
		while(i > 0){
			s += bit[i], i -= i & -i;
		}
		return s;
	}
	BIT(){}
	//初期値がすべて0の場合
	BIT(int sz) : n(sz+1), bit(n, 0){}
	BIT(vector<T>& v) : n((int)v.size()+1), bit(n, 0){
		for(int i = 0; i < n-1; i++){
			add(i,v[i]);
		}
	}
	void print(){
		for(int i = 0; i < n-1; i++){
			cout<<sum(i)-sum(i-1)<< " ";
		}
		cout<<endl;
	}
	//-1スタート
	void print_sum(){
		for(int i = 0; i < n; i++){
			cout<<sum(i-1)<<" ";
		}
		cout<<endl;
	}
};


int main(){
    int n;
    cin >> n;
    vector<int> a(n);
    vector<int> c(100001);
    vector<vector<int> > pp(100001);
    rep(i,n){
        cin >> a[i];
        c[a[i]]++;
        pp[a[i]].push_back(i);
    }
    vector<pair<int,int> > p;
    
    rep(i,100001){
        if(c[i]!=0){
            p.push_back(MP(c[i],i));
        }
    }
    ll sm = 0;
    sort(p.rbegin(),p.rend());
    
    for(int z=0;z<p.size();z++){
        int k = p[z].second;
        if(z<=10){
            vector<int> d(n+1);
            rep(i,n){
                if(a[i]==k){
                    d[i+1] = 1;
                }else{
                    d[i+1] = -1;
                }
            }
            int g = -1;
            for(int i=1;i<n;i++){
                d[i+1] += d[i];
                g = min(g,d[i+1]);
            }
            g = -g;
            int mx = 0;
            for(int i=0;i<=n;i++){
                d[i] += g;
                mx = max(mx,d[i]);
            }
            BIT<int> bit(mx+1);
            
            for(int i=0;i<=n;i++){
                sm += bit.sum(d[i]-1);
                bit.add(d[i],1);
            }
        }else{
            vector<int> d = pp[k];
            int m = d.size();
            for(int i=0;i<m;i++){
                for(int j=i;j<m;j++){
                    int g = j-i+1;
                    if(d[j]-d[i]+1 < 2*g){
                        int P = 2*g-1-(d[j]-d[i]+1);
                        int L,R;
                        if(i==0){
                            L = d[0];
                        }else{
                            L = d[i]-d[i-1]-1;
                        }
                        if(j==m-1){
                            R = n-1-d[j];
                        }else{
                            R = d[i+1]-d[i]-1;
                        }
                        if(R+L<=P){
                            sm += (R+1)*(L+1);
                        }else{
                            sm += (P+2)*(P+1)/2;
                            if(R<P){
                                sm -= (P-R+1)*(P-R)/2;    
                            }
                            if(L<P){
                                sm -= (P-L+1)*(P-L)/2;  
                            }
                        }
                    }
                }
            }
        }
    }
    cout << sm << endl;
    return 0;
}
0