結果

問題 No.284 門松と魔法(2)
コンテスト
ユーザー 紙ぺーぱー
提出日時 2015-09-08 02:40:40
言語 C++11
(gcc 15.2.0 + boost 1.89.0)
コンパイル:
g++-15 -O2 -lm -std=gnu++11 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
TLE  
実行時間 -
コード長 1,638 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 656 ms
コンパイル使用メモリ 73,208 KB
実行使用メモリ 81,180 KB
最終ジャッジ日時 2026-04-02 14:18:19
合計ジャッジ時間 7,869 ms
ジャッジサーバーID
(参考情報)
judge4_1 / judge3_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 24 TLE * 1 -- * 15
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function 'int main()':
main.cpp:21:9: warning: ignoring return value of '_FIter std::unique(_FIter, _FIter) [with _FIter = __gnu_cxx::__normal_iterator<int*, vector<int> >]', declared with attribute 'nodiscard' [-Wunused-result]
   21 |   unique(hs.begin(),hs.end());
      |   ~~~~~~^~~~~~~~~~~~~~~~~~~~~
In file included from /home/linuxbrew/.linuxbrew/Cellar/gcc/15.2.0_1/include/c++/15/algorithm:63,
                 from main.cpp:4:
/home/linuxbrew/.linuxbrew/Cellar/gcc/15.2.0_1/include/c++/15/bits/stl_algo.h:875:5: note: declared here
  875 |     unique(_ForwardIterator __first, _ForwardIterator __last)
      |     ^~~~~~

ソースコード

diff #
raw source code

//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