UVA13070 Palm trees in the snow【序列处理】

The mayor of Marbella, a beautiful coastal city of Spain, has received a worrying weather forecast. The 29th of February a great snowfall is expected at the sea level, which is something very unusual. The city hall gardeners have warned him that some palm trees in the promenade could overcome under the weight of the snow, and he is very worried about the deterioration that the promenade could suffer in case many palm trees fall.
    The promenade is one of the most popular touristic places in the city where lots of people meet in the beach bars in the hot summer. The hundreds of palm trees that grow there provide for shadow and freshness. The gardening service has made a big effort so do not miss any palm tree in the promenade. When a palm tree falls down there isn’t enough time for another one to grow before the summer, so in those places with a small amount of palm trees it will not be possible to install beach bars because of the heat.
    The weather service has estimated the weight of the snow falling on the palm trees. And the gardeners have approximated the maximum weight that each palm tree will be able to resist standing. With this information, the mayor wants to know which part of the promenade will be the most affected one; more specifically, the longest strip in which, after the snowfall, a maximum of 5 palm trees will remain standing.
Input
The first line contains the number of proof cases appearing below. Each proof case consists in the following data: in the first line the weight in kilograms of the snow that the weather service has estimated will fall on each palm tree (a natural number); in the following line the number of palm trees in the promenade (bigger or equal than 1 and less or equal than 100,000) and the sequence of weights in kilograms (natural numbers) that each palm tree can resist, from left to right.
Output
For each proof case write in a different line the length of the most affected strip, that is, the longest which contains at most 5 palm trees standing.
Sample Input
3
30
10
10 30 50 20 40 60 30 40 50 36
40
10
10 30 50 20 40 20 10 10 20 36
20
3
40 10 15
Sample Output
7
10
3

问题链接UVA13070 Palm trees in the snow
问题简述:给定W和若干树高Hi,条件树高Hi>=W,挑选一个最多5棵树满足该条件的区间,求该区间最长为多少?
问题分析
一种做法是,先把Hi>=W的树标记出来,h[i]=1则表示这棵树被推到,h[i]=0则表示不满足条件。然后,再进行计算处理。算法复杂度是O(n2)。
另外一种做法是,先把Hi>=W的树的位置存储在数组a[]中,再进行计算处理,其算法复杂度是O(n)。看程序代码不解释。
程序说明:(略)
参考链接:(略)
题记:(略)

AC的C++语言程序如下:

/* UVA13070 Palm trees in the snow */

#include <bits/stdc++.h>

using namespace std;

const int N = 100000 + 2;
int a[N];

int main()
{
    int t, w, n, h;
    scanf("%d", &t);
    while (t--) {
        scanf("%d%d", &w, &n);
        int cnt = 0;
        a[cnt++] = -1;
        for (int i = 0; i < n; i++) {
            scanf("%d", &h);
            if (h >= w) a[cnt++] = i;
        }
        a[cnt] = n;

        int maxl = 0;
        for (int i = 1; i < cnt; i++) {
            int t = a[min(i + 5, cnt)];
            maxl = max(maxl, t - a[i - 1] - 1);
        }
        if (cnt == 1) maxl = n;

        printf("%d\n", maxl);
    }

    return 0;
}

AC的C++语言程序如下:

/* UVA13070 Palm trees in the snow */

#include <bits/stdc++.h>

using namespace std;

const int N = 100000;
int h[N];

int main()
{
    int t, w, n, a;
    scanf("%d", &t);
    while (t--) {
        scanf("%d%d", &w, &n);
        for (int i = 0; i < n; i++) {
            scanf("%d", &a);
            h[i] = a >= w;
        }

        int j = 0, cnt = 0, maxl = 0;
        for (int i = 0; i < n; i++) {
            while (j < n && cnt + h[j] <= 5)
                cnt += h[j], j++;
            maxl = max(maxl, j - i);
            if (i < j) cnt -= h[i];
        }

        printf("%d\n", maxl);
    }

    return 0;
}
上一篇:圣诞桌面装饰软件Xmas snow for Mac


下一篇:我在Python的艳阳里,大雪纷飞