码迷,mamicode.com
首页 > 其他好文 > 详细

zoj 3607 Lazier Salesgirl 暴力 前缀和

时间:2015-04-19 22:53:54      阅读:150      评论:0      收藏:0      [点我收藏+]

标签:前缀和   贪心   

Lazier Salesgirl

Time Limit: 2 Seconds      Memory Limit: 65536 KB

Kochiya Sanae is a lazy girl who makes and sells bread. She is an expert at bread making and selling. She can sell the i-th customer a piece of bread for price pi. But she is so lazy that she will fall asleep if no customer comes to buy bread for more than w minutes. When she is sleeping, the customer coming to buy bread will leave immediately. It‘s known that she starts to sell bread now and the i-th customer come after ti minutes. What is the minimum possible value of w that maximizes the average value of the bread sold?

Input

There are multiple test cases. The first line of input is an integer T ≈ 200 indicating the number of test cases.

The first line of each test case contains an integer 1 ≤ n ≤ 1000 indicating the number of customers. The second line contains n integers 1 ≤ pi ≤ 10000. The third line containsn integers 1 ≤ ti ≤ 100000. The customers are given in the non-decreasing order of ti.

Output

For each test cases, output w and the corresponding average value of sold bread, with six decimal digits.

Sample Input

2
4
1 2 3 4
1 3 6 10
4
4 3 2 1
1 3 6 10

Sample Output

4.000000 2.500000
1.000000 4.000000

链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3607


题意:

某 在0时刻开店, 然后按顺序来了n个客人,分别出了一个价格p[i],每个人来的时刻为 t[i]。

然后 某  在w时间内没接待客人就会睡觉,如果间隔正好是w,不会睡。 问接待的客人 出价的平均值最大是多少,在满足该条件下 w最低是多少。


做法:

不断求前缀和  还有 到第i个人间隔最大值。 接待的人肯定是连续的 前多少个。   先假设只接待第一个人,然后假设接待前两个,再前三个。 类推。 

如果是正好前i个人,那么w得  大于等于 ( 到第i个人间隔最大值) ,而且要严格小于(第 i和i+1 个人的间隔差)。 所以w就 取 ( 到第i个人间隔最大值),

如果这个值严格小于 (第 i和i+1 个人的间隔差)那么就成立,判断前i个人的平均价格是否更低。


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <malloc.h>
#include <ctype.h>
#include <math.h>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;
#include <stack>
#include <queue>
#include <vector>
#include <deque>
#include <set>
#include <map>
#define INF 999999999
#define eps 0.00001
#define LL __int64d
#define pi acos(-1.0)
struct node
{
	int p, t;
	int sum;
	int big;//qian cha zui xiao
	int cha;
}a[1047];
int cmp(node a,node b)
{
	return a.sum>b.sum;
}
int main()
{
	int t;
	scanf("%d",&t);//xiao w  da pingjun
	while(t--)
	{
		int n, i, j;
		double sum = 0;
		scanf("%d",&n); 
		a[0].sum=0;
		a[0].t=0;
		for(i = 1; i <= n; i++)
		{
			scanf("%d",&a[i].p); 
			a[i].sum=a[i-1].sum+a[i].p;
		}
		for(i = 1; i <= n; i++)
		{
			scanf("%d",&a[i].t); 
			if(i==1)
				a[i].big=a[i].t-a[i-1].t;
			else
				a[i].big=max(a[i-1].big,a[i].t-a[i-1].t);
		}
		for(i=1;i<=n;i++)
		{
			if(i==n)
				a[i].cha=99999999;
			else
				a[i].cha=a[i+1].t-a[i].t;
		}
		
		//sort(a+1,a+1+n,cmp);
		
		double bigavg=-1;
		double big;
		for(i=1;i<=n;i++)
		{
			if((1.0*a[i].sum/i)>bigavg)
			{
				if(a[i].cha>a[i].big)
				{
					big=a[i].big;
					bigavg=(1.0*a[i].sum/i);
				} 
			} 
		}

		printf("%.6lf %.6lf\n",big,bigavg);
	}
	return  0;
}




zoj 3607 Lazier Salesgirl 暴力 前缀和

标签:前缀和   贪心   

原文地址:http://blog.csdn.net/u013532224/article/details/45132727

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!