結果

問題 No.4 おもりと天秤
ユーザー 4885rhkA4885rhkA
提出日時 2015-07-10 08:08:56
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 56 ms / 5,000 ms
コード長 1,719 bytes
コンパイル時間 389 ms
コンパイル使用メモリ 52,188 KB
実行使用メモリ 101,208 KB
最終ジャッジ日時 2023-09-08 16:20:55
合計ジャッジ時間 2,458 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 55 ms
100,992 KB
testcase_01 AC 35 ms
101,208 KB
testcase_02 AC 35 ms
101,068 KB
testcase_03 AC 54 ms
100,984 KB
testcase_04 AC 54 ms
101,040 KB
testcase_05 AC 56 ms
101,048 KB
testcase_06 AC 35 ms
100,992 KB
testcase_07 AC 56 ms
100,996 KB
testcase_08 AC 35 ms
101,108 KB
testcase_09 AC 55 ms
101,044 KB
testcase_10 AC 56 ms
100,996 KB
testcase_11 AC 35 ms
101,060 KB
testcase_12 AC 35 ms
101,052 KB
testcase_13 AC 35 ms
100,984 KB
testcase_14 AC 34 ms
101,116 KB
testcase_15 AC 35 ms
100,992 KB
testcase_16 AC 54 ms
101,040 KB
testcase_17 AC 54 ms
101,032 KB
testcase_18 AC 38 ms
101,000 KB
testcase_19 AC 56 ms
101,112 KB
testcase_20 AC 56 ms
100,988 KB
testcase_21 AC 56 ms
101,108 KB
testcase_22 AC 56 ms
101,000 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

//
//  main.cpp
//  Q19
//
//  Created by AkihiroKOBAYASHI on 7/6/15.
//  Copyright (c) 2015 Akhr5884. All rights reserved.
//

#include <iostream>

const int MAX_N = 5000; // n
const int MAX_W = 5000; // w
int cache[MAX_N + 1][MAX_W + 1];

int *w;
int sum;
int n;
int check;


void judge(int nowWeight, int nowNumber) {
    if(check != 1 && nowNumber < n) {
        if(cache[nowWeight][nowNumber] == -1) {
            if(nowWeight == sum-nowWeight) {
                check = 1;
                for (int i = 0; i < MAX_N + 1; i++) {
                    for (int j = 0; j < MAX_W + 1; j++) {
                        cache[i][j] = 1;
                    }
                }
                return;
            }
            else if(nowWeight < sum-nowWeight){
                
                // add
                judge(nowWeight, nowNumber + 1);
                
                // not add
                judge(nowWeight + w[nowNumber], nowNumber + 1);
            }
        }
        cache[nowWeight][nowNumber] = 0;
        return;
    }
    return;

}

int main(int argc, const char * argv[]) {

//    memset(cache, -1, sizeof(cache));
    for (int i = 0; i < MAX_N + 1; i++) {
        for (int j = 0; j < MAX_W + 1; j++) {
            cache[i][j] = -1;
        }
    }
    
    std::cin >> n;
    w = (int*)malloc(sizeof(w) * n);
    
    int count = 0;
    sum = 0;
    while (count < n) {
        int wi;
        std::cin >> wi;
        w[count] = wi;
        sum += wi;
        count++;
    }
    
    check = 0;
    if(sum%2 == 0) {
        judge(0, 0);
    }
    
    if(check == 1) {
        std::cout << "possible" << "\n";
    }
    else {
        std::cout << "impossible" << "\n";
    }

    return 0;
}
0