POJ 3111
Time Limit: 8000MS | Memory Limit: 65536K | |
Total Submissions: 5177 | Accepted: 1411 | |
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
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 #include <utility> 6 7 using namespace std; 8 9 #define maxn 100005 10 #define eps 1e-8 11 typedef pair<double,int> pii; 12 13 int w[maxn],v[maxn]; 14 double f[maxn]; 15 pii ans[maxn]; 16 int n,k; 17 18 19 bool judge(double x) { 20 for(int i = 1; i <= n; ++i) { 21 f[i] = (v[i] - x * w[i]); 22 } 23 sort(f + 1,f + n + 1); 24 25 double sum = 0; 26 for(int i = n; i >= n - k + 1; --i) { 27 sum += f[i]; 28 } 29 30 return sum >= 0; 31 32 } 33 34 void output(double x) { 35 36 for(int i = 1; i <= n; ++i) { 37 ans[i] = make_pair(v[i] - x * w[i],i); 38 } 39 sort(ans + 1,ans + n + 1); 40 41 printf("%d",ans[n].second); 42 for(int i = n - 1; i >= n - k + 1; --i) { 43 printf(" %d",ans[i].second); 44 } 45 46 printf("\n"); 47 48 49 } 50 51 void solve() { 52 double l = 0,r = 0; 53 for(int i = 1; i <= n; ++i) { 54 r += v[i]; 55 } 56 57 int num = 0; 58 while(num++ < 50 ) { 59 //printf(" l = %f r = %f\n",l,r); 60 //num++; 61 double mid = (l + r) / 2; 62 if(judge(mid)) { 63 l = mid; 64 } else { 65 r = mid; 66 } 67 68 } 69 70 //printf("%f\n",l); 71 output(l); 72 } 73 int main() 74 { 75 76 //freopen("sw.in","r",stdin); 77 78 while(~scanf("%d%d",&n,&k)) 79 { 80 for(int i = 1; i <= n; ++i) { 81 scanf("%d%d",&v[i],&w[i]); 82 } 83 84 solve(); 85 } 86 87 return 0; 88 }