Description
We have received an order from Pizoor Communications Inc. for a special communication system. The system consists of several devices. For each device, we are free to choose from several manufacturers. Same devices from two manufacturers differ in their maximum bandwidths and prices.
By overall bandwidth (B) we mean the minimum of the bandwidths of the chosen devices in the communication system and the total price (P) is the sum of the prices of all chosen devices. Our goal is to choose a manufacturer for each device to maximize B/P.
By overall bandwidth (B) we mean the minimum of the bandwidths of the chosen devices in the communication system and the total price (P) is the sum of the prices of all chosen devices. Our goal is to choose a manufacturer for each device to maximize B/P.
Input
The first line of the input file contains a single integer t (1 ≤ t ≤ 10), the number of test cases, followed by the input data for each test case. Each test case starts with a line containing a single integer n (1 ≤ n ≤ 100), the number of devices in the communication system, followed by n lines in the following format: the i-th line (1 ≤ i ≤ n) starts with mi (1 ≤ mi ≤ 100), the number of manufacturers for the i-th device, followed by mi pairs of positive integers in the same line, each indicating the bandwidth and the price of the device respectively, corresponding to a manufacturer.
Output
Your program should produce a single line for each test case containing a single number which is the maximum possible B/P for the test case. Round the numbers in the output to 3 digits after decimal point.
Sample Input
1 3 3 100 25 150 35 80 25 2 120 80 155 40 2 100 100 120 110
Sample Output
0.649
Source
题意:每个厂家选一个设备,在所选宽带中最小的B除以所选宽带总价P,使得B/P最大。
1贪心:枚举最小宽带B,再去找最小价格P
#include <iostream>
#include <stdio.h>
#include <algorithm>
using namespace std;
const int MAXN = 110;
const double INF = 1e14;
int n;
struct node
{
int val, p;
bool operator < (const node a)const
{
if(val == a.val)
return p < a.p;
return val < a.val;
}
}g[MAXN][MAXN];
int len[MAXN];
int Val[MAXN*MAXN], sz;
double getminp(int xb,int minval)
{
int min1 = -1, max1 = len[xb], mid;
while(max1 > min1 + 1)
{
mid = (max1 + min1)>>1;
if(g[xb][mid].val>=minval)
{
max1 = mid;
}else{
min1 = mid;
}
}
if(max1==len[xb])return -INF;
return g[xb][max1].p;
}
int main()
{
int t, i, j;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
sz = 0;
for(i=0; i<n; i++)
{
scanf("%d",&len[i]);
for(j=0; j<len[i]; j++)
{
scanf("%d%d",&g[i][j].val,&g[i][j].p);
Val[sz++] = g[i][j].val;
}
//按宽带大小排序,然后价格取最小
sort(g[i],g[i]+len[i]);
int minp = g[i][len[i]-1].p;
for(j=len[i]-2; j>=0; j--)
{
if(minp > g[i][j].p)
{
minp = g[i][j].p;
}else{
g[i][j].p = minp;
}
}
}
double ans = 0.0;
sort(Val,Val+sz);
for(i=0; i<sz; i++) //枚举最小的宽带
{
if(i!=0&&Val[i]==Val[i-1])continue;
double minval = Val[i],sum = 0;
for(j=0; j<n; j++)
{
sum += getminp(j, minval); //得到厂商j,宽带大于等于minval的最小价格
}
//cout<<minval<< " "<<sum<<endl;
ans = max(ans,minval/sum);
}
printf("%.3f\n", ans);
}
return 0;
}
2,动态规划,dp[i][j]表示前i个商店,最小宽带为j时的最小花费,由于这是一层一层递推的,所以直接可以用dp[j]表示 最小宽带为j时的最小花费就可以了。j采用了离散化,防止越界。
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <cstring>
using namespace std;
const int MAXN = 110;
const double INF = 1e14;
int n;
struct node
{
int val, p;
bool operator < (const node a)const
{
if(val == a.val)
return p < a.p;
return val < a.val;
}
}g[MAXN][MAXN];
int len[MAXN];
int Val[MAXN*MAXN], sz;
double dp[MAXN*MAXN]; // dp[j]表示最小宽带为j时的最小花费
int main()
{
int t, i, j;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
sz = 0;
for(i=0; i<n; i++)
{
scanf("%d",&len[i]);
for(j=0; j<len[i]; j++)
{
scanf("%d%d",&g[i][j].val,&g[i][j].p);
Val[sz++] = g[i][j].val;
}
//按宽带大小排序,然后价格取最小
sort(g[i],g[i]+len[i]);
int minp = g[i][len[i]-1].p;
for(j=len[i]-2; j>=0; j--)
{
if(minp > g[i][j].p)
{
minp = g[i][j].p;
}else{
g[i][j].p = minp;
}
}
}
sort(Val,Val+sz);
sz = unique(Val,Val+sz) - Val;
memset(dp,0,sizeof dp);
for(i=0; i<n; i++)
{
//商品是排好序的,所以从第一个商品开始
int top = 0;
for(j=0; j<sz; j++)
{
while(top<len[i]&&g[i][top].val < Val[j])top++;
if(top==len[i])
dp[j] = -INF;
else
dp[j] += g[i][top].p;
}
}
double ans = 0.0;
for(j=0; j<sz; j++)
{
//cout<<Val[j]<<" "<<dp[j]<<endl;
ans = max(ans, Val[j]/dp[j]);
}
printf("%.3f\n", ans);
}
return 0;
}