当前位置:网站首页>(2022牛客多校四)A-Task Computing (排序+动态规划)
(2022牛客多校四)A-Task Computing (排序+动态规划)
2022-08-01 04:47:00 【AC__dream】
题目:
样例输入:
5 2
1 2 3 4 5
12000 11000 10000 9000 8000
样例输出:
8.5000000000000000
题意:从n个任务中选m个(a1,a2,……,am)出来并任意排序,收益是
求最大收益。
看到公式的下标分布就知道这个值是跟任务的顺序有关的,所以我们应该先考虑按照什么样的顺序进行排序,一般常见的方法都是逐步调整法,就是说先假设一种顺序,然后交换其中两个相邻元素的位置,算出来两种排序方式对应的答案,进行比较就可以知道按照什么进行排序了!
下面是调整的过程:
我们先对n个任务按照上述方式进行排序,那么从里面任意选出来m个都是满足上述排序规则的,所以问题就转化为了我们如何从n个任务中选出来m个任务。
我一开始想的是利用f[i][j]表示前i个任务中选出j个任务所能得到的最大值,再利用一个mul[i][j]表示达到f[i][j]状态时的乘积,然后递推方程就是:
f[i][j]=max(f[i-1][j],f[i-1][j-1]+w[i]*mul[i-1][j-1]),再更新一下mul数组
但是后来发现这样是存在问题的,原因就在于我们从前i个任务中选j个任务所能得到的最大值并不一定是从前i-1个任务中选j-1个任务或者选j个任务得到的最大值,因为对于从前i-1个任务中选取的任务数不同会直接影响所选取任务的乘积,那么这个是会对答案造成很大影响的,所以不能这么做。
正解:
之所以我们刚才考虑的那种动态转移方程不行就是因为后面的值取决于前面的两个值,一个是乘积另一个是和式,所以我们无法直接根据和式的值来向后转移,但是通过上面的公式变形后我们可以清楚的发现,我们可以从后往前推导,这样前面值只跟后面的和式有关,而不再与乘积有关,这样我们就可以进行动态转移了,只是更新方式会略有变化,这种方法还是挺妙的。
设f[i][j]表示考虑第i~n个任务选出来j个任务的最大值,然后我们直接倒序更新该数组
动态转移方程:f[i][j]=max(f[i+1][j],f[i+1][j-1]*p[i].q+p[i].w)
下面是代码:
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<map>
#include<queue>
#include<vector>
#include<cmath>
using namespace std;
typedef long long ll;
const int N=1e5+10;
struct node{
ll w;
double q;
}p[N];
double f[N][30];//f[i][j]表示考虑第i~n个任务选出来m个任务的最大值
bool cmp(node a,node b)
{
return (a.w-b.q*a.w+a.q*b.w-b.w)>=0;
}
int main()
{
int n,m;
cin>>n>>m;
for(int i=1;i<=n;i++)
scanf("%lld",&p[i].w);
for(int i=1;i<=n;i++)
{
scanf("%lf",&p[i].q);
p[i].q/=10000;
}
sort(p+1,p+n+1,cmp);
for(int i=n;i>=1;i--)
for(int j=1;j<=m;j++)
f[i][j]=max(f[i+1][j],f[i+1][j-1]*p[i].q+p[i].w);
printf("%.8lf",f[1][m]);
return 0;
}
边栏推荐
- Passive anti-islanding-UVP/OVP and UFP/OFP passive anti-islanding model simulation based on simulink
- 6-23漏洞利用-postgresql代码执行利用
- typescript28-枚举类型的值以及数据枚举
- Unity's primary method for implementing PlanarReflection under the BuildIn rendering pipeline
- 智芯传感输液泵压力传感器 为精准智能控制注入科技“强心剂”
- 力扣(LeetCode)212. 单词搜索 II(2022.07.31)
- Dynamic Programming 01 Backpack
- TIM登陆时提示00001(TIM00001)
- typescript21 - Comparison of Interfaces and Type Aliases
- Message queue design based on mysql
猜你喜欢
出现Command ‘vim‘ is available in the following places,vim: command not found等解决方法
Simple and easy to use task queue - beanstalkd
Pyspark Machine Learning: Vectors and Common Operations
High Numbers | 【Re-integration】Line Area Score 880 Examples
"ArchSummit: The cry of the times, technical people can hear"
y83.第四章 Prometheus大厂监控体系及实战 -- prometheus告警机制进阶(十四)
typescript25-类型断言
leetcode:126. Word Solitaire II
Unknown Bounded Array
API设计笔记:pimpl技巧
随机推荐
Typescript20 - interface
干货!如何使用仪表构造SRv6-TE性能测试环境
出现Command ‘vim‘ is available in the following places,vim: command not found等解决方法
MLP neural network, GRNN neural network, SVM neural network and deep learning neural network compare and identify human health and non-health data
【愚公系列】2022年07月 .NET架构班 085-微服务专题 Abp vNext微服务网关
typescript26 - literal types
EntityFramework saves to SQLServer decimal precision is lost
Message Queuing Message Storage Design (Architecture Camp Module 8 Jobs)
Article summary: the basic model of VPN and business types
数据比对功能调研总结
The kernel's handling of the device tree
7月编程排行榜来啦!这次有何新变化?
[FPGA tutorial case 43] Image case 3 - image sobel edge extraction through verilog, auxiliary verification through MATLAB
typescript25 - type assertion
时时刻刻保持敬畏之心
Make your Lottie support word wrapping in text fields
typescript26-字面量类型
开源许可证 GPL、BSD、MIT、Mozilla、Apache和LGPL的区别
RSA主要攻击方法
「以云为核,无感极速」顶象第五代验证码