UESTC 771 最大容积(前缀后缀和)
题目链接:http://acm.uestc.edu.cn/#/problem/show/771
最大容积
Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others)
在 xx 轴的正整数坐标 1,2,⋯,N1,2,⋯,N 上分别竖立着一条线段,高度分别为 h1,h2,⋯,hNh1,h2,⋯,hN。这样只要我们任意选择两条线段 i,ji,j,再加上 xx 轴就能围成一个水槽了(只不过是二维的)。
由于短板效应,这个水槽的容积应该是
那么选哪两条线段与 xx 轴构成的水槽容积最大呢?
Input
输入一共两行:
第一行是一个正整数 NN,代表一共有多少条线段,其中 2<N<1062<N<106;
第二行是 NN 个正整数 h1,h2,⋯,hNh1,h2,⋯,hN,分别表示线段 1,2,⋯,N1,2,⋯,N 的高度,其中 0≤hi<1030≤hi<103。
Output
输出最大容积。
Sample input and outpu
| Sample Input | Sample Output |
|---|---|
5 |
6 |
题解:今天和老房讨论了这个题,方法是枚举左端点,优化右区间。关键是怎么优化。二分不行,线段树搞不通,最后老房灵机一动,想出用后缀和来搞,再次发现前缀和后缀和真的真的很神奇,后缀和记录大于等于某个数字的最远位置,然后枚举左区间,即使每个高度非常高,复杂度也就n+h.然后发现这个题标程有问题。看下文。
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
const int maxn = 1e6+;
int arr[maxn];
int h[];
int main()
{
int n;
scanf("%d",&n);
for(int i=;i<=n;i++) scanf("%d",&arr[i]);
for(int i=n;i>=;i--)
{
if(!h[arr[i]])
{
int cur = arr[i];
while(h[cur]==&&cur>)
{
h[cur] = i;
cur--;
}
}
}
int ans = ;
for(int i=;i<=n;i++)
{
int cur = abs(h[arr[i]]-i)*arr[i];
ans = max(ans,cur);
}
printf("%d\n",ans);
return ;
}
卷珠帘
这份代码ac了,然后他们给我发了一组数据,5 5 4 3 2 1输出的是0,因为还需要从前往后扫一遍,,,QAQ
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
const int maxn = 1e6+;
int arr[maxn];
int h2[];
int h1[];
int main()
{
int n;
scanf("%d",&n);
for(int i=;i<=n;i++) scanf("%d",&arr[i]);
for(int i=n;i>=;i--)
{
if(!h2[arr[i]])
{
int cur = arr[i];
while(h2[cur]==&&cur>)
{
h2[cur] = i;
cur--;
}
}
}
for(int i=;i<=n;i++)
{
if(!h1[arr[i]])
{
int cur = arr[i];
while(h1[cur]==&&cur>)
{
h1[cur] = i;
cur--;
}
}
}
int ans = ;
for(int i=;i<=n;i++)
{
int cur = abs(h2[arr[i]]-i)*arr[i];
ans = max(ans,cur);
}
for(int i=n;i>=;i--)
{
int cur = abs(h1[arr[i]]-i)*arr[i];
ans = max(ans,cur);
}
printf("%d\n",ans);
return ;
}
卷珠帘
UESTC 771 最大容积(前缀后缀和)的更多相关文章
- POJ 2752 (KMP 所有可能长度的前缀后缀) Seek the Name, Seek the Fame
题意: 求一个字符串的相同前缀后缀的所有可能的长度,这里该字符串其本身也算自己的前缀和后缀. 分析: 我们知道next数组的性质是,该字符之前的字符串的最大相同前缀后缀. 既然知道了最大的,即next ...
- hdoj 2594 Simpsons’ Hidden Talents 【KMP】【求串的最长公共前缀后缀】
Simpsons' Hidden Talents Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java ...
- 1280 前缀后缀集合(map)
1280 前缀后缀集合 题目来源: Codility 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题 一个数组包含N个正整数,其中有些是重复的.一个前缀后缀集是满足 ...
- kuangbin专题七 HDU1540 Tunnel Warfare (前缀后缀线段树)
During the War of Resistance Against Japan, tunnel warfare was carried out extensively in the vast a ...
- jQuery EasyUI/TopJUI基本的数字输入框(保留两位小数,带前缀后缀...)
jQuery EasyUI/TopJUI基本的数字输入框(保留两位小数,带前缀后缀...) numberbox(数值输入框) HTML required:必填字段,默认为false:prompt:显示 ...
- Codeforces Round #320 (Div. 2) [Bayan Thanks-Round] D "Or" Game 枚举+前缀后缀
D. "Or" Game ...
- B. Marvolo Gaunt's Ring 前缀后缀
B. Marvolo Gaunt's Ring 这种一般只有三个的都可以处理前缀和后缀,再枚举中间这个值. 这个和之前写过的C. Four Segments 前缀后缀 处理方式很像. #include ...
- FIX_前缀后缀_未提交
问题 B: FIX 时间限制: 1 Sec 内存限制: 64 MB提交: 38 解决: 11[提交][状态][讨论版] 题目描述 如果单词 X 由单词 Y 的前若干个字母构成,我们称 X 是 Y ...
- Simpsons’ Hidden Talents - HDU 2594(求相同的前缀后缀)
题目大意:给你两个字符串,找出一个最大的子串,这个子串要是前面串的前缀并且是后面串的后缀........... 分析:next的简单运用吧,可以把两个串进行合并,中间加一个不能被匹配的字符,然后求 ...
随机推荐
- hdu_5029_relief grain(树链剖分)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=5029 题意:给你一个树,然后给你两点,将这两点之间的点涂上颜色,问涂色最多的那个颜色是什么,如果数量相 ...
- linuxmint更改权限
sudo chmod -R 777 要更改的目录或文件
- PHP xdebug的安装
xdebug实际上就是PHP的一个第三方扩展 安装xdebug步骤和添加一个PHP扩展一样 linux:去xdebug官网下载对应版本的源码,然后像编译其他linux扩展一样,详解我的一篇关于Linu ...
- english 释词
english 释词 [amount of & number of]the amount of /the number of指……的数量an amount of/a number of 指“大 ...
- Asp.Net调用Office组件操作时的DCOM配置 (转)
Asp.Net调用Office组件操作时的DCOM配置 http://blog.csdn.net/gz775/article/details/6447758 在项目中将数据导出为Excel格式时出现“ ...
- 11g init DB software and database
oadmin->administrator2.169set ORACLE_HOME=C:\app\oracle\product\11.2.0\dbhome_1set ORACLE_SID=csm ...
- 转: Windows下安装Oracle Database 12c Release 1(12.1.0.2.0) - Enterprise Edition
http://www.cnblogs.com/xqzt/p/4395053.html Windows下安装Oracle Database 12c Release 1(12.1.0.2.0) - Ent ...
- Animals and Puzzle
Animals and Puzzle time limit per test 5 seconds memory limit per test 512 megabytes input standard ...
- mysql 修改 添加 删除 表字段
添加表的字段 alter table 表名 add 字段名 字段的类型 例子: alter table table1 add transactor varchar(10) n ...
- Singleton ——运行时全局唯一对象
Singleton 运行时全局唯一对象 Singleton模式只解决一个问题,如何做到运行时创建一个全局唯一的对象? 1:隐藏类的实例化操作,即将构造函数声明为private或protected.任何 ...