E. Playlist

Original link: http://www.cnblogs.com/liulangye/archive/2013/01/30/2882490.html

http://codeforces.com/contest/268/problem/E

This question is the key to advance to sort Suppose there are two adjacent order

(L1, p1) (l2, p2)

In this case time is four possibilities and

1, (l1 + l2) * p1 * p2

2, (2 * l1 + l2) * p1 * (1-p2)

3, (l1 + l2) * (1-p1) * p2

4, (l1 + l2) * (1-p1) * (1-p2)

In reverse order, then (l2, p2) (l1, p1)

1, (l2 + l1) * p1 * p2

2, (2 * l2 + l1) * p2 * (1-p1)

3, (l2 + l1) * (1-p2) * p1

4, (l2 + l1) * (1-p2) * (1-p1)

The first order is all we have chosen to be greater than the second in order to obtain after simplification

l1*(p1-p1*p2)>l2*(p2-p1*p2)

This is the sort of function

After sorting the search again from beginning to end is calculated based on the probability of the total time

Code:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<map>
#include<vector>
#include<stack>
#include<set>
#include<map>
#include<queue>
#include<algorithm>
#include<cmath>
#define LL long long
//#pragma comment(linker, "/STACK:1024000000,1024000000")
using namespace std;
const int INF=0x3f3f3f3f;
//const int MOD=1000000009;
const int N=50005;
struct node
{
    int l;
    double p;
}mem[N];
double t[N];
bool cmp(node x,node y)
{
    return (x.l*(x.p-x.p*y.p)>y.l*(y.p-x.p*y.p));
}
int main()
{
    //freopen("data.in","r",stdin);
    int n;
    while(cin>>n)
    {
        for(int i=1;i<=n;++i)
        {
            cin>>mem[i].l>>mem[i].p;
            mem[i].p=mem[i].p/100.0;
        }
        sort(mem+1,mem+n+1,cmp);
        t[0]=0.0;
        double ans=0.0;
        for(int i=1;i<=n;++i)
        {
            t[i]=mem[i].l*mem[i].p+t[i-1];
            ans+=(mem[i].l*mem[i].p+(1.0-mem[i].p)*(t[i-1]+mem[i].l));
        }
        printf("%.9lf\n",ans);
    }
    return 0;
}

  

 

Reproduced in: https: //www.cnblogs.com/liulangye/archive/2013/01/30/2882490.html

Guess you like

Origin blog.csdn.net/weixin_30588907/article/details/94791811