Codeforces 817D Imbalanced Array【思维】好题!

本文介绍了一种计算数组中所有连续子序列的最大值与最小值之差的方法,并提供了一个高效的算法实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

D. Imbalanced Array
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given an array a consisting of n elements. The imbalance value of some subsegment of this array is the difference between the maximum and minimum element from this segment. The imbalance value of the array is the sum of imbalance values of all subsegments of this array.

For example, the imbalance value of array [1, 4, 1] is 9, because there are 6 different subsegments of this array:

  • [1] (from index 1 to index 1), imbalance value is 0;
  • [1, 4] (from index 1 to index 2), imbalance value is 3;
  • [1, 4, 1] (from index 1 to index 3), imbalance value is 3;
  • [4] (from index 2 to index 2), imbalance value is 0;
  • [4, 1] (from index 2 to index 3), imbalance value is 3;
  • [1] (from index 3 to index 3), imbalance value is 0;

You have to determine the imbalance value of the array a.

Input

The first line contains one integer n (1 ≤ n ≤ 106) — size of the array a.

The second line contains n integers a1, a2... an (1 ≤ ai ≤ 106) — elements of the array.

Output

Print one integer — the imbalance value of a.

Example
Input
3
1 4 1
Output
9


题目大意:

让你计算所有连续子序列的最大值-最小值的和。

思路:


很显然,如果对于一个数Ai来讲,如果其有贡献的价值,要么是-Ai作为最小值,要么是Ai作为最大值。
那么Ans=ΣAi*maxn-Ai*minn.
这里maxn表示Ai作为最大值出现的次数,minn表示Ai作为最小值出现的次数。

那么考虑如何计算这个maxn和minn.

我们设定L【i】表示Ai作为最大值时,左边可以延展到的位子,R【i】表示Ai作为最大值时,右边可以延展到的位子。
那么对于Ai来讲,其maxn=(i-L【i】+1)-(R【i】-i+1);

L【i】以及R【i】都可以O(n)维护,所以这里不必要担心时间复杂度的问题。
那么求minn的过程同理相反。

Ac代码:


#include<stdio.h>
#include<string.h>
#include<math.h>
#include<iostream>
using namespace std;
#define ll __int64
int a[1000005];
int l[1000005];
int r[1000005];
int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        ll output=0;
        for(int i=1;i<=n;i++)scanf("%d",&a[i]);
        for(int i=1;i<=n;i++)l[i]=r[i]=i;
        for(int i=2;i<=n;i++)
        {
            int now=i;
            while(now>1&&a[i]>=a[now-1])now=l[now-1];
            l[i]=now;
        }
        for(int i=n-1;i>=1;i--)
        {
            int now=i;
            while(now<n&&a[i]>a[now+1])now=r[now+1];
            r[i]=now;
        }
        for(int i=1;i<=n;i++)output+=(ll)a[i]*(ll)(i-l[i]+1)*(ll)(r[i]-i+1);
        for(int i=1;i<=n;i++)l[i]=r[i]=i;
        for(int i=2;i<=n;i++)
        {
            int now=i;
            while(now>1&&a[i]<=a[now-1])now=l[now-1];
            l[i]=now;
        }
        for(int i=n-1;i>=1;i--)
        {
            int now=i;
            while(now<n&&a[i]<a[now+1])now=r[now+1];
            r[i]=now;
        }
        for(int i=1;i<=n;i++)output-=(ll)a[i]*(ll)(i-l[i]+1)*(ll)(r[i]-i+1);
        printf("%I64d\n",output);
    }
}

 
 
### Codeforces 思维思路和技巧 #### 预处理的重要性 对于许多竞赛编程问而言,预处理能够显著提高效率并简化后续操作。通过提前计算某些固定的数据结构或模式匹配表,可以在实际求解过程中节省大量时间。例如,在字符串处理类目中预先构建哈希表来加速查找过程[^1]。 #### 算法优化策略 针对特定类型的输入数据设计高效的解决方案至关重要。当面对大规模测试案例时,简单的暴力破解往往无法满足时限要求;此时则需考虑更高级别的算法改进措施,比如动态规划、贪心算法或是图论中的最短路径算法等。此外,合理利用空间换取时间也是一种常见的优化手段[^2]。 #### STL库的应用价值 C++标准模板库提供了丰富的容器类型(vector, deque)、关联式容器(set,map)以及各种迭代器支持,极大地便利了程序开发工作。熟练掌握这些工具不仅有助于快速实现功能模块,还能有效减少代码量从而降低出错几率。特别是在涉及频繁插入删除场景下,优先选用双向队列deque而非单向链表list可获得更好的性能表现。 ```cpp #include <iostream> #include <deque> using namespace std; int main(){ deque<int> dq; // 向两端添加元素 dq.push_back(5); dq.push_front(3); cout << "Front element is: " << dq.front() << endl; cout << "Back element is : " << dq.back() << endl; return 0; } ``` #### 实际应用实例分析 以一道具体目为例:给定一系列查询指令,分别表示往左端/右端插入数值或者是询问某个指定位置到边界之间的最小距离。此目的关键在于如何高效地追踪最新状态而无需重复更新整个数组。采用双指针技术配合静态分配的一维数组即可轻松解决上述需求,同时保证O(n)级别的总运行成本[^4]。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值