CFgym:Magic Artifact(概率期望 & 思维)

来源:互联网 发布:西门子仿真软件 编辑:程序博客网 时间:2024/06/04 19:04

"C" Magic Artifact
Time limit2 secondsMemory limit256 megabytes

Maxim is playing a video game. It has n levels, numbered from 1 to n. Levels can be completed in any order, it takes Maxim ai seconds to complete the i-th level.

Maxim can find a magic artifact at one of the levels. There is exactly one magic artifact in the game, and once found it will increase the speed of Maxim's hero and reduce the time needed to complete the level. However, it is not known where the artifact is, the probability that it is at the i-th level is pi. The time needed to complete the i-th level after the artifact is found is bi second (bi ≤ ai). Note that artifact doesn't reduce the time needed to complete the level where it is found.

Maxim wants to choose the order he completes the levels, to minimize the expected time to complete the game. Help him to find the minimal possible expected time. Maxim must choose the order to complete the levels before playing the game, the order must not depend on whether the artifact was found or not at some level.

Recall that the expectation of a random variable is the sum over all possible outcomes of a product of the probability of such outcome and the value of the variable. In this problem the outcome corresponds to the level where the artifact is, and the value is the total time needed if the artifact is at that level.

Input format

Input data contains several test cases. The first line contains t — the number of test cases (1 ≤ t ≤ 1000).

Each test case is described in the following way: the first line contains integer n — the number of levels (1 ≤ n ≤ 105).

The following n lines describe levels. Each level is specified with three integers aibi and xi — the time to complete the level before the artifact was found, the time to complete it after the artifact was found, and the value that helps to find the probability to find the artifact at that level. The probability is calculated using the formula pi = xi / 107 (1 ≤ bi ≤ ai ≤ 105; 0 ≤ xi ≤ 107; the sum of all xi is 107).

The sum of values of n in all test cases of one input data is at most 5·105.

Output format

For each test case output one floating point value — the expected time to complete the game if the optimal order was chosen. The answer must have an absolute or relative error of at most 10 - 6.

Examples
Input data
2310 5 100000005 3 07 3 043 1 25000004 1 250000010 1 25000002 1 2500000
Output data
1610.25
题意:有N个关卡,有一个神器存在某个关卡中,每个关卡有获得神器前后的通关时间,和神器在此关卡的概率,问最合理的安排关卡顺序下完成这N关的最小期望时间。

思路:首先要知道这个期望值怎么算,令ci=ai-bi,假如是从1~N的顺序通关,有time = b1+b2+...+bn+p1*c1+p2*(c1+c2)+p3*(c1+c2+c3)+...+pn(c1+c2+...+cn)。尝试调换第i和第i+1关的顺序,发现式子的变化仅为pi+1*ci变成pi*ci+1,那么就变成贪心问题了,把pi/ci大的放到前面即可,pi=ci=0的关卡特殊处理,他会对排序造成干扰,这些关卡放到哪里都一样。

# include <bits/stdc++.h>using namespace std;const int maxn = 1e5+30;typedef long long LL;struct node{    int p, c;}arr[maxn], brr[maxn];int t, n;bool cmp(node a, node b){    if(a.c == 0) return true;    if(b.c == 0) return false;    return a.p*1.0/a.c > b.p*1.0/b.c;}int main(){    scanf("%d",&t);    while(t--)    {        int a, b, c, cnt=0, cnt2=0;        double ans = 0, sum=0;        scanf("%d",&n);        for(int i=1; i<=n; ++i)        {            scanf("%d%d%d",&a,&b,&c);            ans += b;            if(a-b+c) arr[cnt++] = {c, a-b};            else brr[cnt2++] = {0, 0};        }        sort(arr,arr+cnt,cmp);        for(int i=0; i<cnt2; ++i) arr[cnt++] = brr[i];        for(int i=0; i<cnt; ++i)        {            sum += arr[i].c;            ans += sum*arr[i].p*1.0/1e7;        }        printf("%.10f\n",ans);    }    return 0;}



原创粉丝点击