Noiptgt2 vijosp1002 crossing the river

Source: Internet
Author: User

Crossing the river
(River. PAS/C/CPP)

 

[Problem description]

There is a pig Bridge On The River. A frog wants to jump from one side of the river to the other side. There are some stones on the bridge, and frogs hate to step on these stones. Because the length of the bridge is a positive integer from the distance that frogs Skip at a time, we can regard the points that frogs may reach on the bridge as a series of points on the number axis: 0, 1 ,......, L (where L is the length of the bridge ). A point whose coordinates are 0 indicates the start point of the bridge, and a point whose coordinates are L indicates the end point of the bridge. The frog starts from the starting point of the bridge and keeps jumping to the ending point. The distance of a hop is any positive integer (including S and T) between S and T ). When a frog jumps to or skips a point whose coordinate is l, even if the frog has jumped out of the bridge.
The question shows the length of the bridge, the distance between frogs and the distance S, T, and the stone position on the bridge. Your task is to determine the number of stones that a frog must step on to cross the river.

[Input file] the first line of the input file river. In has a positive integer L (1 <= L <= 10 ^ 9), indicating the length of the bridge. The second row has three positive integers, S, T, and m, respectively, representing the minimum distance, maximum distance, and the number of stones on the bridge for a frog jump, 1 <= S <= T <= 10, 1 <= m <= 100. The third row has m different positive integers, indicating the position of the M stones on the number axis (data ensures that there are no stones at the start and end points of the bridge ). All adjacent integers are separated by a space.

[Output file]

The output file "river. Out" contains only one integer, indicating the number of stones that the frog must step on when crossing the river.
[Example input]
10
2 3 5
2 3 5 6 7

[Sample output]
2

[Data scale] for 30% of data, L <= 10000;
For all data, L <= 10 ^ 9.

I wrote it a long time ago, directly AC.

The state transition equation is not difficult to deduce, mainly state compression. Because the length of L is 1E, but there are only 100 stones, the distribution of stones is sparse, I directly compress a distance greater than 100 to less than 100. Because when the distance is long enough, you can jump to any position before the next stone (almost ). However, note that S = t cannot be compressed in this way. You just need to process it with extra processing. I have never understood this Ra many times.

 

Because I am too lazy to think about it, set it to 100 directly. what is accurate is that "as long as the minimum public multiple of any two numbers in 1-10 is obtained, and then the maximum is obtained, it can be proved that when the distance between the two stones is greater than it, then, each point greater than it can jump through a combination of the two numbers. Therefore, when the distance between the two stones is greater than this least common multiple, then the distance between them is reduced to the minimum public multiple.
After the path is compressed, you can use DP to find the minimum number of stones. Set f [I] to indicate the minimum number of stones to be placed at position I.
Then f [I] = min (F [I-j] + d [I]) (1 <= I <= L + T) (S <= j <= T), d [I] indicates whether the stone exists at the position I.
The final answer is to find the minimum value between L to L + T ." -- From http://blog.sina.com.cn/s/blog_5ef211b10100ch5z.html

var a:array[0..101] of longint; y:array[0..10000] of boolean; f:array[0..10000] of longint; i,j,n,m,ans,l,s,t,x:longint;procedure qs(l,r:longint);var i,j,x,t:longint;begin i:=l;j:=r;x:=a[(l+r) div 2]; while i<j do begin  while a[i]<x do inc(i);  while a[j]>x do dec(j);  if i<=j then begin   t:=a[i];a[i]:=a[j];a[j]:=t;   inc(i);dec(j);  end; end; if i<r then qs(i,r); if l<j then qs(l,j);end;begin readln(l);readln(s,t,m); for i:=1 to m do read(a[i]); a[m+1]:=l; qs(1,m); if s=t then begin  for i:=1 to m do if a[i] mod s=0 then inc(ans);  writeln(ans); end else begin  for i:=1 to m+1 do if a[i]-a[i-1]>100 then begin    x:=a[i]-a[i-1]-100;     for j:=i to m+1 do a[j]:=a[j]-x;   end;   for i:=1 to m do y[a[i]]:=true;   l:=a[m+1];   for i:=1 to l do f[i]:=1000;   for i:=1 to l do    for j:=s to t do     if i-j>=0 then      if y[i] then begin       if f[i-j]+1<f[i] then f[i]:=f[i-j]+1;      end else if f[i-j]<f[i] then f[i]:=f[i-j];      writeln(f[l]);  end;end.

Noiptgt2 vijosp1002 crossing the river

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.