51nod 1257 背包问题 V3
2017-02-15 14:06
267 查看
分数规划。
#include<bits/stdc++.h>
using namespace std;
const int MAXN=50050;
long long resw,resp;
int n,k;
struct node
{
int w,p;
double rate;
}a[MAXN];
bool cmp(node n1,node n2)
{
return n1.rate>n2.rate;
}
bool judge(double p)
{
int i;
double mark;
resw=0;
resp=0;
mark=0;
for(i=1;i<=n;i++)
a[i].rate=a[i].p-a[i].w*p;
sort(a+1,a+n+1,cmp);
for(i=1;i<=k;i++)
{
resw+=a[i].w;
resp+=a[i].p;
mark+=a[i].rate;
}
if(mark>=0)
return 1;
else
return 0;
}
int main()
{
int i;
double lef,rig,mid;
long long tmp,answ,ansp;
while(~scanf("%d%d",&n,&k))
{
for(i=1;i<=n;i++)
{
scanf("%d%d",&a[i].w,&a[i].p);
}
lef=0;
rig=50050;
for(i=0;i<100;i++)
{
mid=(lef+rig)/2;
if(judge(mid))
{
answ=resw;
ansp=resp;
lef=mid;
}
else
rig=mid;
}
tmp=__gcd(answ,ansp);
printf("%lld/%lld\n",ansp/tmp,answ/tmp);
}
}
#include<bits/stdc++.h>
using namespace std;
const int MAXN=50050;
long long resw,resp;
int n,k;
struct node
{
int w,p;
double rate;
}a[MAXN];
bool cmp(node n1,node n2)
{
return n1.rate>n2.rate;
}
bool judge(double p)
{
int i;
double mark;
resw=0;
resp=0;
mark=0;
for(i=1;i<=n;i++)
a[i].rate=a[i].p-a[i].w*p;
sort(a+1,a+n+1,cmp);
for(i=1;i<=k;i++)
{
resw+=a[i].w;
resp+=a[i].p;
mark+=a[i].rate;
}
if(mark>=0)
return 1;
else
return 0;
}
int main()
{
int i;
double lef,rig,mid;
long long tmp,answ,ansp;
while(~scanf("%d%d",&n,&k))
{
for(i=1;i<=n;i++)
{
scanf("%d%d",&a[i].w,&a[i].p);
}
lef=0;
rig=50050;
for(i=0;i<100;i++)
{
mid=(lef+rig)/2;
if(judge(mid))
{
answ=resw;
ansp=resp;
lef=mid;
}
else
rig=mid;
}
tmp=__gcd(answ,ansp);
printf("%lld/%lld\n",ansp/tmp,answ/tmp);
}
}
相关文章推荐
- 51Nod-1257-背包问题 V3
- 51nod 1257 背包问题V3
- 51nod 1257 背包问题 V3(分数规划)
- [51nod] 1257 背包问题 V3
- 51nod 1257 背包问题 V3
- 51nod 1257 背包问题 V3(二分)
- 51 nod oj 1257 背包问题 V3【01分数规划】
- 1257 背包问题 V3——分数规划
- 51Nod 1085 背包问题
- 51nod 1086 背包问题V2(巧妙拆分多重背包)
- 51Nod 有限背包计数问题 题解报告
- 51nod 1086 背包问题 V2
- 51nod-0-1背包问题
- 51nod-【多重背包问题】
- 51Nod 1085 背包问题(01背包 )
- 51nod 1086 背包问题 V2(多重背包)
- 51NOD 背包问题v2(动态规划)
- 51nod-1086 背包问题(多重背包)
- 51Nod-1085-背包问题
- 51nod 1085 背包问题