topcode SRM 577 DIV1 EllysRoomAssignmentsDiv1

注意分情况求期望

(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 };

 

转载于:https://www.cnblogs.com/xiongqiangcs/archive/2013/04/29/3050436.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值