【[AGC005B] Minimum Sum】 题解
题目链接
从1开始从小到大考虑,用set维护每个数左右的扩散范围,然后答案为这个数的区间就是左端点个数 右端点个数。
Code
// Problem: AT2060 [AGC005B] Minimum Sum
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/AT2060
// Memory Limit: 250 MB
// Time Limit: 2000 ms
//
// Powered by CP Editor (https://cpeditor.org)
#include<bits/stdc++.h>
using namespace std;
#define int long long
inline int read(){int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;
ch=getchar();}while(ch>='0'&&ch<='9'){x=(x<<1)+
(x<<3)+(ch^48);ch=getchar();}return x*f;}
//#define mo
//#define M
//#define M
struct node
{
int x, id;
}a[200010];
int n, m, i, j, k;
set<int>s;
set<int>::iterator it;
set<int>::iterator iw;
int ans, l, r;
bool cmp(node x, node y)
{
return x.x<y.x;
}
signed main()
{
// freopen("tiaoshi.in","r",stdin);
// freopen("tiaoshi.out","w",stdout);
n=read();
for(i=1; i<=n; ++i)
a[i].x=read(), a[i].id=i;
sort(a+1, a+n+1, cmp);
s.insert(0); s.insert(n+1);
for(i=1; i<=n; ++i)
{
// printf("> %lld %lld\n", a[i].x, a[i].id);
it = s.upper_bound(a[i].id);
r=*it;
it--;
l=*it;
k=max((long long)0, r-l-1);
++l; --r;
// printf("%lld %lld %lld\n", l, r, k);
ans+=a[i].x*max((long long)0, (a[i].id-l+1)*(r-a[i].id+1));
s.insert(a[i].id);
}
printf("%lld", ans);
return 0;
}
本文来自博客园,作者:zhangtingxi,转载请注明原文链接:https://www.cnblogs.com/zhangtingxi/p/15633178.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!