K Best
Time Limit: 8000MS | Memory Limit: 65536K | |
Total Submissions: 13923 | Accepted: 3563 | |
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 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 jewels S = {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, 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
Northeastern Europe 2005, Northern Subregion
注意:
这个时间卡的hin严,若是还像以前那样二分100次,会超时QwQ,所以我们用right-left>EPF(EPS=1.0e-6)这样来指定终止条件,这样就不超时了。
然后就是这道题居然不要求按顺序输出诶,输出的可以是:3 1 2这样无序的。
但有序也没错。
代码如下:
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<string>
#include<vector>
#include<queue>
#include<set>
const int N=100005;
const double INF=1e6+5,EPS=1.0e-6;
using namespace std;
int n,k;
int a[N],b[N];
struct A{
double val;
int seq;
}c[N];
bool cmp(A x,A y){
return x.val>y.val;
}
bool judge(double x){
for(int i=0;i<n;i++){
c[i].val=a[i]-x*b[i];
c[i].seq=i+1;
}
sort(c,c+n,cmp);
double sum=0;
for(int i=0;i<k;i++){
sum+=c[i].val;
}
return sum>=0;
}
int main(){
while(scanf("%d%d",&n,&k)!=EOF){
for(int i=0;i<n;i++){
scanf("%d%d",&a[i],&b[i]);
}
double left=0,right=INF;
while(right-left>EPS){
double mid=(left+right)/2;
if(judge(mid))left=mid;
else right=mid;
}
set<int>ans;
for(int i=0;i<k;i++){
ans.insert(c[i].seq);
}
for(set<int>::iterator it=ans.begin();it!=ans.end();it++){
if(it==ans.begin())printf("%d",*it);
else printf(" %d",*it);
}
printf("\n");
}
return 0;
}