一道随机算法的题目

随便用什么随机算法

首先我们可以想到枚举类型3的最终类型,然后再做

先贪心出一个较优的序列,首先我们知道肯定是在A机器上先做完类型1的事件再做类型2的事件,机器B也类似,因为这些没有等待时间,而他们做完了后续事情才能做

然后对类型1进行排序,按timeb为第一关键字降序(为了填补空隙,前面的越大排得就越紧密),按timea为第二关键字升序排序(尽量早点让类型1的B机器上的事先做),类型2的也类似

然后随机2000次左右(每次随机交换类型1的两个和类型2的两个)正确率就很高了

  1 const
  2     maxn=22;
  3     inf=10000000;
  4 type
  5     node=record
  6       kind,a,b,time:longint;
  7     end;
  8 var
  9     a:array[0..maxn]of node;
 10     aa,bb,sa,sb:array[0..maxn]of longint;
 11     n,ans,ta,tb,numa,numb,suma,sumb:longint;
 12
 13 function min(x,y:longint):longint;
 14 begin
 15     if x<y then exit(x);
 16     exit(y);
 17 end;
 18
 19 function max(x,y:longint):longint;
 20 begin
 21     if x>y then exit(x);
 22     exit(y);
 23 end;
 24
 25 procedure swap(var x,y:longint);
 26 var
 27     t:longint;
 28 begin
 29     t:=x;x:=y;y:=t;
 30 end;
 31
 32 procedure init;
 33 var
 34     i:longint;
 35 begin
 36     read(n);
 37     for i:=1 to n do
 38       with a[i] do
 39         read(kind,a,b);
 40     ans:=inf;
 41 end;
 42
 43 function geta:boolean;
 44 var
 45         k:longint;
 46 begin
 47     if numa>n then exit(false);
 48     if numa>suma then k:=bb[numa-suma]
 49     else k:=aa[numa];
 50     if (a[k].kind=1) or (a[k].time>0) then
 51     begin
 52       ta:=max(ta,a[k].time)+a[k].a;
 53       a[k].time:=ta;
 54       inc(numa);
 55       exit(true);
 56     end;
 57     exit(false);
 58 end;
 59
 60 function getb:boolean;
 61 var
 62         k:longint;
 63 begin
 64     if numb>n then exit(false);
 65     if numb>sumb then k:=aa[numb-sumb]
 66     else k:=bb[numb];
 67     if (a[k].kind=2) or (a[k].time>0) then
 68     begin
 69       tb:=max(tb,a[k].time)+a[k].b;
 70       a[k].time:=tb;
 71       inc(numb);
 72       exit(true);
 73     end;
 74     exit(false);
 75 end;
 76
 77 procedure get;
 78 var
 79     i,j,lastans:longint;
 80 begin
 81     suma:=0;
 82     sumb:=0;
 83     lastans:=inf;
 84     for i:=1 to n do
 85       if a[i].kind=1 then
 86         begin
 87           inc(suma);
 88           sa[suma]:=i;
 89         end
 90       else
 91         begin
 92           inc(sumb);
 93           sb[sumb]:=i;
 94         end;
 95     for i:=suma downto 2 do
 96       for j:=1 to i-1 do
 97         if (a[sa[j+1]].b>a[sa[j]].b) or ((a[sa[j+1]].b=a[sa[j]].b) and (a[sa[j+1]].a<a[sa[j]].a)) then swap(sa[j],sa[j+1]);
 98     for i:=sumb downto 2 do
 99       for j:=1 to i-1 do
100         if (a[sb[j+1]].a>a[sb[j]].a) or ((a[sb[j+1]].a=a[sb[j]].a) and (a[sb[j+1]].b<a[sb[j]].b)) then swap(sb[j],sb[j+1]);
101     for j:=1 to 2000 do
102       begin
103         aa:=sa;
104         bb:=sb;
105         if suma<>0 then
106         swap(aa[random(150000)mod suma+1],aa[random(150000)mod suma+1]);
107         if sumb<>0 then
108         swap(bb[random(150000)mod sumb+1],bb[random(150000)mod sumb+1]);
109         for i:=1 to n do
110           a[i].time:=0;
111         ta:=0;
112         tb:=0;
113         numa:=1;
114         numb:=1;
115         while (numa<=n) or (numb<=n) do
116           begin
117             if ta<tb then
118               begin
119                 if geta=false then getb;
120               end
121             else
122               if getb=false then geta;
123           end;
124         if lastans>=max(ta,tb) then
125         begin
126           lastans:=max(ta,tb);
127           ans:=min(lastans,ans);
128           sa:=aa;
129           sb:=bb;
130         end;
131     end;
132 end;
133
134 procedure try(x:longint);
135 begin
136         if x=n+1 then get
137         else
138           if a[x].kind=3 then
139             begin
140               a[x].kind:=1;
141               try(x+1);
142               a[x].kind:=2;
143               try(x+1);
144               a[x].kind:=3;
145             end
146           else try(x+1);
147 end;
148
149 begin
150     randomize;
151     init;
152     try(1);
153     write(ans);
154 end.

