結果
問題 |
No.326 あみだますたー
|
ユーザー |
|
提出日時 | 2016-01-22 18:24:54 |
言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 1,783 bytes |
コンパイル時間 | 627 ms |
コンパイル使用メモリ | 79,680 KB |
実行使用メモリ | 5,376 KB |
最終ジャッジ日時 | 2024-09-21 15:00:32 |
合計ジャッジ時間 | 4,746 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | WA * 2 |
other | AC * 3 WA * 23 |
コンパイルメッセージ
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[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; }