分数规划经典。开始精度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. 8086CPU各寄存器的用途

    8086 有14个16位寄存器,这14个寄存器按其用途可分为(1)通用寄存器.(2)指令指针.(3)标志寄存器和(4)段寄存器等4类. 1.通用寄存器有8个, 又可以分成2组,一组是数据寄存器(4个) ...

  2. Activity学习(四)——简单切换

    理论学习Activity之后,我们就来具体的实战,Activity之间相互切换依靠的是“ 意图 ”(Intent),这个 Intent 包含了要跳转到的Activity的一些信息,因为Activity ...

  3. IDEA开发spark本地运行

    1.建立spakTesk项目,建立scala对象Test 2.Tesk对象的代码如下 package sparkTest /** * Created by jiahong on 15-8-2. */ ...

  4. Difference Between Vector and Deque in C++

    1) Dequeue can quickly insert or delete both at the front or the end. However, vector can only quick ...

  5. Linux下进程通信的八种方法

    Linux下进程通信的八种方法:管道(pipe),命名管道(FIFO),内存映射(mapped memeory),消息队列(message queue),共享内存(shared memory),信号量 ...

  6. poj 3228(二分+最大流)

    题目链接:http://poj.org/problem?id=3228 思路:增设一个超级源点和一个超级汇点,源点与每一个gold相连,容量为gold数量,汇点与仓库相连,容量为仓库的容量,然后就是二 ...

  7. 540A: Combination Lock

    题目链接:http://codeforces.com/problemset/problem/540/A 题意: 输入的两个长度一样的数,求对应位置的某位数到下一个数需要最小的步长,每次只能先前或先后走 ...

  8. 安卓RadioButton的使用

    学习目的: 1.掌握在Android中如何建立RadioGroup和RadioButton 2.掌握RadioGroup的常用属性 3.理解RadioButton和CheckBox的区别 4.掌握Ra ...

  9. android学习笔记二:Intent

    1.Intent作用 协助完成各个组建间的通信.如activity间.启动service.Broadcast. 2.Intent构成 1.Componet name:要启动的目的组建. 2.Actio ...

  10. Android 软键盘弹出时把布局顶上去,控件乱套解决方法

    解决办法:方法一:在你的activity中的oncreate中setContentView之前写上这个代码getWindow().setSoftInputMode(WindowManager.Layo ...