Codeforces Round #164 (Div. 2)E Playlist(贪心)

题目链接: 传送门

Playlist

time limit per test:1 second     memory limit per test:256 megabytes

Description

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:

  • 1、Manao listens to A, he likes it, he remembers it.
  • 2、Manao listens to B, he does not like it, so he listens to A, again.
  • 3、Manao listens to C, he likes the song and he remembers it, too.
  • 4、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.

Input

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.

Output

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


4
300 0
300 50
240 50
360 80

Sample Output

537.500000000


2121.000000000

解题思路

  • 1.Consider any two songs which are at positions i and j (i < j) in the playlist. If Manao liked song i and disliked song j, then song i will be listened to again. Therefore, with probability,code> p[i](1-p[j]),/code> the process length will increase by L[i]. The sum of,code> L[i]p[i](1-p[j]) over all pairs (plus the length of all songs since Manao listens to them at least once) is the expected length for the fixed sequence.
    So we have that if there are two songs (l1, p1) and (l2, p2), the first one should be placed earlier in the playlist if l1
    p1(1-p2)>l2p2*(1-p1) and later otherwise.
  • 2.Suppose we have fixed j and are counting the contribution of song j towards the answer if Manao dislikes it. This value is(l1p1 + l2p2 + ... + l[j-1]p[j-1]). For j+1, the corresponding value will be(l1p1+...+l[j-1]p[j-1]+l[j]p[j]). It turns out that these values differ in only a single summand, so we can compute each of them in O(1)
    一开始按照直观感觉来排序,果然交上去就wa,还是刷题太少,这些题目都是套路啊
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

struct Node{
	double val,rate;
};

bool cmp(Node x,Node y)
{
	return x.val*x.rate*(1-y.rate) > y.val*y.rate*(1-x.rate);
}


int main()
{
	int N;
	double sum = 0,tmp = 0;
	Node node[50005];
	memset(node,0,sizeof(node));
	scanf("%d",&N);
	for (int i = 0;i < N;i++)
	{
		scanf("%lf%lf",&node[i].val,&node[i].rate);
		node[i].rate /= 100;
	}
	sort(node,node+N,cmp); 
	for (int i = 0;i < N;i++)
	{
		sum += node[i].val;
		sum += tmp*(1-node[i].rate);
		tmp += node[i].val*node[i].rate;
	}
	printf("%.9lf\n",sum);
}
posted @   zxzhang  阅读(317)  评论(0编辑  收藏  举报
编辑推荐:
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
点击右上角即可分享
微信分享提示

目录导航