POJ3111
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
题解:
AC代码为:
1 #include <iostream> 2 #include <cstdio> 3 #include <algorithm> 4 #include <vector> 5 #include <cstring> 6 #include <string> 7 #include <map> 8 #include <queue> 9 #include <set> 10 11 12 using namespace std; 13 14 15 const int maxn = 100000 + 5; 16 const double inf = 1000000 + 5; 17 int n,k; 18 struct jew 19 { 20 int id; 21 int v,w; 22 double key; 23 void cal(double x) 24 { 25 key = v - x * w; 26 } 27 28 bool operator < (const jew& a) const 29 { 30 return key > a.key; 31 } 32 }j[maxn]; 33 34 35 bool c(double x) 36 { 37 for (int i = 1; i <= n; i++) 38 { 39 j[i].cal(x); 40 } 41 42 sort(j+1,j+n+1); 43 44 double tmp = 0; 45 for (int i = 1; i <= k; i++) 46 tmp += j[i].key; 47 return tmp >= 0; 48 } 49 50 51 int main() 52 { 53 while (cin>>n>>k) 54 { 55 for (int i = 1; i <= n; i++) 56 { 57 scanf("%d%d",&j[i].v,&j[i].w); 58 j[i].id = i; 59 } 60 double l = 0, r = inf; 61 for (int i = 0; i < 100; i++) 62 { 63 double mid = (l + r) / 2; 64 if (c(mid)) 65 l = mid; 66 else 67 r = mid; 68 } 69 for (int i = 1; i <= k; i++) 70 { 71 if (i - 1) 72 printf(" %d",j[i].id); 73 else 74 printf("%d",j[i].id); 75 } 76 77 cout<<endl; 78 } 79 }