結果

問題 No.45 回転寿司
ユーザー nanatutmc
提出日時 2019-10-30 01:01:41
言語 C11(gcc12 gnu拡張)
(gcc 12.4.0)
コンパイル:
gcc-12 -O2 -std=gnu11 -DONLINE_JUDGE -o a.out _filename_ -lm
実行:
./a.out
結果
AC  
実行時間 1 ms / 5,000 ms
コード長 1,220 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 115 ms
コンパイル使用メモリ 32,236 KB
実行使用メモリ 7,844 KB
最終ジャッジ日時 2026-03-08 16:21:26
合計ジャッジ時間 975 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 30
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.c: In function ‘main’:
main.c:31:10: warning: implicit declaration of function ‘takeSushi’ [-Wimplicit-function-declaration]
   31 |     s1 = takeSushi(0, 0, N, V);
      |          ^~~~~~~~~
main.c:9:5: warning: ignoring return value of ‘scanf’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
    9 |     scanf("%d", &N);
      |     ^~~~~~~~~~~~~~~
main.c:13:9: warning: ignoring return value of ‘scanf’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   13 |         scanf("%d", &(V[i]));
      |         ^~~~~~~~~~~~~~~~~~~~

ソースコード

diff #
raw source code

#include <stdio.h>

int scoreTable[1000];

int main() {
    int N;
    int V[1000];
    
    scanf("%d", &N);
    
    int i;
    for (int i=0; i<N; i++) {
        scanf("%d", &(V[i]));
        scoreTable[i] = 0;
    }
    
    if (N == 1) {
        printf("%d\n", V[0]);
        return 0;
    }
    
    if (N == 2) {
        if (V[0] > V[1])
            printf("%d\n", V[0]);
        else
            printf("%d\n", V[1]);
        return 0;
    }
    
    int s1, s2;
    s1 = takeSushi(0, 0, N, V);
    s2 = takeSushi(0, 1, N, V);
    if (s1 > s2)
        printf("%d\n", s1);
    else
        printf("%d\n", s2);
}

int takeSushi(int score, int position, int N, int V[]) {
    if (scoreTable[position] > 0)
        return score+scoreTable[position];

    int score1 = score + V[position];
    
    if (position == N-1 || position == N-2) {
        return score1;
    }
    
    if (position == N-3) {
        return score1 + V[N-1];
    }
    
    int s1, s2;
    s1 = takeSushi(score1, position+2, N, V);
    s2 = takeSushi(score1, position+3, N, V);
    
    if (s1 > s2) {
        scoreTable[position] = s1-score;
        return s1;
    } else {
        scoreTable[position] = s2-score;
        return s2;
    }
}
0