The old Padawan(尺取法 + 技巧模拟)

1998. The old Padawan

Time limit: 0.5 second
Memory limit: 64 MB
Yoda: Use the Force. Yes. Now, the stone. Feel it. Concentrate!
Luke Skywalker is having exhausting practice at a God-forsaken planet Dagoba. One of his main difficulties is navigating cumbersome objects using the Power. Luke’s task is to hold several stones in the air simultaneously. It takes complete concentration and attentiveness but the fellow keeps getting distracted.
Luke chose a certain order of stones and he lifts them, one by one, strictly following the order. Each second Luke raises a stone in the air. However, if he gets distracted during this second, he cannot lift the stone. Moreover, he drops some stones he had picked before. The stones fall in the order that is reverse to the order they were raised. They fall until the total weight of the fallen stones exceeds  k kilograms or there are no more stones to fall down.
The task is considered complete at the moment when Luke gets all of the stones in the air. Luke is good at divination and he can foresee all moments he will get distracted at. Now he wants to understand how much time he is going to need to complete the exercise and move on.

Input

The first line contains three integers:  n is the total number of stones,  m is the number of moments when Luke gets distracted and  k (1 ≤  nm ≤ 10 5, 1 ≤  k ≤ 10 9). Next  n lines contain the stones’ weights  w i (in kilograms) in the order Luke is going to raise them (1 ≤ w i ≤ 10 4). Next  m lines contain moments  t i, when Luke gets distracted by some events (1 ≤  t i ≤ 10 9t i <  t i+1).

Output

Print a single integer — the number of seconds young Skywalker needs to complete the exercise.

Sample

input output
5 1 4
1
2
3
4
5
4
8

Hint

In the first three seconds Luke raises stones that weight 1, 2 and 3 kilograms. On the fourth second he gets distracted and drops stones that weight 2 and 3 kilograms. During the next four seconds he raises all the four stones off the ground and finishes the task.

      题意:

      给出 n(1 ~ 10 ^ 5),m(1 ~ 10 ^ 5),k(1 ~ 10 ^ 9),代表给出 N 个球,后有 M 个停顿时间,每秒钟都会往上抛一个球,如果遇到的是停顿的时间,则不会往上抛球,但是会有球下降下来,直到下降下来的球的总重量大于 k,或者全部都掉下来了才继续抛球。输出过多少时间才会把所有的球给跑到天空中。

 

       思路:

       尺取法预处理当每个球不抛的时候会退回哪一个位置。后循环停顿的时间,维护该时间所需要抛的球是哪个,如果该停顿时间距离上一次停顿时间依然没有抛完所有的球的话,则退回刚刚预处理所需要抛球的位置,不断这样判断,直到判断到能完全抛完所有球为止。如果所有的停顿时间都停顿完了依然没有抛完的话,则最后的总时间还要加上剩下球的个数。

       比赛的时候很烦躁,明明一开始就会做,但是被边界问题处理得很烦,后来也没有心思去 debug 了,以后一定要改进,切忌不能烦躁不能放弃,细心处理好边界问题。

 

       AC:

#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

const int MAX = 100005;

int num[MAX], stop[MAX], ans[MAX];

int main() {
        int n, m, k;

        scanf("%d%d%d", &n, &m, &k);

        for (int i = 1; i <= n; ++i) {
                scanf("%d", &num[i]);
        }

        int sum = 0, from = 1, t = 1;
        while (sum <= k && t <= n) {
                ans[t] = from;
                sum += num[t];
                ++t;
        }

        for (t; t <= n; ++t) {
                while (sum - num[from] > k && from < t - 1) {
                        sum -= num[from];
                        ++from;
                }

                ans[t] = from;
                sum += num[t];
        }

        stop[0] = 0;
        for (int i = 1; i <= m; ++i)
                scanf("%d", &stop[i]);

        int sum_time = 0, now = 1;
        for (int i = 1; i <= m; ++i) {
                int need = n - now + 1;

                if (stop[i] - stop[i - 1] > need) {
                        sum_time = stop[i - 1] + need;
                        now = n;
                        break;
                } else {
                        now = now + stop[i] - stop[i - 1] - 1;
                        now = ans[now];
                        sum_time = stop[i];
                }
        }

        if (now != n) printf("%d\n", sum_time + (n - now + 1));
        else printf("%d\n", sum_time);

        return 0;
}

 

你可能感兴趣的:(技巧)