@@ -1122,27 +1122,46 @@ public function addAction(Request $request): Response
11221122
11231123 /**
11241124 * @param Testcase[] $testcases
1125+ *
1126+ * Assumes testcases are in order of their rank.
11251127 */
11261128 private function addTestcasesToZip (array $ testcases , ZipArchive $ zip , bool $ isSample ): void
11271129 {
1130+
1131+ // Verify whether order of original filenames matches order of testcases by rank.
1132+ // If so, prefer their original name, otherwise replace the name with the rank to ensure same order.
1133+ $ prev = null ;
1134+ $ isStillSorted = true ;
1135+ foreach ($ testcases as $ testcase ) {
1136+ if ($ prev !== null && $ prev >= $ testcase ->getOrigInputFilename ()) {
1137+ $ isStillSorted = false ;
1138+ break ;
1139+ }
1140+ $ prev = $ testcase ->getOrigInputFilename ();
1141+ }
1142+
11281143 $ formatString = sprintf ('data/%%s/%%0%dd ' , ceil (log10 (count ($ testcases ) + 1 )));
11291144 $ rankInGroup = 0 ;
11301145 foreach ($ testcases as $ testcase ) {
11311146 $ rankInGroup ++;
1132- $ filename = sprintf ($ formatString , $ isSample ? 'sample ' : 'secret ' , $ rankInGroup );
1133- $ zip ->addFromString ($ filename . '.in ' , $ testcase ->getContent ()->getInput ());
1134- $ zip ->addFromString ($ filename . '.ans ' , $ testcase ->getContent ()->getOutput ());
1147+ if ($ isStillSorted ) {
1148+ $ filenamePrefix = sprintf ("data/%s/%s " , $ isSample ? 'sample ' : 'secret ' , $ testcase ->getOrigInputFilename ());
1149+ } else {
1150+ $ filenamePrefix = sprintf ($ formatString , $ isSample ? 'sample ' : 'secret ' , $ rankInGroup );
1151+ }
1152+ $ zip ->addFromString ($ filenamePrefix . '.in ' , $ testcase ->getContent ()->getInput ());
1153+ $ zip ->addFromString ($ filenamePrefix . '.ans ' , $ testcase ->getContent ()->getOutput ());
11351154
11361155 if (!empty ($ testcase ->getDescription (true ))) {
11371156 $ description = $ testcase ->getDescription (true );
11381157 if (!str_contains ($ description , "\n" )) {
11391158 $ description .= "\n" ;
11401159 }
1141- $ zip ->addFromString ($ filename . '.desc ' , $ description );
1160+ $ zip ->addFromString ($ filenamePrefix . '.desc ' , $ description );
11421161 }
11431162
11441163 if (!empty ($ testcase ->getImageType ())) {
1145- $ zip ->addFromString ($ filename . '. ' . $ testcase ->getImageType (),
1164+ $ zip ->addFromString ($ filenamePrefix . '. ' . $ testcase ->getImageType (),
11461165 $ testcase ->getContent ()->getImage ());
11471166 }
11481167 }
0 commit comments