51 Nod 1007 dp
1007 正整数分组
- 1 秒
- 131,072 KB
- 10 分
- 2 级题
输入
第1行:一个数N,N为正整数的数量。
第2 - N+1行,N个正整数。
(N <= 100, 所有正整数的和 <= 10000)
输出
输出这个最小差
输入样例
5
1
2
3
4
5
输出样例
1
变形的01背包;
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<bitset>
#include<ctime>
#include<time.h>
#include<deque>
#include<stack>
#include<functional>
#include<sstream>
//#include<cctype>
//#pragma GCC optimize(2)
using namespace std;
#define maxn 20005
#define inf 0x7fffffff
//#define INF 1e18
#define rdint(x) scanf("%d",&x)
#define rdllt(x) scanf("%lld",&x)
#define rdult(x) scanf("%lu",&x)
#define rdlf(x) scanf("%lf",&x)
#define rdstr(x) scanf("%s",x)
#define mclr(x,a) memset((x),a,sizeof(x))
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int U;
#define ms(x) memset((x),0,sizeof(x))
const long long int mod = 1e9 + 7;
#define Mod 1000000000
#define sq(x) (x)*(x)
#define eps 1e-5
typedef pair<int, int> pii;
#define pi acos(-1.0)
//const int N = 1005;
#define REP(i,n) for(int i=0;i<(n);i++)
typedef pair<int, int> pii; inline int rd() {
int x = 0;
char c = getchar();
bool f = false;
while (!isdigit(c)) {
if (c == '-') f = true;
c = getchar();
}
while (isdigit(c)) {
x = (x << 1) + (x << 3) + (c ^ 48);
c = getchar();
}
return f ? -x : x;
} ll gcd(ll a, ll b) {
return b == 0 ? a : gcd(b, a%b);
}
int sqr(int x) { return x * x; } /*ll ans;
ll exgcd(ll a, ll b, ll &x, ll &y) {
if (!b) {
x = 1; y = 0; return a;
}
ans = exgcd(b, a%b, x, y);
ll t = x; x = y; y = t - a / b * y;
return ans;
}
*/ int n;
int a[maxn];
int sum;
int dp[100002];
int t[200], v[200]; int main()
{
// ios::sync_with_stdio(0);
n = rd();
for (int i = 1; i <= n; i++) {
a[i] = rd(); //sum[i] = sum[i - 1] + a[i];
sum += a[i];
t[i] = v[i] = a[i];
}
int minn = inf;
int V = sum / 2;
for (int i = 1; i <= n; i++) {
for (int j = V; j >= t[i]; j--) {
dp[j] = max(dp[j], dp[j - t[i]] + v[i]);
}
}
cout << abs(dp[V] - (sum - dp[V])) << endl;
return 0;
}
51 Nod 1007 dp的更多相关文章
- 51 Nod 1007 正整数分组【类01背包】
1007 正整数分组 基准时间限制:1 秒 空间限制:131072 KB 分值: 10 难度:2级算法题 将一堆正整数分为2组,要求2组的和相差最小. 例如:1 2 3 4 5,将1 2 4分为1组, ...
- 51 nod 1007 正整数分组 (简单01背包) && csu 1547: Rectangle
http://www.51nod.com/onlineJudge/questionCode.html#problemId=1007¬iceId=15020 求出n个数的和sum,然后用s ...
- 51 Nod 1050 dp
1050 循环数组最大子段和 1 秒 131,072 KB 10 分 2 级题 N个整数组成的循环序列a[1],a[2],a[3],…,a[n],求该序列如a[i]+a[i+1]+…+a[j]的连 ...
- 51 nod 1055 最长等差数列(dp)
1055 最长等差数列 基准时间限制:2 秒 空间限制:262144 KB 分值: 80 难度:5级算法题 N个不同的正整数,找出由这些数组成的最长的等差数列. 例如:1 3 5 6 8 9 ...
- 51 nod 1610 路径计数(Moblus+dp)
1610 路径计数 基准时间限制:1 秒 空间限制:131072 KB 分值: 80 难度:5级算法题 路径上所有边权的最大公约数定义为一条路径的值. 给定一个有向无环图.T次修改操作,每次修改一 ...
- 51 nod 1766 树上的最远点对(线段树+lca)
1766 树上的最远点对 基准时间限制:3 秒 空间限制:524288 KB 分值: 80 难度:5级算法题 n个点被n-1条边连接成了一颗树,给出a~b和c~d两个区间,表示点的标号请你求出两个 ...
- 51 nod 1439 互质对(Moblus容斥)
1439 互质对 题目来源: CodeForces 基准时间限制:2 秒 空间限制:131072 KB 分值: 160 难度:6级算法题 有n个数字,a[1],a[2],…,a[n].有一个集合,刚开 ...
- 51 nod 1495 中国好区间
1495 中国好区间 基准时间限制:0.7 秒 空间限制:131072 KB 分值: 80 难度:5级算法题 阿尔法在玩一个游戏,阿尔法给出了一个长度为n的序列,他认为,一段好的区间,它的长度是& ...
- 51 nod 1427 文明 (并查集 + 树的直径)
1427 文明 题目来源: CodeForces 基准时间限制:1.5 秒 空间限制:131072 KB 分值: 160 难度:6级算法题 安德鲁在玩一个叫“文明”的游戏.大妈正在帮助他. 这个游 ...
随机推荐
- 2PC之JTA原理与实现
转自:https://www.ibm.com/developerworks/cn/java/j-lo-jta/index.html 利用 JTA 处理事务 什么是事务处理 事务是计算机应用中不可或缺的 ...
- OpenGL坐标变换专题
OpenGL坐标变换专题(转) OpenGL通过相机模拟.可以实现计算机图形学中最基本的三维变换,即几何变换.投影变换.裁剪变换.视口变换等,同时,OpenGL还实现了矩阵堆栈等.理解掌握了有关坐 ...
- Red Hat 6.5 Samba服务器的搭建(匿名访问,免登录)
搭建Samba服务器是为了实现Linux共享目录之后,在Windows可以直接访问该共享目录. 现在介绍如何在红帽6.5系统中搭建Samba服务. 搭建Samba服务之前,yum源必须配置好,本地源和 ...
- redis集群部署及常用的操作命令(上)
简单说下自己测试搭建简单的redis集群的大体步骤: 1.首先你的有6个redis(官方说最少6个,3master,3slave),可以先在一台机器上搭建,搭建到多台上应该只需要改变启动命令即可(可能 ...
- RMAN备份(转)
原文:http://blog.csdn.net/leshami/article/details/6032739 一.数据库备份与RMAN备份的概念 1.数据库完全备份:按归档模式分为归档和非归档 归档 ...
- 如何查找文件中的schema约束
1.下载一个spring3.2的jar和source 然后打开source的文件(路径:\spring-framework-3.2.5.RELEASE\docs\spring-framework-re ...
- 总结- H5项目常见问题汇总及解决方案(转)
H5项目常见问题及注意事项 Meta基础知识: H5页面窗口自动调整到设备宽度,并禁止用户缩放页面 //一.HTML页面结构 <meta name="viewport" co ...
- 编写高质量代码改善C#程序的157个建议——建议139:事件处理器命名采用组合方式
建议139:事件处理器命名采用组合方式 所谓事件处理器,就是实际被委托执行的那个方法.查看如下代码: public MainWindow() { InitializeComponent(); Butt ...
- modelsim使用常见问题及解决办法集锦③
四.You selected Modelsim-Altera as Simulation Software in EDA Tool Settings,however…… You selected Mo ...
- web项目不能链接数据库
mysql的root没有权限 例如,你想root使用123456从任何主机连接到mysql服务器. 1 mysql>GRANT ALL PRIVILEGES ON *.* TO 'root'@' ...