結果

問題 No.365 ジェンガソート
ユーザー mannshi222mannshi222
提出日時 2019-07-13 20:07:00
言語 C
(gcc 12.3.0)
結果
WA  
実行時間 -
コード長 2,630 bytes
コンパイル時間 222 ms
コンパイル使用メモリ 30,464 KB
実行使用メモリ 10,624 KB
最終ジャッジ日時 2024-05-02 02:07:35
合計ジャッジ時間 4,788 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
10,624 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 WA -
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 WA -
testcase_09 AC 1 ms
5,376 KB
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 AC 1 ms
5,376 KB
testcase_15 AC 1 ms
5,376 KB
testcase_16 TLE -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>
#include <stdlib.h>

#define ENDD '\0'
#define KIGY '\n'

int compare_int(const void *a, const void *b);

int compare_int(const void *a, const void *b)
{
    return *(int*)a - *(int*)b;
}


int main()
{
        int ch;
        char buff[100000+1];
        int buffi;
        int N;
        int *jen;
        int *jennext;
        int jeni;

        buffi = 0;
        while( (ch=getc(stdin)) != KIGY) {
                buff[buffi++] = ch;
        }
        buff[buffi] = ENDD;
        N = atoi(buff);

        buffi=0;
        jeni=0;
        jen = (int *)malloc((sizeof(int))*N);
        while( (ch=getc(stdin)) != EOF) {
                        if( ch==' ' ||  ch==KIGY   ) {
                                buff[buffi] = ENDD;
                                jen[jeni++] = atoi(buff);
                                buffi = 0;
                        }
                        else {
                                buff[buffi++] = ch;
                        }
        }

        /* search max */
        int smax;
        int count  = 0;
        for( smax = N; smax >= 1; smax-- ) {
                /* 一番数字が大きいものを探す */

                /* 探す対象が1の場合、ソートの必要がないので終わり */
                if( smax==1 ) {
                        break;
                }

                /* 一番数字が大きいものを探す */
                jeni = smax-1;
                while( jen[jeni] != smax ){
                        jeni--;
                }

                /* 一番右側に一番大きい数字がある場合 */
                if( jeni == smax-1 ) {
                        continue;
                }

                /* 一番右側にない場合 */
                count += smax-jeni-1;
                /* 次に大きい数字を探す */

                /* 一番大きいものより右側にある数字をソートする */
                qsort(jen+jeni+1, smax-jeni-1, sizeof(int), compare_int);

                /* ソート後の結果を新しい配列左側に移動する */
                jennext = (int *)malloc((sizeof(int))*smax-1);
                int i;
                for( i = 0; i < smax-jeni-1; i++ ) {
                        jennext[i] =jen[jeni+1+i];
                }
                /* 一番大きい数字より左がにあったものを移動する */
                for( int j = 0; j <= jeni-1; j++ ) {
                        jennext[i++] = jen[j];
                }

                free( jen );
                jen = jennext;

        }
        printf("%d\n", count );

        return 0;
}
0