01背包
Task Computing
As it says, Time is Money, Efficiency is Life. A client has a computing task waiting for uploading to the cloud servers. However, due to the resource limits and many other tasks, a task usually cannot be worked on a single server but multiple servers, one after another. You, as an employee of a cloud server provider who specializes in task allocation, need to select several servers from a limited number of cloud servers to perform calculations. We know that even the same servers will perform differently in different environments.
There are nn cloud servers available in the vicinity of the client’s region numbered from $1$ to $n$. The ii-th cloud server has two parameters: $w_i$ and $p_i$, which indicate the computing power and transmission efficiency of that server, respectively. The supervisor requires that each task must be computed by exactly $m$ of these servers. The client’s computational task is not parallelizable, so you must sequence the work of the $m$ servers to maximize the total computational efficiency. We define the total computational efficiency as follows:
$\sum_{i=1}^m {w_i} \prod_{j=0}^{i-1} {p_i}$
输入描述
The first line contains two integers $n$,$m$ ($1 \le n \le 10_{5}$ , $1 \le m \le {min(n,20)}$) denoting the number of cloud servers and servers that you have to select.
The second line contains $n$ integers $w_1$,$w_2$,$\ldots$, $w_n$,($1 \le {w_i} \le 10_{9}$), denoting the servers’ computing power.
The third line contains $n$ integers $q_1$,$q_2$,$\ldots$, $q_n$ ($8000 \le q_i \le 12000$)
where $p_i$ =$\frac {q_i}{10000}$ denotes the ii-th server’s transmission efficiency.
输出描述
Output a float number denoting the maximal total computational efficiency. Your answer is considered correct if the relative or absolute error between yours and the standard solution is not greater than $10^{-6}$
样例1
输入
1 | 5 2 |
输出
1 | 8.500000000 |
思路
1.首先,每个机器选取的先后顺序会影响其价值,先对其进行排序。
设两个机器分别为 $x,y$,
当$x$在前,$y$在后的价值为 : $w_x * p_k + w_y * p_x * p_k$
当$y$在前,$y$在后的价值为 : $w_y * p_k + w_x * p_y * p_k$
令 式1>式2 得 :
$w_x + w_y * p_x > w_y + w_x * p_y$,按照该式子排序
2.排序后,不论从前到后还是从后到前,均为一个有顺序的子序列,所以前后方向不重要。
3.越靠后面选择的,$p$的影响更大。
设三个设备$1,2,3$,则价值为
$w_1 + w_2 * p_1 + w_3 * p_1 * p_2 = p_1 * (w_2 + w_3 * p_2) + w_1$
以此类推
因此从最后选的开始更新,价值越小,排序越靠后,越先更新,即$(n~1)$
4.dp方程 $f[i][k]$ 表示考虑到 第 i 个, 装了 m 个的最大值
转移方程: $f[i][k]=max(f[i][k],f[i+1][k-1] * p[i] + w[i])$
CODE
1 |
|
线性DP
P1681 最大正方形-二
CODE
1 |
|
数位DP
数字计数问题
思路
设计一个 $Count$ 函数,用来统计$ [1,num] $中,$x$ 的个数,
则每一个数码的答案就是 $Count(b,x)- Count(a-1,x)$,类似于前缀和的想法。
其他函数解释
$pow10(x)$ ,用来求 $10^x$。
$get(q,L,R)$, 用来求某个数中,处在$[L,R]$位置上的数值为多少。
Count函数
假设求数字 $abcdefg$ 中 $d$ 位置 $x$ 的个数(先不考虑$0$)
分两种情况:
前面选取 $[000,abc-1]$($0$ 则是 $[001,abc-1]$),后面可以随便选取,即$[000,efg]$,总数:$ans+=abc*1000$。
当前面选取 $abc$,则要继续分类讨论 $d$ 与 $x$ 的大小关系
1). 当 $d<x$ 时,数字不在范围内,不符合要求
2). 当 $d=x$ 时,后面可取 $[000,efg]$,即 $ans+=efg+1$
3). 当 $d>x$ 是,后面可取 $[000,999]$,即 $ans+=1000$
最后,对于每一个数字,循环一下每一个位置,就是的 $Count$ 的值了。
记得开 $long long$ ! ! !
CODE
1 |
|