ZOJ 3607 Lazier Salesgirl 贪心

    这个题比上个题简单得多,也是超过W时间会睡着,睡着就再也不会卖了,
顾客按时间顺序来的,但是可能有顾客同时到(同时到如果醒着就全卖了),
并且每个人只买一块面包,也是求最大的W,使得卖出面包的平均价格最高。 同理最大的W一定是某两个相邻人的时间差。因为睡着了就不会醒了,
所以枚举的时间差必须越来越大。如果某个时间差比前面枚举过的小也没啥意义
(因为在前面就会睡着)。因此,直接枚举第i个人和第(i-1)个人的那个时间差
(在此之前的时间差都比这个小),然后从第(i+1)个人开始连续一段人的时间差都不超过这个值,
直到第j个人的时间差比这个大,则从第一个人到第j个人都没睡着卖出去了,算一个均值。
再从第j个人的时间差开始判断,相当于扫了一遍。时间复杂度O(n)。

类似这样: for (i=1;i<=n;i=j) { for (d=t[i]-t[i-1],j=i;t[j]-t[j-1]<=d;++j) { 累加和不清0} }

  

//#pragma comment(linker, "/STACK:16777216") //for c++ Compiler
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <cstring>
#include <cmath>
#include <stack>
#include <string>
#include <map>
#include <set>
#include <list>
#include <queue>
#include <vector>
#include <algorithm>
#define Max(a,b) (((a) > (b)) ? (a) : (b))
#define Min(a,b) (((a) < (b)) ? (a) : (b))
#define Abs(x) (((x) > 0) ? (x) : (-(x)))
#define MOD 1000000007
#define pi acos(-1.0)

using namespace std;

typedef long long           ll      ;
typedef unsigned long long  ull     ;
typedef unsigned int        uint    ;
typedef unsigned char       uchar   ;

template<class T> inline void checkmin(T &a,T b){if(a>b) a=b;}
template<class T> inline void checkmax(T &a,T b){if(a<b) a=b;}

const double eps = 1e-7      ;
const int N = 210            ;
const int M = 1100011*2      ;
const ll P = 10000000097ll   ;
const int MAXN = 10900000    ;
const int INF = 0x3f3f3f3f   ;

int n;
double t[100003], p[100003], lev[100003];
double sum, ans, anst, w;

int main() {
    std::ios::sync_with_stdio(false);
    int i, j, T, k, u, v, numCase = 0;
    cin >> T;
    while (T--){
        sum = ans = anst = 0;
        cin >> n;
        for (i = 0; i < n; ++i) cin >> p[i];
        for (i = 0; i < n; ++i) cin >> t[i];
        lev[0] = t[0];
        for (i = 1; i < n; ++i){
            lev[i] = Max(t[i] - t[i - 1], lev[i - 1]);//找到相邻两人间的时间差
        }
        for (i = 0; i < n; ++i){
            w = lev[i];
            sum = 0;
            for (j = 0; j < n; ++j){
                if (w >= lev[j]){
                    sum += p[j];
                }
                else break;
            }
            if (ans < sum / j){
                ans = sum / j;
                anst = w;
            } else if (ans == sum / j && anst > w){
                anst = w;
            }
        }
        printf("%.6f %.6f
", anst, ans);
    }

    return 0;
}
原文地址:https://www.cnblogs.com/wushuaiyi/p/4431955.html