一致的单调子序列

时间:2015-03-29 16:21:57

标签: c++ sequence

我对此算法有疑问。它应该搜索最长的一致和单调子序列及其总和。如果几乎没有相同长度的后续序列,它应该返回第一个。
它应该作为单调函数 - http://en.wikipedia.org/wiki/Monotonic_function

输入:1 1 7 3 2 0 0 4 5 5 6 2 1

结果是:6 20 - 所以它有效。

但是输入:23 34 11 5 23 90 11 10 15 12 28 49

结果是:3 113 - 但应该是3 50

我觉得问题在于增加和减少案例之间的转换。有什么想法吗?

代码:

#include <stdio.h>

#define gc getchar
void scan_integer(unsigned long long int* o)
{
    register unsigned long long int c = gc();
    int x = 0;
    for (; ((c<48 || c>57)); c = gc());

    for (; c>47 && c<58; c = gc()) {
        x = (x << 1) + (x << 3) + c - 48;
    }
    *o = x;
}


int main(){
    unsigned long long int current_value, last_value, sum_increasing, sum_decreasing, length_increasing, length_decreasing, max_length, max_sum, is_increasing;
    bool equal = false;
    scan_integer(&current_value);
    last_value = 0;
    sum_increasing = current_value;
    sum_decreasing = current_value;
    length_increasing = 1;
    length_decreasing = 1;
    max_length = 1;
    max_sum = current_value;
    is_increasing = 0;

    while (!feof(stdin))
    {
        last_value = current_value;
        scan_integer(&current_value);

        if (current_value == last_value){
            sum_increasing += current_value;
            sum_decreasing += current_value;
            length_increasing += 1;
            length_decreasing += 1;
            equal = true;
        }
        else {
            if (current_value > last_value){
                sum_increasing += current_value;
                length_increasing += 1;

                if (equal == true){
                    length_decreasing = 1;
                    sum_decreasing = 0;
                    equal = false;
                }

                if (is_increasing < 0){
                    sum_increasing += last_value;
                    if (length_decreasing > max_length){
                        max_length = length_decreasing;
                        max_sum = sum_decreasing;
                    }
                    sum_decreasing = 0;
                    length_decreasing = 1;
                }
                is_increasing = 1;
            }
            else {
                sum_decreasing += current_value;
                length_decreasing += 1;
                if (equal == true){
                    length_increasing = 1;
                    sum_increasing = 0;
                    equal = false;
                }
                if (is_increasing == 1){
                    sum_decreasing += last_value;
                    if (length_increasing > max_length){
                        max_length = length_increasing;
                        max_sum = sum_increasing;
                    }

                sum_increasing = 0;
                length_increasing = 1;
            }
            is_increasing = -1;
        }
    }
}

printf("%llu %llu", max_length, max_sum);


return 0;

}

1 个答案:

答案 0 :(得分:0)

我在代码中看到了一个问题:

           is_increasing = 1;   // here
           // Did you mean to write a continue here?
        }
        else {
            sum_decreasing += current_value;
            length_decreasing += 1;
            if (equal == true){
                length_increasing = 1;
                sum_increasing = 0;
                equal = false;
            }
            if (is_increasing == 1){
                sum_decreasing += last_value;
                if (length_increasing > max_length){
                    max_length = length_increasing;
                    max_sum = sum_increasing;
                }

            sum_increasing = 0;
            length_increasing = 1;
        }
        is_increasing = -1;   // Or you might want to put this inside of the else above

如果我理解正确,&#39; is_increasing = -1&#39;在底部完全使if状态的真实条件(在上面代码的顶部)的设置无效。这就是为什么,在&#34;其他&#34;处理递减序列&#39; is_increasing&#39;始终具有&#39; -1&#39;的值并且这样的序列永远不会被保存为良好的序列。

我在我复制和粘贴的代码中加入了一些代码。我认为处理顺序中两个数字可能需要比那些注释更加谨慎的编码,但这应该让你朝着正确的方向前进。

如果有帮助,请告诉我。