This commit is contained in:
unlockable
2023-10-22 13:25:02 +08:00
parent 03e0e30a95
commit 4726deb30c

View File

@@ -1,84 +1,48 @@
#include <stdio.h> #include <stdio.h>
#define RISING true // 创建一个栈,拿到一个数,与栈顶比较,如果比栈顶小,压栈(正在波峰向波谷下降)
#define FALLING false // 比栈顶大
unsigned long long totalCount; struct NumberNode {
int peakValleyPos[10000]; int data;
int powers[100000]; 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; long long totalPairs;
int i = maxInsidePeak + 2; NumberNode powers[500010];
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);
}
int main() { int main() {
int len = 1;
int N; int N;
int newPeakValleyPosIndex = 1; int input;
bool direction = FALLING; totalPairs = 0;
int firstPeakPos = 0, lastPeakPos = 0; powers[0].data = 0;
powers[0].count = 0;
peakValleyPos[0] = 0;
scanf("%d", &N); scanf("%d", &N);
scanf("%d", &powers[0]); scanf("%d", &powers[1].data);
scanf("%d", &powers[1]); powers[1].count = 1;
if (powers[0] < powers[1]) { N--;
direction = RISING; for (int i = 0; i < N; i++) {
firstPeakPos = 1; scanf("%d", &input);
} while (len != 0 && powers[len].data < input) {
for (int i = 2; i < N; i++) { totalPairs += powers[len].count;
scanf("%d", &powers[i]); len--;
if (powers[i] > powers[i - 1] && direction == FALLING) {
peakValleyPos[newPeakValleyPosIndex++] = i - 1;
direction = RISING;
} }
else if (powers[i] < powers[i - 1] && direction == RISING) { if (len == 0 ) {
peakValleyPos[newPeakValleyPosIndex++] = i - 1; powers[1].data = input;
direction = FALLING; 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};
}
} }
} }
printf("%lld", totalPairs);
if (direction == RISING) { return 0;
peakValleyPos[newPeakValleyPosIndex] = N - 1;
lastPeakPos = newPeakValleyPosIndex;
}
else {
lastPeakPos = newPeakValleyPosIndex - 1;
}
find_pairs(firstPeakPos, lastPeakPos);
totalCount += N - 1;
printf("%lld", totalCount);
} }