結果

問題 No.284 門松と魔法(2)
ユーザー 紙ぺーぱー紙ぺーぱー
提出日時 2015-09-08 02:40:40
言語 C++11
(gcc 11.4.0)
結果
TLE  
実行時間 -
コード長 1,638 bytes
コンパイル時間 541 ms
コンパイル使用メモリ 51,688 KB
実行使用メモリ 10,388 KB
最終ジャッジ日時 2023-09-26 10:03:36
合計ジャッジ時間 8,733 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

//naive TLE O(N^2)
#include <cstdio>
#include <vector>
#include <algorithm>

using namespace std;

typedef pair<int,int> P;
int n,m,x, h[100050];
P inc[100050][2],dec[100050][2],tmp[2];
int main(){
  scanf("%d",&n);
  vector<int> hs(n);
  for(int i=0;i<n;i++){
    scanf("%d",&x);
    h[i]=x;
    hs[i]=x;
  }
  hs.emplace_back(-1);hs.emplace_back(1000000000);
  sort(hs.begin(),hs.end());
  unique(hs.begin(),hs.end());
  m=hs.size();
  for(int i=0;i<n;i++){
    h[i]=lower_bound(hs.begin(),hs.end(),h[i])-hs.begin();
  }
  for(int i=0;i<m;i++){
    inc[i][0]=inc[i][1]=P(-1000000000,-1);
    dec[i][0]=dec[i][1]=P(-1000000000,-1);
  }
  inc[m-1][0]=P(0,0);
  dec[0][0]=P(0,m-1);
  for(int i=0;i<n;i++){
    //dec->inc
    const int p=h[i];
    for(int j=0;j<p;j++){
      for(int k=0;k<2;k++){
        const P v=dec[j][k];
        if(v.second==p)continue;
        if(inc[p][0].first<v.first+1){
          inc[p][1]=inc[p][0];
          inc[p][0]=P(v.first+1,j);
        }else if(inc[p][0].second!=j&&inc[p][1].first<v.first+1){
          inc[p][1]=P(v.first+1,j);
        }
      }
    }
    //inc->dec
    for(int j=p+1;j<m;j++){
      for(int k=0;k<2;k++){
        const P v=inc[j][k];
        if(v.second==p)continue;
        if(dec[p][0].first<v.first+1){
          dec[p][1]=dec[p][0];
          dec[p][0]=P(v.first+1,j);
        }else if(dec[p][0].second!=j&&dec[p][1].first<v.first+1){
          dec[p][1]=P(v.first+1,j);
        }
      }
    }
  }
  int ma=0;
  for(int i=0;i<m;i++){
    for(int j=0;j<2;j++){
      ma=max(ma,inc[i][j].first);
      ma=max(ma,dec[i][j].first);
    }
  }
  if(ma<=2)ma=0;
  printf("%d",ma);

}
0