poj 2576 Tug of War
还是神奇的随机算法,,(看视频说这是爬山法??)
其实就是把序列随机分成两半(我太弱,只知道random_shuffle),然后再每个序列里rand一个位置,x,y然后比较是不是交换之后是更优的。
然后重复这个过程。
神奇。。
#include<cstdio>
#include<cstring>
#include<ctime>
#include<iostream>
#include<algorithm>
#define LL long long
using namespace std; int n,q[],a[],b[],ans1,ans2,ans; int main()
{
cin>>n;
for (int i=; i<=n; i++) scanf("%d",&q[i]);
int CNT=;
while (CNT--)
{
random_shuffle(q+,q+n+);
int lena=,lenb=,suma=,sumb=;
for (int i=; i<=n/; i++) a[++lena]=q[i],suma+=q[i];
for (int i=n/+; i<=n; i++) b[++lenb]=q[i],sumb+=q[i];
int cnt=; ans=1e9;
while (cnt--)
{
int x=rand()%lena+,y=rand()%lenb+;
if (abs(suma-a[x]+b[y]-(sumb-b[y]+a[x]))<abs(suma-sumb))
suma=suma-a[x]+b[y],sumb=sumb-b[y]+a[x],swap(a[x],b[y]);
}
if (abs(suma-sumb)<ans) ans1=suma,ans2=sumb,ans=abs(suma-sumb);
}
if (ans1>ans2) swap(ans1,ans2);
printf("%d %d\n",ans1,ans2);
}
poj 2576 Tug of War的更多相关文章
- 随机算法 poj 2576 Tug of War
Tug of War Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 8187 Accepted: 2204 Descri ...
- HDU-2576 Tug of War
http://poj.org/problem?id=2576 二维数组01背包的变形. Tug of War Time Limit: 3000MS Memory Limit: 65536K Tot ...
- POJ 3114 Countries in War(强连通+最短路)
POJ 3114 Countries in War 题目链接 题意:给定一个有向图.强连通分支内传送不须要花费,其它有一定花费.每次询问两点的最小花费 思路:强连通缩点后求最短路就可以 代码: #in ...
- UVA - 10032 Tug of War (二进制标记+01背包)
Description Problem F: Tug of War A tug of war is to be arranged at the local office picnic. For the ...
- POJ 3114 Countries in War(强连通)(缩点)(最短路)
Countries in War Time Limit: 1000MS Memory Limit: 65536K Total Sub ...
- poj 3114 Countries in War
http://poj.org/problem?id=3114 #include <cstdio> #include <cstring> #include <queue&g ...
- POJ 3114 Countries in War(强联通分量+Tarjan)
题目链接 题意 : 给你两个城市让你求最短距离,如果两个城市位于同一强连通分量中那距离为0. 思路 :强连通分量缩点之后,求最短路.以前写过,总感觉记忆不深,这次自己敲完再写了一遍. #include ...
- uva 10032 Problem F: Tug of War
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...
- BZOJ4124 : [Baltic2015]Tug of war
建立二分图,首先如果存在度数为$0$的点,那么显然无解. 如果存在度数为$1$的点,那么这个点的匹配方案固定,可以通过拓扑排序去掉所有这种点. 那么现在剩下的点度数都至少为$2$,因为左右点数相等,且 ...
随机推荐
- 实现纸牌游戏的随机抽牌洗牌过程(item系列几个内置方法的实例)
实现纸牌游戏的随机抽牌洗牌过程(item系列几个内置方法的实例) 1.namedtuple:命名元组,可以创建一个没有方法只有属性的类 from collections import namedtup ...
- 牛客网Sql
牛客网Sql: 1.查询最晚入职的员工信息 select * from employees where hire_date =(select max(hire_date) from employee ...
- git切换分支导致代码丢失找回(git reflog找回错误的重置)
1.使用git reflog查看日志 2.切换到丢失的分支 3. 创建一个临时分支 如(diff),并切换到dev(原分支),然后合并diff到dev分支 4.查看状态 5.强制合并,然后提交到de ...
- Python 中命令行参数解析工具 docopt 安装和应用
什么是 docopt? 1.docopt 是一种 Python 编写的命令行执行脚本的交互语言. 它是一种语言! 它是一种语言! 它是一种语言! 2.使用这种语言可以在自己的脚本中,添加一些规则限制. ...
- JDBC 存储过程
存储过程 DROP PROCEDURE IF EXISTS `addUser`; CREATE PROCEDURE `addUser` (),in birthday date,in money flo ...
- 从零开始-建站前的准备之django数据库创建的问题
稍微熟悉了一下django里面对于数据的操作,发现遇见了好多的问题. django对数据的操作是代码式的操作. 一开始在models里面开始为某个表创建参数,像username,password这样的 ...
- 蓝桥杯java 迷宫
0101010100101100100101011001011010010000100010101000001000100000101010010000100000001001100110100101 ...
- 新见Java数据类型_需了解
LinkedList<T>.LinkedList.poll() 先给出结论:pop 与 poll 都是取出 LinkedList 的第一个元素,并将该元素删除,等效于:removeFirs ...
- HDU 5506:GT and set bitset+暴力
GT and set Accepts: 35 Submissions: 194 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 655 ...
- 标准模板库中的优先队列(priority_queue)
//C++数据结构与算法(第4版) Adam Drozdek 著 徐丹 吴伟敏<<清华大学出版社>> #include<queue> priority_queu ...