标签:
First I was stuck at how to represent state of "add all except i".. but after checking editorial, it is simply inverted Coin Change problem..
#include <cmath> #include <cstdio> #include <vector> #include <iostream> #include <algorithm> using namespace std; #define MOD 1000000007 #define MAX_VAL 2000 int main() { // Coin Change Problem vector<int> dp(MAX_VAL, MOD); dp[0] = 0; for(int i = 0; i < MAX_VAL; i ++) { if((i + 1) < MAX_VAL) { dp[i + 1] = std::min(dp[i + 1], dp[i] + 1); } if((i + 2) < MAX_VAL) { dp[i + 2] = std::min(dp[i + 2], dp[i] + 1); } if((i + 5) < MAX_VAL) { dp[i + 5] = std::min(dp[i + 5], dp[i] + 1); } } int t; cin >> t; while(t--) { // Get input int n; cin >> n; vector<int> in(n); for(int i = 0; i < n; i ++) cin >> in[i]; int mn = *std::min_element(in.begin(), in.end()); int steps = MOD; for(int v = 0; v <= mn; v ++) { int ans = 0; for(auto e : in) ans += dp[e - v]; steps = std::min(steps, ans); } cout << steps << endl; } return 0; }
Lesson learnt: a lot complex scenarios can be transformed into simpler ones..
标签:
原文地址:http://www.cnblogs.com/tonix/p/4625859.html