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. [2014-08-17]Mac OSX 截图快捷键

    系统:OSX 10.9.4 内容来自网络,存于此以便查阅 基本操作 全屏截图:Command-Shift-3 指定区域截图:Command-Shift-4 指定窗口截图:Commnad-Shift-4 ...

  2. js实现非模态窗口增加数据后刷新父窗口数据

    父窗口是由两个部分组成,一个html的table,一部分是extjs的gird. 点击grid面板[增加]按钮将会弹出非模态窗口进行新数据的编辑页面 下面是按钮的触发函数代码: var a = win ...

  3. mac环境下安装mysql

    一,下载mysql 官网"Community " 下会看到"MySQL Community Server"下方有一个"download"点击 ...

  4. 【Beta】Daily Scrum Meeting——Day2

    站立式会议照片 1.本次会议为第一次Meeting会议: 2.本次会议在中午12:00,在图书馆一楼楼道召开,本次会议为30分钟讨论今天要完成的任务以及接下来的任务安排. 燃尽图 每个人的工作分配 成 ...

  5. 【Beta阶段】第一次scrum meeting

    Coding/OSChina 地址 1. 会议内容 学号 主要负责的方向 昨日任务 昨日任务完成进度 接下去要做 9 9 PM 博客编写,会议总结,代码整理 100% 准备下一次会议内容,并对已完成的 ...

  6. 201521123009 《Java程序设计》第6周学习总结

    1. 本周学习总结 2. 书面作业 Q1:clone方法 1.1 Object对象中的clone方法是被protected修饰,在自定义的类中覆盖clone方法时需要注意什么? 用protected修 ...

  7. 201521123065《Java程序设计》第1周学习总结

    1. 本周学习总结 java是门语言较为简单,并且可以在多种平台运行编译的语言. JDK是java开发工具,可以将源程序编译成字节码. JRE:java运行环境. JVM:虚拟机,是java实现多平台 ...

  8. 201521123072《java程序设计》第十周学习总结

    201521123072<java程序设计>第十周学习总结 1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结异常与多线程相关内容. 2. 书面作业 本次PTA作业题集异 ...

  9. java命令行执行带依赖jar包的main函数

    有时候客户端没有运行环境,需要将程序放到服务器上执行,可按如下操作: 1.创建libs文件夹,将运行过程中依赖的jar包全部拷贝到此处; 2.编译.执行 javac -Djava.ext.dirs=l ...

  10. Jquery基础添加删除内容

    直入主题,工作中比较常用的功能在input框内添加内容,不白话了,上代码! 布局: <div id="content"> <input type="te ...