結果

問題 No.332 数列をプレゼントに
ユーザー rickythetarickytheta
提出日時 2015-12-25 20:21:36
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 490 ms / 2,000 ms
コード長 2,229 bytes
コンパイル時間 1,541 ms
コンパイル使用メモリ 169,084 KB
実行使用メモリ 68,972 KB
最終ジャッジ日時 2023-08-25 21:16:53
合計ジャッジ時間 8,186 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 70 ms
4,380 KB
testcase_06 AC 75 ms
4,380 KB
testcase_07 AC 322 ms
27,248 KB
testcase_08 AC 70 ms
4,380 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 70 ms
4,376 KB
testcase_11 AC 70 ms
4,376 KB
testcase_12 AC 70 ms
4,380 KB
testcase_13 AC 71 ms
4,376 KB
testcase_14 AC 70 ms
4,380 KB
testcase_15 AC 98 ms
4,380 KB
testcase_16 AC 81 ms
4,376 KB
testcase_17 AC 71 ms
4,376 KB
testcase_18 AC 112 ms
4,536 KB
testcase_19 AC 70 ms
4,376 KB
testcase_20 AC 71 ms
4,376 KB
testcase_21 AC 98 ms
4,380 KB
testcase_22 AC 93 ms
4,380 KB
testcase_23 AC 226 ms
31,636 KB
testcase_24 AC 490 ms
68,972 KB
testcase_25 AC 71 ms
4,380 KB
testcase_26 AC 304 ms
46,200 KB
testcase_27 AC 70 ms
4,380 KB
testcase_28 AC 70 ms
4,376 KB
testcase_29 AC 74 ms
4,488 KB
testcase_30 AC 132 ms
17,064 KB
testcase_31 AC 70 ms
4,376 KB
testcase_32 AC 86 ms
7,732 KB
testcase_33 AC 83 ms
4,376 KB
testcase_34 AC 104 ms
4,376 KB
testcase_35 AC 106 ms
4,376 KB
testcase_36 AC 2 ms
4,376 KB
testcase_37 AC 86 ms
4,380 KB
testcase_38 AC 86 ms
4,380 KB
testcase_39 AC 85 ms
4,376 KB
testcase_40 AC 82 ms
4,380 KB
testcase_41 AC 70 ms
4,376 KB
testcase_42 AC 82 ms
4,380 KB
testcase_43 AC 103 ms
4,376 KB
testcase_44 AC 113 ms
4,376 KB
testcase_45 AC 99 ms
4,380 KB
testcase_46 AC 141 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

typedef unsigned long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef complex<double> P;
typedef pair<int,int> pii;
#define REP(i,n) for(ll i=0;i<n;++i)
#define REPR(i,n) for(ll i=1;i<n;++i)
#define FOR(i,a,b) for(ll i=a;i<b;++i)

#define DEBUG(x) cout<<#x<<": "<<x<<endl
#define DEBUG_VEC(v) cout<<#v<<":";REP(i,v.size())cout<<" "<<v[i];cout<<endl
#define ALL(a) (a).begin(),(a).end()

#define MOD (ll)(1e9+7)
#define ADD(a,b) a=((a)+(b))%MOD
#define FIX(a) ((a)%MOD+MOD)%MOD

int main(){
  ll n,x;
  cin>>n>>x;
  vl a(n);
  REP(i,n)cin>>a[i];

  // 先頭20個を別扱いにする解法

  vl b = a;
  sort(ALL(b),greater<ll>());
  vl c,d;
  ll dsz = min<int>(20,n);
  ll csz = n-dsz;
  REP(i,dsz)d.push_back(b[i]);
  FOR(i,dsz,n)c.push_back(b[i]);

  // ナップサック
  ll cmax = 0;
  REP(i,csz)cmax += c[i];
  cmax = min(cmax,x);
  vector< vector<bool> > dp(csz+1,vector<bool>(cmax+1,false));
  vector< vector<bool> > pred(csz+1,vector<bool>(cmax+1,false));
  dp[0][0] = true;
  REP(i,csz){
    ll cost = c[i];
    REP(p,cmax+1){
      if(!dp[i][p])continue;
      dp[i+1][p] = true;
      pred[i+1][p] = false;
      if(p+cost<=cmax){
        dp[i+1][p+cost] = true;
        pred[i+1][p+cost] = true;
      }
    }
  }

  // 大きい値の全探索
  map<ll,ll> XS;
  REP(mask,1<<dsz){
    ll s = 0;
    REP(i,dsz) if(mask>>i&1) s+=d[i];
    if(s>x)continue;
    XS[s] = mask;
  }

  // 合体
  ll ansid = -1;
  REP(i,cmax+1){
    if(dp[csz][i]&&XS.count(x-i)){
      ansid = i;
      break;
    }
  }

  if(ansid==-1){
    cout << "No" << endl;
    return 0;
  }

  // 復元
  multiset<ll> result; // ここに使う値をぶっこんでいく

  // dp,predから
  ll cst = ansid;
  REP(_i,csz){
    ll i = csz-_i;
    if(pred[i][cst]){
      result.insert(c[i-1]);
      cst -= c[i-1];
    }
  }
  assert(cst==0);

  // 全探索のmaskから
  ll mask = XS[x-ansid];
  REP(i,dsz){
    if(mask>>i&1)result.insert(d[i]);
  }

  // 出力
  multiset<ll>::iterator iter;
  REP(i,n){
    iter = result.find(a[i]);
    if(iter!=result.end()){
      cout<<"o";
      result.erase(iter);
    }else{
      cout<<"x";
    }
  }
  cout<<endl;

  return 0;
}
0