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. CodeForces - 512B Fox And Jumping[map优化dp]

    B. Fox And Jumping time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  2. Hibernate之核心文件

    一个Hibernate项目核心配置文件主要分为以下三个方面:1.配置文件hibernate.cfg.xml:2.配置文件*.hbm.xml,此文件一般包括映射文件的结构以及各种属性的映射方式:3.Hi ...

  3. 【ArcGIS for SivlerLight api(3)】基础图层增删改查

    1.基础底图通常使用TiledLayer或者ArcGISDynamicLayer. 本质上都是在本地加载栅格图片.后台生成策略不同而已.从Vs2010的控件栏上拖过来的Map控件默认添加的底图是Esr ...

  4. 用MongoDB取代RabbitMQ(转)

    原文:http://blog.nosqlfan.com/html/3223.html RabbitMQ是当成应用比较广泛的队列服务系统,其配套的客户端和监控运维方案也比较成熟.BoxedIce的队列服 ...

  5. 【转】sql server 订阅发布、快照发布(一)

    原文链接:https://blog.csdn.net/tiandi_5000/article/details/11646023 SQL SERVER 2012 使用订阅发布同步数据库(一) 2013年 ...

  6. 让Windows Server 2008 + IIS 7+ ASP.NET 支持10万并发请求 The serverRuntime@appConcurrentRequestLimit setting is being exceeded.

    今天下午17点左右,博客园博客站点出现这样的错误信息: Error Summary: HTTP Error 503.2 - Service UnavailableThe serverRuntime@a ...

  7. Ambari Rest api 使用

    最近由于项目的需要在看一些Ambari的api去获取集群的一些信息,获取集群节点的状态信息以及各个服务和组件的信息.然后在我们的数据服务管理平台上去做一些监控运维,现在把一些使用总结一下:官网rest ...

  8. loadrunner11的移动端性能测试之结果分析

    测试步骤之结果分析器(Analysis) 进入Analysis 当场景停止运行后,可从Controller中进入.点击[Results]—[Analysis Results]见下图: 若想打开一个已保 ...

  9. PAT 1055 The World's Richest[排序][如何不超时]

    1055 The World's Richest(25 分) Forbes magazine publishes every year its list of billionaires based o ...

  10. mydumper安装

    安装依赖包: yum install glib2-devel mysql-devel zlib-devel pcre-devel openssl-devel cmake 下载二进制包: wget ht ...