题目链接:http://poj.org/problem?id=2976
Description
In a certain course, you take n tests. If you get ai out of bi questions correct on test i, your cumulative average is defined to be
.
Given your test scores and a positive integer k, determine how high you can make your cumulative average if you are allowed to drop any k of your test scores.
Suppose you take 3 tests with scores of 5/5, 0/1, and 2/6. Without dropping any tests, your cumulative average is . However, if you drop the third test, your cumulative average becomes
.
Input
The input test file will contain multiple test cases, each containing exactly three lines. The first line contains two integers, 1 ≤ n ≤ 1000 and 0 ≤ k < n. The second line contains n integers indicating ai for all i. The third line contains n positive integers indicating bi for all i. It is guaranteed that 0 ≤ ai ≤ bi ≤ 1, 000, 000, 000. The end-of-file is marked by a test case with n = k = 0 and should not be processed.
Output
For each test case, write a single line with the highest cumulative average possible after dropping k of the given test scores. The average should be rounded to the nearest integer.
Sample Input
3 1 5 0 2 5 1 6 4 2 1 2 7 9 5 6 7 9 0 0
Sample Output
83 100
Hint
To avoid ambiguities due to rounding errors, the judge tests have been constructed so that all answers are at least 0.001 away from a decision boundary (i.e., you can assume that the average is never 83.4997).
Source
题意:n场考试,每场bi个问题答对了ai个,从中删除k场考试,使其平均值最大化。。。
思路:二分搜素答案,贪心选取n-k个出来判断是否满足条件。。。
代码:
#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <vector>
#include <cctype>
#include <cmath>
#include <stack>
#include <queue>
#include <list>
#include <map>
#include <set>
using namespace std;
#define clr(x, y) memset(x, y, sizeof(x))
#define fr(i,n) for(int i = 0; i < n; i++)
#define fr1(i,n) for(int i = 1; i <= n; i++)
#define upfr(i,j,n) for(int i = j; i <= n; i++)
#define dowfr(i,j,n) for(int i = n; i >= j; i--)
#define scf(n) scanf("%d", &n)
#define scf2(n,m) scanf("%d %d",&n,&m)
#define scf3(n,m,p) scanf("%d %d %d",&n,&m,&p)
#define ptf(n) printf("%d",n)
#define ptf64(n) printf("%I64d",n)
#define ptfs(s) printf("%s",s)
#define ptln() printf("\n")
#define ptk() printf(" ")
#define ptc(c) printf("%c",c)
#define srt(a,n) sort(a,n)
#define LL __int64
#define pi acos(-1.0)
#define eps 1e-10
#define maxn 1005
#define mod 1000000007
#define inf 1000000000
double a[maxn], b[maxn], ave[maxn];
int n,k;
int C(double x)
{
fr(i, n)
ave[i] = a[i] - b[i] * x;
sort(ave, ave+n);
double sum = 0;
int num = n - k;
fr(i, num)
sum += ave[n-i-1];
return sum >= 0;
}
int main()
{
// freopen("in.txt","r",stdin);
while(scf2(n, k) && (n+k))
{
clr(ave, 0);
fr(i, n)
scanf("%lf", &a[i]);
fr(i, n)
scanf("%lf", &b[i]);
double lb = 0, ub = inf;
while(ub - lb > eps)
{
double mid = (lb + ub) / 2;
if(C(mid))
lb = mid;
else
ub = mid;
}
ub=ub*100;
double a=ub-(int)ub;
int f=(int)ub;
if(a>=0.5)
f+=1;
printf("%d\n",f);
}
return 0;
}
相似的题目:POJ 3111
题目链接:http://poj.org/problem?id=3111
Description
Demy has n jewels. Each of her jewels has some value vi and weightwi.
Since her husband John got broke after recent financial crises, Demy has decided to sell some jewels. She has decided that she would keepk best jewels for herself. She decided to keep such jewels that their specific value is as large as possible. That is, denote the specific value of some set of jewelsS = {i1, i2, …, ik} as
.
Demy would like to select such k jewels that their specific value is maximal possible. Help her to do so.
Input
The first line of the input file contains n — the number of jewels Demy got, andk — the number of jewels she would like to keep (1 ≤ k ≤ n ≤ 100 000).
The following n lines contain two integer numbers each — vi andwi (0 ≤ vi ≤ 106, 1 ≤ wi ≤ 106, both the sum of allvi and the sum of all wi do not exceed 107).
Output
Output k numbers — the numbers of jewels Demy must keep. If there are several solutions, output any one.
Sample Input
3 2 1 1 1 2 1 3
Sample Output
1 2
Source
题意:n个结点,每个结点有v,w两个值,从中选k个,使其sum(v) / sum(w)的值最大化,输出编号。。
思路:同poj 2976 ,用了STL中的nth_element函数。。。
代码:
#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <vector>
#include <cctype>
#include <cmath>
#include <stack>
#include <queue>
#include <list>
#include <map>
#include <set>
using namespace std;
#define clr(x, y) memset(x, y, sizeof(x))
#define fr(i,n) for(int i = 0; i < n; i++)
#define fr1(i,n) for(int i = 1; i <= n; i++)
#define upfr(i,j,n) for(int i = j; i <= n; i++)
#define dowfr(i,j,n) for(int i = n; i >= j; i--)
#define scf(n) scanf("%d", &n)
#define scf2(n,m) scanf("%d %d",&n,&m)
#define scf3(n,m,p) scanf("%d %d %d",&n,&m,&p)
#define ptf(n) printf("%d",n)
#define ptf64(n) printf("%I64d",n)
#define ptfs(s) printf("%s",s)
#define ptln() printf("\n")
#define ptk() printf(" ")
#define ptc(c) printf("%c",c)
#define srt(a,n) sort(a,n)
#define LL __int64
#define pi acos(-1.0)
#define eps 1e-8
#define maxn 100010
#define mod 1000000007
#define inf 1e30
struct node
{
double v,w;
int id;
}a[maxn];
int n,k;
double g;
int cmp(node a, node b)
{
return a.v - g*a.w > b.v - g*b.w;
}
int cmp1(node a, node b)
{
return a.id < b.id;
}
int C(double x)
{
g = x;
nth_element(a, a+k-1, a+n, cmp);
double sum = 0;
fr(i, k)
sum += a[i].v - g*a[i].w;
return sum >= 0;
}
int main()
{
// freopen("in.txt","r",stdin);
while(scf2(n, k) == 2)
{
fr(i, n)
{
scanf("%lf %lf", &a[i].v, &a[i].w);
a[i].id = i + 1;
}
double lb = 0, ub = 1e7;
while(ub - lb > eps)
{
double mid = (lb + ub) / 2;
if(C(mid))
lb = mid;
else
ub = mid;
}
sort(a, a+k, cmp1);
fr(i, k-1)
printf("%d ", a[i].id);
printf("%d\n", a[k-1].id);
printf("\n");
}
return 0;
}