結果

問題 No.326 あみだますたー
ユーザー imulanimulan
提出日時 2016-01-22 18:23:33
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 1,774 bytes
コンパイル時間 1,017 ms
コンパイル使用メモリ 80,276 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-21 13:46:11
合計ジャッジ時間 5,398 ms
ジャッジサーバーID
(参考情報)
judge11 / judge10
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
権限があれば一括ダウンロードができます
コンパイルメッセージ
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[a[i]]=i;
  }

  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){
    //a[i]の値はposの何番目にあるか
    int p_index=0;
    for(int j=1; j<=n; ++j){
      if(a[i]==pos[j]){
        p_index=j;
        break;
      }
    }

    if(p_index>i){//indexを減らす方向にswapしていく
      for(int j=p_index; j>i; --j){
        ans.push_back(make_pair(j-1,j));
        swap(pos[j-1],pos[j]);
      }

    }
    else if(p_index<i){//indexを増やす方向にswapしていく
      for(int j=p_index; j<i; ++j){
        ans.push_back(make_pair(j,j+1));
        swap(pos[j],pos[j+1]);
      }
    }

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

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

  return 0;
}
0