您的位置:首页 > 运维架构

topcode SRM 577 DIV1 EllysRoomAssignmentsDiv1

2013-04-29 11:07 357 查看
注意分情况求期望

(1)只分配一个房间时,也就是人数少于20,对应测试用例1

(2)刚好完全分配时,对应测试用例3

(3)不能完全分配,最后还剩余的竞技者的数量小于房间数,随机分配

   a) 当elly刚好在最后剩余的里面,也就是elly排最后几名,对应测试用例4

   b) 当elly不在最后剩余的里面 ,对应测试用例0和2

#include <iostream>
#include <numeric>
#include <vector>
#include <string>
#include <algorithm>
#include <sstream>
#include <cmath>

using namespace std;

class EllysRoomAssignmentsDiv1{
public:
double getAverage(vector <string> ratings){
string split="";
string all = accumulate(ratings.begin(),ratings.end(),split);
stringstream sst(all);
vector<int> score;
int tmp;
while(sst >> tmp) score.push_back(tmp);
int score_elly = score[0];
sort(score.begin(),score.end(),greater<int>());
int cnt_elly = 0;
for(int i = 0 ; i < score.size(); i ++  ){
if( score[i] == score_elly) {
cnt_elly = i;break;
}
}
int R = score.size()%20 ? score.size()/20+1 : score.size()/20;
double value = 0;
for(int i = 0 ; i < score.size()/R; i ++ ){
if( i == cnt_elly/R ) continue;
double sum = 0;
for(int j = 0; j < R; j ++ ) sum +=score[R*i+j];
value +=sum/R;
}
if(R == 1){
return (value+score_elly)/score.size();
}
else if(score.size()%R){
double otherValue = 0;
int otherStart = score.size()/R*R;
if(cnt_elly < otherStart){
for(int i =otherStart; i < score.size(); i ++ )
otherValue += score[i];
otherValue /=(score.size()-otherStart);
return (value+otherValue+score_elly)/(score.size()/R+1)/R*(score.size()-otherStart) + (value+score_elly)/(score.size()/R)/R*(R-score.size()+otherStart);
}
else{
return (value+score_elly)/(score.size()/R+1);
}
}
else{
return (value+score_elly)/(score.size()/R);
}
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: