题意:保留k个宝石,使得平均值尽可能的大。 具体跟上题一样。
解题思路:二分的方法跟上一题一样, 每次把排序的序列保存下来, 输出id就好了。
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
Memory: 3068 KB | Time: 4407 MS | |
Language: G++ | Result: Accepted |
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<cctype>
#include<list>
#include<iostream>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>
using namespace std;
#define FOR(i, s, t) for(int i = (s) ; i <= (t) ; ++i)
#define REP(i, n) for(int i = 0 ; i < (n) ; ++i)
int buf[10];
inline long long read()
{
long long x=0,f=1;
char ch=getchar();
while(ch<'0'||ch>'9')
{
if(ch=='-')f=-1;
ch=getchar();
}
while(ch>='0'&&ch<='9')
{
x=x*10+ch-'0';
ch=getchar();
}
return x*f;
}
inline void writenum(int i)
{
int p = 0;
if(i == 0) p++;
else while(i)
{
buf[p++] = i % 10;
i /= 10;
}
for(int j = p - 1 ; j >= 0 ; --j) putchar('0' + buf[j]);
}
/**************************************************************/
#define MAX_N 100005
const int INF = 0x3f3f3f3f;
int v[MAX_N];
int w[MAX_N];
struct node
{
double v;
int id;
}c[MAX_N];
bool cmp(node a, node b)
{
return a.v > b.v;
}
int n, k;
bool f(double x)
{
for(int i = 0 ; i < n ; i++)
{
c[i].v = v[i] - x * w[i];
c[i].id = i;
}
sort(c, c + n, cmp);
double sum = 0;
for(int i = 0 ; i < k ; i++)
{
sum += c[i].v;
}
return sum >= 0;
}
int main()
{
while(~scanf("%d%d", &n, &k))
{
for(int i = 0 ; i < n ; i++)
{
v[i] = read();
w[i] = read();
}
double lb = 0, ub = INF;
while(ub - lb > 1e-8)
{
double mid = (ub + lb) / 2;
if(f(mid)) lb = mid;
else ub = mid;
}
for(int i = 0 ; i < k - 1 ; i++)
{
cout<<c[i].id + 1<<" ";
}
cout<<c[k -1].id + 1<<endl;
}
return 0;
}