poj 3111 K Best 二分

来源:互联网 发布:听戏曲的软件 编辑:程序博客网 时间:2024/05/21 07:22
K Best
Time Limit: 8000MS Memory Limit: 65536KTotal Submissions: 11677 Accepted: 3016Case 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 = {i1i2, …, 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 21 11 21 3

Sample Output

1 2

题意:从n个宝石里选k个,求的最大值。

分析:经典题啊,公式少许变化,然后排序、二分。

#include <cstdio>#include <cstdlib>#include <iostream>#include <stack>#include <queue>#include <algorithm>#include <cstring>#include <string>#include <cmath>#include <vector>#include <bitset>#include <list>#include <sstream>#include <set>#include <functional>using namespace std;#define INF 0x3f3f3f3f#define MAX 100#define ESP 1e-6typedef long long ll;int n,k;struct node{int id,v,w;double ans;bool operator < (const node &a) const{return ans > a.ans;}}y[1000001];bool C(double x){for (int i = 0; i < n; i += 1) y[i].ans = y[i].v-x*y[i].w;sort(y,y+n);double sum = 0;for (int i = 0; i < k; i += 1) sum += y[i].ans;return sum >= 0;}int main(){#ifndef ONLINE_JUDGE    freopen("in.txt", "r", stdin);    freopen("out.txt", "w", stdout);#endifcin >> n >> k;for (int i = 0; i < n; i += 1) {cin >> y[i].v >> y[i].w;y[i].id = i+1;}double lb = 0,ub = INF;while (ub-lb>=ESP){double mid = (ub+lb)/2;if(C(mid)) lb = mid;else ub = mid;}for (int i = 0; i < k-1; i += 1) printf("%d ",y[i].id);printf("%d\n",y[k-1].id);return 0;}