当前位置:网站首页>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;
}
边栏推荐
- Pull all data of a branch of the code cloud to cover the local code
- Sort ten integers using selection
- 强制删除 Terminating 状态的 namespace
- Six sides tmall has already offered. After a review of my experience, is it really that difficult to enter a big factory?
- 7. Gateway global filter
- Reconstructing thinking series 2-functions and variables
- Kubernetes -- setting up an environment using minicube
- How to remove parentheses in PHP regular
- Detailed explanation of deep learning technology for building an image search engine that can find similar images
- C language data type conversion rules (implicit conversion + explicit conversion)
猜你喜欢

Record local project startup error: invalid source distribution: 8

C语言变量的存储方式和生存期

Accelerate the promotion of industrial Internet, and map out a new blueprint for development

畢業回饋!Apache Doris 社區所有貢獻者來領禮品啦!

js正则表达式实现千分位符

加快推进工业互联网,图扑“智”绘发展新蓝图

When idea creates a method, it uses annotations to prompt method parameters (param), return value (return), and method function (description)

Talk about MySQL's locking rule "hard hitting MySQL series 15"

Kubernetes——裸机搭建集群环境

2022 welder (primary) new version test questions and welder (primary) free test questions
随机推荐
long start = currentTimeMillis();
新建本地内容上传至码云分支
Use of jedispool
冰河十年前的预测如今被阿里实现了,非常震撼
Pull all data of a branch of the code cloud to cover the local code
Create a new local content and upload it to the code cloud branch
Non recursive printing Fibonacci sequence
Understanding of service container, service provider and facade of laravel
89---狄拉克 delta 函数
How to find the source of goods for novice stores and how to find high-quality sources of goods?
Graduation season | a new start, no goodbye
新手开店货源怎么找,怎么找到优质货源?
What problems will be encountered during the implementation of MES management system
jedispool的使用
Rambbmitmq Push Party
Accelerate the promotion of industrial Internet, and map out a new blueprint for development
Remote dictionary server (redis) - a kV based NoSQL database management system used as a cache
C language data type conversion rules (implicit conversion + explicit conversion)
DTS迁移秘籍-SQLSERVER篇
Which methods are not allowed to be overridden?