結果

問題 No.326 あみだますたー
ユーザー imulanimulan
提出日時 2016-01-22 18:56:49
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 3 ms / 2,000 ms
コード長 2,174 bytes
コンパイル時間 622 ms
コンパイル使用メモリ 80,216 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-25 12:01:22
合計ジャッジ時間 1,569 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 3 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 1 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 3 ms
5,376 KB
testcase_10 AC 3 ms
5,376 KB
testcase_11 AC 1 ms
5,376 KB
testcase_12 AC 3 ms
5,376 KB
testcase_13 AC 2 ms
5,376 KB
testcase_14 AC 2 ms
5,376 KB
testcase_15 AC 2 ms
5,376 KB
testcase_16 AC 2 ms
5,376 KB
testcase_17 AC 1 ms
5,376 KB
testcase_18 AC 2 ms
5,376 KB
testcase_19 AC 2 ms
5,376 KB
testcase_20 AC 1 ms
5,376 KB
testcase_21 AC 1 ms
5,376 KB
testcase_22 AC 2 ms
5,376 KB
testcase_23 AC 2 ms
5,376 KB
testcase_24 AC 2 ms
5,376 KB
testcase_25 AC 3 ms
5,376 KB
testcase_26 AC 1 ms
5,376 KB
testcase_27 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main(int, const char**)’:
main.cpp:33:10: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   33 |     scanf(" %d %d", &x, &y);
      |     ~~~~~^~~~~~~~~~~~~~~~~~
main.cpp:37:32: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   37 |   for(int i=1; i<=n; ++i) scanf(" %d", &a[i]);
      |                           ~~~~~^~~~~~~~~~~~~~

ソースコード

diff #

#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <algorithm>
#include <set>
#include <sstream>
#include <utility>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cctype>
#include <climits>
using namespace std;

typedef long long ll;
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) for(int i=0;i<(n);++i)
#define foreach(itr,c) for(__typeof(c.begin()) itr=c.begin(); itr!=c.end(); itr++)

int main(int argc, char const *argv[]) {
  int n,k;
  cin >>n >>k;

  vector<int> p(n+1), a(n+1);
  for(int i=1; i<=n; ++i) p[i]=i;

  REP(i,k){
    int x,y;
    scanf(" %d %d", &x, &y);
    swap(p[x],p[y]);
  }

  for(int i=1; i<=n; ++i) scanf(" %d", &a[i]);

  int pos[101];
  for(int i=1; i<=n; ++i){
    pos[p[i]]=i;
  }

  /*
  printf("p::\n");
  for(int i=1; i<=n; ++i) printf("%d ", p[i]);
  printf("\n");
  */
  /*
  printf("pos::\n");
  for(int i=1; i<=n; ++i) printf("%d ", pos[i]);
  printf("\n");
  */

  vector<pair<int,int> > ans;
  //posをaに一致させるために入れ替えを発生させる
  for(int i=1; i<=n; ++i){
    //iはaの何番目にあるか
    int a_index=0;
    for(int j=1; j<=n; ++j){
      if(a[j]==i) a_index=j;
    }

    //その位置には現在vがある
    int v=pos[a_index];
    //iとvを交換したい
    if(i<v){ //(v,v-1),(v-1,v-2),...,(i+1,i)
      for(int j=v; j>i; --j){
        ans.push_back(make_pair(j-1,j));

        //pos上でj-1とjの値をもつindexを探す
        int tp,tq;
        for(int k=1; k<=n; ++k){
          if(pos[k]==j-1) tp=k;
          if(pos[k]==j) tq=k;
        }
        swap(pos[tp],pos[tq]);
      }
    }
    else if(i>v){ //(v,v+1),(v+1,v+2),...,(i-1,i)
      for(int j=v; j<i; ++j){
        ans.push_back(make_pair(j,j+1));

        //pos上でjとj+1の値をもつindexを探す
        int tp,tq;
        for(int k=1; k<=n; ++k){
          if(pos[k]==j) tp=k;
          if(pos[k]==j+1) tq=k;
        }
        swap(pos[tp],pos[tq]);
      }
    }

  }

  cout << ans.size() << endl;
  REP(i,ans.size()){
    printf("%d %d\n", ans[i].first, ans[i].second);
  }

  return 0;
}
0