結果

問題 No.4 おもりと天秤
ユーザー ramia777ramia777
提出日時 2022-05-19 15:45:50
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,200 bytes
コンパイル時間 751 ms
コンパイル使用メモリ 90,908 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-18 22:47:19
合計ジャッジ時間 1,951 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 2 ms
4,348 KB
testcase_10 WA -
testcase_11 AC 2 ms
4,348 KB
testcase_12 AC 2 ms
4,348 KB
testcase_13 AC 2 ms
4,348 KB
testcase_14 AC 2 ms
4,348 KB
testcase_15 AC 2 ms
4,348 KB
testcase_16 WA -
testcase_17 AC 2 ms
4,348 KB
testcase_18 AC 2 ms
4,348 KB
testcase_19 AC 2 ms
4,348 KB
testcase_20 AC 1 ms
4,348 KB
testcase_21 AC 2 ms
4,348 KB
testcase_22 AC 2 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

//Yukicoder No.4
//重りと天秤


//#include "stdafx.h"
#include <stdio.h>
#include <iostream>
#include <vector>
#include <list>//list
#include <set> //tree
#include <map> //連想配列
#include <unordered_set> //hash
#include <unordered_map> //hash
#include <algorithm>
#include <iomanip>
#include <string>
#include <stdlib.h>


using namespace std;
typedef unsigned long long ULL;
typedef signed long long SLL;
typedef unsigned int UINT;


int memo[2][100];
int N;
int W[100];


int possible = 0;
int _target_weight = 0;

//
// 半分の重さになるおもりの組み合わせがあるかを調べる
//
// n:おもりのインデックス
// total_weight:載せた合計の重さ
// flag :載せるとき1 載せないとき0
int func(int n, int total_weight, int flag)
{
	//cout << "n=" << n << ", total_weight = " << total_weight << endl;

	if (possible)
		return total_weight;

	if (n >= N)
		return total_weight;

	if (_target_weight < total_weight)
	{
		return total_weight;
	}

	if (_target_weight == total_weight)
	{
		possible = 1;
		return total_weight;
	}

	if (memo[flag][n])
		return memo[flag][n];

	memo[1][n] = func(n + 1, total_weight + W[n],1); //先に  n番目を載せた場合を調べる(探索枝を短くするため)
	memo[0][n] = func(n + 1, total_weight,0);        //後から n番目を載せなかった場合を調べる

	return total_weight;
}

//降順
int compare(const int * a, const int *b)
{
	if (*a < *b)
		return (1);
	else if (*a > *b)
		return (-1);
	return (0);
}

int main()
{
	int total = 0;

	cin >> N;

	for (int i=0;i<N;i++)
	{
		cin >> W[i];
		total += W[i];
	}

	
	if (total % 2)
	{
		//そもそも2分割できない合計だったらすぐさまimpossibleを返す
		cout << "impossible" << endl;
		return 0;
	}


	// 半分に割る
	_target_weight = total >> 1;

	//降順のソート
	qsort(W, N, sizeof(int), (int(*)(const void*, const void*))compare);


	//半分の重さになるかを深さ(再帰)探索。重たいものから載せる(探索枝を短くするため)
	func(0,0,1);

	
	if (possible == 1)
	{
		cout << "possible" << endl;
	}
	else
	{
		cout << "impossible" << endl;
	}

	//getchar();


	return 0;
}

0