Submission #2180069


Source Code Expand

<?php
fscanf(STDIN, "%d %d", $n, $k);
fscanf(STDIN, "%s", $s);

$chars = [];
for ($i = 0; $i < $n; $i++) {
	$chars["" . $i] = $s[$i];
}
if ($k < 2) {
	echo $s, "\n";
	return;
}

$before = $chars;
sort($chars);

$score = diff($before, $chars);
if ($score <= $k) {
	echo implode($chars), "\n";
	return;
}

while ($score > $k) {
	//echo implode($chars), "\n";
	$memo = [];
	for ($i = $n; $i > 0; $i--) {
		if ($before[$i] === $chars[$i]) {
			continue;
		}
		for ($j = $i - 1; $j > 0; $j--) {
			if ($chars[$j] === $chars[$i]) {
				continue;
			}
			if ($before[$i] === $chars[$j] || $before[$j] === $chars[$i]) {
				if (!isset($memo[$j])) {
					$memo[$j] = [$i, $j];
				}
				break;
			}
		}
	}
	krsort($memo);
	$first = array_shift($memo);
	//echo "Score:" . $score, "\n";
	//echo json_encode($first), "\n";
	$i = $first[0];
	$j = $first[1];

	if ($before[$i] === $chars[$j] && $before[$j] === $chars[$i]) {
		$score--;
	}
	$score--;
	$temp = $chars[$i];
	$chars[$i] = $chars[$j];
	$chars[$j] = $temp;
}
//echo implode($chars), "\n";
$chars = siage($before, $chars);
echo implode($chars), "\n";

function diff($a, $b)
{
	$l = count($a);
	$score = 0;
	for ($i = 0; $i < $l; $i++) {
		if ($a[$i] !== $b[$i]) {
			$score++;
		}
	}
	return $score;
}

function siage($before, $after)
{
	$l = count($before);
	$memo = [];
	for ($i = 0; $i < $l; $i++) {
		if ($before[$i] !== $after[$i]) {
			$memo[$i] = $after[$i];
		}
	}
	$keys = array_keys($memo);
	asort($memo);
	foreach ($keys as $key) {
		$after[$key] = array_shift($memo);
	}
	//var_dump($memo);
	return $after;
}

Submission Info

Submission Time
Task C - 辞書式順序ふたたび
User katand
Language PHP7 (7.0.15)
Score 0
Code Size 1655 Byte
Status WA
Exec Time 23 ms
Memory 4728 KB

Judge Result

Set Name All
Score / Max Score 0 / 100
Status
AC × 33
WA × 25
Set Name Test Cases
All hand_1_0.txt, hand_1_1.txt, hand_1_2.txt, hand_1_3.txt, hand_1_4.txt, hand_1_5.txt, hand_1_6.txt, hand_1_7.txt, hand_1_8.txt, hand_2_0.txt, hand_2_1.txt, hand_2_10.txt, hand_2_2.txt, hand_2_3.txt, hand_2_4.txt, hand_2_5.txt, hand_2_6.txt, hand_2_7.txt, hand_2_8.txt, hand_2_9.txt, hand_3_2.txt, hand_3_3.txt, hand_3_4.txt, hand_3_5.txt, hand_3_6.txt, hand_4_2.txt, hand_4_3.txt, hand_4_4.txt, hand_4_5.txt, hand_4_6.txt, random_1.txt, random_10.txt, random_11.txt, random_12.txt, random_13.txt, random_14.txt, random_15.txt, random_2.txt, random_3.txt, random_4.txt, random_5.txt, random_6.txt, random_7.txt, random_8.txt, random_9.txt, sample_1.txt, sample_2.txt, sample_3.txt, sample_4.txt, small_1.txt, small_2.txt, small_3.txt, small_4.txt, small_5.txt, small_6.txt, small_7.txt, small_8.txt, small_9.txt
Case Name Status Exec Time Memory
hand_1_0.txt AC 10 ms 4604 KB
hand_1_1.txt AC 9 ms 4604 KB
hand_1_2.txt WA 11 ms 4728 KB
hand_1_3.txt WA 9 ms 4604 KB
hand_1_4.txt AC 9 ms 4604 KB
hand_1_5.txt AC 9 ms 4604 KB
hand_1_6.txt AC 9 ms 4604 KB
hand_1_7.txt AC 9 ms 4604 KB
hand_1_8.txt AC 9 ms 4604 KB
hand_2_0.txt AC 9 ms 4604 KB
hand_2_1.txt AC 9 ms 4604 KB
hand_2_10.txt AC 9 ms 4604 KB
hand_2_2.txt WA 9 ms 4604 KB
hand_2_3.txt WA 9 ms 4604 KB
hand_2_4.txt AC 9 ms 4604 KB
hand_2_5.txt AC 9 ms 4604 KB
hand_2_6.txt WA 9 ms 4604 KB
hand_2_7.txt WA 9 ms 4604 KB
hand_2_8.txt AC 9 ms 4604 KB
hand_2_9.txt AC 9 ms 4604 KB
hand_3_2.txt WA 9 ms 4604 KB
hand_3_3.txt WA 9 ms 4604 KB
hand_3_4.txt WA 9 ms 4604 KB
hand_3_5.txt WA 9 ms 4604 KB
hand_3_6.txt AC 9 ms 4604 KB
hand_4_2.txt WA 9 ms 4604 KB
hand_4_3.txt WA 9 ms 4604 KB
hand_4_4.txt WA 9 ms 4604 KB
hand_4_5.txt WA 9 ms 4604 KB
hand_4_6.txt WA 9 ms 4604 KB
random_1.txt WA 9 ms 4604 KB
random_10.txt WA 21 ms 4604 KB
random_11.txt WA 20 ms 4604 KB
random_12.txt WA 23 ms 4604 KB
random_13.txt WA 19 ms 4604 KB
random_14.txt WA 13 ms 4604 KB
random_15.txt AC 9 ms 4604 KB
random_2.txt AC 10 ms 4604 KB
random_3.txt WA 10 ms 4604 KB
random_4.txt WA 12 ms 4604 KB
random_5.txt AC 9 ms 4604 KB
random_6.txt WA 14 ms 4604 KB
random_7.txt AC 19 ms 4604 KB
random_8.txt AC 9 ms 4604 KB
random_9.txt WA 23 ms 4604 KB
sample_1.txt AC 9 ms 4604 KB
sample_2.txt AC 9 ms 4604 KB
sample_3.txt AC 9 ms 4604 KB
sample_4.txt AC 9 ms 4604 KB
small_1.txt AC 9 ms 4604 KB
small_2.txt AC 9 ms 4604 KB
small_3.txt AC 9 ms 4604 KB
small_4.txt AC 9 ms 4604 KB
small_5.txt AC 9 ms 4604 KB
small_6.txt AC 9 ms 4604 KB
small_7.txt AC 9 ms 4604 KB
small_8.txt AC 9 ms 4604 KB
small_9.txt AC 9 ms 4604 KB