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. node中的对象

    1. class的概念 定义一个class,属性都是private,方法都是public. Hello.js: 使用class index.js: 2. 单例类 使用exports而不是module. ...

  2. 【BZOJ1877】[SDOI2009]晨跑 最小费用最大流

    [BZOJ1877][SDOI2009]晨跑 Description Elaxia最近迷恋上了空手道,他为自己设定了一套健身计划,比如俯卧撑.仰卧起坐等 等,不过到目前为止,他坚持下来的只有晨跑. 现 ...

  3. Redis连接(二)

    Redis 命令 Redis 命令用于在 redis 服务上执行操作. 要在 redis 服务上执行命令需要一个 redis 客户端.Redis 客户端在我们之前下载的的 redis 的安装包中. 语 ...

  4. oracle goldengate安装

    1.ftp工具上传ogg112101_fbo_ggs_Linux_x64_ora11g_64bit.zip分别到source和target服务器 [oracle@localhost mnt]$ ll ...

  5. C# WinForm实现任务栏程序图标闪烁

    相信大家在用QQ的时候都会知道,你打开了QQ聊天窗口,如果窗口不是当前激活的窗口的话,收到QQ消息时,任务栏(不是托盘图标)上的图标会闪一下变成黄色(Win7默认主题下),用以通知用户有消息进来了,之 ...

  6. tow sum

    今天面试好打脸!!! 解决方案方法一:暴力法暴力法很简单.遍历每个元素 xx,并查找是否存在一个值与 target−x 相等的目标元素. public int[] twoSum(int[] nums, ...

  7. JS模块化方案

  8. 大话存储4——RAID磁盘阵列

    RAID是英文Redundant Array of Independent Disks(独立磁盘冗余阵列),简称磁盘阵列.下面将各个级别的RAID介绍如下. RAID0 条带化(Stripe)存储.理 ...

  9. 关于uuid与自增列的选择

    关于uuid与自增列的选择 在db交流群里看到有人提问,说他的userName 登录名是唯一的,可以用其做主键嘛,如果用自增列,那又要多一列. 后面又说,如果要用主键ID,用uuid会不会好一些呢?作 ...

  10. RDD的基础知识

    以下的这些分析都是基于spark2.1进行的 (一)什么是RDD A Resilient Distributed Dataset (RDD), the basic abstraction in Spa ...