結果

問題 No.332 数列をプレゼントに
ユーザー koyumeishikoyumeishi
提出日時 2015-12-25 16:11:16
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 245 ms / 2,000 ms
コード長 2,301 bytes
コンパイル時間 998 ms
コンパイル使用メモリ 106,292 KB
実行使用メモリ 12,320 KB
最終ジャッジ日時 2023-08-25 21:16:13
合計ジャッジ時間 7,475 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 34 ms
11,284 KB
testcase_06 AC 35 ms
11,260 KB
testcase_07 AC 138 ms
11,272 KB
testcase_08 AC 36 ms
11,276 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 37 ms
11,448 KB
testcase_11 AC 41 ms
11,448 KB
testcase_12 AC 38 ms
11,536 KB
testcase_13 AC 38 ms
11,216 KB
testcase_14 AC 38 ms
11,276 KB
testcase_15 AC 51 ms
11,360 KB
testcase_16 AC 46 ms
11,212 KB
testcase_17 AC 38 ms
11,388 KB
testcase_18 AC 45 ms
12,320 KB
testcase_19 AC 36 ms
11,236 KB
testcase_20 AC 40 ms
11,652 KB
testcase_21 AC 39 ms
11,220 KB
testcase_22 AC 37 ms
11,584 KB
testcase_23 AC 118 ms
11,332 KB
testcase_24 AC 234 ms
11,332 KB
testcase_25 AC 81 ms
11,260 KB
testcase_26 AC 168 ms
11,492 KB
testcase_27 AC 39 ms
11,340 KB
testcase_28 AC 36 ms
11,332 KB
testcase_29 AC 66 ms
11,420 KB
testcase_30 AC 115 ms
11,236 KB
testcase_31 AC 39 ms
11,332 KB
testcase_32 AC 49 ms
11,284 KB
testcase_33 AC 112 ms
11,268 KB
testcase_34 AC 234 ms
11,312 KB
testcase_35 AC 241 ms
11,312 KB
testcase_36 AC 1 ms
4,376 KB
testcase_37 AC 232 ms
10,140 KB
testcase_38 AC 233 ms
11,216 KB
testcase_39 AC 232 ms
11,332 KB
testcase_40 AC 238 ms
11,332 KB
testcase_41 AC 39 ms
11,332 KB
testcase_42 AC 226 ms
11,332 KB
testcase_43 AC 208 ms
11,288 KB
testcase_44 AC 46 ms
11,368 KB
testcase_45 AC 245 ms
11,764 KB
testcase_46 AC 230 ms
11,288 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <cstdio>
#include <sstream>
#include <map>
#include <string>
#include <algorithm>
#include <queue>
#include <cmath>
#include <functional>
#include <set>
#include <ctime>
#include <random>
using namespace std;
template<class T> istream& operator >> (istream& is, vector<T>& vec){for(T& val: vec) is >> val; return is;}
template<class T> istream& operator , (istream& is, T& val){ return is >> val;}
template<class T> ostream& operator << (ostream& os, vector<T>& vec){for(int i=0; i<vec.size(); i++) os << vec[i] << (i==vec.size()-1?"\n":" ");return os;}

#include <cassert>

int main(){
	long long n,x;
	cin >> n,x;
	vector<long long> a(n);
	cin >> a;

	deque<pair<__int128,__int128>> b(n);
	long long sum = 0;
	for(int i=0; i<n; i++){
		b[i] = {a[i], i};
		sum += a[i];
	}
	sort(b.begin(), b.end());

	int all_size = 20;

	deque<pair<__int128,__int128>> c;
	while(b.size() && c.size()<all_size){
		sum -= b.back().first;
		auto tmp = b.back(); b.pop_back();
		c.push_front(tmp);
	}

	vector<long long> dp1((1<<c.size()), -1);
	/*
	for(int i=0; i<dp1.size(); i++){
		long long tmp = 0;
		for(__int128 j=0; j<c.size(); j++){
			if((i>>j)&1) tmp += c[j].first;
		}
		dp1[i] = tmp;
	}
	*/
	dp1[0] = 0;
	function<void(int,int,long long)> dfs = [&](int s, int k, long long val){
		if(k==c.size()){
			dp1[s] = val;
			return;
		}
		dfs(s,k+1,val);
		dfs(s|(1<<k),k+1,val+c[k].first);
	};
	dfs(0,0,0);

	vector<__int128> dp2(sum+1, 0);
	
	for(int i=0; i<b.size(); i++){
		for(__int128 j=sum-b[i].first; j>=0; j--){
			if(dp2[j] == 0 && j != 0) continue;
			dp2[j+b[i].first] = dp2[j] | (__int128(1)<<(__int128)i);
		}
	}
	


	for(int i=0; i<dp1.size(); i++){
		long long rem = x - dp1[i];
		if(rem < 0) continue;

		long long tmp = 0;
		string ans(n, 'x');
		for(__int128 j=0; j<c.size(); j++){
			if((i>>j)&1){
				ans[c[j].second] = 'o';
				tmp += c[j].first;
			}
		}

		if(rem == 0){
			assert(tmp == x);
			cout << ans << endl;
			return 0;
		}
		if(rem <= sum && dp2[rem] != 0){
			assert(rem + dp1[i] == x);

			for(__int128 j=0; j<b.size(); j++){

				if((dp2[rem]>>j)&(__int128)1){
					ans[b[j].second] = 'o';
					tmp += b[j].first;
				}
			}
			//assert(tmp == x);

			cout << ans << endl;
			return 0;
		}
	}
	cout << "No" << endl;

	return 0;
}
0