HDU - 5704 Luck Competition(二分)

Luck Competition

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 1051    Accepted Submission(s): 646


Problem Description
Participants of the Luck Competition choose a non-negative integer no more than 100 in their mind. After choosing their number, let K be the average of all numbers, and M be the result of K×23. Then the lucky person is the one who choose the highest number no more than M. If there are several such people, the lucky person is chosen randomly.

If you are given a chance to know how many people are participating the competition and what their numbers are, calculate the highest number with the highest probability to win assuming that you're joining the competition.
 

Input
There are several test cases and the first line contains the number of test cases T(T10).

Each test case begins with an integer N(1<N100), denoting the number of participants. And next line contains N1 numbers representing the numbers chosen by other participants.

 

Output
For each test case, output an integer which you have chosen and the probability of winning (round to two digits after the decimal point), seperated by space.

 

Sample Input
3 4 1 2 3 4 1 1 2 4 20 30 40
 

Sample Output
1 0.50 0 1.00 18 1.00
 

Source
 

Recommend
liuyiding

題意:給你 n - 1個數,你要選出一個數,使你選出的數在不大於這 n 個數的平均數 × 2/3的前提下儘量大;

#include <bits/stdc++.h>
using namespace std;

int t, n, sum;
int a[1001];

int judge(int k){
    //printf("k = %d  %f\n", k, (double)(k+sum)/n*2/3);
    if((double)(k+sum)/n*2/3 < k) return 0;
    return 1;
}

int main(){
    scanf("%d", &t);
    while(t--){
        scanf("%d", &n);
        sum = 0;
        for(int i = 1; i < n; i++){
            scanf("%d", &a[i]);
            sum += a[i];
        }
        int l = 0, r = 100, ans;
        while(l <= r){
            int mid = (l+r)>>1;
            if(judge(mid)) ans = mid, l = mid+1;
            else r = mid-1;
        }
        int cnt = 1;
        for(int i = 1; i < n; i++){
            if(a[i] == ans) cnt++;
        }
        printf("%d %.2f\n", ans, 1.0/cnt);
    }
}




發佈了133 篇原創文章 · 獲贊 5 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章