ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

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

2021-04-15 13:34:31  阅读:209  来源: 互联网

标签:10 cnt int snow trees UVA13070 palm


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;
}

标签:10,cnt,int,snow,trees,UVA13070,palm
来源: https://blog.csdn.net/tigerisland45/article/details/115720838

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有