hdu5037 Frog 贪心

来源:互联网 发布:帽子买什么牌子好 知乎 编辑:程序博客网 时间:2024/04/24 16:34

Frog

Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 754    Accepted Submission(s): 185


Problem Description
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.
 

Input
The 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.
 

Output
For 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
21 10 552 10 336
 

Sample Output
Case #1: 2Case #2: 4
 

Source
2014 ACM/ICPC Asia Regional Beijing Online
题意:青蛙过河,河宽M 米,青蛙每步至多跳L米,已知有N块儿石头,你可以添加无限的石头帮助它过河,青蛙是聪明的,请你选取安放恰当石头使青蛙跳尽可能多的次数;


剩下的看代码解释。
#include<stdio.h>#include<string.h>#include<math.h>#include<string>#include<iostream>#include<algorithm>#include<vector>#include<queue>#include<list>#include<map>#include<set>using namespace std;const int N = 200010;int stone[N];int n, m, l;int work(){    if(l == 1) return m;    int i = 0;    int last = 0, now = 0;    if(stone[i] - now > l ){        now = 1;    }else{        now = stone[i++];     }//第一步    int ans = 1;    while(i < n){        if(now >= m) return ans; //已经走到M         if(now >= stone[i]) { //防止死循环            i++;            continue;        }        int a = now - last; //第 ans步 与第ans - 1步的间距        if(a >= l){ // 间距大于 L ,说明第ans 步是必要的;            ans ++;            last = now;            now = now + 1;// 新迈一步,假设只跳了1个单位,这一步并不一定正确;            continue;        }        int ts = (stone[i] - last) / (l + 1);        if(ts){  // 这样写避免超时 ,用公式计算last, now            ans = ans + ts * 2 - 1;            int tp = last;            last = now + (ts - 1) * (l + 1);            now = tp+ ts * (l + 1);  // 此时now还是小于stone[i] 的,下一次循环再去计算        }else{  // 从last 可以直接跳到 stone[i] 的情况;            int id = i;              while(id < n && stone[id] - last <= l) id ++;            if(id >= n || stone[id] - last > l) id--;            now = stone[id];            i = id + 1;        }    }    return ans;}int main(){//freopen("in.in","r",stdin);    int T;    scanf("%d", &T);    for(int ca = 1; ca <= T; ca++){        scanf("%d%d%d", &n, &m, &l);        for(int i = 0; i < n; i++)            scanf("%d", &stone[i]);        stone[n++] = m;        sort(stone, stone + n);        n = unique(stone , stone + n) - stone;        printf("Case #%d: %d\n",ca ,work());    }return 0;}








0 0
原创粉丝点击