結果

問題 No.1630 Sorting Integers (Greater than K)
ユーザー tailstails
提出日時 2021-07-31 08:05:33
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 3 ms / 2,000 ms
コード長 1,174 bytes
コンパイル時間 697 ms
コンパイル使用メモリ 32,256 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-09-16 09:07:57
合計ジャッジ時間 1,769 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 1 ms
5,376 KB
testcase_04 AC 1 ms
5,376 KB
testcase_05 AC 1 ms
5,376 KB
testcase_06 AC 1 ms
5,376 KB
testcase_07 AC 1 ms
5,376 KB
testcase_08 AC 1 ms
5,376 KB
testcase_09 AC 1 ms
5,376 KB
testcase_10 AC 1 ms
5,376 KB
testcase_11 AC 1 ms
5,376 KB
testcase_12 AC 1 ms
5,376 KB
testcase_13 AC 1 ms
5,376 KB
testcase_14 AC 1 ms
5,376 KB
testcase_15 AC 1 ms
5,376 KB
testcase_16 AC 3 ms
5,376 KB
testcase_17 AC 3 ms
5,376 KB
testcase_18 AC 2 ms
5,376 KB
testcase_19 AC 2 ms
5,376 KB
testcase_20 AC 1 ms
5,376 KB
testcase_21 AC 2 ms
5,376 KB
testcase_22 AC 1 ms
5,376 KB
testcase_23 AC 1 ms
5,376 KB
testcase_24 AC 3 ms
5,376 KB
testcase_25 AC 3 ms
5,376 KB
testcase_26 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.c:13:1: warning: return type defaults to 'int' [-Wimplicit-int]
   13 | main(){
      | ^~~~
main.c: In function 'main':
main.c:18:19: warning: initialization of 'char *' from 'long int' makes pointer from integer without a cast [-Wint-conversion]
   18 |         char*klen=rp-kp-1;
      |                   ^~
main.c:25:16: warning: comparison between pointer and integer
   25 |         if(klen>n){
      |                ^
main.c:26:17: warning: implicit declaration of function 'write' [-Wimplicit-function-declaration]
   26 |                 write(1,"-1",2);
      |                 ^~~~~
main.c:27:17: warning: implicit declaration of function '_exit'; did you mean '_Exit'? [-Wimplicit-function-declaration]
   27 |                 _exit(0);
      |                 ^~~~~
      |                 _Exit
main.c:29:16: warning: comparison between pointer and integer
   29 |         if(klen<n){
      |                ^
main.c:32:25: warning: implicit declaration of function 'memset' [-Wimplicit-function-declaration]
   32 |                         memset(wp,i+'0',c[i]);
      |                         ^~~~~~
main.c:1:1: note: include '<string.h>' or provide a declaration of 'memset'
  +++ |+#include <string.h>
    1 | #pragma GCC optimize("Ofast")
main.c:32:25: warning: incompatible implicit declaration of built-in function 'memset' [-Wbuiltin-declaration-mismatch]
   32 |                         memset(wp,i+'0',c[i]);
      |                         ^~~~~~
main.c:32:25: note: include '<string.h>' or provide a declaration of 'memset'
main.c:73:25: warning: incompatible implicit declaration of built-in function 'memset' [-Wbuiltin-declaration-mismatch]
   73 |                         memset(wp,i+'0',c[i]);
      |                         ^~~~~~
main.c:73:25: note: include '<string.h>' or provide a declaration of 'memset'

ソースコード

diff #

#pragma GCC optimize("Ofast")
#pragma GCC target("avx2")

char*mmap();
char wbuf[1<<25];

#define rd_skip() while(*rp++>=48)
#define rd(v) long v=0;{int _c;while(_c=*rp++-48,_c>=0)v=v*10+_c;}
#define rep(v,e) for(long v=0;v<e;++v)
#define reps(v,s,e) for(long v=s;v<e;++v)
#define chmax(v,a) (v=v>=a?v:a)

main(){
	char*rp=mmap(0l,1l<<25,1,2,0,0ll);
	rd(n);
	char*kp=rp;
	rd_skip();
	char*klen=rp-kp-1;
	long c[10];
	c[0]=0;
	reps(i,1,10){
		rd(ci);
		c[i]=ci;
	}
	if(klen>n){
		write(1,"-1",2);
		_exit(0);
	}
	if(klen<n){
		char*wp=wbuf;
		reps(i,1,10){
			memset(wp,i+'0',c[i]);
			wp+=c[i];
		}
		write(1,wbuf,wp-wbuf);
		_exit(0);
	}
	{
		char*wp=wbuf;
		long d=0;
		while(d<n&&c[kp[d]-'0']){
			--c[kp[d]-'0'];
			*wp++=kp[d++];
		}
		long imax=0;
		reps(i,1,10){
			if(c[i]){
				imax=i;
			}
		}
		if(d==n){
			--d;
			++c[*--wp-'0'];
		}
		while(d>=0&&imax<=kp[d]-'0'){
			chmax(imax,kp[d]-'0');
			--d;
			++c[*--wp-'0'];
		}
		if(d<0){
			write(1,"-1",2);
			_exit(0);
		}
		{
			long i=kp[d]+1-'0';
			while(!c[i]){
				++i;
			}
			*wp++=i+'0';
			--c[i];
		}
		reps(i,1,10){
			memset(wp,i+'0',c[i]);
			wp+=c[i];
		}
		write(1,wbuf,wp-wbuf);
		_exit(0);
	}
}
0