POJ 3111 最大化平均值

来源:互联网 发布:怿不甚知书 编辑:程序博客网 时间:2024/05/11 16:15

K Best

Time Limit: 8000MS Memory Limit: 65536K
Total Submissions: 11347 Accepted: 2925
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

刚开写了一个爆掉,重复利用了以下AC
Northeastern Europe 2005, Northern Subregion

#include<iostream>#include<math.h>#include<cstring>#include<algorithm>#include<set>#include<map>#include<queue>#include<cstdio>#include<vector>const int INF=0x7fffffff;const double EPS=1.0e-6;const int maxn=1e6+10;using namespace std;struct thing{    double v,w;    int number;};int n,k;thing T[maxn];bool C(double x){    double temp[maxn];    for(int i=0;i<n;i++)        temp[i]=T[i].v-x*T[i].w;    sort(temp,temp+n);    double sum=0;    for(int i=0;i<k;i++)        sum+=temp[n-i-1];    return sum>=0.0;}bool cmp(thing a,thing b ){    return a.v<b.v;}int main(){    cin>>n>>k;    for(int i=0;i<n;i++)        {            cin>>T[i].v>>T[i].w;            T[i].number=i;        }    double lb=0;    double ub=INF;    while((ub-lb)>EPS)    {        double mid=(ub+lb)/2;        if(C(mid))            lb=mid;        else            ub=mid;    }    for(int i=0;i<n;i++)        {            T[i].v=T[i].v-ub*T[i].w;        }    sort(T,T+n,cmp);    for(int i=0;i<k;i++)        cout<<T[n-i-1].number+1<<" ";    cout<<endl;    return 0;}
原创粉丝点击