poj 1018 Communication System 枚举 VS 贪心
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 21631 | Accepted: 7689 |
Description
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
Output
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
做这题的时候,一开始的想法就是暴力枚举,虽然这样可能会超时,但是觉得可以做强有力的剪枝,于是就试了下,可是没有做论证推断,不知道应该怎样去做剪枝,其实主要是嫌麻烦,下面是我简单的枚举代码,不过超时了。
#include <stdio.h>
#include <stdlib.h>
#define MAX 100 struct Dev{
int b;
int p; }dev[MAX][MAX]; int n;
float result = -1; void Init()
{
int i, j;
for(i=0; i < MAX; i++)
{
for(j=0; j < MAX; j++)
{
dev[i][j].b= -1;
dev[i][j].p = -1;
}
}
} void PrintData(int *data, int *sum)
{
int min=data[0], temp=sum[0], i;
float tempRes;
for(i=1; i < n; i++)
{
if(data[i] < min) min = data[i];
temp += sum[i];
}
tempRes = min*1.0/ temp;
if(tempRes > result)
result = tempRes;
} void SolveCase(int *data,int *sum, int depth)
{
int i;
for(i=0; i<MAX && dev[depth][i].b != -1; i++)
{
data[depth] = dev[depth][i].b;
sum[depth] = dev[depth][i].p;
if(depth==n-1)
PrintData(data, sum);
else
SolveCase(data,sum, depth+1);
}
} int main()
{
// freopen("input.txt","r",stdin);
int caseNum, number;
int *testData, *sum , i, j; scanf("%d",&caseNum);
while(caseNum > 0)
{
Init();
scanf("%d", &n);
for(i=0; i < n; i++)
{
scanf("%d",&number);
for(j=0; j < number; j++)
scanf("%d %d", &dev[i][j].b, &dev[i][j].p);
}
testData = (int *)malloc(sizeof(int)*n);
sum = (int *)malloc(sizeof(int)*n);
SolveCase(testData,sum,0);
printf("%.3f\n",result);
result = -1;
caseNum--;
}
free(testData);
free(sum);
// fclose(stdin);
return 0;
}
超时之后,感觉可以用贪心做,然后贪心的话每次使得b 值增大,使得 p 值减少,这样才能使得结果是最大了,思路很简单,以为还是不行,结果AC 了.....
#include<cstdio>
#include<cstring>
int main()
{
// freopen("input.txt","r",stdin);
int t,n,m,b[105][105],fac[105],p[105][105],flag[32767],max,min,tp;
scanf("%d",&t);
while(t--){
max=0,min=9999999;
scanf("%d",&n);
memset(flag,0,sizeof(flag));
for(int i=0;i<n;i++){
scanf("%d",&fac[i]);
for(int j=0;j<fac[i];j++){
scanf("%d%d",&b[i][j],&p[i][j]);
flag[b[i][j]]=1;
if(max<b[i][j])
max=b[i][j];
if(min>b[i][j])
min=b[i][j];
}
}
double result=0;
for(int i=min;i<=max;i++){
if(flag[i]){
int sum=0;
for(int j=0;j<n;j++){
tp=99999999;
for(int k=0;k<fac[j];k++){
if(b[j][k]>=i&&p[j][k]<tp){
tp=p[j][k];
}
}
sum+=tp;
}
double temp=(double)i/sum;
if(result<temp)
result=temp;
}
}
printf("%.3f\n",result);
}
// fclose(stdin);
return 0;
}
poj 1018 Communication System 枚举 VS 贪心的更多相关文章
- poj 1018 Communication System (枚举)
Communication System Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 22380 Accepted: ...
- POJ 1018 Communication System(贪心)
Description We have received an order from Pizoor Communications Inc. for a special communication sy ...
- POJ 1018 Communication System(树形DP)
Description We have received an order from Pizoor Communications Inc. for a special communication sy ...
- poj 1018 Communication System
点击打开链接 Communication System Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 21007 Acc ...
- POJ 1018 Communication System (动态规划)
We have received an order from Pizoor Communications Inc. for a special communication system. The sy ...
- POJ 1018 Communication System 贪心+枚举
看题传送门:http://poj.org/problem?id=1018 题目大意: 某公司要建立一套通信系统,该通信系统需要n种设备,而每种设备分别可以有m个厂家提供生产,而每个厂家生产的同种设备都 ...
- POJ 1018 Communication System(DP)
http://poj.org/problem?id=1018 题意: 某公司要建立一套通信系统,该通信系统需要n种设备,而每种设备分别可以有m1.m2.m3.....mn个厂家提供生产,而每个厂家生产 ...
- POJ 1018 Communication System 题解
本题一看似乎是递归回溯剪枝的方法.我一提交,结果超时. 然后又好像是使用DP,还可能我剪枝不够. 想了非常久,无奈忍不住偷看了下提示.发现方法真多.有贪心,DP,有高级剪枝的.还有三分法的.八仙过海各 ...
- poj 1018 Communication System_贪心
题意:给你n个厂,每个厂有m个产品,产品有B(带宽),P(价格),现在要你求最大的 B/P 明显是枚举,当P大于一定值,B/P为零,可以用这个剪枝 #include <iostream> ...
随机推荐
- 枚举最短路径+SPFA
Harry Potter and the Final Battle Submit Status Description The final battle is coming. Now Harry Po ...
- ThinkPHP验证码类
//ThinkPHP验证码类使用$config = array( 'fontSize' => 30, // 验证码字体大小 'length' => 3, // 验证码位数 'useNois ...
- POJ 2479 不相交最大子段和
题目意思还是很好理解的,在一个数列中,找出不相交的两个子串使得其和最大. 解题思路: 对于每个i来说,求出[0 ~ i - 1] 的最大子段和以及[i ~ n - 1]的最大子段和,在加起来,求最大的 ...
- jquery删除动态增加的li
<script type="text/jscript"> //楼主帮你修改调整了下 $(document).ready(function () { $('.zuo li ...
- 【原创】Android 系统稳定性 - ANR(二)
文章都为原创,转载请注明出处,未经允许而盗用者追究法律责任. 很久之前写的了,留着有点浪费,共享之.编写者:李文栋P.S. OpenOffice粘贴过来后格式有些混乱. 1.2 如何分析ANR问题 引 ...
- android JB2连拍降速原理介绍
1.HAL层 (1)alps\mediatek\platform\mt6589\hardware\camera\core\camshot\MultiShot\MultiShot.cpp sleep实现 ...
- C Primer Plus 读书笔记之C基础回顾
目标代码文件.可执行文件和库 C编程的基本策略是使用程序将源代码文件转换为可执行文件,此文件包含可以运行的机器语言代码.C分两步完成这一工作:编译和链接.编译器将源代码转换为中间代码,链接器将此中间代 ...
- ACM比赛(11462 Age Sort)
You are given the ages (in years) of all people of a country with at least 1 year of age. You know t ...
- Linux命令: chown
touch auth.log root@ubuntu:/work# ls -l auth.log -rw-r--r-- 1 root root 0 Feb 18 19:27 auth.log chow ...
- 网页制作之JavaScript部分3--事件及事件传输方式(函数调用 练习题 )重要---持续更新中
一. 事件:说白了就是调用函数的一种方式.它包括:事件源.事件数据.事件处理程序. JS事件 1.js事件通常和函数结合来使用,这样可以通过发生的事件来驱动函数的执行,从而引起html出现不同的效果. ...