UVA 562 Dividing coins
2012-12-15 23:27
357 查看
大意不再赘述。
思路:我最开始想通过枚举构造一个背包,往背包里恰好放进多少个硬币,然后求放进背包里的币值与没放进背包里的币值相减的绝对值最小,测试数据与自己写了几组数据全过了,但是为什么会RE呢??明天再来看看。
思路:我最开始想通过枚举构造一个背包,往背包里恰好放进多少个硬币,然后求放进背包里的币值与没放进背包里的币值相减的绝对值最小,测试数据与自己写了几组数据全过了,但是为什么会RE呢??明天再来看看。
#include <iostream> #include <cstdlib> #include <cstdio> #include <cstring> #include <string> #include <algorithm> #include <cmath> using namespace std; const int MAXN = 10100; const int INF = 0x3f3f3f3f; typedef long long LL; LL f[MAXN]; int V[MAXN]; int MIN, totSum; int n; void init(int C) { for(int i = 1; i <= C; i++) f[i] = -INF; f[0] = 0; } int dp(int C) { init(C); for(int i = 1; i <= n; i++) { for(int v = C; v >= V[i]; v--) { f[v] = max(f[v], f[v-V[i]]+V[i]); } } return f[C]; } void read_case() { scanf("%d", &n); MIN = INF; totSum = 0; for(int i = 1; i <= n; i++) { scanf("%d", &V[i]); MIN = min(MIN, V[i]); totSum += V[i]; } } void solve() { read_case(); LL ans = INF; for(int v = totSum; v >= MIN; v--) { LL a = dp(v); LL dif = abs(2*a-totSum); //差的绝对值 ans = min(ans, dif); } printf("%lld\n", ans); } int main() { int T; scanf("%d", &T); while(T--) { solve(); } }
相关文章推荐
- UVA 562 - Dividing coins
- uva_562 Dividing coins
- UVa 562 Dividing coins
- UVA 562 Dividing coins
- UVA 562 - Dividing coins
- UVA 562 - Dividing coins
- UVA - 562 - Dividing coins (01背包)
- UVA 562 Dividing coins 01背包 Commencel
- UVA 562 Dividing coins
- UVA - 562 Dividing coins
- UVA 562 Dividing coins
- uva 562 - Dividing coins
- UVA 562 Dividing coins
- UVa 562 - Dividing coins
- uva 562 Dividing coins
- UVA 562 - Dividing coins
- UVa 562 - Dividing coins
- uva 562Uva 562 Dividing coins
- UVA 562 Dividing coins
- UVA 562 Dividing coins --01背包的变形