poj 3111 K Best 二分
来源:互联网 发布:听戏曲的软件 编辑:程序博客网 时间:2024/05/21 07:22
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
题意:从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;}
- 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(思维+二分)
- K Best poj 3111 (01分数规划---二分搜索)
- poj 3111 K Best ,二分,牛顿迭代
- POJ 2976 Dropping tests、3111 K Best (二分搜索)
- POJ 3111 K Best (01分数规划+二分)
- POJ 3111 K Best(二分搜索,最大化平均值)
- POJ - 3111 K Best (二分 + 01分数规划)
- poj3111- POJ - 3111 K Best (二分 + 01分数规划)
- POJ 3111 K Best 二分(最小化平均值)
- poj 3111 K Best 二分搜索 最大化平均值
- POJ 3111 K Best(二分——最大化平均值)
- poj 3111 K Best(二分-最大化平均值)
- Android USB 驱动分析与开发----编程
- poj 2456 二分
- HBuilder&&WebStorm部分快捷键
- 的风格
- 锤子剪刀布
- poj 3111 K Best 二分
- python 格式化字符串
- 尾插法建立链表分解
- 转载-总结sublime text3安装package control插件的方法
- 【数据结构】 C++迷宫
- Git的add、commit、push命令
- flask建站实践笔记001-基本结构
- 数据结构上机作业3-哈夫曼编码
- DSP:数字信号处理