当前位置:网站首页>牛客多校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;
}
边栏推荐
- _ _ determinant of a matrix is higher algebra eigenvalue of the product, the characteristic value of matrix trace is combined
- 统计单词数
- (*゚ヮ゚)*【精品C语言整理】*(゚ヮ゚*)女盆友缠着你让你教她写代码怎么办?安排,三万字博文带你走遍C语言,从此不再害怕编程
- Delicious this year
- User Experience | How to Measure User Experience?
- The thing about npm
- 线程池分析
- JS prototype hasOwnProperty in Add method Prototype end point Inherit Override parent class method
- 小程序毕设作品之微信体育馆预约小程序毕业设计成品(4)开题报告
- NgRx Store createSelector 的单步调试和源代码分析
猜你喜欢
JS prototype hasOwnProperty in Add method Prototype end point Inherit Override parent class method
User Experience | How to Measure User Experience?
小程序毕设作品之微信体育馆预约小程序毕业设计成品(3)后台功能
SQL29 Calculate the average next day retention rate of users
LeetCode952三部曲之二:小幅度优化(137ms -> 122ms,超39% -> 超51%)
Delicious this year
Getting Started Database Days4
【C语言实现】最大公约数的3种求法
移动端人脸风格化技术的应用
03、GO语言变量定义、函数
随机推荐
Wechat Gymnasium Appointment Mini Program Graduation Design Finished Work (4) Opening Report
familiar friend
Mini Program Graduation Works WeChat Food Recipe Mini Program Graduation Design Finished Product (8) Graduation Design Thesis Template
论文解读(GSAT)《Interpretable and Generalizable Graph Learning via Stochastic Attention Mechanism》
【C语言实现】最大公约数的3种求法
PAM 回文自动机
Analysis of the development trend of game metaverse
【开源】Sentinel高性能高可用集群限流解决方案
教你VSCode如何快速对齐代码、格式化代码
How to prevent governance attacks in DAOs?
深度学习Course2第二周Optimization Algorithms习题整理
long investment career
一种灵活的智能合约协作方式
Wechat Gymnasium Reservation Mini Program Graduation Design Finished Work Mini Program Graduation Design Finished Product (2) Mini Program Function
PHP算法之电话号码的字母组合
Postman batch test interface detailed tutorial
Still struggling with reporting tool selection?To take a look at this
找工作必备!如何让面试官对你刮目相看,建议收藏尝试!!
number of solutions to solve a multivariate multi-degree equation
JS prototype hasOwnProperty in 加方法 原型终点 继承 重写父类方法