結果

問題 No.4 おもりと天秤
ユーザー mannshi222mannshi222
提出日時 2022-03-29 21:20:41
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,016 bytes
コンパイル時間 912 ms
コンパイル使用メモリ 88,780 KB
実行使用メモリ 10,404 KB
最終ジャッジ日時 2024-04-26 06:53:05
合計ジャッジ時間 7,541 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <vector>
#include <stack>
#include <algorithm>

using namespace std;

int main()
{
	int N;
	cin >> N;

	vector<int> W(N);
	for(int i = 0 ; i < N; i++ ){
		cin  >> W[i];
	}
	sort( W.begin(), W.end() );
	int half = 0;
	for( auto x: W ) {
		half += x;
	}
	half = half / 2;

	// cout << "half " << half << endl;

	stack<int> s;

	int sum =0;
	int i = 0;
	while( 1 ) {
		if( i >= N ) {
			if( s.empty() ) {
				cout << "impossible" << endl;
				return 0;
			}
			i = s.top();
			sum -= W[i];
			i++;
			s.pop();
		}
		if( half == sum + W[i] ) {
			cout << "possible" << endl;
			while( s.empty() ) {
				cout << s.top() << endl;
				s.pop();
			}


			return 0;
		}
		else if( half >  sum + W[i] ) {
			sum += W[i];
			s.push( i );
			i++;
		}
		else if( half < sum + W[i] ) {
			if( s.empty() ) {
				cout << "impossible" << endl;
				return 0;
			}
			i = s.top();
			sum -= W[i];
			// cout << "W " << i << " " << W[i] << endl;
			s.pop();
			// i = s.top();
			i+=1;
		}
	}
	
	return 0;
}
0