首页 > ACM题库 > HDU-杭电 > HDU 3233-Download Manager-模拟-[解题报告]HOJ
2014
03-09

HDU 3233-Download Manager-模拟-[解题报告]HOJ

Download Manager

问题描述 :

Jiajia downloads a lot, a lot more than you can even imagine. Some say that he starts downloading up to 20,000 files together. If 20,000 files try to share a limited bandwidth then it will be a big hazard and no files will be downloaded properly. That is why, he uses a download manager.

If there are T files to download, the download manger uses the following policy while downloading files:

1. The download manager gives the smaller files higher priority, so it starts downloading the smallest n files at startup. If there is a tie, download manager chooses the one with less bytes remaining (for download). We assume that with at least 50 Mega Bytes/sec of bandwidth, n files can be downloaded simultaneously without any problem.

2. The available bandwidth is equally shared by the all the files that are being downloaded. When a file is completely downloaded its bandwidth is instantaneously given to the next file. If there are no more files left except the files that are being downloaded, this bandwidth is immediately shared equally by all remaining files that are being downloaded.

Given the size and completed percentage of each file, your task is to intelligently simulate the behavior of the download manager to find the total time required to download all the files.

输入:

The will be at most 10 test cases. Each case begins with three integers T (1 <= T <= 20000), n (1 <= n <= 2000 and 1 <= n <= T) and B (50 <= B <= 1000). Here B denotes the total bandwidth available to Jiajia (In Megabytes/sec). Please note that the download manager always downloads n files in parallel unless there are less than n files available for download. Each of next T lines contains one non-negative floating-point number S (less than 20,000, containing at most 2 digits after the decimal places) and one integer P (0 <= P <= 100). These two numbers denote a file whose size is S megabyte and which has been downloaded exactly P% already. Also note that although theoretically it is not possible that the size of a file or size of its remaining part is a fraction when expressed in bytes, for simplicity please assume that such thing is possible in this problem. The last test case is followed by T=n=B=0, which should not be processed.

输出:

The will be at most 10 test cases. Each case begins with three integers T (1 <= T <= 20000), n (1 <= n <= 2000 and 1 <= n <= T) and B (50 <= B <= 1000). Here B denotes the total bandwidth available to Jiajia (In Megabytes/sec). Please note that the download manager always downloads n files in parallel unless there are less than n files available for download. Each of next T lines contains one non-negative floating-point number S (less than 20,000, containing at most 2 digits after the decimal places) and one integer P (0 <= P <= 100). These two numbers denote a file whose size is S megabyte and which has been downloaded exactly P% already. Also note that although theoretically it is not possible that the size of a file or size of its remaining part is a fraction when expressed in bytes, for simplicity please assume that such thing is possible in this problem. The last test case is followed by T=n=B=0, which should not be processed.

样例输入:

6 3 90
100.00 90
40.40 70
60.30 70
40.40 80
40.40 85
40.40 88
1 1 56
12.34 100
0 0 0

样例输出:

Case 1: 0.66

Case 2: 0.00

Hint
Explanation In the first sample, there are 6 files and the download manager can download 3 files simultaneously. The size of the smallest file is 40.40 Megabyte but there are four such files (2nd, 4th, 5th and 6th files). So the download manager chooses the 6th, 5th and 4th files for download as they have less bytes remaining. All these files get equal bandwidth (30.00 Megabyte/Sec). Of these three files the 8th file is finished first. So instantaneously the 2nd file starts downloading. Then, 5th file is finished. So the next larger file (3rd file) starts downloading. This process goes on until all files are downloaded.

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3233

题目大意: 给你一些文件,要你下载。  可是你家的下载带宽是有限的,而且你的下载器一次能下载的文件个数也是有限的。而且你下载文件时要按照文件从小到大来下,如果有文件大小相同,则按文件剩余的大小从小到大来下。问你下完所有文件要多久时间。 还有一点就是下载的带宽要平均分掉。

算法分析: 可以直接弄出答案就所有文件下完时间。当然我是用模拟的,所以写写报告。

程序代码:

方法1: 普通版

#include<cstdio>

int main()
{
	int T,n,cnt = 1,B;
	while(scanf("%d%d%d",&T,&n,&B),(T || n || B))
	{

		double s,p,sum = 0;
		while(T--)
		{
			scanf("%lf%lf", &s, &p);
			sum += s*(1.0-p/100.0);
		}
		printf("Case %d: %.2f\n\n",cnt++,sum/(B*1.0));
	}
	return 0;
}

方法2: 找抽版,模拟

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

#define N 200010

struct Node
{
	double size,rest;
	int p;
};
Node node[N];

int cmp(Node a, Node b)
{
	if(a.size != b.size)
		return a.size < b.size;
	return a.p > b.p;
}

int main()
{
	int T,n,B,cas = 1;
	while(scanf("%d%d%d", &T,&n,&B) && (T||n||B))
	{
		for(int i = 1; i <= T; i++)
		{
			scanf("%lf%d", &node[i].size,&node[i].p);
			node[i].rest = 1.0*node[i].size*(1.0-0.01*node[i].p);
		}
		sort(node+1,node+1+T,cmp);	//按下载要求来排列下载次序
		double time = 0,M = (double)B;
		for(int j = 1; j <= T; j++)
		{
			int q = j+n-1;
			if(q > T)				//如果剩余的个数少于n个,平均带宽为M/(T-j+1)
			{
				time += (node[j].rest / (M/((T-j+1)*1.0)));
				for(int i = j+1 ; i <= T ; i++)
				{
					node[i].rest -= node[j].rest;
				}
			}
			else					//如果剩余个数不小于n个,剩余带宽为M/n
			{
				time += (node[j].rest / (M/(n*1.0)));
				for(int i = j+1 ; i <= q ; i++)
				{
					node[i].rest -= node[j].rest;
				}
			}
		}
		printf("Case %d: %.2f\n\n",cas++,time);
	}
}

参考:http://blog.csdn.net/runningapple/article/details/8944964


  1. I like your publish. It is great to see you verbalize from the coronary heart and clarity on this essential subject matter can be easily noticed.