2336: [HNOI2011]任务调度 - BZOJ
一道随机算法的题目
随便用什么随机算法
首先我们可以想到枚举类型3的最终类型,然后再做
先贪心出一个较优的序列,首先我们知道肯定是在A机器上先做完类型1的事件再做类型2的事件,机器B也类似,因为这些没有等待时间,而他们做完了后续事情才能做
然后对类型1进行排序,按timeb为第一关键字降序(为了填补空隙,前面的越大排得就越紧密),按timea为第二关键字升序排序(尽量早点让类型1的B机器上的事先做),类型2的也类似
然后随机2000次左右(每次随机交换类型1的两个和类型2的两个)正确率就很高了
const
maxn=;
inf=;
type
node=record
kind,a,b,time:longint;
end;
var
a:array[..maxn]of node;
aa,bb,sa,sb:array[..maxn]of longint;
n,ans,ta,tb,numa,numb,suma,sumb:longint; function min(x,y:longint):longint;
begin
if x<y then exit(x);
exit(y);
end; function max(x,y:longint):longint;
begin
if x>y then exit(x);
exit(y);
end; procedure swap(var x,y:longint);
var
t:longint;
begin
t:=x;x:=y;y:=t;
end; procedure init;
var
i:longint;
begin
read(n);
for i:= to n do
with a[i] do
read(kind,a,b);
ans:=inf;
end; function geta:boolean;
var
k:longint;
begin
if numa>n then exit(false);
if numa>suma then k:=bb[numa-suma]
else k:=aa[numa];
if (a[k].kind=) or (a[k].time>) then
begin
ta:=max(ta,a[k].time)+a[k].a;
a[k].time:=ta;
inc(numa);
exit(true);
end;
exit(false);
end; function getb:boolean;
var
k:longint;
begin
if numb>n then exit(false);
if numb>sumb then k:=aa[numb-sumb]
else k:=bb[numb];
if (a[k].kind=) or (a[k].time>) then
begin
tb:=max(tb,a[k].time)+a[k].b;
a[k].time:=tb;
inc(numb);
exit(true);
end;
exit(false);
end; procedure get;
var
i,j,lastans:longint;
begin
suma:=;
sumb:=;
lastans:=inf;
for i:= to n do
if a[i].kind= then
begin
inc(suma);
sa[suma]:=i;
end
else
begin
inc(sumb);
sb[sumb]:=i;
end;
for i:=suma downto do
for j:= to i- do
if (a[sa[j+]].b>a[sa[j]].b) or ((a[sa[j+]].b=a[sa[j]].b) and (a[sa[j+]].a<a[sa[j]].a)) then swap(sa[j],sa[j+]);
for i:=sumb downto do
for j:= to i- do
if (a[sb[j+]].a>a[sb[j]].a) or ((a[sb[j+]].a=a[sb[j]].a) and (a[sb[j+]].b<a[sb[j]].b)) then swap(sb[j],sb[j+]);
for j:= to do
begin
aa:=sa;
bb:=sb;
if suma<> then
swap(aa[random()mod suma+],aa[random()mod suma+]);
if sumb<> then
swap(bb[random()mod sumb+],bb[random()mod sumb+]);
for i:= to n do
a[i].time:=;
ta:=;
tb:=;
numa:=;
numb:=;
while (numa<=n) or (numb<=n) do
begin
if ta<tb then
begin
if geta=false then getb;
end
else
if getb=false then geta;
end;
if lastans>=max(ta,tb) then
begin
lastans:=max(ta,tb);
ans:=min(lastans,ans);
sa:=aa;
sb:=bb;
end;
end;
end; procedure try(x:longint);
begin
if x=n+ then get
else
if a[x].kind= then
begin
a[x].kind:=;
try(x+);
a[x].kind:=;
try(x+);
a[x].kind:=;
end
else try(x+);
end; begin
randomize;
init;
try();
write(ans);
end.
2336: [HNOI2011]任务调度 - BZOJ的更多相关文章
- [HNOI2011]任务调度
题目描述 有 N 个任务和两台机器 A 与 B.每个任务都需要既在机器 A 上执行,又在机器 B 上执行, 第 i 个任务需要在机器 A 上执行时间 Ai,且需要在机器 B 上执行时间 Bi.最终的目 ...
- bzoj2336 [HNOI2011]任务调度
Description 正解:搜索+随机化. 先写个搜索,枚举所有没有要求的任务属于哪一种任务,然后再用爬山来更新最优解. 具体来说就是先把所有先做任务$A$的按照$a$时间从大到小排序,先做任务$B ...
- bzoj AC倒序
Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...
- BZOJ 2326: [HNOI2011]数学作业( 矩阵快速幂 )
BZOJ先剧透了是矩阵乘法...这道题显然可以f(x) = f(x-1)*10t+x ,其中t表示x有多少位. 这个递推式可以变成这样的矩阵...(不会用公式编辑器...), 我们把位数相同的一起处理 ...
- ●BZOJ 2329 [HNOI2011]括号修复.cpp
题链: http://www.lydsy.com/JudgeOnline/problem.php?id=2329 题解: Splay 类似 BZOJ 2329 [HNOI2011]括号修复 只是多了一 ...
- 【概率DP/高斯消元】BZOJ 2337:[HNOI2011]XOR和路径
2337: [HNOI2011]XOR和路径 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 682 Solved: 384[Submit][Stat ...
- [BZOJ 2326] [HNOI2011] 数学作业 【矩阵乘法】
题目链接:BZOJ - 2326 题目分析 数据范围达到了 10^18 ,显然需要矩阵乘法了! 可以发现,向数字尾部添加一个数字 x 的过程就是 Num = Num * 10^k + x .其中 k ...
- BZOJ 2329: [HNOI2011]括号修复( splay )
把括号序列后一定是))))((((这种形式的..所以维护一个最大前缀和l, 最大后缀和r就可以了..答案就是(l+1)/2+(r+1)/2...用splay维护,O(NlogN). 其实还是挺好写的, ...
- BZOJ 2337: [HNOI2011]XOR和路径( 高斯消元 )
一位一位考虑异或结果, f(x)表示x->n异或值为1的概率, 列出式子然后高斯消元就行了 --------------------------------------------------- ...
随机推荐
- AngularJS 整理资料
AngularJS是Google开源的一款JavaScript MVC框架,弥补了HTML在构建应用方面的不足,其通过使用指令(directives)结构来扩展HTML词汇,使开发者可以使用HTML来 ...
- Ajax概述
- oracle 所有下级
--所有下级 SELECT SAP_ORGAN_CODE FROM SAP_ORGAN_LEVEL CONNECT BY PRIOR SAP_FATHER_ORGAN_CODE= SAP_ORGAN_ ...
- 链接器工具错误 LNK2011
问题描述: 使用visual studio 2015编译apr-iconv失败,提示"链接器工具错误 LNK2011:未链接预编译对象:映像可能不能运行"错误. 原因分析: MSD ...
- Linux命令(7):rm命令
1.作用: 删除一个目录中的一个或多个文件或目录: 2.格式: rm [选项] 文件或目录 3.常见参数: 4.使用实例: [root@www hello]# rm –r -i ./why 5.使用 ...
- MySQL免安装版配置
mysql版本:mysql-5.5.33-win32 1.解压免安装包到指定目录MYSQL_HOME2.cmd进入MYSQL_HOME\bin 注册mysql服务:mysqld --install M ...
- Cocos2d-x开发中Ref内存管理
Ref类是Cocos2d-x根类,Cocos2d-x中的很多类都派生自它,例如,我们熟悉的节点类Node也派生自Ref.我们介绍Ref内存管理.内存引用计数Ref类设计来源于Cocos2d-iphon ...
- Cocos2d-x中停止播放背景音乐
停止背景音乐播放代码放置到什么地方比较适合呢?例如:在HelloWorld场景中,主要代码如下: bool HelloWorld::init() { return true; } void Hello ...
- 编辑器笔记——sublime text3 编译sass
如已经安装sass 和 sass build两个插件到ST,command+b编译也没有问题,那么另外安装koala,在koala中引入你要编辑的sass,并把该sass文件设置为自动编译,那么用ST ...
- Hibernate 拥有 Mybits 的SQL/HQL特性 (注解、XML两不误)
第一次写博客.文章有点渣,喜欢就看看,不喜欢路过点个赞. 效果:直接一条语句多种用法 FROM User A WHERE 1=1 <#if id??&g ...