The N monsters stood in a row to let the mage's fireball attack. The most powerful attacking monster caused a damage, which caused B damage to nearby monsters. If the blood volume of the monsters is less than 0, the system will die, find the minimum number of attacks and output the target of each attack (The two-side monster cannot be attacked
#include <vector>#include <list>#include <map>#include <set>#include <queue>#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 <ctime>#include <cstring>using namespace std;#define REP(i,n) for((i)=0;(i)<(int)(n);(i)++)#define foreach(c,itr) for(__typeof((c).begin()) itr=(c).begin();itr!=(c).end();itr++)typedef long long ll;typedef long double ld;int n, b, a;int h[20];int dp[11][20][20];int calc(int ind, int H0, int H1) { int h0 = H0, h1 = H1; if (dp[ind][H0][H1] != -1) return dp[ind][H0][H1]; int h2 = h[ind + 1]; int res = 1 << 20; int cur = 0; if (ind == n - 2) { while (h0 > 0 || h1 > 0 || h2 > 0) { cur++; h0 -= a; h1 -= b; h2 -= a; } return dp[ind][H0][H1] = cur; } while (h0 > 0) { cur++; h0 -= a; h1 -= b; h2 -= a; } for (int i = 0; i < 20; ++i) { res = min(res, cur + calc(ind + 1, max(h1, 0), max(h2, 0))); cur++; h0 -= a; h1 -= b; h2 -= a; } return dp[ind][H0][H1] = res;}string sp = "";void build(int ind, int H0, int H1) { int h0 = H0, h1 = H1; int h2 = h[ind + 1]; int res = dp[ind][H0][H1]; int cur = 0; if (ind == n - 2) { while (h0 > 0 || h1 > 0 || h2 > 0) { cout << sp << ind+1; sp = " "; cur++; h0 -= a; h1 -= b; h2 -= a; } return; } while (h0 > 0) { cout << sp << ind+1; sp = " "; cur++; h0 -= a; h1 -= b; h2 -= a; } for (int i = 0; i < 20; ++i) { if( res == cur + calc(ind + 1, max(h1, 0), max(h2, 0))) { build(ind + 1, max(h1, 0), max(h2, 0)); return; } cout << sp << ind+1; sp = " "; cur++; h0 -= a; h1 -= b; h2 -= a; }}int main() { cin >> n >> b >> a; for (int i = 0; i < n; ++i) { cin >> h[i]; h[i]++; } memset(dp, -1, sizeof dp); int res = calc(1, h[0], h[1]); cout << res << endl; build(1, h[0], h[1]); cout << endl; return 0;}
6d-Dynamic Planning