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.
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 k numbers — the numbers of jewels Demy must keep. If there are several solutions, output any one.
3 2 1 1 1 2 1 3Sample Output
1 2
题意 : 从 n 个物品中选出 k 个,问怎么选平均总价值最大 ?
思路 :
先取前k个元素算出S0 =∑(vi/wi) 作为初始值 ,然后对每一个元素(n个)求s=vi-x*wi
要体会二分搜索过程的关键点,二分的 mid 值就是你所要一直求得值。
代码 :
const int eps = 1e5+5; const double pi = acos(-1.0); const int inf = 0x3f3f3f3f; #define Max(a,b) a>b?a:b #define Min(a,b) a>b?b:a #define ll long long int n, k; struct node { int v, w, pt; double s; bool operator< (const node& ff)const{ return s > ff.s; } }pre[eps]; bool check(double x){ for(int i = 1; i <= n; i++){ pre[i].s = 1.0*pre[i].v - x*pre[i].w; } sort(pre+1, pre+n+1); double sum = 0; for(int i = 1; i <= k; i++){ sum += pre[i].s; } return sum >= 0; } int main() { //freopen("in.txt", "r", stdin); //freopen("out.txt", "w", sttout); while(~scanf("%d%d", &n, &k)){ for(int i = 1; i <= n; i++){ pre[i].pt = i; scanf("%d%d", &pre[i].v, &pre[i].w); } double l = 0, r = 1.0*inf; for(int i = 1; i <= 200; i++){ double mid = (l + r) / 2.0; if (check(mid)) l = mid; else r = mid; } for(int i = 1; i < k; i++){ printf("%d ", pre[i].pt); } printf("%d ", pre[k].pt); //printf("%.2f ", ans); } return 0; }