注意分情况求期望
(1)只分配一个房间时,也就是人数少于20,对应测试用例1
(2)刚好完全分配时,对应测试用例3
(3)不能完全分配,最后还剩余的竞技者的数量小于房间数,随机分配
a) 当elly刚好在最后剩余的里面,也就是elly排最后几名,对应测试用例4
b) 当elly不在最后剩余的里面 ,对应测试用例0和2
1 #include <iostream> 2 #include <numeric> 3 #include <vector> 4 #include <string> 5 #include <algorithm> 6 #include <sstream> 7 #include <cmath> 8 9 using namespace std; 10 11 class EllysRoomAssignmentsDiv1{ 12 public: 13 double getAverage(vector <string> ratings){ 14 string split=""; 15 string all = accumulate(ratings.begin(),ratings.end(),split); 16 stringstream sst(all); 17 vector<int> score; 18 int tmp; 19 while(sst >> tmp) score.push_back(tmp); 20 int score_elly = score[0]; 21 sort(score.begin(),score.end(),greater<int>()); 22 int cnt_elly = 0; 23 for(int i = 0 ; i < score.size(); i ++ ){ 24 if( score[i] == score_elly) { 25 cnt_elly = i;break; 26 } 27 } 28 int R = score.size()%20 ? score.size()/20+1 : score.size()/20; 29 double value = 0; 30 for(int i = 0 ; i < score.size()/R; i ++ ){ 31 if( i == cnt_elly/R ) continue; 32 double sum = 0; 33 for(int j = 0; j < R; j ++ ) sum +=score[R*i+j]; 34 value +=sum/R; 35 } 36 if(R == 1){ 37 return (value+score_elly)/score.size(); 38 } 39 else if(score.size()%R){ 40 double otherValue = 0; 41 int otherStart = score.size()/R*R; 42 if(cnt_elly < otherStart){ 43 for(int i =otherStart; i < score.size(); i ++ ) 44 otherValue += score[i]; 45 otherValue /=(score.size()-otherStart); 46 return (value+otherValue+score_elly)/(score.size()/R+1)/R*(score.size()-otherStart) + (value+score_elly)/(score.size()/R)/R*(R-score.size()+otherStart); 47 } 48 else{ 49 return (value+score_elly)/(score.size()/R+1); 50 } 51 } 52 else{ 53 return (value+score_elly)/(score.size()/R); 54 } 55 } 56 };