結果

問題 No.956 Number of Unbalanced
ユーザー climpetclimpet
提出日時 2019-12-20 23:42:02
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 52 ms / 2,000 ms
コード長 1,045 bytes
コンパイル時間 488 ms
コンパイル使用メモリ 56,612 KB
実行使用メモリ 7,516 KB
最終ジャッジ日時 2023-09-25 03:35:44
合計ジャッジ時間 3,989 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 20 ms
5,740 KB
testcase_07 AC 19 ms
4,440 KB
testcase_08 AC 19 ms
5,676 KB
testcase_09 AC 14 ms
4,380 KB
testcase_10 AC 15 ms
4,376 KB
testcase_11 AC 29 ms
7,384 KB
testcase_12 AC 20 ms
6,064 KB
testcase_13 AC 11 ms
4,376 KB
testcase_14 AC 25 ms
7,508 KB
testcase_15 AC 12 ms
4,376 KB
testcase_16 AC 18 ms
5,904 KB
testcase_17 AC 11 ms
4,380 KB
testcase_18 AC 30 ms
7,452 KB
testcase_19 AC 11 ms
4,376 KB
testcase_20 AC 29 ms
7,456 KB
testcase_21 AC 12 ms
6,604 KB
testcase_22 AC 11 ms
4,380 KB
testcase_23 AC 18 ms
5,912 KB
testcase_24 AC 11 ms
4,428 KB
testcase_25 AC 26 ms
4,376 KB
testcase_26 AC 12 ms
4,376 KB
testcase_27 AC 52 ms
4,536 KB
testcase_28 AC 25 ms
4,380 KB
testcase_29 AC 31 ms
7,516 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <algorithm>
#include <vector>
using namespace std;

namespace{
typedef long long LL;

void main2(){
	int n;
	scanf("%d", &n);
	vector<int> as(n);
	for(int i = 0; i < n; ++i){
		scanf("%d", &as[i]);
	}
	int mval = *max_element(as.begin(), as.end());
	vector<vector<int> > postbl(mval + 1);
	for(int i = 0; i < n; ++i){
		postbl[as[i]].push_back(i);
	}

	vector<int> cnts;
	cnts.reserve(2 * n + 10);
	LL ans = 0;
	for(int x = 0; x <= mval; ++x){
		const vector<int> &poss = postbl[x];
		int sz = poss.size();
		for(int l = 0; l < sz; ){
			int r;
			for(r = l + 1; r < sz && poss[r] - poss[r - 1] <= sz; ++r);
			int beg = max(poss[l] - (r - l) + 1, 0);
			int en = min(poss[r - 1] + (r - l), n);
			cnts.assign((en - beg) * 2 + 8, 0);
			int h = en - beg + 4;
			cnts[h] = 1;
			LL low = 0;
			for(int i = beg; i < en; ++i){
				if(x == as[i]){
					low += cnts[h++];
				}
				else{
					low -= cnts[--h];
				}
				++cnts[h];
				ans += low;
			}
			l = r;
		}
	}
	printf("%lld\n", ans);
}

}

int main(){ main2(); }
0