結果

問題 No.10 +か×か
ユーザー たこしたこし
提出日時 2015-09-12 20:30:13
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 846 ms / 5,000 ms
コード長 1,738 bytes
コンパイル時間 710 ms
コンパイル使用メモリ 95,020 KB
実行使用メモリ 384,744 KB
最終ジャッジ日時 2023-08-21 06:14:00
合計ジャッジ時間 4,871 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 122 ms
162,724 KB
testcase_01 AC 121 ms
162,740 KB
testcase_02 AC 121 ms
162,912 KB
testcase_03 AC 846 ms
384,744 KB
testcase_04 AC 165 ms
164,788 KB
testcase_05 AC 122 ms
162,752 KB
testcase_06 AC 831 ms
381,468 KB
testcase_07 AC 421 ms
233,944 KB
testcase_08 AC 166 ms
171,076 KB
testcase_09 AC 160 ms
166,336 KB
testcase_10 AC 121 ms
162,920 KB
testcase_11 AC 122 ms
163,012 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <fstream>
#include <queue>
#include <complex>

#define LL long long
#define PI acos(-1)
#define INF 1e8
#define EPS 1e-9

using namespace std;

#define MAX_N 51
#define MAX_TOTAL 100001

int N;
int Total;
int A[MAX_N];

string mMin(string s1, string s2){

  if(s1 == "-1" && s2 != "-1")
    return s2;
  else if(s1 != "-1" && s2 == "-1")
    return s1;

  if(s1 == s2)
    return s1;

  for(int i = 0; i < min(s1.length(), s2.length()); i++){
    if(s1[i] == '+' && s2[i] == '*'){
      return s1;
    }
    else if(s1[i] == '*' && s2[i] == '+'){
      return s2;
    }
  }

  if(s1.length() < s2.length())
    return s1;
  else
    return s2;

}

string dp[MAX_N][MAX_TOTAL];

void init(){

  for(int i = 0; i < MAX_N; i++){
    for(int j = 0; j < MAX_TOTAL; j++){
      dp[i][j] = "-1";
    }
  }

}

int main(){

  cin >> N;
  cin >> Total;
  for(int i = 0; i < N; i++){
    cin >> A[i];
  }

  init();

  dp[0][A[0]] = "";

  for(int i = 0; i < N-1; i++){
    for(int j = 0; j <= Total; j++){
      if(dp[i][j] == "-1")
	continue;
      int nextInt = j + A[i+1];
      if(nextInt <= Total){
	dp[i+1][nextInt] = mMin(dp[i+1][nextInt], dp[i][j] + "+");
      }
      nextInt = j * A[i+1];
      if(nextInt <= Total){
	dp[i+1][nextInt] = mMin(dp[i+1][nextInt], dp[i][j] + "*");
      }
    }
  }

  cout << dp[N-1][Total] << endl;

  return 0;

}
0