zoukankan      html  css  js  c++  java
  • HDU5037 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.

    InputThe first line contains only one integer T, which indicates the number of test cases. 

    For each test case, the first line contains N, M, L (0<=N<=2*10^5,1<=M<=10^9, 1<=L<=10^9). 

    And in the following N lines, each line contains one integer within (0, M) indicating the position of rock.OutputFor each test case, just output one line “Case #x: y", where x is the case number (starting from 1) and y is the maximal number of steps Matt should jump.Sample Input

    2
    1 10 5
    5
    2 10 3
    3
    6

    Sample Output

    Case #1: 2
    Case #2: 4

    打错题号就进来了……看了看还挺有挑战性的

    贪心

    青蛙会选择最优策略(有丰富的人生经验?),最远能跳L,为了最大化它跳的次数,我们要尽量在当前位置x+1和x+L+1的位置放石头,这样它就不得不跳两次。

    计算是要统计上次青蛙跳跃距离/(L+1)的余数last,和当前位置距离下一块石头的距离x综合考虑,若last+x>L+1,还可以多放块石头让它跳。

    http://blog.csdn.net/u014569598/article/details/39471913

    ↑这里有张图比较直观

     1 /*by SilverN*/
     2 #include<iostream>
     3 #include<algorithm>
     4 #include<cstring>
     5 #include<cstdio>
     6 #include<cmath>
     7 using namespace std;
     8 const int mxn=200010;
     9 int read(){
    10     int x=0,f=1;char ch=getchar();
    11     while(ch<'0' || ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    12     while(ch>='0' && ch<='9'){x=x*10+ch-'0';ch=getchar();}
    13     return x*f;
    14 }
    15 int n,m,L;
    16 int a[mxn];
    17 int main(){
    18     int i,j;
    19     int T=read();
    20     int cas=0;
    21     while(T--){
    22         n=read();m=read();L=read();
    23         for(i=1;i<=n;i++)a[i]=read();
    24         a[++n]=m;
    25         sort(a+1,a+n+1);
    26         int left=L;
    27         int ans=0;
    28         for(i=1;i<=n;i++){
    29             int mod=(a[i]-a[i-1])%(L+1);
    30             int dis=(a[i]-a[i-1])/(L+1);
    31             if(left+mod<L+1){
    32                 left=left+mod;
    33                 ans+=dis*2;
    34             }
    35             else{
    36                 left=mod;
    37                 ans+=dis*2+1;
    38             }
    39         }
    40         printf("Case #%d: %d
    ",++cas,ans);
    41     }
    42     return 0;
    43 }
  • 相关阅读:
    mysql中in 做条件匹配 带逗号的字符串 数据异常解决
    [LeetCode] Kth Largest Element in a Stream 数据流中的第K大的元素
    [LeetCode] Binary Search 二分搜索法
    [LeetCode] Search in a Sorted Array of Unknown Size 在未知大小的有序数组中搜索
    [LeetCode] Insert into a Binary Search Tree 二叉搜索树中插入结点
    [LeetCode] Search in a Binary Search Tree 二叉搜索树中搜索
    [LeetCode] Design Circular Deque 设计环形双向队列
    [LeetCode] Design Circular Queue 设计环形队列
    [LeetCode] N-ary Tree Preorder Traversal N叉树的前序遍历
    [LeetCode] 589. N-ary Tree Postorder Traversal N叉树的后序遍历
  • 原文地址:https://www.cnblogs.com/SilverNebula/p/6528998.html
Copyright © 2011-2022 走看看