結果

問題 No.4 おもりと天秤
ユーザー ramia777ramia777
提出日時 2022-05-20 22:01:19
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 5 ms / 5,000 ms
コード長 2,934 bytes
コンパイル時間 973 ms
コンパイル使用メモリ 91,348 KB
実行使用メモリ 8,392 KB
最終ジャッジ日時 2023-10-20 12:40:11
合計ジャッジ時間 2,437 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
8,388 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 4 ms
8,388 KB
testcase_03 AC 4 ms
8,388 KB
testcase_04 AC 3 ms
8,388 KB
testcase_05 AC 3 ms
8,388 KB
testcase_06 AC 3 ms
8,388 KB
testcase_07 AC 4 ms
8,392 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 4 ms
8,388 KB
testcase_10 AC 4 ms
8,392 KB
testcase_11 AC 4 ms
8,388 KB
testcase_12 AC 3 ms
8,388 KB
testcase_13 AC 4 ms
8,388 KB
testcase_14 AC 3 ms
8,388 KB
testcase_15 AC 4 ms
8,388 KB
testcase_16 AC 3 ms
8,388 KB
testcase_17 AC 3 ms
8,388 KB
testcase_18 AC 5 ms
8,392 KB
testcase_19 AC 3 ms
8,388 KB
testcase_20 AC 4 ms
8,392 KB
testcase_21 AC 4 ms
8,392 KB
testcase_22 AC 3 ms
8,388 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>
#include <cstring>

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


int memo[20000][200];
int N;
int W[100];

int _debug_flag = 0;

int possible = 0;
int _target_weight = 0;

//
// 半分の重さになるおもりの組み合わせがあるかを調べる
// target_weight : おもり
// n:おもりのインデックス


int  func(int n,  int target_weight)
{
	//cout << "n=" << n << ",target_weight=" << target_weight << endl;

	if (possible)
		return 1;

	if (n >= N)
		return 0;

	if (target_weight > _target_weight)
		return 0;

	if (target_weight == _target_weight)
	{
		possible = 1;
		//cout << "possible:n=" << n << ",target_weight=" << _target_weight << endl;
		return 1;
	}

	//同じ引数で関数を通るときは答えは同じなので前回の答えを返す
	if (-1 != memo[target_weight][n])
	{
		//cout << "memo:n=" << n << ",target_weight=" << target_weight << endl;
		//cout << "memo ret=" << memo[target_weight][n]<< endl;
		return memo[target_weight][n];
	}

	_debug_flag |= (1 << n);

	//最初は n番目のおもりを置いてみる
	if (func(n + 1, target_weight + W[n]))
	{
		//cout << "possible:n=" << n << ",target_weight=" << _target_weight << endl;

		memo[target_weight][n] = 1;
		return 1;
	}
	
	_debug_flag &= ~(1 << n);

	// 上手くいかなかったら n番目のおもりを置かない場合を試してみる
	if (func(n + 1, target_weight))
	{
		//cout << "possible:n=" << n << ",target_weight=" << _target_weight << endl;

		memo[target_weight][n] = 1;
		return 1 ;
	}

	

	//n番目の重りを置いてもおかなくてもだめだった
	memo[target_weight][n] = 0;

	return 0;
}

//降順
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;

	//cout << _target_weight << endl;

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

	memset(memo,-1, 20000*200);

	//半分の重さになるかを深さ(再帰)探索
	if(func(0, 0))
	{

		/*
		for (int i=0;i<N;i++)
		{
			if (_debug_flag & (1 << i))
				cout << W[i] << ",";
		}
		cout << endl;
		*/

		cout << "possible" << endl;
	}
	else
	{
		cout << "impossible" << endl;
	}

	//getchar();


	return 0;
}

0