[模拟]LeetCode 11. Container With Most Water 题解
来源:互联网 发布:linux关机 编辑:程序博客网 时间:2024/06/05 15:53
(传送门)
网络可能会不稳定……
题目大意
给出一个长度为n的数组a,求
解题分析
真的要说能想出这个想法的都是神犇
终于找出Manchery搬来的题的原题了
貌似用各种神奇的扫描算法加排序可以防被卡?不自道啊。
标算其实很简单,首先先把ans求成L=1,R=n时的答案
然后找一下L,R中较小的那个,假设为L,那么指针k=L+1,k往R的方向移动,要找到一个比
复杂度:
时间:
空间:
#include<cstdio>#include<algorithm>using namespace std;#define LL long longint n,a[10000005]; LL ans;inline void readi(int &x){ x=0; char ch=getchar(); while ('0'>ch||ch>'9') {ch=getchar();} while ('0'<=ch&&ch<='9') {x=x*10+ch-'0'; ch=getchar();}}int main(){ freopen("a.in","r",stdin); freopen("a.out","w",stdout); readi(n); for (int i=1;i<=n;i++) readi(a[i]); ans=(LL)min(a[1],a[n])*(n-1); int L=1,R=n; while (L<=R){ if (a[L]<=a[R]){ int k=L+1; while (k<=R&&a[k]<=a[L]) k++; L=k; }else{ int k=R-1; while (L<=k&&a[k]<=a[R]) k--; R=k; } if (L<=R) ans=max(ans,(LL)min(a[L],a[R])*(R-L)); } printf("%lld",ans); return 0;}
阅读全文
0 0
- [模拟]LeetCode 11. Container With Most Water 题解
- Leetcode题解:11. Container With Most Water
- leetcode题解-11. Container With Most Water
- 11. Container With Most Water LeetCode题解
- Leetcode题解-11. Container With Most Water
- 【LeetCode】【Python题解】Container with most water
- leetcode题解||Container With Most Water问题
- LeetCode题解:Container With Most Water
- LeetCode题解-11-Container With Most Water
- leetcode题解Java | 11. Container With Most Water
- LeetCodet题解--11. Container With Most Water
- 11. Container With Most Water题解
- LeetCode 11. Container With Most Water
- [LeetCode]11.Container With Most Water
- LeetCode --- 11. Container With Most Water
- LeetCode 11.Container With Most Water
- [Leetcode] 11. Container With Most Water
- [leetcode] 11.Container With Most Water
- 15个顶级Java多线程面试题及答案
- SQL--constraint 约束
- Emgu.CV学习(一) 介绍与安装
- Spring Boot (一):初识之入门篇
- spring-mvc框架配置详解
- [模拟]LeetCode 11. Container With Most Water 题解
- redis(2)——redis的数据结构
- BGP笔记-3
- 数据处理——One-Hot Encoding
- bzoj 2561: 最小生成树
- 交换机堆叠技术
- Springboot 入门级报错Whitelabel Error Page This application
- 【leetcode】537. Complex Number Multiplication(Python & C++)
- 文件下载测试的测试点