【贪心】Communication System POJ 1018
2017-07-15 14:51
387 查看
题目链接:http://poj.org/problem?id=1018
题目大意:有n种通讯设备,每种有mi个制造商,bi、pi分别是带宽和价格。在每种设备中选一个制造商让最小带宽B与总价格P的比值B/P最大。
解法是枚举最小带宽B,每种设备在带宽大于B的制造商中找价格最小的,最后取比值最大的。
详见代码:
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
int n,m,d[105][105],p[105][105],Min,Max,t;
int main()
{
scanf("%d",&t);
while(t--)
{
memset(d,0,sizeof d);
memset(p,0,sizeof p);
Min=0xffff;
Max=0;
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%d",&d[i][0]);
for(int j=1;j<=d[i][0];j++)
{
scanf("%d%d",&d[i][j],&p[i][j]);
Max=Max<d[i][j]?d[i][j]:Max;
Min=Min>d[i][j]?d[i][j]:Min;
}
}
double ans=0;
for(int i=Min;i<=Max;i++)
{
int sum=0;
for(int j=1;j<=n;j++){
int mx=0xffff;
for(int k=1;k<=d[j][0];k++)
if(d[j][k]>=i&&mx>p[j][k])
mx=p[j][k];
sum+=mx;
}
if(ans<i*1.0/sum) ans=i*1.0/sum;
}
printf("%.3lf\n",ans);
}
}
4000
题目大意:有n种通讯设备,每种有mi个制造商,bi、pi分别是带宽和价格。在每种设备中选一个制造商让最小带宽B与总价格P的比值B/P最大。
解法是枚举最小带宽B,每种设备在带宽大于B的制造商中找价格最小的,最后取比值最大的。
详见代码:
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
int n,m,d[105][105],p[105][105],Min,Max,t;
int main()
{
scanf("%d",&t);
while(t--)
{
memset(d,0,sizeof d);
memset(p,0,sizeof p);
Min=0xffff;
Max=0;
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%d",&d[i][0]);
for(int j=1;j<=d[i][0];j++)
{
scanf("%d%d",&d[i][j],&p[i][j]);
Max=Max<d[i][j]?d[i][j]:Max;
Min=Min>d[i][j]?d[i][j]:Min;
}
}
double ans=0;
for(int i=Min;i<=Max;i++)
{
int sum=0;
for(int j=1;j<=n;j++){
int mx=0xffff;
for(int k=1;k<=d[j][0];k++)
if(d[j][k]>=i&&mx>p[j][k])
mx=p[j][k];
sum+=mx;
}
if(ans<i*1.0/sum) ans=i*1.0/sum;
}
printf("%.3lf\n",ans);
}
}
4000
相关文章推荐
- POJ 1018 Communication System(DP+离散化)
- POJ 1018 Communication System DP
- POJ 1018 Communication System
- poj 1018 Communication System DP思想
- POJ 1018 Communication System dp
- 【贪心】【POJ-1018】Communication System
- poj 1018 Communication System 【动态规划】||【贪心】
- POJ 1018 Communication System(搜索/贪心/动归)
- 【POJ 1018】Communication System(dp|贪心)
- Communication System dp 动态规划 || 贪心
- poj 1018 Communication System_贪心
- POJ1018 Communication System(DP||贪心)
- pku 1018 Communication System dp
- poj 1018 Communication System_贪心
- poj 1018 Communication System 枚举 VS 贪心
- poj 1018 Communication System 枚举 VS 贪心
- poj 1018 Communication System (dp | 贪心)
- POJ 1018 Communication System 贪心+枚举
- POJ 1018 Communication System 贪心+枚举
- poj 1018 Communication System 贪心