第十四届华中科技大学程序设计竞赛 I 题 Neat Tree

Neat Tree

【题目链接】


题目

链接:https://www.nowcoder.com/acm/contest/106/I
来源:牛客网

题目描述
It’s universally acknowledged that there’re innumerable trees in the campus of HUST.

There is a row of trees along the East-9 Road which consists of N trees. Now that we know the height of each tree, the gardeners of HUST want to know the super-neatness of this row of trees. The neatness of a sequence of trees is defined as the difference between the maximum height and minimum height in this sequence. The super-neatness of this sequence of trees is defined as the sum of neatness of all continous subsequences of the trees.

Multiple cases please process until the end of input. There are at most 100 test cases.

输入描述:

For each case:
The first line contains an integer N represents the number of
trees.
The next line n positive integers followed, in which hi represent the
height of the tree i.

输出描述:

For each test case, output a integer in a single line which represents the super-neatness of the row
of trees.

示例1
输入

3
1 3 1
4
1 2 3 4

输出

6
10

说明

As for the first case, the super-neatness of the row of trees [1, 3, 1] is 6, because there are 6 different subsequence of this row of trees: [1] (from index 0 to index 0), neatness is 0; [1, 3] (from index 0 to index 1), neatness is 2; [1, 3, 1] (from index 0 to index 2), neatness is 2; [3] (from index 1 to index 1), neatness is 0; [3, 1] (from index 1 to index 2), neatness is 2; [1] (from index 2 to index 2), neatness is 0.


解题思路

题目不太难,但是很繁琐,现场写的话可能一个小时都过不了,132发提交只过了25个。
题目大意就是定义一个子区间的值是该区间内的(最大值-最小值),求所给序列的所有子区间的和。
很明显要通过算贡献来求,就需要预处理出每个点作为最大值(最小值)的最长区间。
l[i][0] //以下标为i的点为最小值的区间最左边点的下标
l[i][1] //以下标为i的点为最大值的区间最右边点的下标
r[i][0] //以下标为i的点为最小值的区间最左边点的下标
r[i][1] //以下标为i的点为最大值的区间最右边点的下标
然后正向遍历求l[i][0]和l[i][1],反向遍历求r[i][0]和r[i][1],这个转移方程应该都能推出来。
预处理之后对于每一个点作为最大值(最小值)的情况,假设左边有x个点,右边有y个点(相当于枚举子区间左右端点),贡献就是(x+1)*(y+1)。
然后还有一个点是对于a[i]==a[j]==a[k]之类的情况,可以预处理一下每个点上一个和它大小相同的位置,如果包含在它的左区间内就把左边的点去掉(相当于已经枚举过的左端点)。


AC代码

#include <bits/stdc++.h>
#define LL long long
using namespace std;

const int maxn=1e6+7;
const int INF=0x3f3f3f3f;

int l[maxn][2],r[maxn][2],a[maxn];
int h[maxn],pre[maxn];
int main()
{
    int n,i,j,k,ll,rr;
    while(scanf("%d",&n)!=EOF)
    {
        LL ans=0,xx;
        for(i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
        }
        l[1][0]=l[1][1]=1;
        for(i=2;i<=n;i++)
        {
            if(a[i-1]<a[i])
            {
                l[i][0]=i;
                j=l[i-1][1];
                while(a[j]<=a[i]&&j>=1)
                {
                    j--;
                }
                l[i][1]=j+1;

            }
            else if(a[i-1]>a[i])
            {
                j=l[i-1][0];
                while(a[j]>=a[i]&&j>=1)
                {
                    j--;
                }
                l[i][0]=j+1;
                l[i][1]=i;
            }
            else if(a[i-1]==a[i])
            {
                l[i][0]=l[i-1][0];
                l[i][1]=l[i-1][1];
            }
        }
        r[n][0]=r[n][1]=n;
        for(i=n-1;i>=1;i--)
        {
            if(a[i]<a[i+1])
            {
                j=r[i+1][0];
                while(a[j]>=a[i]&&j<=n)
                {
                    j++;
                }
                r[i][0]=j-1;
                r[i][1]=i;
            }
            else if(a[i]>a[i+1])
            {
                r[i][0]=i;
                j=r[i+1][1];
                while(a[j]<=a[i]&&j<=n)
                {
                    j++;
                }
                r[i][1]=j-1;
            }
            else
            {
                r[i][0]=r[i+1][0];
                r[i][1]=r[i+1][1];
            }
        }
//        for(i=1;i<=n;i++)
//        {
//            printf("(%d,%d) ",l[i][0],r[i][0]);
//        }printf("\n");
//        for(i=1;i<=n;i++)
//        {
//            printf("(%d,%d) ",l[i][1],r[i][1]);
//        }printf("\n");
        for(i=1;i<=n;i++)
        {
            pre[i]=h[a[i]];
            h[a[i]]=i;
            //printf("pre[%d]=%d ",i,pre[i]);
        }//printf("\n");
        for(i=1;i<=n;i++)
        {
            if(pre[i]==0)
            {
                ll=l[i][1];
            }
            else
            {
                ll=max(l[i][1],pre[i]+1);
            }
            ll=i-ll;
            rr=r[i][1]-i;
            //printf("ll=%d rr=%d ",ll,rr);
            xx=(LL)(ll+1)*(rr+1);
            if(pre[i]==0)
            {
                ll=l[i][0];
            }
            else
            {
                ll=max(l[i][0],pre[i]+1);
            }
            ll=i-ll;
            rr=r[i][0]-i;
            //printf("ll=%d rr=%d ",ll,rr);
            xx-=(LL)(ll+1)*(rr+1);
            xx*=a[i];
            ans+=xx;
            //printf("ans=%lld\n",ans);
        }
        printf("%lld\n",ans);
        for(i=1;i<=n;i++)
        {
            h[a[i]]=0;
        }

    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值