zoukankan      html  css  js  c++  java
  • *[codility]Peaks

    https://codility.com/demo/take-sample-test/peaks

    http://blog.csdn.net/caopengcs/article/details/17491791

    其实可以做到O(n)

    #include <iostream>
    #include <sstream>
    using namespace std;
    
    int solution(vector<int> &A) {
        int n = A.size();
        int prevPeak = -1;
        int maxDist = -1;
        vector<int> peaks(n);
        for (int i = 0; i < n; i++) {
            if (i == 0) {
                peaks[i] = 0;
            } else if (i == n - 1) {
                peaks[i] = peaks[i - 1];
            } else if (A[i] > A[i - 1] && A[i] > A[i + 1]) {
                peaks[i] = peaks[i - 1] + 1;
                if (prevPeak == -1) {
                    prevPeak = i;
                } else {
                    maxDist = max(maxDist, i - prevPeak);
                    prevPeak = i;
                }
            } else {
                peaks[i] = peaks[i - 1];
            }
        }
        if (peaks[n - 1] == 0) return 0;
        if (peaks[n - 1] == 1) return 1;
        for (int dist = maxDist / 2; dist <= n; dist++) {
            if (n % dist != 0)
                continue;
            bool found = true;
            for (int i = n - 1; i - dist > 0; i -= dist) {
                if (peaks[i] == peaks[i - dist]) {
                    found = false;
                    break;
                }
            }
            if (found) return n / dist;
        }
        return 0;
    }
    

      

  • 相关阅读:
    1697 ⑨要写信
    1220 数字三角形
    4979 数塔
    bzoj1618[Usaco2008 Nov]Buying Hay 购买干草
    bzoj1066[SCOI2007]蜥蜴
    bzoj1008[HNOI2008]越狱
    cf437D The Child and Zoo
    cf437C The Child and Toy
    cf437B The Child and Set
    cf437A The Child and Homework
  • 原文地址:https://www.cnblogs.com/lautsie/p/4110313.html
Copyright © 2011-2022 走看看