View Code

转载于:https://www.cnblogs.com/Randolph87/p/3644474.html

2336: [HNOI2011]任务调度 - BZOJ相关推荐

  1. BZOJ 2326: [HNOI2011]数学作业( 矩阵快速幂 )

    BZOJ先剧透了是矩阵乘法...这道题显然可以f(x) = f(x-1)*10t+x ,其中t表示x有多少位. 这个递推式可以变成这样的矩阵...(不会用公式编辑器...), 我们把位数相同的一起处理 ...

  2. BZOJ 2337: [HNOI2011]XOR和路径( 高斯消元 )

    一位一位考虑异或结果, f(x)表示x->n异或值为1的概率, 列出式子然后高斯消元就行了 --------------------------------------------------- ...

  3. bzoj 2326: [HNOI2011]数学作业(矩阵快速幂)

    2326: [HNOI2011]数学作业 Time Limit: 10 Sec  Memory Limit: 128 MB Submit: 2249  Solved: 1301 [Submit][St ...

  4. [Luogu P3214] [BZOJ 4339] [HNOI2011]卡农

    洛谷传送门 BZOJ传送门 题目描述 众所周知卡农是一种复调音乐的写作技法,小余在听卡农音乐时灵感大发,发明了一种新的音乐谱写规则.他将声音分成 nnn 个音阶,并将音乐分成若干个片段.音乐的每个片段 ...

  5. bzoj 2326: [HNOI2011]数学作业

    Description 小 C 数学成绩优异,于是老师给小 C 留了一道非常难的数学作业题:给定正整数 N 和 M 要求计算 Concatenate (1 .. N) Mod M 的值,其中 Conc ...

  6. 【HNOI2011】数学作业(BZOJ 2326)

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2326   设f[i]表示i的答案,那么 f[i] = f[i - 1] * 10 + i ...

  7. BZOJ.1558.[JSOI2009]等差数列(线段树 差分)

    BZOJ 洛谷 首先可以把原序列\(A_i\)转化成差分序列\(B_i\)去做. 这样对于区间加一个等差数列\((l,r,a_0,d)\),就可以转化为\(B_{l-1}\)+=\(a_0\),\(B ...

  8. Java基于Quartz的定时任务调度服务(一)

    Quartz的基本用法 一 Quartz的简单介绍 Quartz 是 OpenSymphony 开源组织在任务调度领域的一个开源项目,完全基于 Java 实现,一个优秀的开源调度框架,其特点是:强大的 ...

  9. S-T平面图中利用最短路求最小割(BZOJ 1001)

    BZOJ 1001: [BeiJing2006]狼抓兔子 最小割 题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1001 现在小朋友们最喜欢 ...

最新文章

  1. dispatch callback ant design pro 网络请求回调函数
  2. Socket2实现tcp端口扫描
  3. Android 自定义view时用到的TypedArray
  4. [Swift]LeetCode835. 图像重叠 | Image Overlap
  5. selecte设置不可用使用disabled属性注意
  6. idc机房运维巡检_智和信通赋能国产信创 建设IT智能监控运维体系 - 蔚颖willing...
  7. 如何更流畅地使用 Google 搜索?
  8. 全向轮机器人直线运动分析
  9. 金志文机器人歌叫什么_MIR发布《移动机器人部署安全指南》白皮书
  10. 在Struts2中实现登陆后跳转到登录前页面
  11. 华为慧通和华为区别_华为P30与华为P30 pro 的区别
  12. C/C++ QT图形开发高级组件 [空]
  13. 屏幕录制专家——录制视频没声音的解决办法
  14. 巴特沃斯数字低通滤波器的设计步骤
  15. 微信公众平台版面设计需要服务器,谈谈微信公众号如何快速在线排版设计好看又实用的方法...
  16. 读npy、pck、nii格式数据集
  17. 移植FreeRTOS时Undefined symbol vApplicationTickHook referred from tasks.o
  18. OpenGL 之 EGL 使用实践
  19. 怎样使用Chrome浏览器截取整个网页?
  20. window8系统 dns有问题怎么修复,浏览器代理设置,浏览器提示网页无法显示

热门文章

  1. Java输出流需要注意的几点细节
  2. Docker 容器时区时间不一致问题解决
  3. S5PV210开发 -- 烧写/启动模式
  4. APP重复点击,出现多次同样的响应
  5. Tensorflow学习笔记——word2vec
  6. python空气质量分析报告_Python数据可视化:2018年北上广深空气质量分析
  7. python参数传递_python中的*和**参数传递机制
  8. python中plotly.express中线条图,Plotly在Python中表达的意外行
  9. mysql事务实战_MySQL - 实战 (2) - 事务隔离
  10. 获取qt保存对话框中输入文本_PyQt5 输入对话框QInputDialog