洛谷 - P7286

· · 个人记录

题意

潇有一个数组 k,下标为 1 到 n 。

潇定义 f(x,y)=\begin{cases} \min(k_x,k_y) \times (x + y) &x \ne y \\ k_x\times x&x=y \end{cases} 。

潇想知道对于任意的 1 \le x,y \le n,f(x,y) 的最大值是多少。但是她不会做,于是就问了善良的 小 Z,然而非常想在妹子面前表现的 小 Z 发现他也不会做,就只能够求助善良的你了。

思路

显然第二种 x = y 的情况不会比第一种更优,所以我们来推一下第一种的式子。

令 k_x \le k_y

\min(k_x, k_y) \times (x + y) = k_x \times (x + y) = k_x \times x + k_x \times y

所以我们枚举 x,找到使这个式子最大的 y, 可以从大到小排序,y 取前缀最大值。

代码

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;

const int N = 1e6 + 3;
int n;
struct shu{
    ll s;
    int id;
} k[N];

int max_id;
ll ans;

int main(){
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);

    cin >> n;
    for(int i = 1; i <= n; i ++){
        cin >> k[i].s;
        k[i].id = i;
    }

    sort(k + 1, k + 1 + n, [](const shu & x, const shu & y){ return x.s > y.s; });

    for(int i = 1; i <= n; i ++){
        ans = max(ans, k[i].s * (k[i].id + max_id));
        max_id = max(max_id, k[i].id);
    }

    cout << ans;

    return 0;
}