HDU 5037 Frog (efficient)

Source: Internet
Author: User

Link: HDU 5037 frog

Given N, M, L, indicates that there is a river with a width of M. The frog must jump from 0 to m, and the maximum jump capability of the frog is L, now there are n stones, and now we need to put any number of stones, so that frogs need to jump to m with the maximum number of steps.

Solution: maintain two pointers. Pos indicates the current position of the frog, and MV indicates the farthest position that the frog can jump to in the previous position. Then the length of the period L + 1 allows the frog to jump twice. However, note that the last cycle of each segment must be determined, because it may only take one step.

#include <cstdio>#include <cstring>#include <vector>#include <algorithm>using namespace std;int N, M, L;vector<int> g;void init () {    int x;    g.clear();    scanf("%d%d%d", &N, &M, &L);    for (int i = 0; i < N; i++) {        scanf("%d", &x);        g.push_back(x);    }    g.push_back(0);    g.push_back(M);    sort(g.begin(), g.end());}int solve () {    int ret = 0;    int pos = 0, mv = 0;    while (true) {        int idx = upper_bound(g.begin(), g.end(), pos + L) - g.begin() - 1;        if (g[idx] <= pos) {            int k = (g[idx+1] - pos) / (L + 1);            if (k == 1) {                ret++;                int tmp = mv + 1;                mv = pos + L;                pos = tmp;            } else {                k--;                ret += k * 2;                pos = pos + k * (L + 1);                mv = mv + k * (L + 1);            }        } else {            mv = pos + L;            pos = g[idx];            ret++;        }        if (pos == M)            break;    }    return ret;}int main () {    int cas;    scanf("%d", &cas);    for (int kcas = 1; kcas <= cas; kcas++) {        init();        printf("Case #%d: %d\n", kcas, solve());    }    return 0;}

HDU 5037 Frog (efficient)

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.