题目链接:

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

E. Playlist

time limit per test 1 second
memory limit per test 256 megabytes
#### 问题描述
> Manao's friends often send him new songs. He never listens to them right away. Instead, he compiles them into a playlist. When he feels that his mind is open to new music, he opens the playlist and starts to listen to the songs.
>
> Of course, there are some songs that Manao doesn't particuarly enjoy. To get more pleasure from the received songs, he invented the following procedure of listening to the playlist:
>
> If after listening to some song Manao realizes that he liked it, then he remembers it and starts to listen to the next unlistened song.
> If after listening to some song Manao realizes that he did not like it, he listens to all the songs he liked up to this point and then begins to listen to the next unlistened song.
> For example, if Manao has four songs in the playlist, A, B, C, D (in the corresponding order) and he is going to like songs A and C in the end, then the order of listening is the following:
>
> Manao listens to A, he likes it, he remembers it.
> Manao listens to B, he does not like it, so he listens to A, again.
> Manao listens to C, he likes the song and he remembers it, too.
> Manao listens to D, but does not enjoy it and re-listens to songs A and C.
> That is, in the end Manao listens to song A three times, to song C twice and songs B and D once. Note that if Manao once liked a song, he will never dislike it on a subsequent listening.
>
> Manao has received n songs: the i-th of them is li seconds long and Manao may like it with a probability of pi percents. The songs could get on Manao's playlist in any order, so Manao wants to know the maximum expected value of the number of seconds after which the listening process will be over, for all possible permutations of the songs in the playlist.
#### 输入
> The first line contains a single integer n (1 ≤ n ≤ 50000). The i-th of the following n lines contains two integers, separated by a single space — li and pi (15 ≤ li ≤ 1000, 0 ≤ pi ≤ 100) — the length of the i-th song in seconds and the probability that Manao will like the song, in percents.
#### 输出
> In a single line print a single real number — the maximum expected listening time over all permutations of songs. The answer will be considered valid if the absolute or relative error does not exceed 10 - 9.
#### 样例
> **sample input**
> 3
> 150 20
> 150 50
> 100 50
>
> **sample output**
> 537.500000000

题意

有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\n", 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\n",ans);
return 0;
} //end-----------------------------------------------------------------------

Codeforces Round #164 (Div. 2) E. Playlist 贪心+概率dp的更多相关文章

  1. Codeforces Round #388 (Div. 2) 749E(巧妙的概率dp思想)

    题目大意 给定一个1到n的排列,然后随机选取一个区间,让这个区间内的数随机改变顺序,问这样的一次操作后,该排列的逆序数的期望是多少 首先,一个随机的长度为len的排列的逆序数是(len)*(len-1 ...

  2. Codeforces Round #367 (Div. 2) C. Hard problem(DP)

    Hard problem 题目链接: http://codeforces.com/contest/706/problem/C Description Vasiliy is fond of solvin ...

  3. Codeforces Round #272 (Div. 2) B. Dreamoon and WiFi dp

    B. Dreamoon and WiFi 题目连接: http://www.codeforces.com/contest/476/problem/B Description Dreamoon is s ...

  4. Codeforces Round #202 (Div. 1) A. Mafia 贪心

    A. Mafia Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/348/problem/A D ...

  5. Codeforces Round #164 (Div. 2)

    A. Games 模拟. B. Buttons 简单计数. C. Beautiful Sets of Points 显然每行每列只能有一个点,那么最大点数为\(1+min(n, m)\). 在不考虑\ ...

  6. Codeforces Round #382 (Div. 2)B. Urbanization 贪心

    B. Urbanization 题目链接 http://codeforces.com/contest/735/problem/B 题面 Local authorities have heard a l ...

  7. Codeforces Round #180 (Div. 2) B. Sail 贪心

    B. Sail 题目连接: http://www.codeforces.com/contest/298/problem/B Description The polar bears are going ...

  8. Codeforces Round #192 (Div. 1) A. Purification 贪心

    A. Purification Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/329/probl ...

  9. Codeforces Round #274 (Div. 1) A. Exams 贪心

    A. Exams Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/480/problem/A Des ...

随机推荐

  1. C# Datetime类常用技巧

    C#类常用技巧 //今天DateTime.Now.Date.ToShortDateString();//昨天,也就是今天的日期减一DateTime.Now.AddDays(-1).ToShortDat ...

  2. Kettle 合并记录报错!

    在Kettle的合并记录过程的时候,在“为了转换解除补丁开始 ”这一步的时候报错.具体错误如图所示: Kettle的转换如图所示: 问题原因:可能是你的数据库链接驱动和Kettle的版本不兼容. 解决 ...

  3. (转载)前端构建工具gulp使用

    前端构建工具gulp使用 前端自动化流程工具,用来合并文件,压缩等. Gulp官网 http://gulpjs.com/ Gulp中文网 http://www.gulpjs.com.cn/ Gulp中 ...

  4. php中关于抽象(abstract)类和抽象方法的问题解析

    在面向对象(OOP)语言中,一个类可以有一个或多个子类,而每个类都有至少一个公有方法作为外部代码访问的接口.而抽象方法就是为了方便继承而引入的,现在来看一下抽象类和抽象方法分别是如何定义以及他们的特点 ...

  5. uglifyjs压缩JS

    一.故事总有其背景 年末将至,很多闲适的时间,于是刷刷微博,接触各种纷杂的信息——美其名曰“学习”.运气不错,遇到了一个新名词,uglifyjs. 据说是用来压缩JS文件的,据说还能优化JS,据说是基 ...

  6. python时间-time模块

    time是python自带的模块,用于处理时间问题,提供了一系列的操作时间的函数. 以下说明针对于 python2.7,其他版本可能有所差异. 模块提供了两个种表示时间的格式: 1.时间戳,是以秒表示 ...

  7. WPF 多项选择下拉菜单

    背景 项目中有一个多项选择筛选的功能, 由于筛选条件太多, 用户又习惯在平板上进行操作, 所以要求我们把checkbox 放到一个combobox里面, 然后checkbox的选项要在combobox ...

  8. 部分手机不能连PC adb

    http://www.th7.cn/Program/java/201407/232139.shtml 1. 命令行中执行 android update adb [这一步的目的是产生下面第二步的路径和文 ...

  9. microsoft azure 速度测试网址

    http://www.azurespeed.com/ 选择你附近的区域.可以使用 azurespeed.com 查找延迟最低的数据中心.

  10. ZLG_GUI配置与函数介绍

    http://www.docin.com/p-825479457.html ZLG_GUI配置与函数介绍