結果

問題 No.3114 0→1
ユーザー Sillpherth
提出日時 2025-04-20 17:46:18
言語 C
(gcc 13.3.0)
結果
RE  
実行時間 -
コード長 1,078 bytes
コンパイル時間 863 ms
コンパイル使用メモリ 26,368 KB
実行使用メモリ 7,848 KB
最終ジャッジ日時 2025-04-20 17:46:24
合計ジャッジ時間 6,022 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other RE * 30
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.c: In function ‘main’:
main.c:51:5: warning: ignoring return value of ‘scanf’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   51 |     scanf("%d", &N);
      |     ^~~~~~~~~~~~~~~
main.c:52:5: warning: ignoring return value of ‘scanf’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   52 |     scanf("%s", s);
      |     ^~~~~~~~~~~~~~

ソースコード

diff #

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

void toBitarray(const char* s, bool* S, int k, int fullN) {
    for (int i = 0; i < k - 1; i++) {
        S[i] = true;
    }

    for (int i = k - 1; i < fullN; i++) {
        S[i] = (s[i - k + 1] == '1');
    }
}

int minFind(const char* s, int N, int n) {
    int weightMin = (n + 1) / 2;
    int fullN = N + weightMin - 1;
    int change = 0;
    int HWeight = 0;
    bool S[fullN];

    toBitarray(s, S, weightMin, fullN);

    for (int i = 0; i < n; i++) {
        HWeight += S[i] ? 1 : 0;
    }

    if (HWeight < weightMin) {
        S[n - 1] = true;
        HWeight++;
        change++;
    }

    for (int i = 1; i < fullN - n + 1; i++) {
        HWeight -= S[i - 1] ? 1 : 0;
        HWeight += S[i + n - 1] ? 1 : 0;
        if (HWeight < weightMin) {
            S[i + n - 1] = true;
            HWeight++;
            change++;
        }
    }

    return change;
}

int main() {
    int N;
    char s[1001];

    scanf("%d", &N);
    scanf("%s", s);

    printf("%d\n", minFind(s, N, 3));

    return 0;
}
0