結果

問題 No.1369 交換門松列・竹
ユーザー 👑 Nachia
提出日時 2021-01-29 21:36:05
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 15 ms / 2,000 ms
コード長 1,064 bytes
コンパイル時間 2,694 ms
コンパイル使用メモリ 194,972 KB
最終ジャッジ日時 2025-01-18 08:59:21
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 33
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘void loop()’:
main.cpp:16:8: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   16 |   scanf("%d",&N);
      |   ~~~~~^~~~~~~~~
main.cpp:17:17: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   17 |   rep(i,N) scanf("%d",&A[i]);
      |            ~~~~~^~~~~~~~~~~~
main.cpp: In function ‘int main()’:
main.cpp:39:15: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   39 |   int T; scanf("%d",&T);
      |          ~~~~~^~~~~~~~~

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
using LL=long long;
using ULL=unsigned long long;
#define rep(i,n) for(int i=0; i<(n); i++)

bool iskadomatsu(int a,int b,int c){
  if(a==b||b==c||a==c) return false;
  return ((a<b&&c<b)||(a>b&&c>b));
}

int N;
int A[50000];

void loop(){
  scanf("%d",&N);
  rep(i,N) scanf("%d",&A[i]);
  int prob = -1;
  vector<int> wrongs;
  rep(i,N-2) if(!iskadomatsu(A[i],A[i+1],A[i+2])) wrongs.push_back(i);
  prob=wrongs[0];
  bool ans=false;
  for(int l=prob; l<prob+3; l++){
    rep(r,N){
      bool ok=true;
      swap(A[l],A[r]);
      if(ok) for(int w:wrongs) if(!iskadomatsu(A[w],A[w+1],A[w+2])){ ok=false; break; }
      if(ok) for(int i=max(0,l-2); i<min(N-2,l+2); i++) if(!iskadomatsu(A[i],A[i+1],A[i+2])){ ok=false; break; }
      if(ok) for(int i=max(0,r-2); i<min(N-2,r+2); i++) if(!iskadomatsu(A[i],A[i+1],A[i+2])){ ok=false; break; }
      swap(A[l],A[r]);
      if(ok) ans=true;
    }
    if(ans) break;
  }
  printf(ans?"Yes\n":"No\n");
}

int main(){
  int T; scanf("%d",&T);
  while(T--) loop();
  return 0;
}
0