楼主: 〇〇

[精华] ACM题(#2634 Collecting Stones)

[复制链接]
论坛徽章:
131
2006年度最佳技术回答
日期:2007-01-24 12:58:48福特
日期:2013-10-24 13:57:422014年新春福章
日期:2014-02-18 16:41:11马上有车
日期:2014-02-18 16:41:11马上有车
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14马上有对象
日期:2014-02-19 11:55:14马上加薪
日期:2014-02-19 11:55:142013年新春福章
日期:2013-02-25 14:51:24
61#
发表于 2011-4-12 15:16 | 只看该作者
也许可以再细分,12,3,4 这样的走

使用道具 举报

回复
论坛徽章:
131
2006年度最佳技术回答
日期:2007-01-24 12:58:48福特
日期:2013-10-24 13:57:422014年新春福章
日期:2014-02-18 16:41:11马上有车
日期:2014-02-18 16:41:11马上有车
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14马上有对象
日期:2014-02-19 11:55:14马上加薪
日期:2014-02-19 11:55:142013年新春福章
日期:2013-02-25 14:51:24
62#
发表于 2011-4-12 15:19 | 只看该作者
倒数第四行
and  b.tst = :M - a.tst

也是个关键优化点,从 b.tst + a.tst = :M 改过来, 使其走hash

使用道具 举报

回复
论坛徽章:
407
紫蛋头
日期:2012-05-21 10:19:41迷宫蛋
日期:2012-06-06 16:02:49奥运会纪念徽章:足球
日期:2012-06-29 15:30:06奥运会纪念徽章:排球
日期:2012-07-10 21:24:24鲜花蛋
日期:2012-07-16 15:24:59奥运会纪念徽章:拳击
日期:2012-08-07 10:54:50奥运会纪念徽章:羽毛球
日期:2012-08-21 15:55:33奥运会纪念徽章:蹦床
日期:2012-08-21 21:09:51奥运会纪念徽章:篮球
日期:2012-08-24 10:29:11奥运会纪念徽章:体操
日期:2012-09-07 16:40:00
63#
 楼主| 发表于 2011-4-12 15:31 | 只看该作者
原帖由 rollingpig 于 2011-4-12 15:10 发表
我尝试 12,34,87,65,14,85,18分步做

基本性能是这样的(不考虑

m=1912,是有解的,能算出来?

使用道具 举报

回复
论坛徽章:
407
紫蛋头
日期:2012-05-21 10:19:41迷宫蛋
日期:2012-06-06 16:02:49奥运会纪念徽章:足球
日期:2012-06-29 15:30:06奥运会纪念徽章:排球
日期:2012-07-10 21:24:24鲜花蛋
日期:2012-07-16 15:24:59奥运会纪念徽章:拳击
日期:2012-08-07 10:54:50奥运会纪念徽章:羽毛球
日期:2012-08-21 15:55:33奥运会纪念徽章:蹦床
日期:2012-08-21 21:09:51奥运会纪念徽章:篮球
日期:2012-08-24 10:29:11奥运会纪念徽章:体操
日期:2012-09-07 16:40:00
64#
 楼主| 发表于 2011-4-12 15:49 | 只看该作者
SQL> exec :m:=300
已用时间:  00: 00: 00.00
SQL> @c:\downloads\26345

T.PATH||'*'||T2.PATH
--------------------------------------------
,01,02,03,04*13,14,23,32,40,48,56,64,
已用时间:  00: 00: 00.02
SQL> @c:\downloads\log

         J          V          J          V
---------- ---------- ---------- ----------
         3         58          4        242
已用时间:  00: 07: 06.94

使用道具 举报

回复
论坛徽章:
407
紫蛋头
日期:2012-05-21 10:19:41迷宫蛋
日期:2012-06-06 16:02:49奥运会纪念徽章:足球
日期:2012-06-29 15:30:06奥运会纪念徽章:排球
日期:2012-07-10 21:24:24鲜花蛋
日期:2012-07-16 15:24:59奥运会纪念徽章:拳击
日期:2012-08-07 10:54:50奥运会纪念徽章:羽毛球
日期:2012-08-21 15:55:33奥运会纪念徽章:蹦床
日期:2012-08-21 21:09:51奥运会纪念徽章:篮球
日期:2012-08-24 10:29:11奥运会纪念徽章:体操
日期:2012-09-07 16:40:00
65#
 楼主| 发表于 2011-4-12 15:58 | 只看该作者
58翻译增加2个<:M就快了

  1. with
  2. d(n) as (select level-1 n from dual connect by level<=8),
  3. cost(i, j, v) as (select d1.n, d2.n, d1.n * 8 + d2.n + 1 from d d1, d d2),
  4. direction(i, j) as
  5. (
  6.         select 1, 1 from dual union
  7.         select 1, 0 from dual union
  8.         select 1, -1 from dual union
  9.         select 0, 1 from dual union
  10.         select 0, -1 from dual ),
  11. path14(i, j, v, s) as
  12. (
  13.         select 0, 0, v,
  14.                 cast('x' as char(64)) s from cost where i = 0 and j = 0
  15.         union all
  16.         select p.i + d.i, p.j + d.j, p.v + c.v,
  17.                 cast(substr(p.s,1, 8 * (p.i + d.i) + p.j + d.j + 1-1)|| 'x'||substr(p.s, 8 * (p.i + d.i) + p.j + d.j + 1+1) as char(64)) s
  18.         from path14 p, direction d, cost c
  19.         where p.i + d.i >= 0 and p.i + d.i < 4 -- 在网格内
  20.                 and p.j + d.j >= 0 and p.j + d.j < 8
  21.                 and substr(p.s, 8 * (p.i + d.i) + p.j + d.j + 1, 1) <> 'x' -- 没访问过
  22.                 and p.i + d.i = c.i and p.j + d.j = c.j -- 取得费用
  23.                 and p.v+c.v<:M
  24. ),
  25. path85(i, j, v, s) as
  26. (
  27.         select 7, 7, v,
  28.                 cast(substr(lpad(' ',64,' '), 1,8 * 7 + 7 + 1-1)|| 'x' as char(64)) s
  29.         from cost where i = 7 and j = 7
  30.         union all
  31.         select p.i - d.i, p.j - d.j, p.v + c.v,
  32.                 cast(substr(p.s, 1,8 * (p.i - d.i) + p.j - d.j + 1-1)|| 'x'||substr(p.s, 8 * (p.i - d.i) + p.j - d.j + 1+1) as char(64)) s
  33.         from path85 p, direction d, cost c
  34.         where p.i - d.i >= 0 and p.i - d.i >= 4 -- 在网格内
  35.                 and p.j - d.j >= 0 and p.j - d.j < 8
  36.                 and substr(p.s, 8 * (p.i - d.i) + p.j - d.j + 1, 1) <> 'x' -- 没访问过
  37.                 and p.i - d.i = c.i and p.j - d.j = c.j -- 取得费用
  38.                 and p.v+c.v<:M
  39. ),
  40. pa(j, v) as (select j,v from path14 where i=3),
  41. pb(j, v) as (select j,v from path85 where i=4)
  42. select * from pa, pb
  43. where
  44.         (pa.j = pb.j or pa.j  = pb.j-1 or pa.j = pb.j+1)
  45.         and pa.v + pb.v = :M -- 取值
  46. and rownum=1;
复制代码

SQL> @c:\downloads\log1

         J          V          J          V
---------- ---------- ---------- ----------
         3         58          4        242
已用时间:  00: 00: 00.72

使用道具 举报

回复
论坛徽章:
407
紫蛋头
日期:2012-05-21 10:19:41迷宫蛋
日期:2012-06-06 16:02:49奥运会纪念徽章:足球
日期:2012-06-29 15:30:06奥运会纪念徽章:排球
日期:2012-07-10 21:24:24鲜花蛋
日期:2012-07-16 15:24:59奥运会纪念徽章:拳击
日期:2012-08-07 10:54:50奥运会纪念徽章:羽毛球
日期:2012-08-21 15:55:33奥运会纪念徽章:蹦床
日期:2012-08-21 21:09:51奥运会纪念徽章:篮球
日期:2012-08-24 10:29:11奥运会纪念徽章:体操
日期:2012-09-07 16:40:00
66#
 楼主| 发表于 2011-4-12 16:00 | 只看该作者
不过数字大了还是不行。ms和ora的差别?

SQL> exec :m:=800
已用时间:  00: 00: 00.00
SQL> @c:\downloads\log1

         J          V          J          V
---------- ---------- ---------- ----------
         0         52          1        748
已用时间:  00: 00: 14.77
SQL> @c:\downloads\26345

T.PATH||'*'||T2.PATH
-------------------------------------------------------------------------------------
,01,02,03,04*13,06,14,22,30,38,46,54,62,55,47,39,31,23,15,07,08,16,24,32,40,48,56,64,
已用时间:  00: 00: 00.46

使用道具 举报

回复
论坛徽章:
8
玉兔
日期:2015-11-16 10:18:00铁扇公主
日期:2015-10-27 21:47:42九尾狐狸
日期:2015-12-11 22:31:15
67#
发表于 2011-4-12 16:08 | 只看该作者
MS 好像是并行执行的,只要表中有数据,最后的那个查询就开始了,所以小数字很快能找到结果,不需要在path14,85中做限制

不过问题也在这里,数字大的时候比方1912,如果我先计算好两张临时表,然后直接join,基本1分钟出结果,但是如果使用CTE就很慢

我觉得这题要快还是要应用这个条件(总数量不超过2000001),C里面可以开个bit数组然后一查就知道了,SQL中...

使用道具 举报

回复
论坛徽章:
131
2006年度最佳技术回答
日期:2007-01-24 12:58:48福特
日期:2013-10-24 13:57:422014年新春福章
日期:2014-02-18 16:41:11马上有车
日期:2014-02-18 16:41:11马上有车
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14马上有对象
日期:2014-02-19 11:55:14马上加薪
日期:2014-02-19 11:55:142013年新春福章
日期:2013-02-25 14:51:24
68#
发表于 2011-4-12 16:11 | 只看该作者
170ms, 数字不管
1-8全拆散

  1. var m number
  2. exec :m:=1912

  3. WITH                                                                                                  
  4. p1(srcblock,destblock,tst,pathstr) AS (
  5.   SELECT  /*+ materialize no_merge */ a.n,b.n,a.st+b.st,cast(chr(a.n)||chr(b.n) as varchar2(100))
  6.   from grid a , grid b
  7.    where
  8.    a.n=1    -- Start with first block
  9.    and b.n in (a.n + 8)
  10.   UNION ALL
  11.   SELECT /*+ materialize */
  12.   a.srcblock,b.n,
  13.   a.tst + b.st,--st
  14.   pathstr||chr(b.n)
  15.   FROM  p1 a , (select * from grid where mod(n,8) in (1)) b
  16.   WHERE         b.n in (
  17.   a.destblock + 8
  18.   ) and b.n!=1
  19.   AND a.tst + b.st <=:M
  20.   and instr(pathstr,chr(n))=0
  21. ),
  22. p2(srcblock,destblock,tst,pathstr) AS (
  23.   SELECT  /*+ materialize no_merge */ a.n,b.n,a.st+b.st,cast(chr(a.n)||chr(b.n) as varchar2(100))
  24.   from grid a , grid b
  25.    where  mod(a.n,8) in (2)
  26.    and b.n in (a.n + 8, a.n -8 )
  27.   UNION ALL
  28.   SELECT
  29.   a.srcblock,b.n,
  30.   a.tst + b.st,--st
  31.   pathstr||chr(b.n)
  32.   FROM  p2 a , (select * from grid where mod(n,8) in (2)) b
  33.   WHERE         b.n in (
  34.   a.destblock + 8  , a.destblock - 8
  35.   )
  36.   AND a.tst + b.st <=:M
  37.   and instr(pathstr,chr(n))=0
  38. ),
  39. p3(srcblock,destblock,tst,pathstr) AS (
  40.   SELECT  /*+ materialize no_merge*/ a.n,b.n,a.st+b.st,cast(chr(a.n)||chr(b.n) as varchar2(100))
  41.   from grid a , grid b
  42.    where  mod(a.n,8) in (3)
  43.    and b.n in (a.n + 8, a.n -8 )
  44.   UNION ALL
  45.   SELECT
  46.   a.srcblock,b.n,
  47.   a.tst + b.st,--st
  48.   pathstr||chr(b.n)
  49.   FROM  p3 a , (select * from grid where mod(n,8) in (3)) b
  50.   WHERE         b.n in (
  51.   a.destblock + 8  , a.destblock - 8
  52.   )
  53.   AND a.tst + b.st <=:M
  54.   and instr(pathstr,chr(n))=0
  55. ),
  56. p4(srcblock,destblock,tst,pathstr) AS (
  57.   SELECT  /*+ materialize no_merge*/ a.n,b.n,a.st+b.st,cast(chr(a.n)||chr(b.n) as varchar2(100))
  58.   from grid a , grid b
  59.    where  mod(a.n,8) in (4)
  60.    and b.n in (a.n + 8, a.n -8 )
  61.   UNION ALL
  62.   SELECT
  63.   a.srcblock,b.n,
  64.   a.tst + b.st,--st
  65.   pathstr||chr(b.n)
  66.   FROM  p4 a , (select * from grid where mod(n,8) in (4)) b
  67.   WHERE         b.n in (
  68.   a.destblock + 8  , a.destblock - 8
  69.   )
  70.   AND a.tst + b.st <=:M
  71.   and instr(pathstr,chr(n))=0
  72. ),
  73. p5(srcblock,destblock,tst,pathstr) AS (
  74.   SELECT  /*+ materialize no_merge*/ a.n,b.n,a.st+b.st,cast(chr(a.n)||chr(b.n) as varchar2(100))
  75.   from grid a , grid b
  76.    where  mod(a.n,8) in (5)
  77.    and b.n in (a.n + 8, a.n -8 )
  78.   UNION ALL
  79.   SELECT
  80.   a.srcblock,b.n,
  81.   a.tst + b.st,--st
  82.   pathstr||chr(b.n)
  83.   FROM  p5 a , (select * from grid where mod(n,8) in (5)) b
  84.   WHERE         b.n in (
  85.   a.destblock + 8  , a.destblock - 8
  86.   )
  87.   AND a.tst + b.st <=:M
  88.   and instr(pathstr,chr(n))=0
  89. ),
  90. p6(srcblock,destblock,tst,pathstr) AS (
  91.   SELECT  /*+materialize no_merge*/ a.n,b.n,a.st+b.st,cast(chr(a.n)||chr(b.n) as varchar2(100))
  92.   from grid a , grid b
  93.    where  mod(a.n,8) in (6)
  94.    and b.n in (a.n + 8, a.n -8 )
  95.   UNION ALL
  96.   SELECT
  97.   a.srcblock,b.n,
  98.   a.tst + b.st,--st
  99.   pathstr||chr(b.n)
  100.   FROM  p6 a , (select * from grid where mod(n,8) in (6)) b
  101.   WHERE         b.n in (
  102.   a.destblock + 8  , a.destblock - 8
  103.   )
  104.   AND a.tst + b.st <=:M
  105.   and instr(pathstr,chr(n))=0
  106. ),
  107. p7(srcblock,destblock,tst,pathstr) AS (
  108.   SELECT  /*+materialize no_merge*/ a.n,b.n,a.st+b.st,cast(chr(a.n)||chr(b.n) as varchar2(100))
  109.   from grid a , grid b
  110.    where  mod(a.n,8) in (7)
  111.    and b.n in (a.n + 8, a.n -8 )
  112.   UNION ALL
  113.   SELECT
  114.   a.srcblock,b.n,
  115.   a.tst + b.st,--st
  116.   pathstr||chr(b.n)
  117.   FROM  p7 a , (select * from grid where mod(n,8) in (7)) b
  118.   WHERE         b.n in (
  119.   a.destblock + 8  , a.destblock - 8
  120.   )
  121.   AND a.tst + b.st <=:M
  122.   and instr(pathstr,chr(n))=0
  123. ),
  124. p8(srcblock,destblock,tst,pathstr) AS (
  125.   SELECT  /*+materialize no_merge*/ a.n,b.n,a.st+b.st,cast(chr(a.n)||chr(b.n) as varchar2(100))
  126.   from grid a , grid b
  127.    where a.n=64
  128.    and b.n in (a.n -8 )
  129.   UNION ALL
  130.   SELECT
  131.   a.srcblock,b.n,
  132.   a.tst + b.st,--st
  133.   pathstr||chr(b.n)
  134.   FROM  p8 a , (select * from grid where mod(n,8) in (0)) b
  135.   WHERE         b.n in ( a.destblock - 8 )
  136.   AND a.tst + b.st <=:M
  137.   and instr(pathstr,chr(n))=0
  138. ),
  139. p12 as (
  140. select /*+ materialize no_merge use_concat ordered use_hash(a b)   */ distinct b.destblock,a.tst+b.tst  tst
  141. from p1 a,p2 b
  142. where
  143.   b.srcblock in (
  144.   a.destblock + 1 ,
  145.   a.destblock - 7 ,
  146.   a.destblock + 9
  147. )
  148.   AND a.tst + b.tst <:M
  149. ),
  150. p13 as (
  151. select /*+ materialize no_merge ordered use_concat  use_hash(a b)   */ distinct b.destblock,a.tst+b.tst  tst
  152. from  p3 b , p12 a
  153. where
  154.   b.srcblock in (
  155.   a.destblock + 1 ,
  156.   a.destblock - 7 ,
  157.   a.destblock + 9
  158. )
  159.   AND a.tst + b.tst <:M
  160. ),
  161. p14 as (
  162. select /*+ materialize no_merge ordered use_concat  use_hash(a b)  */ distinct b.destblock,a.tst+b.tst  tst
  163. from p4 b , p13 a   
  164. where
  165.   b.srcblock in (
  166.   a.destblock + 1 ,
  167.   a.destblock - 7 ,
  168.   a.destblock + 9
  169. )
  170.   AND a.tst + b.tst <:M
  171. ) ,
  172. p87 as (
  173. select /*+ materialize no_merge use_concat ordered use_hash(a b)   */ distinct b.destblock,a.tst+b.tst  tst
  174. from p8 a,p7 b
  175. where
  176.   b.srcblock in (
  177.   a.destblock - 1 ,
  178.   a.destblock + 7 ,
  179.   a.destblock - 9
  180. )
  181.   AND a.tst + b.tst <:M
  182. ),
  183. p86 as (
  184. select /*+ materialize no_merge ordered use_concat  use_hash(a b)   */ distinct b.destblock,a.tst+b.tst  tst
  185. from  p6 b , p87 a
  186. where
  187.   b.srcblock in (
  188.   a.destblock - 1 ,
  189.   a.destblock + 7 ,
  190.   a.destblock - 9
  191. )
  192.   AND a.tst + b.tst <:M
  193. ),
  194. p85 as (
  195. select /*+ materialize no_merge ordered use_concat  use_hash(a b)  */ distinct b.destblock,a.tst+b.tst  tst
  196. from  p5 b,p86 a     
  197. where
  198.   b.srcblock in (
  199.   a.destblock - 1 ,
  200.   a.destblock + 7 ,
  201.   a.destblock - 9
  202. )
  203.   AND a.tst + b.tst <:M
  204. ),
  205. p18 as (
  206. select /*+use_hash(a b)  */'Y' f from p14 a,p85 b
  207. where
  208.   b.destblock in (
  209.   a.destblock + 1 ,
  210.   a.destblock - 7 ,
  211.   a.destblock + 9
  212. ) and  b.tst = :M - a.tst
  213. and rownum = 1
  214. )
  215. select nvl(max(f),'N') from p18  

复制代码

使用道具 举报

回复
论坛徽章:
407
紫蛋头
日期:2012-05-21 10:19:41迷宫蛋
日期:2012-06-06 16:02:49奥运会纪念徽章:足球
日期:2012-06-29 15:30:06奥运会纪念徽章:排球
日期:2012-07-10 21:24:24鲜花蛋
日期:2012-07-16 15:24:59奥运会纪念徽章:拳击
日期:2012-08-07 10:54:50奥运会纪念徽章:羽毛球
日期:2012-08-21 15:55:33奥运会纪念徽章:蹦床
日期:2012-08-21 21:09:51奥运会纪念徽章:篮球
日期:2012-08-24 10:29:11奥运会纪念徽章:体操
日期:2012-09-07 16:40:00
69#
 楼主| 发表于 2011-4-12 16:13 | 只看该作者

回复 #68 rollingpig 的帖子

nice

使用道具 举报

回复
论坛徽章:
131
2006年度最佳技术回答
日期:2007-01-24 12:58:48福特
日期:2013-10-24 13:57:422014年新春福章
日期:2014-02-18 16:41:11马上有车
日期:2014-02-18 16:41:11马上有车
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14马上有对象
日期:2014-02-19 11:55:14马上加薪
日期:2014-02-19 11:55:142013年新春福章
日期:2013-02-25 14:51:24
70#
发表于 2011-4-12 16:17 | 只看该作者
MAX(TST),MIN(TST)
1948,328

使用道具 举报

回复

您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

TOP技术积分榜 社区积分榜 徽章 团队 统计 知识索引树 积分竞拍 文本模式 帮助
  ITPUB首页 | ITPUB论坛 | 数据库技术 | 企业信息化 | 开发技术 | 微软技术 | 软件工程与项目管理 | IBM技术园地 | 行业纵向讨论 | IT招聘 | IT文档
  ChinaUnix | ChinaUnix博客 | ChinaUnix论坛
CopyRight 1999-2011 itpub.net All Right Reserved. 北京盛拓优讯信息技术有限公司版权所有 联系我们 未成年人举报专区 
京ICP备16024965号-8  北京市公安局海淀分局网监中心备案编号:11010802021510 广播电视节目制作经营许可证:编号(京)字第1149号
  
快速回复 返回顶部 返回列表