Tug of War
Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 8187   Accepted: 2204

Description

A tug of war is to be arranged at the local office picnic. For the tug of war, the picnickers must be divided into two teams. Each person must be on one team or the other; the number of people on the two teams must not differ by more than 1; the total weight of the people on each team should be as nearly equal as possible.

Input

The first line of input contains n the number of people at the picnic. n lines follow. The first line gives the weight of person 1; the second the weight of person 2; and so on. Each weight is an integer between 1 and 450. There are at most 100 people at the picnic.

Output

Your output will be a single line containing 2 numbers: the total weight of the people on one team, and the total weight of the people on the other team. If these numbers differ, give the lesser first.

Sample Input

3
100
90
200

Sample Output

190 200

 #include <iostream>
#include <string.h>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
int aabs(int a)
{
if(a>)return a;
else return -a;
}
int main()
{
int n,a[][],b1=,b0=,sum0=,sum1=,cha,i;
scanf("%d",&n);
srand((unsigned)time(NULL));
for(i=; i<n; i++)
{
if(i&)
scanf("%d",&a[][b1]),sum1+=a[][b1++];
else scanf("%d",&a[][b0]),sum0+=a[][b0++];
}
cha=aabs(sum1-sum0);
n=;
while(n--)
{
int c0=rand()%b0;
int c1=rand()%b1;
if(cha>aabs((sum0-a[][c0]+a[][c1])-(sum1-a[][c1]+a[][c0])))
{
sum0=sum0-a[][c0]+a[][c1];
sum1=sum1-a[][c1]+a[][c0];
swap(a[][c0],a[][c1]);
cha=aabs(sum0-sum1);
}
}
if(sum0>sum1)swap(sum0,sum1);
cout<<sum0<<" "<<sum1<<endl;
}

随机算法 poj 2576 Tug of War的更多相关文章

  1. poj 2576 Tug of War

    还是神奇的随机算法,,(看视频说这是爬山法??) 其实就是把序列随机分成两半(我太弱,只知道random_shuffle),然后再每个序列里rand一个位置,x,y然后比较是不是交换之后是更优的. 然 ...

  2. POJ 3318:Matrix Multiplication(随机算法)

    http://poj.org/problem?id=3318 题意:问A和B两个矩阵相乘能否等于C. 思路:题目明确说出(n^3)的算法不能过,但是通过各种常数优化还是能过的. 这里的随机算法指的是随 ...

  3. POJ 3318 Matrix Multiplication(随机算法)

    题目链接 随机算法使劲水...srand((unsigned)time(0))比srand(NULL)靠谱很多,可能是更加随机. #include <cstdio> #include &l ...

  4. HDU-2576 Tug of War

    http://poj.org/problem?id=2576 二维数组01背包的变形. Tug of War Time Limit: 3000MS   Memory Limit: 65536K Tot ...

  5. 数学#素数判定Miller_Rabin+大数因数分解Pollard_rho算法 POJ 1811&2429

    素数判定Miller_Rabin算法详解: http://blog.csdn.net/maxichu/article/details/45458569 大数因数分解Pollard_rho算法详解: h ...

  6. 随机算法 - Miller_Rabin pollard_rho

    #include<stdio.h> #include<string.h> #include<stdlib.h> #include<time.h> #in ...

  7. 微信红包中使用的技术:AA收款+随机算法

    除夕夜你领到红包了吗?有的说“我领了好几K!”“我领了几W!” 土豪何其多,苦逼也不少!有的说“我出来工作了,没压岁钱了,还要发红包”.那您有去抢微信红包吗?微信群中抢“新年红包”春节爆红.618微信 ...

  8. 抽奖随机算法的技术探讨与C#实现

    一.模拟客户需求 1.1 客户A需求:要求每次都按照下图的概率随机,数量不限,每个用户只能抽一次,抽奖结果的分布与抽奖概率近似. 1.2 客户B需求:固定奖项10个,抽奖次数不限,每个用户只能抽一次, ...

  9. hdu 4712 (随机算法)

    第一次听说随机算法,在给的n组数据间随机取两个组比较,当随机次数达到一定量时,答案就出来了. #include<stdio.h> #include<stdlib.h> #inc ...

随机推荐

  1. Ext.grid.EditorGridPanel分页刷新

    store.reload(); var start = grid.getBottomToolbar().cursor;//获取当前页开始条数 上面获取当前页第一条记录的方法有时候说未定义,我现在使用下 ...

  2. [js高手之路]深入浅出webpack系列2-配置文件webpack.config.js详解

    接着上文,重新在webpack文件夹下面新建一个项目文件夹demo2,然后用npm init --yes初始化项目的package.json配置文件,然后安装webpack( npm install ...

  3. C++ 将函数作为形参

    今天偶然看到一段代码,其中将函数作为形参进行设计,一开始不理解,自己试了一下,发现果然可行 #include <iostream> using namespace std; void vi ...

  4. ehcache memcache redis 三大缓存对比

    详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcyt268 最近项目组有用到这三个缓存,去各自的官方看了下,觉得还真的各有千秋!今 ...

  5. CSS文本效果

    前面的话 本文将详细介绍CSS文本效果 凸版印刷效果 这种效果尤其适用于中等亮度背景配上深色文字的场景:但它也可用于深色底.浅色字的场景,只要文字不是黑色并且背景不是纯黑或纯白就行 [浅色背景深色文本 ...

  6. 使用BootStrap框架设置全局CSS样式

    一.排版 标题 HTML 中的所有标题标签,<h1> 到 <h6> 均可使用.另外,还提供了 .h1 到 .h6 类,为的是给内联(inline)属性的文本赋予标题的样式. & ...

  7. JS中的运算符和JS中的分支结构

    JS中的运算符 1.算术运算(单目运算符) + .-.*. /. %取余.++自增 .--自减 +:两种作用,链接字符串/加法运算.当+两边全为数字时,进行加法运算:当+两边有任意一边为字符串时,起链 ...

  8. 第07周-集合与GUI

    1. 本周作业简评与建议 本周8分以上的同学有32人(占1/4),其中9分以上的同学有13人(占1/10).大作业未完成的最高评分一般高于6分.被认定为抄袭的为-10分.请大家认真完成大作业,后面每周 ...

  9. 201521123096《Java程序设计》第二周学习总结

    1.本周学习总结 (1)学会使用码云管理代码: (2)了解数组和字符串的操作: (3)对完全限定类名有一定的认识. 2.书面作业 (1)使用Eclipse关联jdk源代码,并查看String对象的源代 ...

  10. 201521123060 《Java程序设计》第13周学习总结

    1.本周学习总结 以你喜欢的方式(思维导图.OneNote或其他)归纳总结多网络相关内容. 2.书面作业 1.网络基础 1.1比较ping www.baidu.com与ping cec.jmu.edu ...