CFgym:Magic Artifact(概率期望 & 思维)
来源:互联网 发布:西门子仿真软件 编辑:程序博客网 时间:2024/06/04 19:04
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 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 ai, bi 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.
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.
2310 5 100000005 3 07 3 043 1 25000004 1 250000010 1 25000002 1 2500000
1610.25
思路:首先要知道这个期望值怎么算,令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;}
- CFgym:Magic Artifact(概率期望 & 思维)
- CFgym:Treasure Island(搜索 & 思维)
- 概率&期望(存档)
- LIGHTOJ 1027(概率 - 期望)
- hdu 5162(概率期望)
- HDU_5781_ATM_Mechine(概率期望dp)
- 概率 期望
- 期望概率
- bzoj4318 OSU!(期望概率DP,期望的线性性)
- CFgym:Castle(KMP)
- CFGym
- CFGym
- CFGym
- CFGym
- CFGym
- ZOJ_3569_Dr. Zomboss's Revenge(概率期望)
- LA 4485 Crossing Rivers(概率,期望)
- hdu 4465 Candy (概率期望)
- ★实验10-2 1.编写程序测试sigaction()函数。 2.sa_flags 设置为以下标志时,程序的行为有何不同。 对比程序运行情况予以说明。 SA_SIGINFO SA_NOCLDST
- JVM虚拟机
- 2000:最长公共子上升序列
- css的选择器
- 洛谷P1010 幂次方
- CFgym:Magic Artifact(概率期望 & 思维)
- 字典树模板 hdu1251统计难题
- Eclipse调试器和debug视图
- 【shell】find命令及常见用法
- 51单片机学习之路
- 批处理自动填写密码
- ubuntu: Error installing pg解决办法
- 数据结构封装之《LinkQueue链式队列》
- UVa1112