原題鏈接 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])新聞熱點
疑難解答