SRM 402 DIV1 Easy - RandomSort

問題


http://community.topcoder.com/stat?c=problem_statement&pm=8590&rd=12174

解き方


全探索可能。
配列の状態を持つのでdpではなくmapを利用してDFSで解く。

コード


map<vector<int>,double> m;
int n;

class RandomSort {

public:

double rec(vector<int> p){
if(m.find(p)!=m.end())return m[p];

int cnt=0;
FORE(i,0,n)FORE(j,i+1,n)if(p[i]>p[j])cnt++;
if(cnt==0)return m[p]=0;

double ret=0;
FORE(i,0,n)FORE(j,i+1,n){
if(p[i]>p[j]){
swap(p[i],p[j]);
ret+=rec(p)+1;
swap(p[i],p[j]);
}
}

return m[p]=ret/cnt;
}

double getExpected(vector<int> permutation) {
m.clear();
n=permutation.size();
return rec(permutation);
}

};

Share this

Related Posts

Previous
Next Post »