結果

問題 No.115 遠足のおやつ
ユーザー ty70
提出日時 2015-06-02 02:15:17
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 7 ms / 5,000 ms
コード長 2,053 bytes
コンパイル時間 789 ms
コンパイル使用メモリ 96,476 KB
実行使用メモリ 19,108 KB
最終ジャッジ日時 2025-01-03 01:04:17
合計ジャッジ時間 2,041 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 40
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <string>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <algorithm>	// require sort next_permutation count __gcd reverse etc.
#include <cstdlib>	// require abs exit atof atoi 
#include <cstdio>		// require scanf printf
#include <functional>
#include <numeric>	// require accumulate
#include <cmath>		// require fabs
#include <climits>
#include <limits>
#include <cfloat>
#include <iomanip>	// require setw
#include <sstream>	// require stringstream 
#include <cstring>	// require memset
#include <cctype>		// require tolower, toupper
#include <fstream>	// require freopen
#include <ctime>		// require srand
#define rep(i,n) for(int i=0;i<(n);i++)
#define ALL(A) A.begin(), A.end()

using namespace std;

typedef long long ll;
typedef pair<int, int> P;
typedef pair<P,int> PI;

const int MAX_N = 105;
const int MAX_D = 1005;
const int MAX_K = 12;

bool dp[MAX_N][MAX_K][MAX_D];
PI src[MAX_N][MAX_K][MAX_D];

int main()
{
	memset (dp, false, sizeof (dp ) );
	ios_base::sync_with_stdio(0);
	int N, D, K; cin >> N >> D >> K;
	dp[0][0][0] = true;
	src[0][0][0] = PI (P(-1, -1 ), -1 );
	rep (i, N ){
		for (int j = K; j >= 0; j-- ){
			for (int k = D; k >= 0; k-- ){
				if (dp[i][j][k] ){
					dp[i+1][j+1][k+i+1] |= dp[i][j][k];
					dp[i+1][j][k] |= dp[i][j][k];
					src[i+1][j+1][k+i+1] = PI (P (i, j ), k );
					src[i+1][j][k] = PI (P (i, j ), k );
				} // end if
			} // end for
		} // end rep
	} // end rep
//	cerr << (dp[N][K][D] ? "YES" : "NO" ) << endl; 

	vector<int> path; path.clear();
	if (dp[N][K][D] ){
		int n, k, d;
		n = N, k = K, d = D;
		while (n != -1 ){
			if (path.size() == 0 || path.back() != d ) path.push_back (d );
			PI curr = src[n][k][d];
			n = curr.first.first;
			k = curr.first.second;
			d = curr.second;
		} // end while
		rep (i, path.size()-1 ){
			cout << (path[path.size() - i - 2] - path[path.size() - i - 1] ) << (i != path.size() - 2 ? ' ' : '\n' );
		} // end rep
	}else{
		cout << -1 << endl;
	} // end if

	return 0;
}
0