結果

問題 No.1371 交換門松列・松
ユーザー leaf_1415leaf_1415
提出日時 2021-01-29 22:40:42
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 158 ms / 4,000 ms
コード長 2,899 bytes
コンパイル時間 775 ms
コンパイル使用メモリ 88,080 KB
実行使用メモリ 25,128 KB
最終ジャッジ日時 2023-09-09 16:14:53
合計ジャッジ時間 3,990 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 8 ms
14,692 KB
testcase_01 AC 8 ms
14,740 KB
testcase_02 AC 8 ms
14,792 KB
testcase_03 AC 8 ms
14,704 KB
testcase_04 AC 8 ms
14,840 KB
testcase_05 AC 8 ms
14,768 KB
testcase_06 AC 8 ms
14,772 KB
testcase_07 AC 8 ms
14,860 KB
testcase_08 AC 7 ms
14,880 KB
testcase_09 AC 7 ms
14,656 KB
testcase_10 AC 7 ms
14,816 KB
testcase_11 AC 7 ms
14,660 KB
testcase_12 AC 8 ms
14,692 KB
testcase_13 AC 8 ms
14,704 KB
testcase_14 AC 7 ms
14,856 KB
testcase_15 AC 7 ms
14,772 KB
testcase_16 AC 8 ms
14,704 KB
testcase_17 AC 8 ms
14,660 KB
testcase_18 AC 69 ms
24,904 KB
testcase_19 AC 69 ms
24,944 KB
testcase_20 AC 69 ms
24,928 KB
testcase_21 AC 71 ms
24,888 KB
testcase_22 AC 70 ms
25,128 KB
testcase_23 AC 145 ms
24,900 KB
testcase_24 AC 148 ms
24,932 KB
testcase_25 AC 141 ms
24,964 KB
testcase_26 AC 151 ms
24,912 KB
testcase_27 AC 136 ms
24,912 KB
testcase_28 AC 147 ms
24,860 KB
testcase_29 AC 147 ms
24,884 KB
testcase_30 AC 142 ms
24,936 KB
testcase_31 AC 158 ms
24,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstdlib>
#include <cassert>
#include <vector>
#include <list>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <bitset>
#include <string>
#include <algorithm>
#include <utility>
#include <complex>
#define rep(x, s, t) for(llint (x) = (s); (x) <= (t); (x)++)
#define chmin(x, y) (x) = min((x), (y))
#define chmax(x, y) (x) = max((x), (y))
#define all(x) (x).begin(),(x).end()
#define inf 1e18

using namespace std;

typedef long long llint;
typedef long long ll;
typedef pair<llint, llint> P;

struct BIT{
	int size;
	vector<llint> bit;
	BIT(){size = 0;}
	BIT(int s){
		size = s;
		bit.resize(size+1);
		init();
	}
	void init(){
		for(int i = 1; i <= size; i++) bit[i] = 0;
	}
	llint query(int i){
		llint ret = 0;
		while(i > 0){
			ret += bit[i];
			i -= i&(-i);
		}
		return ret;
	}
	void add(int i, llint x){
		while(i <= size){
			bit[i] += x;
			i += i&(-i);
		}
	}
};


ll n;
ll a[200005];
vector<ll> avec[200005], qvec[200005];
BIT bit(200005);

bool check(ll a, ll b, ll c)
{
	if(a == b || b == c || c == a) return false;
	if(a > b && b < c) return true;
	if(a < b && b > c) return true;
	return false;
}
bool calc(ll x)
{
	if(x <= 1 || x >= n) return true;
	return check(a[x-1], a[x], a[x+1]);
}

int main(void)
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	
	cin >> n;
	rep(i, 1, n) cin >> a[i];
	
	ll ans = 0;
	rep(i, 2, n-1){
		ll l, h;
		if(a[i-1] < a[i]){
			l = max(a[i-1], a[i+1]), h = a[i];
			avec[h].push_back(l);
			qvec[l].push_back(h);
		}
	}
	for(int i = n; i >= 1; i--){
		for(auto q : qvec[i]) ans += bit.query(q-1);//, cout << i << " " << q << endl;
		for(auto q : avec[i]) bit.add(q, 1);
		
		//rep(j, 0, n) cout << bit.query(j) << " ";  cout << endl;
	}
	
	//cout << ans << endl;
	
	bit.init();
	rep(i, 1, n) avec[i].clear(), qvec[i].clear();
	rep(i, 2, n-1){
		ll l, h;
		if(a[i-1] > a[i]){
			h = min(a[i-1], a[i+1]), l = a[i];
			avec[h].push_back(l);
			qvec[l].push_back(h);
		}
	}
	for(int i = n; i >= 1; i--){
		for(auto q : qvec[i]) ans += bit.query(q-1);
		for(auto q : avec[i]) bit.add(q, 1);
	}
	
	ans -= n-2;
	ans /= 2;
	
	
	bit.init();
	rep(i, 1, n) avec[i].clear(), qvec[i].clear();
	rep(i, 2, n-1){
		ll l, h;
		if(a[i-1] < a[i]){
			l = max(a[i-1], a[i+1]), h = a[i];
			qvec[h].push_back(l);
		}else{
			h = min(a[i-1], a[i+1]), l = a[i];
			avec[h].push_back(l);
		}
	}
	for(int i = n; i >= 1; i--){
		for(auto q : qvec[i]) ans += bit.query(n) - bit.query(q);
		for(auto q : avec[i]) bit.add(q, 1);
	}
	
	
	rep(i, 2, n){
		swap(a[1], a[i]);
		bool flag = true;
		rep(j, -1, 1) flag &= calc(1+j) && calc(i+j);
		if(flag) ans++;
		swap(a[1], a[i]);
	}
	rep(i, 2, n-1){
		swap(a[n], a[i]);
		bool flag = true;
		rep(j, -1, 1) flag &= calc(n+j) && calc(i+j);
		if(flag) ans++;
		swap(a[n], a[i]);
	}
	
	cout << ans << endl;
	
	return 0;
}
0