51nod1257 背包问题 V3
分数规划经典。开始精度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;
}
第1行:包括2个数N, K(1 <= K <= N <= 50000)
第2 - N + 1行:每行2个数Wi, Pi(1 <= Wi, Pi <= 50000)
输出单位体积的价值(用约分后的分数表示)。
3 2
2 2
5 3
2 1
3/4
51nod1257 背包问题 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 ...
- 1257 背包问题 V3——分数规划
N个物品的体积为W1,W2......Wn(Wi为整数),与之相对应的价值为P1,P2......Pn(Pi为整数),从中选出K件物品(K <= N),使得单位体积的价值最大. Input 第1 ...
- 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 ...
- 51nod——1086、1257背包问题V2(多重背包二进制拆分转01) V3(分数规划+二分贪心)
V3其实和dp关系不大,思想挂标题上了,丑陋的代码不想放了.
- HDU 3466 Proud Merchants(背包问题,要好好理解)
Problem Description Recently, iSea went to an ancient country. For such a long time, it was the most ...
- 【ACM】Knapsack without repetition - 01背包问题
无界背包中的状态及状态方程已经不适用于01背包问题,那么我们来比较这两个问题的不同之处,无界背包问题中同一物品可以使用多次,而01背包问题中一个背包仅可使用一次,区别就在这里.我们将 K(ω)改为 K ...
随机推荐
- Rust: move和borrow
感觉Rust官方的学习文档里关于ownship,borrow和lifetime介绍的太简略了,无法真正理解这些语法设计的原因以及如何使用(特别是lifetime).所以找了一些相关的blog来看,总结 ...
- (翻译)Google Guava Cache
翻译自Google Guava Cache This Post is a continuation of my series on Google Guava, this time covering G ...
- applicationContext.xml xxx-servlet.xml
applicationContext.xml是随ContextLoaderListener的加载而执行的,而xxx-servlet.xml是随DispatcherServlet的加载而执行的,在web ...
- Sqli-labs less 25a
Less-25a 不同于25关的是sql语句中对于id,没有''的包含,同时没有输出错误项,报错注入不能用.其余基本上和25示例没有差别.此处采取两种方式:延时注入和联合注入. http://127. ...
- BZOJ2435: [Noi2011]道路修建
这种水题真是……没一次AC都不好意思见人啊 P.S. LINUX无限栈真是爽炸了… 我爱递归 /**************************************************** ...
- 13test06:花朵数
#include<iostream> using namespace std; #define N a//定义宏时后面不加:否则会把':'一起定义为宏. int getP(int,int) ...
- ASP.NET Session的七点认识
原文:http://kb.cnblogs.com/page/108689/ ASP.NET Session的使用当中我们会遇到很多的问题,那么这里我们来谈下经常出现的一些常用ASP.NET Sessi ...
- Javascript Arguments,calle,caller,call,apply
一.Arguments 该对象代表正在执行的函数和调用他的函数的参数. [function.]arguments[n] 参数function :选项.当前正在执行的 Function 对象的名字. n ...
- 用C语言简单加密解密
使用char表示的字符型数据,在本质上与我们前面介绍的整型数据并无太大的区别,只是char类型占用的内存字节数更小,能够表示的数据范围更小而已.在使用上,char被专门用来表示C语言的字符集中的各种字 ...
- android-exploitme(一):生成apk
exploitme是一个国外的android安全测试环境,http://securitycompass.github.io/AndroidLabs/index.html,通过它可以学习一些基本的测试方 ...