题目大意:
一个有很多头(题目描述就这么说的)的人喜欢到处乱顶,从
i
i
i处(顶)到
j
j
j处就获得
a
[
j
]
×
(
j
−
i
)
a[j]\times (j-i)
a[j]×(j−i)
对于60%的数据,n<=1000;
对于100%的数据,n<=100000,0<=a[j]<=50。
解题思路:
60
p
t
s
O
(
n
2
)
60pts\ O(n^2)
60pts O(n2)的
d
p
dp
dp很容易想出来的啦
100
p
t
s
100pts
100pts在
O
(
n
2
)
d
p
O(n^2)dp
O(n2)dp的基础上加斜率优化
o
r
or
or 贪心水过
A c c e p t e d c o d e : Accepted\ code: Accepted code:
#include<cstdio>
#include<algorithm>
int n;
long long ans;
int a[100005], id[100005];
inline bool cmp(int x, int y) {
return a[x] > a[y];
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; ++i) scanf("%d", &a[i]), id[i] = i;
std::sort(id+1, id+n+1, cmp);
int now = 0, i = 1;
while (now != n) {
while (id[i] <= now) ++i;
ans += a[id[i]] * (id[i] - now);
now = id[i];
}
printf("%lld\n", ans);
}