You have an upcoming trip to Washington D.C. and you are fascinated with the intricacies of Congressional committee hearings. You wish to attend as many hearings as possible during your trip, and your local representative has provided you with a pass that will get you into the audience of any hearing. But there are some challenges in planning your schedule. Specifically:
-
There are many committees, and thus many hearings, some of which take place at overlapping times.
-
While the committees are extremely punctual in terms of when to start a hearing, they are notoriously unpredictable in terms of how long the hearing lasts. Fortunately, rules do not actually allow for a filibuster of a committee hearing, so they cannot last forever.
-
It is considered rude to enter a hearing that is already underway, or to leave a hearing before it is complete. Given that you do not wish to embarrass the representative who provided your tickets, if you attend you must attend the entire hearing. Fortunately, hearings are very near to each other; as soon as one hearing is done, you can immediately join another hearing that is about to start.
Well in advance of your trip, Congress publishes a schedule of hearings, indicating for each one the time ss at which the hearing will start, and then values aa and bbwhich represent, respectively, the shortest and longest possible length of that particular hearing. You are to assume that the actual length of the hearing will be a uniformly random integer over the inclusive interval [a,b][a,b].
Your goal is to develop a strategy that maximizes the expected number of hearings that you can attend during your trip. As an example, consider a situation in which there are four hearings with parameters as follows:
hearing | ss | aa | bb |
Social media and elections | 1 | 1 | 7 |
NASA missions | 3 | 2 | 3 |
Oil and gas exploration | 5 | 1 | 4 |
Hurricane recovery efforts | 6 | 10 | 10 |
For this schedule, the optimal strategy will allow you to achieve an expected value of 2.1252.125 hearings. To achieve this, you begin by attending the NASA hearing, which starts at time 33 and ends with equal probability at either time 55 or time 66 (given the hearing length that is uniformly distributed over {2,3}{2,3}). If the NASA hearing does end at time 55 you will immediately head to the oil and gas exploration hearing, and there is a 1414 chance that hearing will end at time 66, allowing you to make yet a third hearing (about hurricane recovery efforts). If the NASA hearing instead ends at time 66, you will go straight to the hurricane hearing.
By this strategy you will attend 33 hearings 12.5% of the time and 22 hearings the other 87.5% of the time, and thus expected value of 2.1252.125. Note that if you were to start by attending the social media and elections hearing, you might optimistically make four hearings. However, a careful analysis will demonstrate that if you attend the first hearing, your optimal expected value is only 2.107142.10714.
Input
The input begins with an integer nn that designates the total number of scheduled hearings (1≤n≤1041≤n≤104). Following that are nn lines, each containing three integers ss, aa, and bb, respectively representing the start time, minimum length, and maximum length of a hearing, such that 1≤s≤1061≤s≤106 and 1≤a≤b≤1061≤a≤b≤106. The hearings will be listed in nondecreasing order of their start times.
Output
Display the expected number of hearings of an optimal strategy. Your answer should have an absolute or relative error of at most 10−310−3.
Sample Input 1 | Sample Output 1 |
---|---|
4 1 1 7 3 2 3 5 1 4 6 10 10 | 2.125 |
Sample Input 2 | Sample Output 2 |
---|---|
5 1 1 7 1 1 6 3 2 3 5 1 4 6 10 10 | 2.29166667 |
题意:
题意:每场听证会都有一个起始时间与开会最短时间与最长时间,问最多能听几场听证会的数学期望
思路:可以发现,一场听证会后面的听证会才会影响该场的数学期望,所以我们从后往前扫描,因为存在最早完成时间和最晚完成时间,所以对于每一场听证会,它的dp[i]为参加自身的1加上在它最早结束时间到最晚结束时间内的开始的听证会的dp[j]的值乘对应的发生概率
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <map>
#include <set>
#include <queue>
#include <cmath>
using namespace std;
const int MAXN=1e6+5;
struct node
{
int x;
int be;
int en;
}a[MAXN];
double dp[MAXN];
int main()
{
int n;
scanf("%d",&n);
memset(dp,0,sizeof(dp));
for(int i=0;i<n;i++)
scanf("%d%d%d",&a[i].x,&a[i].be,&a[i].en);
for(int i=n-1;i>=0;i--){
int fir=a[i].x+a[i].be;// 最早的时间
int las=a[i].x+a[i].en;// 最晚的时间
int j=n-1,com; //
double maxx=0;
dp[i]++;
int len=las-fir+1; //得到时间段
while(las>=fir){ //进行计算
while(j>i){
if(a[j].x<las)
break;
maxx=max(maxx,dp[j]); //更新这一秒的最大值
j--;
}
com=max(fir,a[j].x+1);
dp[i]+=1.0*maxx*(las-com+1)/len; //代入
las=com-1;
}
}
double sum=0;
for (int i=0;i<n;i++)
sum=max(sum,dp[i]);
printf("%f\n",sum);
return 0;
}