此题一开始以为是dp题目,但是想了很久都想不出dp的解法,最后是用贪心过的
贪心思路如下:
1.首先对所有的宽带进行排序
2.对每组数据按照价钱排序
3.然后从小到打枚举每种宽带的可能性
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define MAXN 101
typedef struct B_NODE {
int b, p, ori_pos;
}BNODE;
typedef struct VAL_ {
int b, p;
}VAL;
typedef struct NODE_ {
int m;
VAL val[MAXN];
}NODE;
NODE type[MAXN];
BNODE b_node[MAXN*MAXN];
int pos;
int b_node_cmp(const BNODE &a, const BNODE &b)
{
return a.b < b.b;
}
int type_cmp(const VAL &a, const VAL &b)
{
return a.p < b.p;
}
int select_ans(const int &x_pos, const int &up, const int &n)
{
int rst(0), flag;
for(int i = 0; i < n; i ++) {
if( i == x_pos ) {
continue;
}
flag = 0;
for(int j = 0; j < type[i].m; j ++) {
if( type[i].val[j].b < up ) {
continue;
}
rst += type[i].val[j].p; flag = 1; break;
}
if( !flag ) {
return -1;
}
}
return rst;
}
double forces_greed(const int &n)
{
double ans(0.0);
int select_rst, up, down;
for(int i = 0; i < pos; i ++) {
up = b_node[i].b; down = b_node[i].p;
if( -1 == (select_rst = select_ans(b_node[i].ori_pos, up, n)) ) {
return ans;
}
ans = max(ans, (double)up/(down+select_rst));
}
return ans;
}
int main(int argc, char const *argv[])
{
#ifndef ONLINE_JUDGE
freopen("test.in", "r", stdin);
#endif
int cas, n;
scanf("%d", &cas);
while( cas -- ) {
scanf("%d", &n); pos = 0;
for(int i = 0; i < n; i ++) { scanf("%d", &type[i].m);
for(int j = 0; j < type[i].m; j ++) {
scanf("%d %d", &type[i].val[j].b, &type[i].val[j].p); b_node[pos].ori_pos = i;
b_node[pos].b = type[i].val[j].b; b_node[pos ++].p = type[i].val[j].p;
}
}
sort(b_node, b_node+pos, b_node_cmp);
for(int i = 0; i < n; i ++) {
sort(type[i].val, type[i].val+type[i].m, type_cmp);
}
printf("%.3lf\n", forces_greed(n));
}
return 0;
}