• 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 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 nintegers 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
    

    Author: WU, Zejun
    Contest: The 9th Zhejiang Provincial Collegiate Programming Contest

    分析:

    求当睡觉时间最短,卖出的面包平均价格最高时的W和平均值,注意:当他睡着的时候就不再醒啦,,,

    AC代码:

     1 #include<cstdio>
     2 #include<algorithm>
     3 using namespace std;
     4 int sum[1005],b[1005];
     5 int main() {
     6     int t;
     7     scanf("%d",&t);
     8     while(t--) {
     9         int n;
    10         scanf("%d",&n);
    11         int tmp;
    12         for(int i = 1;i <= n;i++) {
    13             scanf("%d",&tmp);
    14             sum[i] = sum[i-1] + tmp;
    15         //    printf("%d -- ",sum[i]);
    16         }
    17         
    18         for(int i = 1;i <= n;i++){
    19             scanf("%d",&b[i]);
    20         }
    21         
    22         double ma = 0;
    23         int maxT = 0,res = 0;
    24         for(int i = 1;i <= n;i++) {
    25             if(b[i] - b[i - 1] > maxT) {
    26                 maxT = b[i] - b[i - 1];
    27             }
    28             while(i <= n && b[i] - b[i - 1] <= maxT) i++;
    29             i--;
    30             if(1.0 * sum[i] / (i) > ma) {
    31                 ma = 1.0 * sum[i] / (i);
    32                 res = maxT;
    33             }
    34         }
    35         printf("%.6lf %.6lf
    ",res * 1.0,ma);
    36     }
    37     return 0;
    38 }
    View Code
  • 相关阅读:
    过程作为黑箱抽象——《计算机程序的构造和解释》
    过程与它们所产生的计算——《计算机程序的构造和解释》
    重构手法(四)之在对象之间搬移特性
    重构手法(三)之简化条件表达式
    重构手法(二)之简化函数调用
    重构手法(一)之重新组织函数
    代码的坏味道
    泛型算法(二十三)之排列算法
    泛型算法(二十二)之集合操作算法
    泛型算法(二十一)之比较算法
  • 原文地址:https://www.cnblogs.com/jeff-wgc/p/4472239.html
Copyright © 2020-2023  润新知