题目
https://ac.nowcoder.com/acm/contest/33189/A
思路
- 考虑给定 m 个数,怎么排序:
- 考虑相邻两个数何时交换更优,就发现这些数有个关于
(p-1)/w
的偏序关系。 - 那么对所有数排好序后,就变成了选一些数使得最终值最大了,简单dp一下即可。
- 一开始我没想到 i j 相邻这个情况来入手,结果推式子的时候老是要带着中间的东西,就找不出结论。这下学习了。
- 时间复杂度是 O(n*m)
代码
#include <bits/stdc++.h>
using namespace std;
struct node {
long long w;
long long q;
double p;
};
bool cmp(const node &x, const node &y){return (y.q-10000)*x.w > (x.q-10000)*y.w;}
int n, m;
node a[100005];
double mx[50];
void solve()
{
scanf("%d%d", &n, &m);
for (int i=1; i<=n; i++) scanf("%lld", &a[i].w);
for (int i=1; i<=n; i++) scanf("%lld", &a[i].q), a[i].p = a[i].q/10000.0;
sort(a+1, a+n+1, cmp);
//for (int i=1; i<=n; i++) printf("%lld %lf %lf\n", a[i].w, a[i].p, 1.0*(a[i].p-1)/a[i].w);
for (int i=1; i<=m; i++) mx[i] = 0;
for (int i=1; i<=n; i++) {
for (int j=m; j>0; j--) {
mx[j] = max(mx[j], mx[j-1]*a[i].p+a[i].w);
}
}
printf("%.15lf\n", mx[m]);
}
int main()
{
solve();
return 0;
}
/*
5 2
1 2 3 4 5
12000 11000 10000 9000 8000
*/