Ch round #45 energy release

Source: Internet
Author: User
Energy Release Ch round #45-Alan has some traps IIIDescription

Alan gets an ore consisting of an energy crystal. for each energy crystal in the ore, if a chemical substance is used to stimulate an energy crystal, it can release energy.

The energy release strength is related to the energy value of the crystal and the position of the crystal.

Alan made the following definitions to facilitate research.

Energy set: a collection of the first energy crystal in an ore to the first energy crystal (including and.

Energy Storage point: for the Energy crystals and, if any, an ore is called an energy storage point.

Energy Release point: in an energy concentration, if an energy crystal exists, all the energy crystals except it are its energy storage points, the energy crystal is the energy point of the energy set.

Energy Release strength: For an energy release point in an energy concentration, to stimulate this energy crystal, other energy crystals in the energy concentration will release energy. The energy release intensity of this energy set is equal to the maximum difference between the energy values of other energy crystals in this energy set and the energy values of the energy release points. The energy release strength of the first energy crystal is the maximum energy release strength of all energy sets.

Alan will give the number of Energy crystals in the ore and the energy value of each energy crystal, asking you to find the energy release strength of each energy crystal.

Input Format

The first line is an integer that represents the number of Energy crystals in the ore.

The second line is an integer that represents the energy value of the first energy crystal.

Output Format

Contains only one integer. The first integer indicates the energy release strength of the first energy crystal.

Sample Input
53 2 1 2 3
Sample output
0 1 2 1 0
Data scope and conventions

  • For 20% of data :.
  • For 60% of data :.
  • For 100% of data :.
  • Reminder: The data volume of this question is large. Please try your best to use input/output optimization.

In the test room, I wrote a monotonous queue of O (N) (which is actually equivalent to a monotonous stack ...) + O (nlogn) rmq
The result is only half of the result.
Later, I realized that if the data size reaches more than one million, I would have to use the O (n) algorithm. I am still too young...
I used a monotonous queue when I was preparing to perform the noi2005 magnificent waltz today. I suddenly had an idea about this question and I would like to do it again.

This time, my algorithm should be O (n... I think so...

In fact, considering that if the element on the left of a [I] is smaller than a [J], then the scope of J's jurisdiction I can also be (J's initial value is i-1)

Therefore, we can directly jump to the left of J's Jurisdiction Scope, that is, j = L [J]-1, and use it to update f [I] at the same time.

When the last hop fails, J + 1 is L [I]

The right side is similar...

I don't know how complex it is? How to estimate? Please advise

Self-testing times out slightly, and the last few points are about 1.2s.


1. It takes 22 s to finish all the data in the test room.

 1 var  i,j,n,h,t:longint; 2      f:array[0..2000000+10,0..20] of longint; 3      a,q,l,r:array[0..2000000+10] of longint; 4   procedure init; 5    begin 6      readln(n); 7      for i:=1 to n do read(a[i]); 8    end; 9  procedure queue;10    begin11      a[0]:=-2100000000;a[n+1]:=a[0];12      fillchar(q,sizeof(q),0);13      h:=0;t:=0;14      for i:=1 to n+1 do15       begin16         while (h<t) and (a[i]<a[q[t]]) do17            begin18              r[q[t]]:=i-1;19              dec(t);20            end;21         inc(t);q[t]:=i;22       end;23      fillchar(q,sizeof(q),0);24      h:=0;t:=0;25      for i:=n downto 0 do26        begin27          while (h<t) and (a[i]<a[q[t]]) do28            begin29              l[q[t]]:=i+1;30              dec(t);31            end;32          inc(t);q[t]:=i;33        end;34      end;35 function max(x,y:longint):longint;36  begin37    if x>y then exit(x) else exit(y);38  end;39 40 procedure rmq;41   begin42     fillchar(f,sizeof(f),127);43     for i:=1 to n do f[i,0]:=a[i];44     for i:=1 to 22 do45      begin46        if (1<<i)>n then break;47        for j:=1 to n-(1<<i)+1 do48         f[j,i]:=max(f[j,i-1],f[j+1<<(i-1),i-1]);49      end;50   end;51 function query(x,y:longint):int64;52  var k:longint;53  begin54   k:=trunc(ln(y-x+1.0)/ln(2.0));55   exit(max(f[x,k],f[y-(1<<k)+1,k]));56  end;57 58 procedure main;59   begin60     queue;61     rmq;62   //  for i:=1 to n do writeln(l[i],‘ ‘,r[i]);63     for i:=1 to n do64       write(query(l[i],r[i])-a[i],‘ ‘);65   end;66 begin67   assign(input,‘input.txt‘);assign(output,‘output.txt‘);68   reset(input);rewrite(output);69   init;70   main;71   close(input);close(output);72 end.73                                    
View code

2.20140806 it takes 6 s to run all the data

 1 {$inline on} 2 const maxn=2000000+100; 3 var l,r,a,f:array[0..maxn] of longint; 4     i,n,j:longint; 5     function max(x,y:longint):longint;inline; 6      begin 7        if x>y then exit(x) else exit(y); 8      end; 9 10 procedure init;11  begin12    readln(n);13    for i:=1 to n do begin read(a[i]);f[i]:=a[i];end;14  end;15 procedure main;16  begin17    a[0]:=-maxlongint;a[n+1]:=-maxlongint;18    l[1]:=1;19    for i:=2 to n do20     begin21     j:=i-1;22     while a[i]<=a[j] do23      begin24      if f[j]>f[i] then f[i]:=f[j];25      j:=l[j]-1;26      end;27     l[i]:=j+1;28     end;29    r[n]:=n;30    for i:=n-1 downto 1 do31     begin32     j:=i+1;33     while a[i]<=a[j] do34      begin35      if f[j]>f[i] then f[i]:=f[j];36      j:=r[j]+1;37      end;38     r[i]:=j-1;39     end;40    for i:=1 to n-1 do write(f[i]-a[i],‘ ‘);writeln(f[n]-a[n]);41   end;42 begin43   assign(input,‘input.txt‘);assign(output,‘output.txt‘);44   reset(input);rewrite(output);45   init;46   main;47   close(input);close(output);48 end.    
View code


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.