結果

問題 No.4 おもりと天秤
ユーザー mannshi222mannshi222
提出日時 2022-04-03 21:22:05
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,445 bytes
コンパイル時間 862 ms
コンパイル使用メモリ 86,008 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-15 20:15:17
合計ジャッジ時間 1,868 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

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

using namespace std;

void pall(stack<int> c, vector<int> w)
{
	int sum =  0;
	while( !c.empty() ) {
		sum += w[c.top()];
		cout << w[c.top()]<< " ";

		c.pop();
	}
	cout << endl << "sum=" << sum << endl;
}
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() );
	/*
	for( int i = 0; i < W.size(); i++ ) {
		cout << W[i] << " " ;
	}
	cout << endl;
	*/

	int half = 0;
	for( auto x: W ) {
		half += x;
	}
	if( half%2 == 1 )  {
		cout << "impossible" << endl;
		return 0;
	}

	half = half / 2;

	stack<int> s;

	int sum =0;
	int i = 0;
	int a = 0;
	int b = 0;
	while( 1 ) {
		if( i >= N ) {
			if( !s.empty() ) {
				i = s.top();
				s.pop();
			}
			if( i == N-1 ) { //
				// cout << __LINE__ << endl;
				cout << "impossible" << endl;
				return 0;
			}
			sum -= W[i];
			i++;
		} else if( half == sum + W[i] ) {
			cout << "possible" << endl;
			return 0;
		} else if( half >  sum + W[i] ) {
			sum += W[i];
			s.push( i );
			i++;
		} else if( half <  sum + W[i] ) {
			//pall( s, W ) ;
			// cout << "BIG " << i << " " << sum << " " <<W[i] << endl;
			if( s.empty() ) {
				cout << "impossible" << endl;
				return 0;
			}
			i = s.top();
			sum -= W[i];
			// assert( sum > 0 );
			i++;
			s.pop();
		} else { cout << "arienai" << endl; return 0; }

	}
	
	return 0;
}
0