結果

問題 No.1371 交換門松列・松
ユーザー 夕叢霧香(ゆうむらきりか)夕叢霧香(ゆうむらきりか)
提出日時 2020-11-03 02:25:47
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 1,867 bytes
コンパイル時間 1,171 ms
コンパイル使用メモリ 85,412 KB
実行使用メモリ 9,880 KB
最終ジャッジ日時 2023-09-30 01:59:30
合計ジャッジ時間 7,207 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 1 ms
4,376 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 2 ms
4,376 KB
testcase_17 AC 1 ms
4,376 KB
testcase_18 TLE -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC optimize ("O3")
#include<algorithm>
#include<cassert>
#include<iostream>
#include<vector>
using namespace std;
typedef long long lint;
typedef vector<int>vi;
typedef pair<int,int>pii;
#define rep(i,n)for(int i=0;i<(int)(n);++i)

bool kado(lint a,lint b,lint c){
  return (a-b)*(c-b)>0;
}

const int N=300000;
pii cand[N];

int main(){
  int n;
  cin>>n;
  vi a(n);
  rep(i,n)cin>>a[i],a[i]--;
  lint ans=0;
  //近い要素
  rep(i,n){
    for(int j=i+1;j<=min(n-1,i+2);j++){
      swap(a[i],a[j]);
      int ok=1;
      for(int k=max(0,i-2);k<=min(n-3,j);k++){
        ok&=kado(a[k],a[k+1],a[k+2]);
      }
      if(ok)ans++;
      swap(a[i],a[j]);
    }
  }
  //遠い要素
  //cand[i] を計算する
  rep(i,n){
    int ma=n-1,mi=0;
    int x=a[i];
    if(i<n-2){
      if(a[i+1]<a[i+2])mi=max(mi,a[i+1]+1);
      else ma=min(ma,a[i+1]-1);
    }
    if(i>=2){
      if(a[i-1]<a[i-2])mi=max(mi,a[i-1]+1);
      else ma=min(ma,a[i-1]-1);
    }
    vector<pii>c;
    if(i>0&&i<n-1){
      int l=min(a[i-1],a[i+1])-1,r=max(a[i-1],a[i+1])+1;
      if(mi<=l)c.push_back(pii(mi,l));
      if(r<=ma)c.push_back(pii(r,ma));
    }else{
      c.push_back(pii(mi,ma));
    }
    //実は、区間はちょうど 1 個。
    assert(c.size()==1);
    cand[x]=c[0];
  }
  //TLE: BIT を使わずに O(N^2) で計算する。
  rep(i,n){
    int l=cand[i].first,r=cand[i].second;
    r=min(r,i-1);
    for(int j=l;j<=r;j++){
      int lj=cand[j].first,rj=cand[j].second;
      if(lj<=i&&i<=rj)ans++;
    }
  }
  //近い要素の影響を消す。
  rep(i,n){
    int l=cand[a[i]].first,r=cand[a[i]].second;
    for(int j=max(0,i-2);j<=min(n-1,i+2);j++){
      if(a[i]<=a[j])continue;
      if(l<=a[j]&&a[j]<=r){
        int lj=cand[a[j]].first,rj=cand[a[j]].second;
        if(lj<=a[i]&&a[i]<=rj){
          ans--;
        }
      }
    }
  }
  cout<<ans<<endl;
}
0