题目链接:
http://codeforces.com/problemset/problem/268/E
E. Playlist
memory limit per test 256 megabytes
题意
有n首歌,每首听完耗时为li,喜欢它的概率为pi,每首歌至少听一遍,如果听到不喜欢听的歌,会把所有听过的喜欢听的歌都重新听一遍,问决定一个听歌的顺序使得听完歌的期望时间最大。
题解
贪心来决定听歌的顺序。
求期望用期望dp做。(全概率递推一下)
官方题解
代码
#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn = 55555;
struct Node {
double l, p;
bool operator < (const Node& tmp) const {
return l*p*(1 - tmp.p)>tmp.l*tmp.p*(1 - p);
}
}nds[maxn];
int main() {
int n;
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%lf%lf", &nds[i].l, &nds[i].p);
nds[i].p /= 100;
}
//贪心
sort(nds, nds + n);
double ansExp = 0, lovedLenExp = 0;
for (int i = 0; i < n; i++) {
ansExp += nds[i].l;
//第i个对答案的贡献
ansExp += nds[i].p * 0 + (1 - nds[i].p)*lovedLenExp;
//根据全期望公式有E[i]=pi*(E[i-1]+Li)+(1-pi)*E[i-1]=E[i-1]+pi*Li。 其中E[i]表示前i个的lovedLenExp。
lovedLenExp += nds[i].p*nds[i].l;
}
printf("%.15lf
", ansExp);
return 0;
}
纪念版
#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<stack>
#include<ctime>
#include<vector>
#include<cstdio>
#include<string>
#include<bitset>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
using namespace std;
#define X first
#define Y second
#define mkp make_pair
#define lson (o<<1)
#define rson ((o<<1)|1)
#define mid (l+(r-l)/2)
#define sz() size()
#define pb(v) push_back(v)
#define all(o) (o).begin(),(o).end()
#define clr(a,v) memset(a,v,sizeof(a))
#define bug(a) cout<<#a<<" = "<<a<<endl
#define rep(i,a,b) for(int i=a;i<(b);i++)
#define scf scanf
#define prf printf
typedef int LL;
typedef vector<int> VI;
typedef pair<int,int> PII;
typedef vector<pair<int,int> > VPII;
const int INF=0x3f3f3f3f;
const LL INFL=10000000000000000LL;
const double eps=1e-9;
const double PI = acos(-1.0);
//start----------------------------------------------------------------------
const int maxn=5e4+10;
struct Node{
int l,p;
bool operator < (const Node& tmp) const {
return (100-tmp.p)*l*p>(100-p)*tmp.l*tmp.p;
}
}nds[maxn];
int n;
int main() {
scf("%d",&n);
rep(i,0,n) scf("%d%d",&nds[i].l,&nds[i].p);
sort(nds,nds+n);
double sum=0,ans=0;
rep(i,0,n){
ans+=nds[i].l;
ans+=(1-nds[i].p*1.0/100)*sum;
sum+=nds[i].l*(nds[i].p*1.0/100);
}
prf("%.9lf
",ans);
return 0;
}
//end-----------------------------------------------------------------------