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$,因为左右点数相等,且 ...
随机推荐
- Swift3.0-字符串和字符
一.简介 Swift中的字符串和字符与OC中在表示上并无大的区别,考虑到字符串在平时的开发工作中属于频繁使用的类型,重点需要掌握的内容是String各种操作函数的写法. 二.Swift与OC的区别 三 ...
- 从npz文件中读取图片并显示的小例子
前提:我把自己的数据集存成了npz的形式,也就是npy的压缩形式.如果电脑上安装了解压软件,双击npz文件的话,会出现每一部分压缩文件的名字例如npz文件的名称为:mnist.npz文件,用好压解压软 ...
- SSD boot test script for(if)循环
;Author : Bing Song ;// ;Usage: modify log drictory according to actual drictory gettime timestr &qu ...
- 【剑指Offer面试编程题】题目1506:求1+2+3+...+n--九度OJ
题目描述: 求1+2+3+...+n,要求不能使用乘除法.for.while.if.else.switch.case等关键字及条件判断语句(A?B:C). 输入: 输入可能包含多个测试样例. 对于每个 ...
- 02-09Android学习进度报告九
今天我学习了关于Adapter的基础知识,了解了Android开发的一些思路和架构. 首先我了解了Adapter的概念以及开发过程中常用的Adapter: BaseAdapter:抽象类,实际开发中我 ...
- 「CH6201」走廊泼水节
「CH6201」走廊泼水节 传送门 考虑 \(\text{Kruskal}\) 的过程以及用到一个最小生成树的性质即可. 在联通两个联通块时,我们肯定会选择最小的一条边来连接这两个联通块,那么这两个联 ...
- 在idea中设置指向源代码(scala)
1.到官网下载scala源代码 点击如下链接下载源码:http://www.scala-lang.org/download/all.html 选择需要的版本点击进行下载,我选择的是2.11.8版本,如 ...
- JPG加入RAR文件原理详解
在水木看到有人上传了一张图片,说如果将其后缀改为rar,解压后会有别的文件,试了一下,果然如此.用十六进制的编辑器看了看,发现的确有理. 先是,文件头部是以JPG格式起始的,如下: ......JFI ...
- pytorc人工神经网络Logistic regression与全连接层
//2019.10.08神经网络与全连接层1.logistics regression逻辑回归的思想是将数据利用激活函数sigmoid函数转换为0-1的概率,然后定义一定的阈值0.5,大于阈值则为一类 ...
- python如何画三维图像?
python三维图像输出的代码如下所示:#画3D函数图像输出from mpl_toolkits.mplot3d import Axes3Dfrom matplotlib import cmimport ...