TopCoder SRM 664 Div2 Level Three
2015-08-02 21:44
363 查看
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:
BearSortsDiv2Method:
getProbabilityParameters:
vectorReturns:
doubleMethod 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) 4000 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); }
相关文章推荐
- linux命令行播放声音
- vmware-workstation-11中centos-6.6安装
- 收集的55个Linux系统管理中常用的一些shell命令
- Linux学习笔记02之文件管理
- linux下打开windows txt文件中文乱码解决方法
- Learn OpenGLES: 正交变换
- openstack git
- TopCoder SRM 664 Div2 Level One
- 关于在使用tomcat做开发测试的某些时候无端报ClassNotFound异常的解决方法
- 文本分块切割
- linux_c开发(5-4)进程间通讯_共享内存通讯
- centos下安装axel
- nginx 平滑升级tengine
- 平面设计入门之Mac OS photoshop安装
- 一个没水平的钓鱼网站黑三个人:人们的安全意识和安全知识的缺失
- 在 Linux 下安装基于 Python 的机器学习编程环境
- 《Linux系统精讲》学习总结(二)
- vRedHat Enterprise Linux 6.4使用Centos 6 的yum源
- centos下的tomcat的安装
- 基于规则引擎的电信CRM系统架构研究