POJ 3111 K Best(最大化平均值)
来源:互联网 发布:如何设计一个软件 编辑:程序博客网 时间:2024/05/16 13:14
题意:保留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 21 11 21 3
Sample Output
1 2
#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 100005const 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;}
- POJ 3111 K Best(最大化平均值)
- poj 3111 K Best && poj 2976(最大化平均值)
- POJ 3111 K Best 最大化平均值
- POJ 3111 K Best(最大化平均值)
- POJ 3111 K Best 【二分:最大化平均值】
- POJ 3111 K Best 二分 最大化平均值
- poj 3111 K Best(二分-最大化平均值)
- POJ 3111 K Best(二分搜索,最大化平均值)
- poj 3111 K Best 二分搜索 最大化平均值
- POJ 3111 K Best(二分——最大化平均值)
- poj K Best 最大化平均值 二分搜索
- POJ 3111 K Best &&NYOJ 914 (二分+ 贪心,最大化平均值)
- POJ3111 K Best 最大化平均值(二分)
- POJ3111 K Best(二分 最大化平均值)
- POJ3111-K Best-最大化平均值
- poj3111 K Best【最大化平均值】
- K Best(最大化平均值(二分搜索))
- 7_13_F题 K Best(二分、最大化平均值)
- Android之TextView实现跑马灯的效果(笔记)
- C#求平均分和高于平均分的同学姓名。
- linux下动态库的符号冲突、隐藏和强制优先使用库内符号
- [Hnoi2013]游走(bzoj3143)
- java通过HTTP接收xml和返回xml
- POJ 3111 K Best(最大化平均值)
- zoj2112(单点修改区间第K小)
- 将成员变量声明为private和用非成员、非友友提高封装性(Effective C++_22、23))
- 智力类笔试题基础(图形1
- hadoop权威指南(第四版)要点翻译(6)——Chapter 4. YARN(1)
- poj3159candies
- hdu3466
- python操作redis之连接reids
- Graham求凸包。