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

探讨了一个涉及游戏关卡顺序优化的问题,旨在通过合理安排关卡顺序来最小化完成所有关卡的期望时间。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

"C" Magic Artifact
Time limit 2 seconds
Memory limit 256 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
2
3
10 5 10000000
5 3 0
7 3 0
4
3 1 2500000
4 1 2500000
10 1 2500000
2 1 2500000
Output data
16
10.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;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值