zoj 3607 Lazier Salesgirl 暴力 前缀和

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;
}
时间: 2024-08-08 13:59:08

zoj 3607 Lazier Salesgirl 暴力 前缀和的相关文章

ZOJ 3607 Lazier Salesgirl (贪心)

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

[ZOJ 3607] Lazier Salesgirl

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3607 思路:最小的相隔时间肯定是两个时间的间隔,只需要计算每个时间间隔所获的价值的平均值,取最大的平均值以及其对应的时间间隔即可,注意,当平均值相等的时候,取时间间隔小的. AC 代码: #include <cstdio> #include <vector> #include <iostream> using namespace std

ZOJ 3607 Lazier Salesgirl(贪心啊 )

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4710 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 laz

zjuoj 3607 Lazier Salesgirl

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3607 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 th

H - Lazier Salesgirl

Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu Submit Status Practice ZOJ 3607 Description 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 custome

UVa 10755 Garbage Heap (暴力+前缀和)

题意:有个长方体由A*B*C组成,每个废料都有一个价值,要选一个子长方体,使得价值最大. 析:我们暴力枚举上下左右边界,然后用前缀和来快速得到另一个,然后就能得到长方体,每次维护一个最小值,然后差就是最大值. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string> #include <cstdlib> #incl

ZOJ 2856 Happy Life 暴力求解

因为是Special Judge 的题目,只要输出正确答案即可,不唯一 暴力力求解, 只要每次改变 happiness 值为负的人的符号即可. 如果计算出当前人的 happiness 值为负,那么将其 p(i) 值赋值为-p(i) 即可这题是保证有解的,至至于为何难以证明. Source Code: //#pragma comment(linker, "/STACK:16777216") //for c++ Compiler #include <stdio.h> #incl

ZOJ 3813 Pretty Poem (暴力)

Pretty Poem Time Limit: 2 Seconds      Memory Limit: 65536 KB Poetry is a form of literature that uses aesthetic and rhythmic qualities of language. There are many famous poets in the contemporary era. It is said that a few ACM-ICPC contestants can e

HDU 5273(暴力前缀和)

Dylans loves sequence Accepts: 250 Submissions: 806 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) 问题描述 Dylans得到了N个数a[1]...a[N]. 有Q个问题,每个问题形如(L,R) 他需要求出L?R这些数中的逆序对个数. 更加正式地,他需要求出二元组(x,y)的个数,使得L≤x,y≤R且x<y且a[x]>a[y]