HDU 5074 Luck Competition (暴力,概率)

题意:有 n 个人参加比赛,给出n-1个人的成绩,然后要选出一个幸运的人,先把所有的分数求平均数,然后再*2/3,那个不大于这个数,且最接近的数,就是最幸运的,

让你设置最后一个人的分,使他是最幸运的。

析:题目说了,最多是100,那么这么少,完全可以暴力啊,然后不断更新最大概率。

代码如下:

#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
using namespace std ;
typedef long long LL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f3f;
const double eps = 1e-8;
const int maxn = 100 + 5;
const int dr[] = {0, 0, -1, 1};
const int dc[] = {-1, 1, 0, 0};
char s[maxn][maxn];
int n, m;
int vis[maxn][maxn];
inline bool is_in(int r, int c){
    return r >= 0 && r < n && c >= 0 && c < m;
}
int a[maxn];

int main(){
    int T; cin >> T;
    while(T--){
        scanf("%d", &n);
        int sum = 0;
        for(int i = 1; i < n; ++i){
            scanf("%d", &a[i]);
            sum += a[i];
        }
        sort(a+1, a+n);
        int indx = 1000;
        double ans = 0.0;
        int p = -1;
        for(int i = 0; i <= 100; ++i){
            double t = sum*1.0 + i*1.0;
            t = t * 2.0 / 3.0 / (n*1.0);
            int tt = (int)t;
            if(i > tt) continue;
            bool ok = true;
            int cnt = 0;
            for(int j = n-1; j > 0; --j){
                if(a[j] <= tt && a[j] > i){ ok = false;  break; }
                else if(a[j] == i) ++cnt;
                else if(a[j] < i)  break;
            }
            if(!ok)  continue;
            if(indx >= cnt){
                indx = cnt;
                p = i;
            }
        }
        printf("%d %.2lf
", p, 1.0/(indx+1)*1.0);
    }
    return 0;
}
原文地址:https://www.cnblogs.com/dwtfukgv/p/5719361.html