http://codeforces.com/gym/101334

题意:
给定一串数,求一个区间,使得该区间的所有数之和乘以该区间内最小的数的乘积最大。

思路:
先预处理一下,计算出前缀和。

我们可以把每个点都当成该区间内最小的数,那么这个区间的最大长度就能延伸到左端和右端第一个比它小的数处。

这样一来,我们先计算出每个数左端和右端第一个比它小的数的坐标,最后遍历即可。

但是,求得时候还需要优化一下,如果在寻找的时候,碰到了比它大的数,那么直接移动到那个数所对应的比它小的数坐标,具体详见代码。

 #include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<sstream>
#include<vector>
#include<stack>
#include<queue>
#include<cmath>
#include<map>
using namespace std;
typedef long long ll;
typedef pair<int,int> pll;
const int INF=0x3f3f3f3f3f;
const int maxn=+; int n;
ll a[maxn];
ll sum[maxn];
ll L[maxn],R[maxn]; int main()
{
freopen("feelgood.in","r",stdin);
freopen("feelgood.out","w",stdout);
//freopen("D:\\input.txt","r",stdin);
while(~scanf("%d",&n))
{
sum[]=;
for(int i=;i<=n;i++)
{
scanf("%lld",&a[i]);
sum[i]=sum[i-]+a[i];
} int i,j;
for(i=;i<=n;i++)
{
for(j=i-;j>;j--)
{
if(a[j]<a[i]) {L[i]=j;break;}
else if(a[j]>=a[i]) {j=L[j];j++;}
}
if(j==) L[i]=;
} for(i=n;i>;i--)
{
for(j=i+;j<=n;j++)
{
if(a[j]<a[i]) {R[i]=j;break;}
else if(a[j]>=a[i]) {j=R[j];j--;}
}
if(j==n+) R[i]=n+;
} ll ans=;
int l,r;
for(int i=;i<=n;i++)
{
ll temp=a[i]*(sum[R[i]-]-sum[L[i]]);
if(ans<=temp)
{
ans=temp;
l=L[i]+;
r=R[i]-;
}
}
printf("%lld\n%d %d\n",ans,l,r);
}
}

Gym 101334F Feel Good的更多相关文章

  1. Gym - 101334F 单调栈

    当时我的第一想法也是用单调栈,但是被我写炸了:我也不知道错在哪里: 看了大神的写法,用数组模拟的: 记录下单调递增栈的下标,以及每个数字作为最小值的最左边的位置. 当有数据要出栈的时候,说明栈里的数据 ...

  2. ACM: Gym 101047M Removing coins in Kem Kadrãn - 暴力

     Gym 101047M Removing coins in Kem Kadrãn Time Limit:2000MS     Memory Limit:65536KB     64bit IO Fo ...

  3. ACM: Gym 101047K Training with Phuket's larvae - 思维题

     Gym 101047K Training with Phuket's larvae Time Limit:2000MS     Memory Limit:65536KB     64bit IO F ...

  4. ACM: Gym 101047E Escape from Ayutthaya - BFS

    Gym 101047E Escape from Ayutthaya Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I6 ...

  5. ACM: Gym 101047B Renzo and the palindromic decoration - 手速题

     Gym 101047B  Renzo and the palindromic decoration Time Limit:2000MS     Memory Limit:65536KB     64 ...

  6. Gym 101102J---Divisible Numbers(反推技巧题)

    题目链接 http://codeforces.com/gym/101102/problem/J Description standard input/output You are given an a ...

  7. Gym 100917J---Judgement(01背包+bitset)

    题目链接 http://codeforces.com/gym/100917/problem/J Description standard input/outputStatements The jury ...

  8. Gym 100917J---dir -C(RMQ--ST)

    题目链接 http://codeforces.com/gym/100917/problem/D problem description Famous Berland coder and IT mana ...

  9. Gym 101102D---Rectangles(单调栈)

    题目链接 http://codeforces.com/gym/101102/problem/D problem  description Given an R×C grid with each cel ...

随机推荐

  1. CentOS配置bond

    Bonding的模式一共有7种: #defineBOND_MODE_ROUNDROBIN       0   (balance-rr模式)网卡的负载均衡模式 #defineBOND_MODE_ACTI ...

  2. HUB、SPAN、TAP比较

    在获取数据包进行网络分析时,常用的方法有三种:HUB.SPAN和TAP. 一 HUB    HUB 很“弱智”,但这种方法却是最早的数据包获取方法.HUB是半双工的以太网设备,在广播数据包时,无法同时 ...

  3. Windows Phone WebClient的使用

    webClient对象可用来下载XML文件,程序集等这些数据,其可以实现按需下载,所以还是有必要了解的.其主要包含几个事件:                                       ...

  4. HDCMS常用的一些调用!

    HDCMS常用的一些调用: 头部的标题/描述/关键词的调用: <title><?php if($hdcms['aid']):?><?php if($hdcms['seo_ ...

  5. 170619、springboot编程之HelloWorld

    springboot资料看了一段时间了,个人觉得开发效率相当高,也参考了网上很多大牛的技术博客,在这里面我也记录一下,方便以后自己翻阅查看,同时也给新手最一点点指引.如果有侵权大牛博客文章,请告诉我, ...

  6. chinese-typesetting:更好的中文文案排版

    欢迎指正.GitHub 地址:https://github.com/jxlwqq/chinese-typesetting 更好的中文文案排版 统一中文文案.排版的相关用法,降低团队成员之间的沟通成本, ...

  7. python使用MySqlDB

    下载安装MySQLdb <1>linux版本 http://sourceforge.net/projects/mysql-python/ 下载,在安装是要先安装setuptools,然后在 ...

  8. kibana 和ES安装配置常见问题解决

    1.下载相同版本的kibana和ES: es5.6.5下载地址:https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-5 ...

  9. kitkat-s5p4418drone 记录

      查看帮助: ./device/nexell/tools/build.sh  -h   编译u-boot: ./device/nexell/tools/build.sh -b drone2 -t u ...

  10. explain 分析 聚合统计语句的性能

    EXPLAIN SELECT COUNT(1) FROM question; id select_type table partitions type possible_keys key key_le ...