POJ 3111 K Best(01分数规划)
Time Limit: 8000MS | Memory Limit: 65536K | |
Total Submissions: 9876 | Accepted: 2535 | |
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
题目链接:POJ 3111
这题多了一个得输出方案序号,用结构体记录一下id就行,代码的话迭代或者二分都可以,但是迭代速度快很多很多
迭代代码:
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <sstream>
#include <numeric>
#include <cstring>
#include <bitset>
#include <string>
#include <deque>
#include <stack>
#include <cmath>
#include <queue>
#include <set>
#include <map>
using namespace std;
#define INF 0x3f3f3f3f
#define LC(x) (x<<1)
#define RC(x) ((x<<1)+1)
#define MID(x,y) ((x+y)>>1)
#define CLR(arr,val) memset(arr,val,sizeof(arr))
#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);
typedef pair<int, int> pii;
typedef long long LL;
const double PI = acos(-1.0);
const int N = 100010;
const double eps = 1e-9;
struct info
{
double w, v, d;
int id;
bool operator<(const info &rhs)const
{
return d > rhs.d;
}
};
info A[N]; double getnewk(int n, int k, double r)
{
double V = 0.0, W = 0.0;
for (int i = 0; i < n; ++i)
A[i].d = A[i].v - r * A[i].w;
sort(A, A + n);
for (int i = 0; i < k; ++i)
{
V += A[i].v;
W += A[i].w;
}
return V / W;
}
int main(void)
{
int n, k, i;
while (~scanf("%d%d", &n, &k))
{
for (i = 0; i < n; ++i)
{
scanf("%lf%lf", &A[i].v, &A[i].w);
A[i].id = i + 1;
}
double ans = 1, temp = 1;
while (1)
{
temp = getnewk(n, k, ans);
if (fabs(temp - ans) < eps)
break;
ans = temp;
}
for (i = 0; i < k; ++i)
printf("%d%s", A[i].id, i == k - 1 ? "\n" : " ");
}
return 0;
}
POJ 3111 K Best(01分数规划)的更多相关文章
- POJ - 3111 K Best 0-1分数规划 二分
K Best Time Limit: 8000MS Memory Limit: 65536K Total Submissions: 12812 Accepted: 3290 Case Time ...
- POJ 2976 Dropping tests 01分数规划
给出n(n<=1000)个考试的成绩ai和满分bi,要求去掉k个考试成绩,使得剩下的∑ai/∑bi*100最大并输出. 典型的01分数规划 要使∑ai/∑bi最大,不妨设ans=∑ai/∑bi, ...
- POJ 2976 Dropping tests 01分数规划 模板
Dropping tests Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6373 Accepted: 2198 ...
- POJ 2728 Desert King (01分数规划)
Desert King Time Limit: 3000MS Memory Limit: 65536K Total Submissions:29775 Accepted: 8192 Descr ...
- $POJ$2976 $Dropping\ tests$ 01分数规划+贪心
正解:01分数规划 解题报告: 传送门! 板子题鸭,,, 显然考虑变成$a[i]-mid\cdot b[i]$,显然无脑贪心下得选出最大的$k$个然后判断是否大于0就好(,,,这么弱智真的算贪心嘛$T ...
- POJ - 2976 Dropping tests(01分数规划---二分(最大化平均值))
题意:有n组ai和bi,要求去掉k组,使下式值最大. 分析: 1.此题是典型的01分数规划. 01分数规划:给定两个数组,a[i]表示选取i的可以得到的价值,b[i]表示选取i的代价.x[i]=1代表 ...
- POJ 2728 Desert King ★(01分数规划介绍 && 应用の最优比率生成树)
[题意]每条路径有一个 cost 和 dist,求图中 sigma(cost) / sigma(dist) 最小的生成树. 标准的最优比率生成树,楼教主当年开场随手1YES然后把别人带错方向的题Orz ...
- POJ3111 K Best —— 01分数规划 二分法
题目链接:http://poj.org/problem?id=3111 K Best Time Limit: 8000MS Memory Limit: 65536K Total Submissio ...
- POJ 3621 Sightseeing Cows 01分数规划,最优比例环的问题
http://www.cnblogs.com/wally/p/3228171.html 题解请戳上面 然后对于01规划的总结 1:对于一个表,求最优比例 这种就是每个点位有benefit和cost,这 ...
- POJ 2728 Desert King 01分数规划,最优比率生成树
一个完全图,每两个点之间的cost是海拔差距的绝对值,长度是平面欧式距离, 让你找到一棵生成树,使得树边的的cost的和/距离的和,比例最小 然后就是最优比例生成树,也就是01规划裸题 看这一发:ht ...
随机推荐
- 基础I/O
基础IO: c库文件IO操作接口:(详细查看c语言中的文件操作函数总结:https://www.cnblogs.com/cuckoo-/p/10560640.html) fopen 打开文件 fclo ...
- Javascript笔记部分
写入HTML输出 document.write(“<h1>”); 改变HTML内容 x = document.getElementById(“demo”) //查找元素 后面可以.valu ...
- shell脚本,利用awk计算指定范围内的和。
期望得到结果如下: vivi 42800Tom 32500John 104500 解题方法如下: 1.利用数组来进行解题.
- spring中@Autowrite注解和@Resource的区别
spring不但支持自己定义的@Autowired注解,还支持几个由JSR-250规范定义的注解,它们分别是@Resource.@PostConstruct以及@PreDestroy. @Resour ...
- C++的新特性for-each
C++实验课要求用for each 循环来实现关联容器 map 的输出,一开始完全萌比.查了好久的资料才整理出下面的: C++11新特性之一就是类似java的for each循环: map<in ...
- shell 流水账
在shell脚本运行时,会先查找系统环境变量ENV,该变量指定了环境文件(加载顺序通常是/etc/profile ~/.bash_profile ~/.bashrc /etc/bashrc) 设置全局 ...
- Python 正则表达式 匹配次数
管道可以匹配多个正则表达式中的一个 >>> >>> m=re.search(r'Batman|Tina Fey','Batman and Tina Fey')> ...
- thinkphp3.2.3如何只改变地址url中的某一个分隔符,其它保持不变
今天教大家一个关于使用thinkphp3.2.3改变只改变地址url中的某一个分隔符的方法,首先大家来看看这个地址! 它的原始地址应该是/Home/Index/index/page/2.html,那我 ...
- North American Invitational Programming Contest (NAIPC) 2017
(待补) A. Pieces of Parentheses 将括号处理完成后排序,方式参加下面的博客.然后做一遍背包即可. 2018 Multi-University Training Contest ...
- (ADO.NET)关于C#中“配置”sqlite问题
配置打引号,只是因为觉得只是一些小问题,在此记录一下,第一次遇到还真有点手足无措,昨天到今天~终于可以开始放肆的写sqlite了. 好,第一个问题是引用已下载的system.data.sqlite.d ...