zoukankan      html  css  js  c++  java
  • 【NOIP2016】蚯蚓(单调队列)

    题意:

    思路:

    我们发现,
    对于任意两次切割i和j,i<j,
    在进行完第j次切割后,第i次切割的u/v部分一定大于等于第j次切割的u/v部分,
    第i次的1-u/v部分也一定大于等于第j次的1-u/v部分
    证明很显然,
    假设在第i次切割的时候,切割的蚯蚓长度为x,
    第j次的时候为y+(i-j)q,
    那么有x>=y,px+(i-j)q显然大于等于p(y+(i-j)q)
    那么我们维护三个队列,
    刚开始的时候把所有蚯蚓排序后推到第一个队列里,
    然后每次取三个队头里最长的,
    把他弹出来,再把切开的两半分别推入第二和第三个队列即可

    From http://blog.csdn.net/neither_nor/article/details/53300255

    线性复杂度还是被卡一个点 也是醉了

     1 var q:array[1..3,1..8000000]of longint;
     2     t,w:array[1..3,0..0]of longint;
     3     a:array[1..200000]of longint;
     4     n,m,q1,u,v,t1,i:longint;
     5     now,s1,s2:longint;
     6 
     7 procedure swap(var x,y:longint);
     8 var t:longint;
     9 begin
    10  t:=x; x:=y; y:=t;
    11 end;
    12 
    13 procedure qsort(l,r:longint);
    14 var i,j,mid:longint;
    15 begin
    16  i:=l; j:=r; mid:=a[(l+r)>>1];
    17  repeat
    18   while mid<a[i] do inc(i);
    19   while mid>a[j] do dec(j);
    20   if i<=j then
    21   begin
    22    swap(a[i],a[j]);
    23    inc(i); dec(j);
    24   end;
    25  until i>j;
    26  if l<j then qsort(l,j);
    27  if i<r then qsort(i,r);
    28 end;
    29 
    30 function h(x:longint):longint;
    31 begin
    32  if t[x,0]>w[x,0] then exit(-maxlongint);
    33  exit(q[x,t[x,0]]);
    34 end;
    35 
    36 begin
    37  assign(input,'bzoj4721.in'); reset(input);
    38  assign(output,'bzoj4721.out'); rewrite(output);
    39  readln(n,m,q1,u,v,t1);
    40  for i:=1 to n do read(a[i]);
    41  qsort(1,n);
    42  for i:=1 to n do q[1,i]:=a[i];
    43  t[1,0]:=1; t[2,0]:=1; t[3,0]:=1;
    44  w[1,0]:=n; w[2,0]:=0; w[3,0]:=0;
    45  for i:=1 to m do
    46  begin
    47   if (h(1)<>-maxlongint)and(h(1)>=h(2))and(h(1)>=h(3)) then
    48   begin
    49    now:=h(1); inc(t[1,0]);
    50   end
    51    else if (h(2)<>-maxlongint)and(h(2)>=h(1))and(h(2)>=h(3)) then
    52    begin
    53     now:=h(2); inc(t[2,0]);
    54    end
    55     else
    56     begin
    57      now:=h(3); inc(t[3,0]);
    58     end;
    59   now:=now+(i-1)*q1;
    60   if i mod t1=0 then
    61   begin
    62    write(now);
    63    if i+t1<=m then write(' ');
    64   end;
    65   s1:=int64(now)*u div v;
    66   s2:=int64(now)-s1;
    67   inc(w[2,0]); q[2,w[2,0]]:=s1-i*q1;
    68   inc(w[3,0]); q[3,w[3,0]]:=s2-i*q1;
    69  end;
    70  writeln;
    71  for i:=1 to n+m do
    72  begin
    73   if (h(1)<>-maxlongint)and(h(1)>=h(2))and(h(1)>=h(3)) then
    74   begin
    75    now:=h(1); inc(t[1,0]);
    76   end
    77    else if (h(2)<>-maxlongint)and(h(2)>=h(1))and(h(2)>=h(3)) then
    78    begin
    79     now:=h(2); inc(t[2,0]);
    80    end
    81     else
    82     begin
    83      now:=h(3); inc(t[3,0]);
    84     end;
    85   now:=now+m*q1;
    86   if i mod t1=0 then
    87   begin
    88    write(now);
    89    if i+t1<=n+m then write(' ');
    90   end;
    91  end;
    92 
    93 
    94 
    95  close(input);
    96  close(output);
    97 end.
  • 相关阅读:
    数据库乐观锁应用
    maven 引入本地jar
    GTS 分布式事务
    redis 做冥等
    服务器 启动命令
    pgAdmin4的应用
    PostgreSQL 分区
    压测工具
    BigDecimal 比较大小
    移动端px转化为rem
  • 原文地址:https://www.cnblogs.com/myx12345/p/7419745.html
Copyright © 2011-2022 走看看