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. mysql如何查询日期的列表?

    转自:http://blog.csdn.net/liufei198613/article/details/72643345 select @num:=@num+1,date_format(adddat ...

  2. 一键搞定JavaEE应用,JRE+Tomcat+Mysql-JaveEE绿色运行环境JTM0.9版 (转载)

    这是一个集成了jre,tomcat,mysql的绿色运行环境, 直接解压之后就可以运行.不需要用户自己安装jre,tomcat,mysql,一键到位,看起来像是桌面式的web应用.换句话说,就像是we ...

  3. 170721、springboot编程之注解(annotation)列表

    (1)@SpringBootApplication 申明让spring boot自动给程序进行必要的配置,这个配置等同于: @Configuration ,@EnableAutoConfigurati ...

  4. 170707、springboot编程之监控和管理生产环境

    spring-boot-actuator模块提供了一个监控和管理生产环境的模块,可以使用http.jmx.ssh.telnet等拉管理和监控应用.审计(Auditing). 健康(health).数据 ...

  5. angular $http服务详解

    它是对原生XMLHttpRequest对象的简单封装, 这个方法会返回一个promise对象,具有sccess和error两个方法. 当然,我们也可以在响应返回时用then 方法来处理,会得到一个特殊 ...

  6. Linux的操作系统I2C驱动架构解说

    Linux的操作系统I2C驱动架构解说 发布时间:2006.10.16 04:52 来源:赛迪网技术社区 作者:LoneStar 最近因为工作需要涉及到了I2C总线.虽然我过去用过I2c,但看了 Li ...

  7. Spring Boot - Building RESTful Web Services

    Spring Boot Building RESTful Web Services https://www.tutorialspoint.com/spring_boot/spring_boot_bui ...

  8. 转!idea启动后发现tomcat前面出现红色或是灰色的问号

    原博文地址:https://blog.csdn.net/z_zhy/article/details/83068168 直接在idea里 点击File------settings,在搜索框直接搜tomc ...

  9. T-SQL练习题

    转自:http://www.cnblogs.com/jenrrychen/p/5348546.html 1 - 3 题: 数据表结构: OrderID ProductID OrderDate  Sal ...

  10. 204-React DOM 元素

    一.概述 为了提高性能和跨浏览器兼容性,React实现了一个独立于浏览器的DOM系统. 在React中,所有DOM属性和属性(包括事件处理程序)都应该是camelCased的.例如,HTML属性tab ...