当前位置:网站首页>P1318 积水面积
P1318 积水面积
2022-06-22 05:23:00 【hhyy_d】


解题思路:对于每一个点,去往前找比它大的第一个数,去往后找比他大的第一个数,然后算min(arr[left]-arr[i],arr[right]-arr[i]),答案加上这个值乘以(right-left-1),如果在往前找的时候,发现某个数和他相等,那么说明之前已经处理过了,就不处理了,直接返回。
#include<bits/stdc++.h>
#define MAX 10001
using namespace std;
int n;
int arr[MAX];
long long res=0;
int left_high(int j)
{
for(int i=j-1;i>=0;i--)
{
if(arr[i]==arr[j])
{
return -1;
}
if(arr[i]>arr[j])
{
return i;
}
}
return -1;
}
int right_hight(int j)
{
for(int i=j+1;i<n;i++)
{
if(arr[i]>arr[j])
{
return i;
}
}
return -1;
}
int main()
{
cin>>n;
for(int i=0;i<n;i++)
{
cin>>arr[i];
}
int left=-1,right=-1,temp=0;
for(int i=1;i<n;i++)
{
if(arr[i]==arr[i-1]) continue;
left = left_high(i);
if(left !=-1)
{
right = right_hight(i);
if(right!=-1)
{
temp = min(arr[left]-arr[i],arr[right]-arr[i]);
//cout<<left<<" "<<right<<" "<<temp<<endl;
res+=(temp*(right-left-1));
}
}
}
cout<<res<<endl;
return 0;
}
边栏推荐
- Record local project startup error: invalid source distribution: 8
- Kubernetes——使用minikube搭建环境
- MySQL day04 class notes
- Monorepo silk slide methodology: reference module hot update
- Reconstructing thinking series 2-functions and variables
- Free questions for polymerization process and test papers for polymerization process in 2022
- What is cloud hosting and what are its advantages?
- Six sides tmall has already offered. After a review of my experience, is it really that difficult to enter a big factory?
- Some considerations of C language custom function
- [chrome] Google tips Google browser comes with scrolling screenshot (full screen shot)
猜你喜欢

What problems will be encountered during the implementation of MES management system

风阀执行EN 1634-1耐火测试完整性能坚持 4小时吗?

Free questions for polymerization process and test papers for polymerization process in 2022

Graduation season | a new start, no goodbye

Service migration when deploying SuperMap iserver war package

1108. Defanging an IP Address

Kubernetes - deploy application to cluster

记本地项目启动报错:无效的源发行版: 8

Flynk deployment mode summary

Hanoi Tower problem
随机推荐
[chrome] Google tips Google browser comes with scrolling screenshot (full screen shot)
TIDB-升级版本
Solve the shortage of developers. Maybe it can help you
DTS迁移秘籍-SQLSERVER篇
C语言数据类型转换规则(隐式转换+显式转换)
[cloud native] 2.2 kubeadm create cluster
How to remove parentheses in PHP regular
ActiveMQ knowledge summary of Message Oriented Middleware
C#中Cookie设置与读取
Running yolov5 reports an error attributeerror: cant get attribute sppf on module models common
MySQL day02 class notes
DeformConv
Monorepo丝滑方法论:引用模块热更新
部署SuperMap iServer war包时的服务迁移
postmanUtils工具类,模拟postman的get,post请求
Rétroaction sur la remise des diplômes! Tous les contributeurs de la communauté Apache Doris sont ici pour recevoir des cadeaux!
C language data type conversion rules (implicit conversion + explicit conversion)
Squoosh - Google's free open source image compression tool, reducing the image size by 90%! Support API development calls
What are the high availability designs of yarn?
中小企业签署ERP合同时,需要留意这几点