(思维)2476 小b和序列

2476 小b和序列

  1. 2 秒
  2.  
  3. 262,144 KB
  4.  
  5. 20 分
  6.  
  7. 3 级题

小b有一个长度为n的序列a,她想求maxi<j{min(ai,aj)×(j−i)}maxi<j{min(ai,aj)×(j−i)},即选出两个数,使得它们的最小值乘上它们在序列上的距离最大。

你能帮帮她吗?

样例解释:选择a2a2和a9a9,答案为min(8,7)×(9−2)=49min(8,7)×(9−2)=49,不存在其他选法答案更大,因此最大值就是49。

 收起

输入

第一行一个数n,0<n≤50000;
第二行n个数表示a_i,以空格隔开,0<=a_i<=10000。

输出

输出一个数表示这个最大值

输入样例

9
1 8 6 2 5 4 8 3 7

输出样例

49

题解:先排序,这样就确定了前面的肯定是小的,然后再固定一个距离就好了;

           反序遍历,得到对当前值来说的最小位置和最大位置

           那么,距离 = max(|最小位置-当前位置|,|最大位置-当前位置|)

#include<set>
#include<map>
#include<list>
#include<queue>
#include<stack>
#include<math.h>
#include<vector>
#include<bitset>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#define eps (1e-8)
#define MAX 0x3f3f3f3f
#define u_max 1844674407370955161
#define l_max 9223372036854775807
#define i_max 2147483647
#define re register
#define pushup() tree[rt]=max(tree[rt<<1],tree[rt<<1|1])
#define nth(k,n) nth_element(a,a+k,a+n);  // 将 第K大的放在k位
#define ko() for(int i=2;i<=n;i++) s=(s+k)%i // 约瑟夫
#define ok() v.erase(unique(v.begin(),v.end()),v.end()) // 排序,离散化
using namespace std;

inline int read(){
    char c = getchar(); int x = 0, f = 1;
    while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
    while(c >= '0' & c <= '9') x = x * 10 + c - '0', c = getchar();
    return x * f;
}

typedef long long ll;
const double pi = atan(1.)*4.;
const int M=1e3+5;
const int N=5e5+5;
int a[N],l[N],r[N];
struct fun{
    int x,id;
}f[N];
bool cmp(fun aa,fun bb){
    if(aa.x!=bb.x) return aa.x<bb.x;
    return aa.id<bb.id;
}
int main(){
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        scanf("%d",&a[i]);
        f[i].x=a[i];
        f[i].id=i;
    }

    sort(f+1,f+n+1,cmp);
    int minn=n+1,maxx=0;
    for(int i=n;i>=1;i--){
        l[i]=minn;
        r[i]=maxx;
        minn=min(minn,f[i].id);
        maxx=max(maxx,f[i].id);
    }
    int ans=0;
    for(int i=1;i<n;i++){
        ans=max(ans,f[i].x*abs(f[i].id-l[i]));
        ans=max(ans,f[i].x*abs(f[i].id-r[i]));
    }
    printf("%d\n",ans);
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值