1257 背包问题 V3——分数规划
第1行:包括2个数N, K(1 <= K <= N <= 50000)
第2 - N + 1行:每行2个数Wi, Pi(1 <= Wi, Pi <= 50000)
输出单位体积的价值(用约分后的分数表示)。
#include<cstdio>
#include<cstring>
#include<algorithm>
#define LL long long
using namespace std;
const int M=;
int read(){
int ans=,f=,c=getchar();
while(c<''||c>''){if(c=='-') f=-; c=getchar();}
while(c>=''&&c<=''){ans=ans*+(c-''); c=getchar();}
return ans*f;
}
int n,k;
int c[M],w[M];
LL sumx,sumy,ansx,ansy;
LL gcd(LL x,LL y){
while(y){
LL p=x%y;
x=y;
y=p;
}
return x;
}
struct node{double v; int pos;}e[M];
bool cmp(node a,node b){return a.v-b.v>1e-;}
bool check(double mid){
for(int i=;i<=n;i++) e[i].v=1.0*w[i]-1.0*c[i]*mid,e[i].pos=i;
sort(e+,e++n,cmp);
double sum=; sumx=; sumy=;
for(int i=;i<=k;i++){
sum+=e[i].v;
sumx+=w[e[i].pos];
sumy+=c[e[i].pos];
}
return sum>=;
}
int main()
{
n=read(); k=read();
for(int i=;i<=n;i++) c[i]=read(),w[i]=read();
double l=,r=;
while(r-l>1e-){
double mid=(l+r)/;
if(check(mid)) l=mid,ansx=sumx,ansy=sumy;
else r=mid;
}
LL d=gcd(ansx,ansy);
printf("%lld/%lld\n",ansx/d,ansy/d);
return ;
}
1257 背包问题 V3——分数规划的更多相关文章
- 51nod 1257 背包问题 V3
1257 背包问题 V3 基准时间限制:3 秒 空间限制:131072 KB 分值: 80 难度:5级算法题 N个物品的体积为W1,W2......Wn(Wi为整数),与之相对应的价值为P1,P2.. ...
- 1257 背包问题 V3(二分)
1257 背包问题 V3 3 秒 131,072 KB 80 分 5 级题 题意 : 从n个物品中选出k个,使单位体积价值最大 思路: 一开始正面想,试过很多种,排序什么的..总是结果不对,最后想到二 ...
- 51nod 1257 背包问题 V3(分数规划)
显然是分数规划...主要是不会求分数的形式,看了题解发现自己好傻逼QAQ 还是二分L值算出d[]降序选K个,顺便记录选择时候的p之和与w之和就可以输出分数形式了... #include<iost ...
- 51nod 1257 背包问题 V3(这不是背包问题是二分)
题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1257 题解:不能按照单位价值贪心,不然连样例都过不了 要求的 ...
- 51nod 1257 01分数规划/二分
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1257 1257 背包问题 V3 基准时间限制:3 秒 空间限制:1310 ...
- 51nod1257 背包问题 V3
分数规划经典.开始精度1e-3/1e-4都不行,1e-5就A了 #include<cstdio> #include<cstring> #include<cctype> ...
- 【bzoj4753】[Jsoi2016]最佳团体 分数规划+树形背包dp
题目描述 JSOI信息学代表队一共有N名候选人,这些候选人从1到N编号.方便起见,JYY的编号是0号.每个候选人都由一位编号比他小的候选人Ri推荐.如果Ri=0则说明这个候选人是JYY自己看上的.为了 ...
- POJ3621Sightseeing Cows[01分数规划 spfa(dfs)负环 ]
Sightseeing Cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9703 Accepted: 3299 ...
- 【BZOJ 1758】【WC 2010】重建计划 分数规划+点分治+单调队列
一开始看到$\frac{\sum_{}}{\sum_{}}$就想到了01分数规划但最终还是看了题解 二分完后的点分治,只需要维护一个由之前处理过的子树得出的$tb数组$,然后根据遍历每个当前的子树上的 ...
随机推荐
- php mysql find_in_set函数 检索单子段 逗号分隔序列
FIND_IN_SET($kwd,field) 例如在 表 AA中 numbers 字段 保存列数据 1,4,8,75,41,7 就可以使用 FIND_IN_SET(8,numbers) 查询记 ...
- input标签中的name
<input>标签是java web的jsp页面中最常用的标签,特别是用来在页面和servlet中传递内容, 但是我们看到<input>标签中有很多内容,这边我们只提一下主要的 ...
- POJ:2139-Six Degrees of Cowvin Bacon
传送门:http://poj.org/problem?id=2139 Six Degrees of Cowvin Bacon Time Limit: 1000MS Memory Limit: 6553 ...
- MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ':ge
数据库表里命名有这个字段,可怎么就是报错呢,大神的解释: 加上之后立马好用!!!
- 4,版本控制git --忽略特殊文件
有些时候,你必须把某些文件放到Git工作目录中,但又不能提交它们,比如保存了数据库密码的配置文件啦,等等,每次git status都会显示Untracked files ...,有强迫症的童鞋心里肯定 ...
- Hibernate---架构
Hibernate 架构是分层的,作为数据访问层,你不必知道底层 API .Hibernate 利用数据库以及配置数据来为应用程序提供持续性服务(以及持续性对象). 下面是一个非常高水平的 Hiber ...
- struts2官方 中文教程 系列三:使用struts2 标签 tag
避免被爬,先贴上本帖地址:struts2 官方系列教程一:使用struts2 标签 tag http://www.cnblogs.com/linghaoxinpian/p/6901316.html 本 ...
- 怎么设置才能让外网ip可以访问mysql数据库[转]
转自: http://www.hongyanliren.com/89.html 使用mysql中,很多人都会遇到这样的问题:在vps服务器或者云服务器上安装了mysql后,使用其他工具在外网ip之下根 ...
- 将有效慢日志转存到数据库v2
import re import sys import getopt import MySQLdb from subprocess import call import os host='10.76. ...
- split array
public boolean splitArray(int[] nums) { return dividSameSumGroup(0,nums, 0,0); } public boolean divi ...