您的位置:首页 > 其它

ZOJ _3607_Lazier Salesgirl(贪心)

2013-05-29 23:38 363 查看
题型:贪心

题目

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 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 contains n 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


题意

一个小姑娘非常懒但是卖面包非常厉害。她能在以Pi的价格将面包卖给第i个人。但是她一旦在w的时间段内木有顾客,她就会睡着,一觉不起。。。

现在给出买个每个顾客的价钱和相关时间点,现在想要求出在最少的w里卖出最大的面包价钱的平均值,问这一平均值和w是多少。

分析

贪心问题,只需关心最符合题意的情况。但是有一些注意点:

1、当前对象的w是之前所有时间间隔中最大的,不然就卖不到这了;

2、当前对象的w一定要比卖到下一个顾客的时间间隔要小,否则就会买个下一个人,就平均值不一定是最小了。

方法,用一个结构体分别存入卖到当前顾客的平均值、w、和到下一个人的时间间隔,然后由大到小排序,最后从头找到符合w<下一个人的时间间隔的即可。

代码

#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <queue>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <string>
#include <cstring>
#include <cctype>
#include <assert.h>
using namespace std;
struct node{
double ave;//平均值
double w;
double nextT;//到下一个顾客的时间间隔
}a[1234];

double cmp(node a,node b){//按比较的优先级写比较函数
if(a.ave==b.ave){
return a.w<b.w;//当ave相同时,w小的优先
}
else return a.ave>b.ave;
}

int main(){
int t,n;
double price[1234],time[1234];
while(~scanf("%d",&t)){
while(t--){
scanf("%d",&n);
for(int i=0;i<n;i++){
scanf("%lf",&price[i]);
}
for(int i=0;i<n;i++){
scanf("%lf",&time[i]);
}
double sum=0;
for(int i=0;i<n;i++){
sum+=price[i];
a[i].ave=sum/(i+1);
if(i==0){
a[i].w=time[i];
}
else a[i].w=time[i]-time[i-1];
if(i==n-1){
a[i].nextT=999999;//最后一个顾客后的时间定为无限大
}
else a[i].nextT=time[i+1]-time[i];
}
int max=a[0].w;
for(int i=0;i<n;i++){
if(max>a[i].w){
a[i].w=max;
}
else{
max=a[i].w;
}
}
sort(a,a+n,cmp);
/*for(int i=0;i<n;i++){
printf("***%.3lf %d %d\n",a[i].ave,a[i].w,a[i].nextT);
}*/
for(int i=0;i<n;i++){
if(a[i].w<a[i].nextT){
printf("%.6lf %.6lf\n",a[i].w,a[i].ave);
break;
}
}
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: