本题一看似乎是递归回溯剪枝的方法。我一提交,结果超时。

然后又好像是使用DP,还可能我剪枝不够。

想了非常久,无奈忍不住偷看了下提示。发现方法真多。有贪心,DP,有高级剪枝的。还有三分法的。八仙过海各显神通啊。

坏习惯了,没思考够深入就偷看提示了。

幸好及时回头,还不须要看别人的代码了。自己做出来之后,有空看看多种解法的代码也好。

然后我想出自己的思路了,使用贪心,剪枝,DP综合优化下,呵呵。最后程序有点复杂。优化到了16ms,运气好点,或者vector换成原始数组的话,应该能够0MS了。

整体思路就是:

1 利用STL 的set容器记录有多少不同的B值

2 依据不同的B值,用表tbl记录该B值下的最优解

最后比較全部B值下的最优解。得出终于最优解。

以下是优化过的程序,用了不少技巧,加了凝视,希望提高參考价值吧。

#include <stdio.h>
#include <float.h>
#include <limits.h>
#include <algorithm>
#include <vector>
#include <set>
using namespace std; const int MAX_N = 101;
int N, M; struct BP
{
int B, P;
bool operator<(const BP &b) const
{
return B < b.B;
}
}; BP arr[MAX_N][MAX_N]; float DP(set<int> &bset)
{
for (int i = 0; i < N; i++)
{
for (int j = arr[i][0].B-1; j > 0 ; j--)
{
arr[i][j].P = min(arr[i][j].P, arr[i][j+1].P);
}//计算结果为当前大于某个B的最小P值,优化以下填表
} vector<int> bvec(bset.begin(), bset.end());
int M = (int)bvec.size(); //总共同拥有多少个不同的B值
vector<vector<int> > tbl(N, vector<int>(M));//记录当前B下的最优P值
vector<int> idx(N, 1); //arr行的当前下标 for (int j = 0; j < M; j++)
{
for (int i = 0; i < N; i++)
{
for ( ; idx[i] <= arr[i][0].B; idx[i]++)
{
if (arr[i][idx[i]].B >= bvec[j])
{
tbl[i][j] = arr[i][idx[i]].P;
break;
}
}
if (idx[i] > arr[i][0].B)//某行无法选出比B更大的值了
{
tbl[0][j] = -1;//做好标志,剪枝
goto out;
}
}
}
out:;
float ans = 0.0f;
for (int j = 0; j < M && tbl[0][j] != -1; j++)
{
int totalP = 0;
for (int i = 0; i < N; i++)
{
totalP += tbl[i][j];
}
ans = max(ans, float(bvec[j])/float(totalP));
}
return ans;
} int main()
{
int T;
scanf("%d", &T);
while (T--)
{
scanf("%d", &N);
set<int> bset;
for (int i = 0; i < N; i++)
{
scanf("%d", &arr[i][0].B); //记录当前维长度
for (int j = 1; j <= arr[i][0].B; j++)
{
scanf("%d %d", &arr[i][j].B, &arr[i][j].P);
bset.insert(arr[i][j].B);//记录有多少个不同的B值
}
sort(arr[i]+1, arr[i]+arr[i][0].B+1);//每维按B值由小到大排序
}
printf("%.3f\n", DP(bset));
}
return 0;
}

POJ 1018 Communication System 题解的更多相关文章

  1. POJ 1018 Communication System(树形DP)

    Description We have received an order from Pizoor Communications Inc. for a special communication sy ...

  2. POJ 1018 Communication System (动态规划)

    We have received an order from Pizoor Communications Inc. for a special communication system. The sy ...

  3. poj 1018 Communication System

    点击打开链接 Communication System Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 21007   Acc ...

  4. poj 1018 Communication System 枚举 VS 贪心

    Communication System Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 21631   Accepted:  ...

  5. POJ 1018 Communication System(贪心)

    Description We have received an order from Pizoor Communications Inc. for a special communication sy ...

  6. poj 1018 Communication System (枚举)

    Communication System Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 22380   Accepted:  ...

  7. POJ 1018 Communication System(DP)

    http://poj.org/problem?id=1018 题意: 某公司要建立一套通信系统,该通信系统需要n种设备,而每种设备分别可以有m1.m2.m3.....mn个厂家提供生产,而每个厂家生产 ...

  8. POJ 1018 Communication System 贪心+枚举

    看题传送门:http://poj.org/problem?id=1018 题目大意: 某公司要建立一套通信系统,该通信系统需要n种设备,而每种设备分别可以有m个厂家提供生产,而每个厂家生产的同种设备都 ...

  9. poj 1018 Communication System_贪心

    题意:给你n个厂,每个厂有m个产品,产品有B(带宽),P(价格),现在要你求最大的 B/P 明显是枚举,当P大于一定值,B/P为零,可以用这个剪枝 #include <iostream> ...

随机推荐

  1. Ext.js二级联动

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <link href ...

  2. [python学习篇][书籍学习][python standrad library][内建类型]迭代器类型

    我们已经知道,可以直接作用于for循环的数据类型有以下几种:一类是集合数据类型,如list.tuple.dict.set.str等:一类是generator,包括生成器和带yield的generato ...

  3. [python 测试框架学习篇] 分享 uiautomator测试框架

    uiautomator测试框架 :https://testerhome.com/topics/4194

  4. ICCID

     ICCID:Integrate circuit card identity 集成电路卡识别码(固化在手机SIM卡中) ICCID为IC卡的唯一识别号码,共有20位数字+英文组成,其编码格式为:XXX ...

  5. Phpstrom 书签应用

    F11增加书签 书签 Ctrl + F11切换书签助记符 Ctrl +#[0-9]转到编号书签 Shift + F11显示书签

  6. C++之Effective STL学习笔记Item21

    好了,先贴一段英文,真心不想翻译过来,而且感觉也翻译不到那么到位: The STL is awash in comparisons of objects to see if they have the ...

  7. 【bzoj3307】雨天的尾巴 权值线段树合并

    题目描述 N个点,形成一个树状结构.有M次发放,每次选择两个点x,y,对于x到y的路径上(含x,y)每个点发一袋Z类型的物品.完成所有发放后,每个点存放最多的是哪种物品. 输入 第一行数字N,M接下来 ...

  8. 性能学习之--loaderunner压测

    打开一个脚本,tools-create Controllwer Scenario,开始场景的设计 一.场景设计--手工测试 1.初始化 2.start vu 一般选择simultaneously,用户 ...

  9. debian 切换最新源

    deb http://ftp.cn.debian.org/debian sid main#deb http://ftp.debian.org/debian/ wheezy main

  10. linux--bash: redis-server: 未找到命令

    linux 安装redis过程中出现了异常,make不通过,异常如下: [root@localhost redis-2.8.3]# make cd src && make all ma ...