# BZOJ4709: [Jsoi2011]柠檬(决策单调性)

## Sol

\[f[i] = max(f_{j - 1} + a[i] * (s[i] - s[j] +1)^2)\]

```#include<bits/stdc++.h>
#define chmax(a, b) (a = (a > b ? a : b))
#define chmin(a, b) (a = (a < b ? a : b))
#define LL long long
//#define int long long
using namespace std;
const int MAXN = 1e6 + 10;
int x = 0, f = 1; char c = getchar();
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;
}
int N, a[MAXN], s[MAXN], cnt[MAXN]; LL f[MAXN];//s[i] i位置的数第几次出?
vector<int> v[MAXN];
LL calc(int pos, LL val) {
return f[pos - 1] + 1ll * a[pos] * val * val;
}
int lower(int x, int y) {
int l = 1, r = N, ans = N + 1;
while(l <= r) {
int mid = l + r >> 1;
if(calc(x, mid - s[x] + 1) >= calc(y, mid - s[y] + 1)) r = mid - 1, ans = mid;
else l = mid + 1;
}
return ans;
}
main() {
for(int i = 1, siz, S; i <= N; i++) {
s[i] = ++cnt[S = a[i] = read()];
while((((siz = v[S].size()) >= 2) && (lower(v[S][siz - 2], v[S][siz - 1]) <= lower(v[S][siz - 1], i)))) v[S].pop_back();
v[S].push_back(i);
while(((siz = v[S].size()) >= 2) && (lower(v[S][siz - 2], v[S][siz - 1]) <= s[i]))v[S].pop_back();
f[i] = calc(v[S][v[S].size() - 1], s[i] - s[v[S][v[S].size() - 1]] + 1);
}
cout << f[N];
}```

0 条评论

## 相关文章

762

2735

4059

3956

3844

### 9.22模拟赛解题报告

T2读题就花了半个小时，而且一开始没认真理解题目的意思，前后各dp了一遍，后来仔细揣摩了一下题意，细心品味了一下出题人的语言，正着的dp好像是没用的。。。

853

5725

9599

3447

### 解锁 Leetcode 新题：寻找明星

Suppose you are at a party with n people (labeled from 0 to n - 1) and among the...

3676