【HDU 5037】Frog

传送门戳这里

题目描述

Once upon a time, there is a little frog called Matt. One day, he came to a river.

The river could be considered as an axis.Matt is standing on the left bank now (at position 0). He wants to cross the river, reach the right bank (at position M). But Matt could only jump for at most L units, for example from 0 to L.

As the God of Nature, you must save this poor frog.There are N rocks lying in the river initially. The size of the rock is negligible. So it can be indicated by a point in the axis. Matt can jump to or from a rock as well as the bank.

You don't want to make the things that easy. So you will put some new rocks into the river such that Matt could jump over the river in maximal steps.And you don't care the number of rocks you add since you are the God.

Note that Matt is so clever that he always choose the optimal way after you put down all the rocks.

解题思路

代码

1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 using namespace std;
 6 int t,n,m,l;
 7 int a[200050];
 8 inline void read(int &x){
 9     x=0; register char ch=getchar();
10     while(ch<'0'||ch>'9')ch=getchar();
11     while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
12 }
13 int main(){
14     read(t);
15     for(register int frog=1;frog<=t;frog++){
16         read(n),read(m),read(l);
17         for(register int i=1;i<=n;i++)read(a[i]);
18         sort(a+1,a+n+1);
19         a[0]=0,a[n+1]=m;
20         register int now=0,pre=-19260817,num=1,ans=0;
21         while(now<m){
22             while(num<=n+1&&a[num]-now<=l){
23                 num++;
24             }
25             if(a[num-1]>now&&a[num-1]<=now+l){
26                 pre=now;
27                 now=a[num-1];
28                 ans++;
29             }
30             else {
31                 int fuck=(a[num]-now)/(l+1)-1;
32                 if(fuck<=0){
33                     int temp=pre;
34                     pre=now;
35                     now=max(temp+l+1,now+1);
36                     ans++;
37                 }
38                 else if(fuck>0){
39                     int temp=max(now+1,pre+1+l);
40                     now+=fuck*(l+1);
41                     pre=temp+(fuck-1)*(l+1);
42                     ans+=fuck*2;
43                 }
44             }
45         }
46         printf("Case #%d: %d\n",frog,ans);
47     }
48 }

相关推荐