結果

問題 No.628 Tagの勢い
ユーザー ElkElk
提出日時 2018-06-07 00:18:19
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 10 ms / 2,000 ms
コード長 3,597 bytes
コンパイル時間 287 ms
コンパイル使用メモリ 36,604 KB
実行使用メモリ 8,064 KB
最終ジャッジ日時 2024-04-16 20:15:17
合計ジャッジ時間 965 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 2 ms
5,376 KB
testcase_12 AC 9 ms
8,064 KB
testcase_13 AC 9 ms
7,936 KB
testcase_14 AC 10 ms
7,936 KB
testcase_15 AC 9 ms
7,936 KB
testcase_16 AC 6 ms
7,936 KB
testcase_17 AC 2 ms
5,376 KB
testcase_18 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

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

typedef struct{
    int point;
    char name[21];
}TAG;

typedef struct{
    int id;
    int tag_cnt;
    char tag_name[1000][21];
    int point;
}picture;

int main(){
    int N, i, j, k, l = 0;
    int tag_p[1000] = {0};
    char tag[1000][21] = {'\0'};
    int cnt = 0, flag = 0, flag3 = 0, pre_flag3 = 0;
    picture pic[1000], *p;
    TAG a[1000], b[1000], tmp, *p1;
    int flag2[1000] = {0};

    scanf("%d\n", &N);
    for(i = 0; i < N; i++){
        p = &pic[i];
        scanf("%d %d %d ", &p->id, &p->tag_cnt, &p->point);
        for(j = 0; j < p->tag_cnt; j++){
            scanf("%s ", p->tag_name[j]);
        }
        if(i == 0){
            for(j = 0; j < p->tag_cnt; j++){
                strcpy(tag[j], p->tag_name[j]);
                tag_p[j] += p->point;
                cnt++;
            }
        }else{
            for(j = 0; j < p->tag_cnt; j++){
                for(k = 0; k < cnt; k++){
                    if(strcmp(p->tag_name[j], tag[k]) != 0){
                        flag++;
                    }else{
                        tag_p[k] += p->point;
                    }
                }
                if(flag == cnt){
                    strcpy(tag[cnt++], p->tag_name[j]);
                    tag_p[cnt - 1] += p->point;
                }
                flag = 0;
            }
        }
        //printf("cnt %d\n", cnt);
    }

    for(i = 0; i < cnt; i++){
        p1 = &a[i];
        p1->point = tag_p[i];
        strcpy(p1->name, tag[i]);
    }
    qsort(tag_p, cnt, sizeof(int), func);
    for(i = 0; i < cnt;i++){
        for(j = 0; j < cnt; j++){
            if(flag2[j] == 0 && tag_p[i] == a[j].point){
                b[i] = a[j];
                //printf("i: %d %d\n", i, b[i].point);
                flag2[j] = 1;
                break;
            }
        }
        if(i > 0){
            if(b[i - 1].point == b[i].point){
                pre_flag3 = flag3;
                flag3++;
                //printf("i: %d flag3 %d %d %d\n", i, flag3, b[i - 1].point, b[i].point);
            }else if(flag3 > 0 && b[i - 1].point != b[i].point){
                pre_flag3++;
                //printf("ok2 flag3 %d pre_flag3 %d\n", flag3, pre_flag3);
            }
            if(flag3 > 0 && flag3 - pre_flag3 == 0){
                for(k = i - flag3; k < i; k++){
                    for(l = i - flag3; l < i; l++){
                        //printf("ok flag3 %d\n", flag3);
                        if(strcmp(b[l - 1].name, b[l].name) > 0){
                            tmp = b[l - 1];
                            b[l - 1] = b[l];
                            b[l] = tmp;
                        }
                    }
                }
                flag3 = 0;
                pre_flag3 = 0;
            }else if(flag3 > 0 && i == cnt - 1){
                for(k = i - flag3 + 1; k < cnt; k++){
                    for(l = i - flag3 + 1; l < cnt; l++){
                        //printf("i %d flg3 %d %s %s %s\n", i, flag3, b[i - flag3].name, b[i - flag3 + 1].name, b[i - flag3 + 2].name);
                        if(strcmp(b[l - 1].name, b[l].name) > 0){
                            tmp = b[l - 1];
                            b[l - 1] = b[l];
                            b[l] = tmp;
                        }
                    }
                }
            }
        }
    }
    if(cnt > 10)    cnt = 10;
    for(i = 0; i < cnt; i++){
        printf("%s %d\n", b[i].name, b[i].point);
    }
    return 0;

}
0