POJ 3111 K Best 【二分:最大化平均值】
Time Limit: 8000MS | Memory Limit: 65536K |
Total Submissions: 10371 | Accepted: 2672 |
Case Time Limit: 2000MS | Special Judge |
Description
Demy has n jewels. Each of her jewels has some value vi and weight wi.
Since her husband John got broke after recent financial crises, Demy has decided to sell some jewels. She has decided that she would keep k
.
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, and k — the number of jewels she would like to keep (1 ≤ k ≤ n ≤ 100 000).
The following n lines contain two integer numbers each — vi and wi (0 ≤ vi ≤ 106, 1 ≤ wi ≤ 106, both the sum of all vi 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
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<map>
#include<algorithm>
using namespace std;
#define ll long long
#define ms(a,b) memset(a,b,sizeof(a))
const int M=1e5+10;
const int inf=0x3f3f3f3f;
const int mod=1e9+7;
int i,j,k,n,m;
int a[M];
int b[M];
double y[M];
struct node
{
int num;
int v;
int w;
double y;
bool operator < (const node&x) const
{
return y>x.y;
}
}p[M];
bool cmp(node a,node b)
{
return a.y>b.y;
}
int kk[M];
bool check(double mid)
{
for(int i=1;i<=n;i++)
p[i].y=p[i].v-p[i].w*mid;
sort(p+1,p+n+1);
double sum=0;
for(int i=1;i<=k;i++){
sum+=p[i].y;
kk[i]=p[i].num;
}
return sum>=0;
}
int main()
{
while(~scanf("%d%d",&n,&k)){
if(n==0&&k==0)break;
for(int i=1;i<=n;i++){
scanf("%d %d",&p[i].v,&p[i].w);
p[i].num=i;
}
double lb=0,ub=1000000000;
for(int i=0;i<100;i++){
double mid=(lb+ub)/2;
if(check(mid))lb=mid;
else ub=mid;
}
for(int i=1;i<=k;i++)
if(i>1)printf(" %d",kk[i]);
else printf("%d",kk[i]);
printf("\n");
}
return 0;
}