diff --git a/2023203/main.cpp b/2023203/main.cpp index 1b00824..d0ebe54 100644 --- a/2023203/main.cpp +++ b/2023203/main.cpp @@ -1,84 +1,48 @@ #include -#define RISING true -#define FALLING false +// 创建一个栈,拿到一个数,与栈顶比较,如果比栈顶小,压栈(正在波峰向波谷下降) +// 比栈顶大 -unsigned long long totalCount; -int peakValleyPos[10000]; -int powers[100000]; +struct NumberNode { + int data; + int count; +}; -void find_pairs(int startPeakNum, int endPeakNum) { - if (startPeakNum + 2 == endPeakNum) { - totalCount += - (peakValleyPos[endPeakNum] - peakValleyPos[startPeakNum + 1]) * - (peakValleyPos[startPeakNum + 1] - peakValleyPos[startPeakNum]) - 1; - return; - } - int maxInsidePeak = startPeakNum + 2; - int i = maxInsidePeak + 2; - while (i < endPeakNum) { - if (powers[i] > powers[peakValleyPos[maxInsidePeak]]) { - maxInsidePeak = i; - } - i += 2; - } - - int countLeft = 0, countRight = 0; - i = startPeakNum; - while (powers[i] >= powers[peakValleyPos[maxInsidePeak]]) { - countLeft++; - i++; - } - - i = endPeakNum; - while (peakValleyPos[i] >= powers[peakValleyPos[maxInsidePeak]]) { - countRight++; - i--; - } - - totalCount += countLeft * countRight; - - find_pairs(startPeakNum, maxInsidePeak); - find_pairs(maxInsidePeak, endPeakNum); -} +long long totalPairs; +NumberNode powers[500010]; int main() { + int len = 1; int N; - int newPeakValleyPosIndex = 1; - bool direction = FALLING; - int firstPeakPos = 0, lastPeakPos = 0; - - peakValleyPos[0] = 0; + int input; + totalPairs = 0; + powers[0].data = 0; + powers[0].count = 0; scanf("%d", &N); - scanf("%d", &powers[0]); - scanf("%d", &powers[1]); - if (powers[0] < powers[1]) { - direction = RISING; - firstPeakPos = 1; - } - for (int i = 2; i < N; i++) { - scanf("%d", &powers[i]); - if (powers[i] > powers[i - 1] && direction == FALLING) { - peakValleyPos[newPeakValleyPosIndex++] = i - 1; - direction = RISING; + scanf("%d", &powers[1].data); + powers[1].count = 1; + N--; + for (int i = 0; i < N; i++) { + scanf("%d", &input); + while (len != 0 && powers[len].data < input) { + totalPairs += powers[len].count; + len--; } - else if (powers[i] < powers[i - 1] && direction == RISING) { - peakValleyPos[newPeakValleyPosIndex++] = i - 1; - direction = FALLING; + if (len == 0 ) { + powers[1].data = input; + powers[1].count = 1; + len = 1; + } else { + if (powers[len].data == input) { + totalPairs += powers[len].count + (len > 1); + powers[len].count++; + } + else { + totalPairs += 1; + powers[++len] = NumberNode {input, 1}; + } } } - - if (direction == RISING) { - peakValleyPos[newPeakValleyPosIndex] = N - 1; - lastPeakPos = newPeakValleyPosIndex; - } - else { - lastPeakPos = newPeakValleyPosIndex - 1; - } - - find_pairs(firstPeakPos, lastPeakPos); - - totalCount += N - 1; - printf("%lld", totalCount); - + printf("%lld", totalPairs); + return 0; } \ No newline at end of file