当前位置:网站首页>(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;
}
边栏推荐
猜你喜欢
typescript22-接口继承
Message queue design based on mysql
【愚公系列】2022年07月 Go教学课程 024-函数
产品经理访谈 | 第五代验证码的创新与背景
Excel做题记录——整数规划优化模型
Software Testing Weekly (Issue 82): In fact, all those who are entangled in making choices already have the answer in their hearts, and consultation is just to get the choice that they prefer.
7 行代码搞崩溃 B 站,原因令人唏嘘!
Invalid classes inferred from unique values of `y`. Expected: [0 1 2], got [1 2 3]
Simple and easy to use task queue - beanstalkd
认真对待每一个时刻
随机推荐
Passive anti-islanding-UVP/OVP and UFP/OFP passive anti-islanding model simulation based on simulink
/etc/fstab
PAT乙级 1002 写出这个数
lambda
JS new fun(); class and instance JS is based on object language Can only act as a class by writing constructors
This article takes you to understand the past and present of Mimir, Grafana's latest open source project
Flink 1.13 (8) CDC
项目风险管理必备内容总结
MySQL3
Immutable
56:第五章:开发admin管理服务:9:开发【文件上传到,MongoDB的GridFS中,接口】;(把文件上传到GridFS的SOP)
The Flow Of Percona Toolkit pt-table-checksum
typescript23-元组
Message queue design based on mysql
The difference between scheduleWithFixedDelay and scheduleAtFixedRate
Input input box cursor automatically jumps to the last bug after the previous input
智芯传感输液泵压力传感器 为精准智能控制注入科技“强心剂”
typescript28 - value of enumeration type and data enumeration
Excel record of integer programming optimization model to solve the problem
产品经理访谈 | 第五代验证码的创新与背景