分数规划经典。开始精度1e-3/1e-4都不行,1e-5就A了

#include<cstdio>
#include<cstring>
#include<cctype>
#include<algorithm>
using namespace std;
#define rep(i,s,t) for(int i=s;i<=t;i++)
#define dwn(i,s,t) for(int i=s;i>=t;i--)
#define clr(x,c) memset(x,c,sizeof(x))
#define ll long long
int read(){
int x=0;char c=getchar();
while(!isdigit(c)) c=getchar();
while(isdigit(c)) x=x*10+c-'0',c=getchar();
return x;
}
const double eps=1e-5;
const int nmax=5e4+5;
const int inf=0x7f7f7f7f;
struct node{
long double x;int cur;
bool operator<(const node&rhs)const{
return x<rhs.x;}
};
node ns[nmax];
int a[nmax],b[nmax];
ll gcd(ll a,ll b){
return b==0?a:gcd(b,a%b);
}
int main(){
int n=read(),K=read();
rep(i,1,n) b[i]=read(),a[i]=read();
double l=0,r=10000,mid,ans,sm;
while(r-l>eps){
mid=(r+l)/2;
rep(i,1,n) ns[i].cur=i,ns[i].x=a[i]*1.0-mid*b[i];
sort(ns+1,ns+n+1);
sm=0;rep(i,n-K+1,n) sm+=ns[i].x;
if(sm>=0) ans=l=mid;
else r=mid;
}
ll ta=0,tb=0;
rep(i,n-K+1,n) ta+=a[ns[i].cur],tb+=b[ns[i].cur];
ll tp=gcd(ta,tb);
printf("%lld/%lld\n",ta/tp,tb/tp);
return 0;
}

  

基准时间限制:3 秒 空间限制:131072 KB 分值: 80 难度:5级算法题
 收藏
 关注
N个物品的体积为W1,W2......Wn(Wi为整数),与之相对应的价值为P1,P2......Pn(Pi为整数),从中选出K件物品(K <= N),使得单位体积的价值最大。
Input
第1行:包括2个数N, K(1 <= K <= N <= 50000)
第2 - N + 1行:每行2个数Wi, Pi(1 <= Wi, Pi <= 50000)
Output
输出单位体积的价值(用约分后的分数表示)。
Input示例
3 2
2 2
5 3
2 1
Output示例
3/4

51nod1257 背包问题 V3的更多相关文章

  1. 51nod 1257 背包问题 V3

    1257 背包问题 V3 基准时间限制:3 秒 空间限制:131072 KB 分值: 80 难度:5级算法题 N个物品的体积为W1,W2......Wn(Wi为整数),与之相对应的价值为P1,P2.. ...

  2. 1257 背包问题 V3(二分)

    1257 背包问题 V3 3 秒 131,072 KB 80 分 5 级题 题意 : 从n个物品中选出k个,使单位体积价值最大 思路: 一开始正面想,试过很多种,排序什么的..总是结果不对,最后想到二 ...

  3. 51nod 1257 背包问题 V3(分数规划)

    显然是分数规划...主要是不会求分数的形式,看了题解发现自己好傻逼QAQ 还是二分L值算出d[]降序选K个,顺便记录选择时候的p之和与w之和就可以输出分数形式了... #include<iost ...

  4. 1257 背包问题 V3——分数规划

    N个物品的体积为W1,W2......Wn(Wi为整数),与之相对应的价值为P1,P2......Pn(Pi为整数),从中选出K件物品(K <= N),使得单位体积的价值最大. Input 第1 ...

  5. 51nod 1257 背包问题 V3(这不是背包问题是二分)

    题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1257 题解:不能按照单位价值贪心,不然连样例都过不了 要求的 ...

  6. 51nod 1257 01分数规划/二分

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1257 1257 背包问题 V3 基准时间限制:3 秒 空间限制:1310 ...

  7. 51nod——1086、1257背包问题V2(多重背包二进制拆分转01) V3(分数规划+二分贪心)

    V3其实和dp关系不大,思想挂标题上了,丑陋的代码不想放了.

  8. HDU 3466 Proud Merchants(背包问题,要好好理解)

    Problem Description Recently, iSea went to an ancient country. For such a long time, it was the most ...

  9. 【ACM】Knapsack without repetition - 01背包问题

    无界背包中的状态及状态方程已经不适用于01背包问题,那么我们来比较这两个问题的不同之处,无界背包问题中同一物品可以使用多次,而01背包问题中一个背包仅可使用一次,区别就在这里.我们将 K(ω)改为 K ...

随机推荐

  1. 使用Assetbundle时可能遇到的坑

    原地址:http://www.cnblogs.com/realtimepixels/p/3652128.html 一 24 十一郎未分类 No Comments 转自 http://www.unity ...

  2. myeclipse 10的破解以及运行run.bat错误或者双击立即消失的问题

    安装包下载: ed2k://|file|[myeclipse.10.0.更新发布].myeclipse-10.0-offline-installer-windows.exe|947752488|73b ...

  3. 二维码(支持arc,苹果自带扫描,zbar扫描,二维码生成)

    下载地址:http://pan.baidu.com/s/1mgvJzyo

  4. POJ 2027

    #include<iostream> using namespace std; int main() { int time; cin>>time; int a; int b; ...

  5. POJ 2533 Longest Ordered Subsequence

    题目描述:LIS(Longest Increasing Subsequence)模板题 分析:O(n^2)的方法 状态表示:d[i]表示以i结尾的最长上升子序列长度 转移方程:d[i]=max{ 1, ...

  6. VMware 使用

    1.客户操作系统被禁用: BIOS中开启VT(Virtual Technology)

  7. ExtJs之Ext.isEmpty

    <!DOCTYPE html> <html> <head> <title>ExtJs</title> <meta http-equiv ...

  8. Gvim for php 安装配置

    VIM for PHP Windows 2011-05-14 11:51:51|  分类: Php|举报|字号 订阅     虽然vim本质上只是一个编辑器.但只要配合一些适当的插件,vim也能变成一 ...

  9. poj 3635(bfs+优先队列)

    题目链接:http://poj.org/problem?id=3635 思路:本题主要运用的还是贪心思想,由于要求st->ed的最小花费,那么每经过一个城市,能不加油就尽量不加油,用dp[i][ ...

  10. MongoDB 管理工具:Robomongo

    http://www.open-open.com/lib/view/open1383029577546.html