#define _CRT_SECURE_NO_WARNINGS
#include <cstdio>
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <math.h>
#include <algorithm>
#include <vector>
#include <map>
constexpr int MAX_N = 50000;
constexpr auto MAX_VAL = 10e9 + 1;
constexpr auto MIN_VAL = -10e9 - 1;
inline int GetNumberOfSwaps(std::vector<double> allTones, bool nextShouldBeLower)
{
double prevTone = allTones[0];
int cnt = 0;
for (auto i = 1; i < allTones.size(); i++)
{
auto curTone = allTones[i];
if (nextShouldBeLower && curTone >= prevTone)
{
curTone = MIN_VAL;
cnt++;
}
else if (!nextShouldBeLower && curTone <= prevTone)
{
curTone = MAX_VAL;
cnt++;
}
nextShouldBeLower = !nextShouldBeLower;
prevTone = curTone;
}
return cnt;
}
int main()
{
int n;
auto _ = scanf("%d\n", &n);
std::vector<double> allTones;
for (auto i = 0; i < n; i++)
{
int tone;
auto _ = scanf("%d_", &tone);
allTones.push_back(tone);
}
int result = std::min(GetNumberOfSwaps(allTones, true), GetNumberOfSwaps(allTones, false));
printf("%d\n", result);
return 0;
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | #define _CRT_SECURE_NO_WARNINGS #include <cstdio> #include <stdio.h> #include <iostream> #include <string.h> #include <math.h> #include <algorithm> #include <vector> #include <map> constexpr int MAX_N = 50000; constexpr auto MAX_VAL = 10e9 + 1; constexpr auto MIN_VAL = -10e9 - 1; inline int GetNumberOfSwaps(std::vector<double> allTones, bool nextShouldBeLower) { double prevTone = allTones[0]; int cnt = 0; for (auto i = 1; i < allTones.size(); i++) { auto curTone = allTones[i]; if (nextShouldBeLower && curTone >= prevTone) { curTone = MIN_VAL; cnt++; } else if (!nextShouldBeLower && curTone <= prevTone) { curTone = MAX_VAL; cnt++; } nextShouldBeLower = !nextShouldBeLower; prevTone = curTone; } return cnt; } int main() { int n; auto _ = scanf("%d\n", &n); std::vector<double> allTones; for (auto i = 0; i < n; i++) { int tone; auto _ = scanf("%d_", &tone); allTones.push_back(tone); } int result = std::min(GetNumberOfSwaps(allTones, true), GetNumberOfSwaps(allTones, false)); printf("%d\n", result); return 0; } |
English