首页 > 学院 > 开发设计 > 正文

POJ3111-K Best-最大化平均值

2019-11-08 18:30:14
字体:
来源:转载
供稿:网友

原题链接 K Best Time Limit: 8000MS Memory Limit: 65536K Total Submissions: 9788 Accepted: 2510 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 题意:从n个宝石中找出加权平均数最大的k个数 思路:二分该加权平均数

#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>using namespace std;const double EPS = 1.0e-5;const double INF = 10000010.0;const int maxn = 1e5 + 10;int v[maxn],w[maxn],n,k;pair<double,int> c[maxn];bool used[maxn];bool _can(double x){ memset(used,false,sizeof(used)); for(int i=1;i<=n;i++){ c[i].first = v[i] - x*w[i]; c[i].second = i; } sort(c+1,c+n+1); double sum = 0; for(int i=n-k+1;i<=n;i++){ sum += c[i].first; used[c[i].second]=true; } return sum>=0.0;}int main(){ scanf("%d%d",&n,&k); for(int i=1;i<=n;i++) scanf("%d%d",&v[i],&w[i]); double l=0,r=INF; while(r-l>EPS){//这里的平均数可不是一个整数,而且我们需要一定的精度,注意不用while而用for循环100次会超时 double mid = (l+r)/2; if(_can(mid)) l=mid; else r=mid; } for(int i=1;i<=n;i++) if(used[i]) PRintf("%d ",i); return 0;}
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表