codeforces 817D Imbalanced Array

本文解析了CodeForces上的一道题目,通过计算每个数作为连续子串最大值和最小值出现的次数来求解所有连续子串的最大值与最小值之差的总和。介绍了使用RMQ和单调栈等算法进行高效计算的方法。

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

题目链接:

http://codeforces.com/contest/817/problem/D


题意:

给出一组数,求所有连续子串的最大值与最小值差的和


题解:

从每个数可作为最大值被计算次数Maxki和最小值计算次数Minki入手,答案就是sum(Maxki*num[i]-Minki*num[i])

那么如何计算Maxki和Minki呢?

首先假设这个数num[i]是连续子串的第一位数,那么我们向右查询到第一个大于等于它的数的下标为Maxridx,则以这个数为第一位数的连续子串中这个数作为最大值被计算了Maxridx-i+1次,同样作为最小值可以采取同样的方法这个数被计算了Minridx-i+1次。这里的查找可以采用二分+RMQ因为最大值和最小值都有单调性。

接下来再从这个数不是连续子串的第一位开始考虑:

因为第i个数前面也可能存在比它大的数,所以可以从第i个数向左查找到第一个大于等于它的数的下标Maxlidx,则这个数作为最大值被计算了(i-Maxlidx+1)*(Maxridx-i+1)次,

作为最小值被计算了(i-Minlidx+1)*(Minridx-i+1)次。这个向左查找最值的方法采用单调栈的方法。

所以最后第i个数对答案的贡献为(i-Maxlidx+1)*(Maxridx-i+1)*num[i]-(i-Minlidx+1)*(Minridx-i+1)*num[i]


#include<bits/stdc++.h>
using namespace std;
#define inf 0x3f3f3f3f
#define ll long long
#define ull unsigned long long
const int mod = 1e4+7;
const ll INF = 2e18;
typedef pair<int, int>P;
const double eps = 1e-6;
const int maxn = 1e6+5;

int n;
int num[maxn];
int minsum[maxn][20];
int maxsum[maxn][20];
void init_RMQ(int n)
{
    for(int i=1;i<=n;i++)
        maxsum[i][0] = minsum[i][0] = num[i];
    int k = log2(1.0*n);
    for(int j=1;j<=k;j++) {
        for(int i=1;i<=n;i++) {
            if(i+(1<<j)-1<=n) {
                maxsum[i][j] = max(maxsum[i][j-1], maxsum[i+(1<<(j-1))][j-1]);
                minsum[i][j] = min(minsum[i][j-1], minsum[i+(1<<(j-1))][j-1]);
            }
        }
    }
}
int getMax(int i,int j)
{
    int k = (int)log2(1.0*(j-i+1));
    return max(maxsum[i][k], maxsum[j-(1<<k)+1][k]);
}
int getMin(int i,int j)
{
    int k = (int)log2(1.0*(j-i+1));
    return min(minsum[i][k], minsum[j-(1<<k)+1][k]);
}
int bsmn(int L)
{
    int l = L+1;
    int r = n;
    int ans = L;
    while(l <= r) {
        int mid = (l+r)>>1;
        if(getMin(L+1, mid) > num[L]) {
            ans = max(ans, mid);
            l = mid + 1;
        }
        else {
            r = mid - 1;
        }
    }
    return ans;
}
int bsmx(int L)
{
    int l = L+1;
    int r = n;
    int ans = L;
    while(l <= r) {
        int mid = (l+r)>>1;
        if(getMax(L+1, mid) < num[L]) {
            ans = max(ans, mid);
            l = mid + 1;
        }
        else {
            r = mid - 1;
        }
    }
    return ans;
}
int main()
{
    cin >> n;
    for(int i=1; i<=n; i++) {
        scanf("%d", &num[i]);
    }
    init_RMQ(n);
    ll ans = 0;
    int mxidx = bsmx(1);
    int mnidx = bsmn(1);
    ans += 1ll*mxidx*num[1];
    ans -= 1ll*mnidx*num[1];
    stack<P>mx;
    stack<P>mn;
    mx.push(P(num[1], 1));
    mn.push(P(num[1], 1));
    for(int i=2; i<=n; i++) {
        mxidx = bsmx(i);
        mnidx = bsmn(i);
        int mxk = i;
        int mnk = i;
        while(mx.size() && mx.top().first <= num[i]) {
            mxk = mx.top().second;
            mx.pop();
        }
        mx.push(P(num[i], mxk));
        while(mn.size() && mn.top().first >= num[i]) {
            mnk = mn.top().second;
            mn.pop();
        }
        mn.push(P(num[i], mnk));
        //printf("%d %d %d %d\n", i-mxk+1, i-mnk+1, mxidx-i+1, mnidx-i+1);
        ans += 1ll*(i-mxk+1)*num[i]*(mxidx-i+1);
        ans -= 1ll*(i-mnk+1)*num[i]*(mnidx-i+1);
    }
    cout << ans << endl;
}



### Codeforces 1487D Problem Solution The problem described involves determining the maximum amount of a product that can be created from given quantities of ingredients under an idealized production process. For this specific case on Codeforces with problem number 1487D, while direct details about this exact question are not provided here, similar problems often involve resource allocation or limiting reagent type calculations. For instance, when faced with such constraints-based questions where multiple resources contribute to producing one unit of output but at different ratios, finding the bottleneck becomes crucial. In another context related to crafting items using various materials, it was determined that the formula `min(a[0],a[1],a[2]/2,a[3]/7,a[4]/4)` could represent how these limits interact[^1]. However, applying this directly without knowing specifics like what each array element represents in relation to the actual requirements for creating "philosophical stones" as mentioned would require adjustments based upon the precise conditions outlined within 1487D itself. To solve or discuss solutions effectively regarding Codeforces' challenge numbered 1487D: - Carefully read through all aspects presented by the contest organizers. - Identify which ingredient or component acts as the primary constraint towards achieving full capacity utilization. - Implement logic reflecting those relationships accurately; typically involving loops, conditionals, and possibly dynamic programming depending on complexity level required beyond simple minimum value determination across adjusted inputs. ```cpp #include <iostream> #include <vector> using namespace std; int main() { int n; cin >> n; vector<long long> a(n); for(int i=0;i<n;++i){ cin>>a[i]; } // Assuming indices correspond appropriately per problem statement's ratio requirement cout << min({a[0], a[1], a[2]/2LL, a[3]/7LL, a[4]/4LL}) << endl; } ``` --related questions-- 1. How does identifying bottlenecks help optimize algorithms solving constrained optimization problems? 2. What strategies should contestants adopt when translating mathematical formulas into code during competitive coding events? 3. Can you explain why understanding input-output relations is critical before implementing any algorithmic approach? 4. In what ways do prefix-suffix-middle frameworks enhance model training efficiency outside of just tokenization improvements? 5. Why might adjusting sample proportions specifically benefit models designed for tasks requiring both strong linguistic comprehension alongside logical reasoning skills?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值