codeforces - Gym101257G - 24

题目链接:点击打开链接

题目:

24
time limit per test
0.5 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Round 16,777,216 of Codeforce-is has just finished, and all the contestants were able to pass the pretests in problem A.

Maike, the founder of Codeforce-is wants to measure how weak the tests for problem A were, so he became interested in finding the amount of changes that will occur on the contest’s scoreboard after the system tests.

Given the score for problem A, the score of each contestant before the system tests, and the probability of failing the system tests for each contestant’s solution, can you help Maike find the expected number of pairs of contestants that will swap their relative order after the system tests? That is, the number of pairs of contestants ij such that the scorei  >  scorej before the system tests, but scorei  <  scorejafter the system tests.

Input

The first line of input consists of two integers, N (2 ≤ N ≤ 105), and SA (1 ≤ SA ≤ 105), the number of contestants in the round, and the score the contestants achieved for passing the pretests of problem A.

The second line contains N space-separated integers, the Kth integer is TK (SA ≤ TK ≤ 105) which represents the total score in the contest for the Kth contestant before the system tests, Ti  ≥  Ti + 1 for (1 ≤ i < N).

The third and final line contains N space-separated numbers, the Kth number is PK (0.01 ≤ PK ≤ 1.00) which represents the probability that the Kth contestant's solution will fail the system tests. Numbers are given with exactly two digits after the decimal point.

Output

On a single line, output the expected number of pairs of contestants that will swap their relative order after the system tests are over.

Examples
input
2 10
25 20
0.50 0.50
output
0.250000000
input
4 10
25 20 15 15
1.00 0.50 1.00 0.50
output
0.750000000

题意:

现在有一场比赛,有n个人,每个人有一个分数。从n个人中随意抽两个人出来(一个人的分数比另一个的大),分数少的人赢了比赛后获得s分后,分数超过了原来分数较大的人,即一种符合要求的情况。每个人输的概率为p[i]。问所有满足要求的情况的期望和。


思路:

对每一个人,求[score[i], score[i]-s) 区间内的期望和。二分求得score[i]-s,设下标为r。求期望即求和p[i] * (1-p[j]), (j from i to r)。

注意的是,题目只给了0.5秒。求下标r,求的是离score[i]-s(左闭右开,即大于score[i]-s)最近的那个人的下标。此外,题目中表明当两个人的分数相同时,可以不用考虑,那么对当前的这个人,我们要找到离他最远的,和他分数相同的那个人的下标。同时处理一个1-p[j]的前缀和,避免重复计算。


代码:

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>

using namespace std;

const int MAXN = 1e5 + 5;

int a[MAXN], n, s;
double p[MAXN], ans=0;
double pre[MAXN];

int main(){
	scanf("%d%d", &n, &s);
	for(int i=0; i<n; ++i) scanf("%d", &a[i]);
	for(int i=0; i<n; ++i) scanf("%lf", &p[i]);
	for(int i=0; i<n; ++i){
        pre[i+1] = pre[i] + (1-p[i]);
	}
	for(int i=0; i<n; ++i){
        int key = a[i]-s, l=i+1, r=n-1, mid;
        while(l<=r){
            mid = (l+r)>>1;
            if(a[mid] > key)
                l = mid+1;
            else
                r = mid-1;
        }
		if(a[r]==key) r-=1;


		int ll=i+1, rr=n-1, mm;
		while(ll<=rr){
            mm = (ll+rr)>>1;
            if(a[mm] >= a[i])
                ll = mm+1;
            else
                rr = mm-1;
		}
		ans += p[i]*(pre[r+1]-pre[rr+1]);
	}
	printf("%.9lf\n", ans);
	return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值