結果

問題 No.397 NO MORE KADOMATSU
ユーザー moyashi_senpaimoyashi_senpai
提出日時 2016-07-15 23:18:53
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 28 ms / 2,000 ms
コード長 2,012 bytes
コンパイル時間 556 ms
コンパイル使用メモリ 74,772 KB
実行使用メモリ 24,348 KB
平均クエリ数 102.22
最終ジャッジ日時 2023-09-24 00:17:00
合計ジャッジ時間 2,242 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 28 ms
23,520 KB
testcase_01 AC 23 ms
24,348 KB
testcase_02 AC 24 ms
24,024 KB
testcase_03 AC 23 ms
24,048 KB
testcase_04 AC 24 ms
23,388 KB
testcase_05 AC 23 ms
23,376 KB
testcase_06 AC 24 ms
23,508 KB
testcase_07 AC 23 ms
23,364 KB
testcase_08 AC 23 ms
23,520 KB
testcase_09 AC 25 ms
23,352 KB
testcase_10 AC 24 ms
24,300 KB
testcase_11 AC 24 ms
24,024 KB
testcase_12 AC 25 ms
24,324 KB
testcase_13 AC 24 ms
23,628 KB
testcase_14 AC 24 ms
23,640 KB
testcase_15 AC 25 ms
23,352 KB
testcase_16 AC 24 ms
23,640 KB
testcase_17 AC 24 ms
24,336 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <cstdio>
#include <vector>
#include <cmath>
#include <cstring>
#include <numeric>
#include <algorithm>
#include <functional>
#include <array>
#include <map>
using namespace std;

#define Getsign(n) ((n > 0) - (n < 0))

typedef vector<int> Ivec;
typedef pair<int, int> Pos;
vector <pair<int, int>> history;
long long int tesu = 0;
/* 配列の要素を交換する */
void Swap(int x [], int i, int j)
{
	int temp;

	temp = x[i];
	x[i] = x[j];
	x[j] = temp;

	history.push_back({i,j});
	tesu++;
}
/* クイックソートを行う */
void QSort(int x [], int left, int right)
{
	int i, j;
	int pivot;

	i = left;                      /* ソートする配列の一番小さい要素の添字 */
	j = right;                     /* ソートする配列の一番大きい要素の添字 */

	pivot = x[(left + right) / 2]; /* 基準値を配列の中央付近にとる */

	while (1) {                    /* 無限ループ */

		while (x[i] < pivot)       /* pivot より大きい値が */
			i++;                   /* 出るまで i を増加させる */

		while (pivot < x[j])       /* pivot より小さい値が */
			j--;                   /*  出るまで j を減少させる */
		if (i >= j)                /* i >= j なら */
			break;                 /* 無限ループから抜ける */

		Swap(x, i, j);             /* x[i] と x[j]を交換 */
		i++;                       /* 次のデータ */
		j--;
	}
	if (left < i - 1)              /* 基準値の左に 2 以上要素があれば */
		QSort(x, left, i - 1);     /* 左の配列を Q ソートする */
	if (j + 1 <  right)            /* 基準値の右に 2 以上要素があれば */
		QSort(x, j + 1, right);    /* 右の配列を Q ソートする */
}


int main() {
	int n, a[100];
	

	scanf("%d", &n);
	for (int i = 0; n > i; i++) {
		scanf("%d", &a[i]);
	}
	QSort(a, 0, n-1);
	printf("%d\n", tesu);
	for (int i = 0; tesu > i; i++) {
		printf("%d %d\n", history[i].first, history[i].second);
	}
	return 0;
}
0