TopCoder SRM 664 Div2 Level Three

来源:互联网 发布:mac 10.13 卡 编辑:程序博客网 时间:2024/05/16 18:08

Problem Statement

Bear Limak was chilling in the forest when he suddenly found a computer program. The program was a correct implementation of MergeSort. Below you can find the program in pseudocode.

# mergeSort(left,right) sorts elements left, left+1, …, right-1 of a global array T
function mergeSort(left,right):
# if there is at most one element, we are done
if left+1 >= right: return

# otherwise, split the sequence into halves, sort each half separately
mid = (left + right) div 2
mergeSort(left,mid)
mergeSort(mid,right)

# then merge the two halves together
merged = [] # an empty sequence
p1 = left
p2 = mid
while (p1 < mid) or (p2 < right):
if p1 == mid:
merged.append( T[p2] )
p2 += 1
else if p2 == right:
merged.append( T[p1] )
p1 += 1
else:
if LESS( T[p1], T[p2] ):
merged.append( T[p1] )
p1 += 1
else:
merged.append( T[p2] )
p2 += 1

# finally, move the merged elements back into the original array
for i from left to right-1 inclusive:
T[i] = merged[i-left]

Limak noticed that one part of the implementation was missing: the function LESS. You can probably guess that the function is supposed to return a boolean value stating whether the first argument is less than the second argument. However, Limak is a bear and he didn’t know that. Instead he implemented his own version of this function. Limak’s function uses a true random number generator. Each of the two possible return values (true, false) is returned with probability 50 percent.

The random values generated in different calls to Limak’s function LESS are mutually independent. Note that even if you call LESS twice with the same arguments, the two return values may differ.

After implementing LESS, Limak decided to run his brand new program. He initialized the global array T to contain N elements. Then, he filled the values 1 through N into the array: for each valid i, he set T[i] to i+1. Finally, he executed the function mergeSort(0,N).

Even with Limak’s new LESS function, the program never crashes. On the other hand, it does not necessarily produce a sorted sequence when it terminates. In general, when the program terminates, the array T will contain some permutation of the numbers 1 through N.

After running the program many times, Limak has noticed that different output permutations are produced with different probabilities. Your task is to help him learn more about these probabilities. More precisely, your task is to compute the probability that a given sequence will appear as the output of Limak’s program.

You are given a vector sortedSequence with N elements, containing a permutation of 1 through N. Let P be the probability that Limak’s program, when run on an array with N elements, outputs this permutation. Return the value log(P), where log denotes the natural (base e) logarithm.
Definition

Class:

BearSortsDiv2

Method:

getProbability

Parameters:

vector

Returns:

double

Method signature:

double getProbability(vector seq)
(be sure your method is public)

Limits

Time limit (s):
2.000
Memory limit (MB):
256
Stack limit (MB):
256
Notes
Your return value must have absolute or relative error smaller than 1e-9.
You may assume that for each N and for each permutation P of 1 through N the probability that P appears as the output of Limak’s program is strictly positive.
Constraints
sortedSequence will contain exactly N elements.
N will be between 1 and 40, inclusive.
Elements of sortedSequence will be between 1 and N, inclusive.
Elements of sortedSequence will be pairwise distinct.

Examples

0)

{1,2}
Returns: -0.6931471805599453
Limak is sorting a 2-element sequence. The algorithm will split it into two 1-element sequences and then it will merge those together. While merging, the algorithm will call LESS(1, 2) to “compare” the two elements. If LESS(1, 2) returns true, the resulting permutation will be {1, 2}, otherwise it will be {2, 1}. Therefore, the probability of each of those two permutations is equal to 0.5. The return value is log(0.5).

1)

{1,3,2}
Returns: -1.3862943611198906
When {1, 2, 3} is sorted, it is first split into {1} and {2, 3}. After that, in order to obtain {1, 3, 2} in the end, two things must happen, one after another:
When {2, 3} is recursively sorted, the result must be {3, 2}. From Example 0 we know this happens with probability 0.5.
When merging {1} and {3, 2}, the first call to LESS will be LESS(1, 3). This call must return true. Again, this happens with probability 0.5.
Therefore, the probability is 0.5 * 0.5 = 0.25, and the answer is log(0.25).

2)

{10,13,18,2,4,6,24,22,19,5,7,20,23,14,21,17,25,3,1,11,12,8,15,16,9}
Returns: -57.53121598647546

This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.

题意

熊孩子发现了一个归并排序,然后把LESS函数改成了随机的。之后给一个长度N的序列,包含1到N。问对{1…N}进行排序后得到给定序列的概率取对数是多少。。。

题解

可以利用归并的思想进行dfs,AC代码如下

#include <vector>#include <list>#include <map>#include <set>#include <deque>#include <stack>#include <bitset>#include <algorithm>#include <functional>#include <numeric>#include <utility>#include <sstream>#include <iostream>#include <iomanip>#include <cstdio>#include <cmath>#include <cstdlib>#include <ctime>using namespace std;class BearSortsDiv2 {public:    double dfs(vector<int> A,int s,int f)    {        int Ls,Rs,As,i;        int mid=(s+f+1)/2;        if((As=A.size())==1)            return 1.0;        vector<int> L,R;        double ans=1.0;        Ls=As/2;        Rs=As-Ls;        for(i=0;L.size()<Ls&&R.size()<Rs;i++)        {            if(A[i]<mid)                L.push_back(A[i]);            else                R.push_back(A[i]);            ans*=0.5;        }        while(L.size()<Ls)            L.push_back(A[i++]);        while(R.size()<Rs)            R.push_back(A[i++]);        return ans*dfs(L,s,mid-1)*dfs(R,mid,f);    }    double getProbability(vector <int>);};double BearSortsDiv2::getProbability(vector <int> seq) {    double ans=dfs(seq,1,seq.size());    return log(ans);}
0 0
原创粉丝点击