uva562 Dividing coins 01背包

时间:2022-12-19 09:06:16

link:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=503

分成2半,并且两半的差距最小,背包的体积变成V/2

 #include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cctype>
#include <algorithm>
#include <queue>
#include <deque>
#include <queue>
#include <list>
#include <map>
#include <set>
#include <vector>
#include <utility>
#include <functional>
#include <fstream>
#include <iomanip>
#include <sstream>
#include <numeric>
#include <cassert>
#include <ctime>
#include <iterator>
const int INF = 0x3f3f3f3f;
const int dir[][] = {{-,},{,},{,-},{,},{-,-},{-,},{,-},{,}};
using namespace std;
int V, c[], w[], f[], n;
int main(void)
{
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
#endif // ONLINE_JUDGE
int t; cin>>t;
while (t--)
{
cin>>n; int sum=;
for (int i = ; i < n; ++i)
{
cin>>c[i]; sum+=c[i];
}
V=sum/;
memset(f, , sizeof(f));
for (int i = ; i < n; ++i)
{
for (int v = V; v>=c[i]; --v)
{
f[v] = max(f[v], f[v-c[i]]+c[i]);
}
}
cout<<fabs(f[V]-(sum-f[V]))<<endl;
}
return ;
}

这题就用codeblocks写的,写的好纠结,不习惯,囧

uva562 Dividing coins 01背包的更多相关文章

  1. UVA 562 Dividing coins --01背包的变形

    01背包的变形. 先算出硬币面值的总和,然后此题变成求背包容量为V=sum/2时,能装的最多的硬币,然后将剩余的面值和它相减取一个绝对值就是最小的差值. 代码: #include <iostre ...

  2. UVA 562 Dividing coins &lpar;01背包&rpar;

    //平分硬币问题 //对sum/2进行01背包,sum-2*dp[sum/2] #include <iostream> #include <cstring> #include ...

  3. UVA 562 Dividing coins&lpar;dp &plus; 01背包&rpar;

    Dividing coins It's commonly known that the Dutch have invented copper-wire. Two Dutch men were figh ...

  4. HDOJ&lpar;HDU&rpar;&period;3466 Dividing coins &lpar; DP 01背包 无后效性的理解&rpar;

    HDOJ(HDU).3466 Dividing coins ( DP 01背包 无后效性的理解) 题意分析 要先排序,在做01背包,否则不满足无后效性,为什么呢? 等我理解了再补上. 代码总览 #in ...

  5. uva 562 Dividing coins(01背包)

      Dividing coins  It's commonly known that the Dutch have invented copper-wire. Two Dutch men were f ...

  6. UVA 562 Dividing coins (01背包)

    题意:给你n个硬币,和n个硬币的面值.要求尽可能地平均分配成A,B两份,使得A,B之间的差最小,输出其绝对值.思路:将n个硬币的总价值累加得到sum,   A,B其中必有一人获得的钱小于等于sum/2 ...

  7. UVA 562 Dividing coins 分硬币(01背包,简单变形)

    题意:一袋硬币两人分,要么公平分,要么不公平,如果能公平分,输出0,否则输出分成两半的最小差距. 思路:将提供的整袋钱的总价取一半来进行01背包,如果能分出出来,就是最佳分法.否则背包容量为一半总价的 ...

  8. Codeforces 2016 ACM Amman Collegiate Programming Contest A&period; Coins(动态规划&sol;01背包变形)

    传送门 Description Hasan and Bahosain want to buy a new video game, they want to share the expenses. Ha ...

  9. Gym 101102A Coins -- 2016 ACM Amman Collegiate Programming Contest(01背包变形)

    A - Coins Time Limit:3000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Descript ...

随机推荐

  1. 敏捷遇上UML—软创基地马年大会(广州站 2014-4-19)

        我们将在广州为您奉献高端知识大餐,当敏捷遇上UML,会发生怎样的化学作用呢?首席专家张老师将会为您分享需求分析及软件设计方面的最佳实践,帮助您掌握敏捷.UML及两者相结合的实战技巧. 时间:2 ...

  2. Java多线程-新特性-线程池

    Sun在Java5中,对Java线程的类库做了大量的扩展,其中线程池就是Java5的新特征之一,除了线程池之外,还有很多多线程相关的内容,为多线程的编程带来了极大便利.为了编写高效稳定可靠的多线程程序 ...

  3. How does browsersync work&quest;

    How Does BrowserSync Work? BrowserSync starts a small web server. If you’re already using a local we ...

  4. contentProvider-联系人的CURD

    1.联系人的查找 返回一个ArrayList<HashMap<String,  String>>类型 //通过管理联系人的URI获取游标对象 Cursor cursor= ge ...

  5. JS调用PHP 和 PHP调用JS的方法举例

    http://my.oschina.net/jiangchike/blog/220988 1.JS方式调用PHP文件并取得PHP中的值举一个简单的例子来说明:如在页面test_json1中用下面这句调 ...

  6. 【HDOJ】3367 Pseudoforest

    并查集. #include <cstdio> #include <cstring> #include <cstdlib> #define MAXN 10005 #d ...

  7. leetcode Search in Rotated Sorted Array python

      #Suppose a sorted array is rotated at some pivot unknown to you beforehand. #(i.e., 0 1 2 4 5 6 7  ...

  8. 如果不知道MySQL当前使用配置文件&lpar;my&period;cnf&rpar;的路径的解决方法

    如果不知道当前使用的配置文件的路径,可以尝试下面的操作: # which mysqld /usr/local/mysql/bin/mysqld # /usr/local/mysql/bin/mysql ...

  9. Visual C&plus;&plus; 6&period;0中if语句的常见问题

    # include <stdio.h> int main (void) { > )//如果在第四行加分号的话,编译的时候就会在第六行出错 printf("你好\n&quot ...

  10. Git命令速查

    Alias 下面的只是例子,想改成什么跟随自己的意愿即可. git config --global alias.st status //status 缩写成 st git config --globa ...