目录
M Dirty Work (数学期望/贪心)<签到题>
I Impatient Patient(数学期望)<签到题>
原题地址:PTA | 程序设计类实验辅助教学平台 (pintia.cn)
It is another ICPC contest. Your teammates sketched out all solutions to the problems in a fraction of a second and went away to celebrate, but now you are stuck at implementing them!
There are n problems in total, the i-th of which will take ai seconds to implement. You have the flexibility to choose any unsolved problem to implement, either at the beginning of the contest or right after solving the previous problem.
The total penalty is calculated in a similar manner to ordinary ICPC contests. If you submit and pass problem i at time ti, the total penalty is ∑i=1nti. Note that unsuccessful submissions are omitted from penalty calculations for the sake of convenience.
After completing the implementation of a problem, you will submit it immediately. However, there is a probability pi that your code for the i-th problem fails. In such cases, it will take additional bi seconds to fix and re-implement the code, and you must address the issue promptly without switching to another problem. It is guaranteed that the second attempt will always succeed.
Now you wonder what the minimal expected total penalty is if you employ an optimal strategy for selecting the next problem to implement.
Input
The first line contains a positive integer T (1≤T≤104), denoting the number of testcases.
For each testcase:
- The first line contains one integer n (1≤n≤5000), denoting the number of problems.
- The following n lines each contain two integers ai and bi (1≤ai,bi≤104), as well as one decimal number pi (0≤pi≤1). These values represent the time required for the first and second attempts, respectively, as well as the probability of failure for the first attempt. The decimal number pi has exactly one decimal place.
It is guaranteed that ∑n≤106, and ∑i=1n(ai+bi)≤18000 for each testcase, so that you can always solve all problems in five hours.
Output
For each testcase, print one decimal number, denoting the minimal expected total penalty if you employ an optimal strategy for selecting the next problem to implement. The answer will be considered correct if its absolute or relative error is less than 10−9.
Sample Input
3 3 6 1 1.0 5 1 1.0 4 1 1.0 2 4 100 0.9 70 1 0.5 7 222 848 0.4 501 640 0.6 1534 495 0.7 1132 1433 1.0 1552 171 1.0 1777 25 0.2 1325 2325 0.8
Sample Output
34.000000000000 235.000000000000 38937.900000000001
Notes
In the first test case, you should tackle problem 3 first, followed by problem 2, and finally problem 1.
代码长度限制
64 KB
时间限制
1000 ms
内存限制
512 MB
题目大意:
共有n道题,每道题有(1-pi)的概率需要 ai 的时间AC,有 pi 的概率需要(ai+bi)的时间AC,
现给出n道题的ai,bi,qi,求做完所有题目需要的最小期望罚时。
根据ICPC规则,对于第i道题,它的罚时为从比赛开始直到它被AC的总时间。
思路:
算出每道题AC耗时的期望 p[i] 再对期望从小到大排序。
代码:
#include
#include
using namespace std;
const int N=5010;
double p[N];
void solve(int n1)
{
int a,b;
double c;
for(int i=0;i>a>>b>>c;
p[i]=a*(1-c)+(a+b)*c;
}
sort(p,p+n1);
double sum=0,ans=0;
for(int i=0;i>n;
int n1;
for(int i=0;i>n1;
solve(n1);
if(i!=n-1)
cout<
You accidentally sprained your ankle, and now you are facing a long recovery phase. Initially, you are at stage 0, and your recovery progresses until you reach stage n.
Each day, if you rest properly, you advance by exactly one stage. So it takes n days for you to recover, that is, if you do not do anything improper.
However, instead of resting, you have the option to challenge yourself, which also takes one day. If you are at stage i and succeed, you will instantly recover. However, if you fail, you will regress to stage ai (0≤ai≤i). The probability of success is pi.
Now, you are wondering what the expected time required for your recovery would be, assuming you adopt the best strategy.
Input
The first line contains a positive integer T (1≤T≤104), denoting the number of test cases.
For each test case:
- The first line contains one integer n (1≤n≤5×105), denoting the number of stages of recovery.
- The next line contains n integers a0,a1,⋯,an−1 (0≤ai≤i), denoting the stage you will go back to if you fail at stage i.
- The next line contains n integers q0,q1,⋯,qn−1 (0≤qi≤105), where pi=qi/105 denotes the probability of success at stage i.
It is guaranteed that ∑n≤5×105.
Output
For each test case, print one decimal number, denoting the expected time needed for you to recover if you take the best strategy. The answer will be considered correct if its absolute or relative error is less than 10−9.
Sample Input
5 1 0 0 3 0 1 2 99999 0 0 3 0 1 2 0 50001 100000 5 0 1 2 0 1 21735 25194 37976 89936 99999 8 0 0 2 2 4 3 4 1 12839 27084 17777 35472 31951 64686 96898 0
Sample Output
1.000000000000 1.000010000100 2.999960000800 4.447607187333 7.096039133935
代码长度限制
64 KB
时间限制
1000 ms
内存限制
512 MB
题目大意:
共n个阶段,要从第0个阶段到达第n个阶段,有两种方法:(1)每天走一个阶段,第n天后到达第n个阶段。(2)开启“挑战” 模式,成功直接到第n阶段,成功概率为p/1e5;失败则回到第a[i]阶段.
思路:
最优策略下,只会在一个点挑战,该点成功即到达第n阶段,枚举每个点作为挑战点,取最小值。
代码:
#include
using namespace std;
const int N=500010;
const int W=1e5;
double dp[N];
int a[N];
long long int p[N];
void solve()
{
int n1;
cin>>n1;
for(int i=0;i>a[i];
for(int i=0;i>p[i];
double res=n1;
for(int i=0;i>n;
while(n--)
{
solve();
if(n)
cout<