Cf101e candies and stones

Source: Internet
Author: User

[Description]

Little Gerald and his coach Mike play an interesting game. At the beginning of the game there is a pile consistingNCandies
And a pile consistingMStones.
Gerald and Mike move in turns, Mike goes first. During his move Mike checks how to adjust candies and stones Gerald has eaten. Let Gerald eatACandies
AndBStones.
Then Mike awards GeraldF(A, Bytes,B) Prize
Points. gerald during his move either eats a candy from the pile of candies or a stone from the pile of stones. as Mike sees that Gerald has eaten everything apart one candy and one stone, he awards points for the last time and the game ends. gerald is not
Allowed to eat all the candies, and he is not allowed to eat all the stones too. tell Gerald how to play to get the largest possible number of points: it is required to find one of the possible optimal playing strategies for Gerald.

[Translation]

A and B play games, with N piles of stones and M piles of candy. B moves first.

Mr. A's operations take away a pile of stones or candy.

Small B's operation. If I heap stones and J heap candy are removed in this round, the score is added (XI + YJ) % P.

Requirement: until all are obtained, the score is the largest, and the solution is output (from Tian cong)

[Time limit] 15 s

[Null limit] 45 MB

[Question]

The dp of N ^ 2 should all know. The problem is how to output the scheme in the case of a card space.

It is imperative to use a rolling array, but how to use a rolling array to output a scheme?

One of the most bare ideas is: the first DP to FN m-1, the second re DP to FN + m-2, the output scheme, and then continue to do n times DP, You can output all the solutions.

Obviously, it is definitely a good idea to do so, but it provides us with a good idea.

In order to achieve the balance between time and space, we divide the DP equation by SQRT (N), remember all States every SQRT (n), and then output the scheme from the back to the front.

SQRT (n) DP is performed in the output scheme. Each DP uses an SQRT (n) * m array to remember the status. This solution can be implemented.

Because each State is counted only twice, the total time complexity is O (n * n), and the space complexity is O (SQRT (n) * m ), because I wrote Fi, J indicates that I have J candy, so n = 40000, It is very slow to run (with various switches 10 s, without switching 14 S ).
Because the first score block is too large at the beginning, and the shot data is too small, the results are kept unchanged and the data is handed in to WA.

Tourist has a very Nb method: fi, J indicates taking I stone and J candy. in this way, the space is much less than that of me. so he can add an array to record the status of each SQRT (m) Column !!!!

As a result, the tourist State is divided into several submatrices of SQRT (n) * SQRT (M), recursively returning FN and M, and the DP Solution is implemented for each submatrix, as long as we traverse the O (n) matrix, the total complexity is O (n * n + SQRT (n) * m), almost only the complexity of the bare DP !!!!! Although the complexity of the theory is the same as that of mine, the time for constant explosion is also 1/3.

My code:

program NoName;const skip=200;type        int=longint;        arr=array[0..20000]of int;        point=^arr;var        i,j:longint;        tot,k,m,n,max,min,now:int;s,p:int;        f,g,wx,wy:arr;        prev,next,temp:point;        list:array[0..201]of arr;        ff:array[0..skip+1]of arr;        pos:array[1..201]of int;        solution:array[0..40000]of char;procedure getans(x:int);var i,j:longint;begin        fillchar(ff,sizeof(ff),0);        ff[0]:=list[x];s:=pos[x+1]-pos[x];        for i:=1 to s do begin                min:=0;max:=m;p:=i+pos[x];                if p<m then max:=p;                if(min<p-n)then min:=p-n;                for j:=min to max do begin                        if(j<>0)and(ff[i-1,j-1]>ff[i-1,j])then ff[i,j]:=ff[i-1,j-1]                                else ff[i,j]:=ff[i-1,j];                        ff[i,j]:=ff[i,j]+(wx[p-j]+wy[j])mod k;                end;        end;        for i:=pos[x+1]-1 downto pos[x] do begin                s:=i-pos[x];                if(now=0)or(ff[s,now]>ff[s,now-1])then begin                        solution[i]:='C';                end else begin                        solution[i]:='S';dec(now);                end;        end;end;procedure dp;begin        f[0]:=(wx[0]+wy[0])mod k;        prev:=@f;next:=@g;        tot:=1;list[1]:=f;        for i:=1 to n+m do begin                min:=0;max:=m;                if i<m then max:=i;                if(min<i-n)then min:=i-n;                for j:=min to max do begin                        if(j<>0)and(prev^[j]<prev^[j-1])then next^[j]:=prev^[j-1]                                else next^[j]:=prev^[j];                        next^[j]:=next^[j]+(wx[i-j]+wy[j])mod k;                end;                if(i mod skip=0)and(i<>n+m)then begin                        inc(tot);                        for j:=min to max do list[tot][j]:=next^[j];                        pos[tot]:=i;                end;                temp:=prev;prev:=next;next:=temp;        end;        writeln(prev^[m]);        inc(tot);list[tot]:=prev^;pos[tot]:=n+m;        now:=m;        for i:=tot-1 downto 1 do getans(i);        for i:=0 to n+m-1 do write(solution[i]);end;begin        assign(input,'E.in');reset(input);        assign(output,'E.out');rewrite(output);        read(n,m,k);        dec(n);dec(m);        for i:=0 to n do read(wx[i]);        for i:=0 to m do read(wy[i]);        dp;        close(input);close(output);end.

The program of tourist should go to CF.

By QW

Reprinted please indicate the source

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: info-contact@alibabacloud.com 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.