結果
| 問題 |
No.326 あみだますたー
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2016-01-22 18:56:49 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
AC
|
| 実行時間 | 3 ms / 2,000 ms |
| コード長 | 2,174 bytes |
| コンパイル時間 | 734 ms |
| コンパイル使用メモリ | 80,312 KB |
| 実行使用メモリ | 5,248 KB |
| 最終ジャッジ日時 | 2024-11-07 23:25:00 |
| 合計ジャッジ時間 | 1,940 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 26 |
コンパイルメッセージ
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]);
| ~~~~~^~~~~~~~~~~~~~
ソースコード
#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;
}