当前位置:网站首页>牛客多校4 A.Task Computing 思维
牛客多校4 A.Task Computing 思维
2022-08-01 22:16:00 【Strezia】
题意
n ( n ≤ 1 e 5 ) n(n\leq 1e5) n(n≤1e5) 个物品,每个物品有 w i , p i w_i, p_i wi,pi ,现从中取出 m ( m ≤ 20 ) m(m\leq 20) m(m≤20) 件,最大化
思路
这种题一般都是通过比较交换相邻两项后对答案的贡献,得出一种排序方法,排序之后计算即可。
假设现在选了 k k k 件物品,这个式子其实就是 S u m = w 1 + w 2 ∗ ( p 1 ) + w 3 ∗ ( p 1 p 2 ) + . . . + w k ∗ ( p 1 p 2 . . . p k − 1 ) Sum = w_1 + w_2*(p_1) + w_3*(p_1p_2)+...+w_k*(p_1p_2...p_{k-1}) Sum=w1+w2∗(p1)+w3∗(p1p2)+...+wk∗(p1p2...pk−1) 。考虑如果再选一件 ( w 0 , p 0 ) (w_0,p_0) (w0,p0) 放在最前面,则 S u m = w 0 + p o ∗ S u m Sum = w_0+p_o*Sum Sum=w0+po∗Sum 。推导如图。之后排序即可。
代码
struct Node {
double w, p;
bool operator < (const Node &x) const {
return w * (1 - x.p) > x.w * (1 - p);
}
}node[maxn];
int n, m;
double f[maxn][25]; //i-n中选j个最大值
void solve() {
cin >> n >> m;
for(int i = 1; i <= n; i++) {
cin >> node[i].w;
}
for(int i = 1; i <= n; i++) {
cin >> node[i].p;
node[i].p /= 10000;
}
sort(node + 1, node + n + 1);
for(int i = 1; i <= n; i++) {
for(int j = 0; j <= m; j++) f[i][j] = -INF;
}
f[n + 1][0] = 0;
for(int i = n; i >= 1; i--) {
for(int j = 0; j <= m; j++) {
f[i][j] = f[i+1][j];
if(j)
f[i][j] = max(f[i+1][j], f[i+1][j-1] * node[i].p + node[i].w);
}
}
cout << f[1][m] << endl;
}
边栏推荐
猜你喜欢
![[Niu Ke brush questions-SQL big factory interview questions] NO4. Travel scene (a taxi)](/img/26/4c3080f1b21efb9401d8c3a55bc15d.png)
[Niu Ke brush questions-SQL big factory interview questions] NO4. Travel scene (a taxi)

论文解读(GSAT)《Interpretable and Generalizable Graph Learning via Stochastic Attention Mechanism》

不卷了!入职字节跳动一周就果断跑了。

PHP算法之电话号码的字母组合

(Translation) How the contrasting color of the button guides the user's actions

render-props and higher order components

统计单词数

Ten years after graduation, financial freedom: those things that are more important than hard work, no one will ever teach you

seaborn笔记:可视化统计关系(散点图、折线图)

恒星的正方形问题
随机推荐
Kubernetes第零篇:认识kubernetes
小程序毕设作品之微信美食菜谱小程序毕业设计成品(7)中期检查报告
小程序毕设作品之微信体育馆预约小程序毕业设计成品(1)开发概要
SOM网络1:原理讲解
NgRx Selector 的 Memoization 特性学习笔记
安全第五次课后练习
render-props and higher order components
PHP算法之电话号码的字母组合
HCIP---Multiple Spanning Tree Protocol related knowledge points
小程序毕设作品之微信美食菜谱小程序毕业设计成品(5)任务书
Postman batch test interface detailed tutorial
感觉自己好傻
SOM Network 2: Implementation of the Code
SOM网络2: 代码的实现
图论——强连通分量缩点+拓扑排序
more grown, more lonely
统计单词数
易周金融分析 | 银行ATM机智能化改造提速;互联网贷款新规带来挑战
2022 版 MySQL 巅峰教程,收藏好,慢慢看
如何给 UE4 场景添加游戏角色