#include <stdio.h>
#define Min -10000000
#define Max 10000000
void shouldBeGreater(int a, int& prev, int& result) {
if(prev < a) {
prev = a;
}
else //>=
{
prev = Max;
result++;
}
}
void shouldBeLower(int a, int& prev, int& result) {
if(prev > a) {
prev = a;
}
else //<=
{
prev = Min;
result++;
}
}
int main() {
int n=0; scanf("%d ", &n);
int result1 = 0;
int result2 = 0;
int prev1 = Min; //t0 is greater (than prev, t1)
int prev2 = Max; //t0 is lower
for(int t=0; t < n; t++) {
int a; scanf("%d ", &a);
if(t % 2 == 0)
{
shouldBeGreater(a, prev1, result1);
shouldBeLower(a, prev2, result2);
}
else{
shouldBeLower(a, prev1, result1);
shouldBeGreater(a, prev2, result2);
}
}
printf("%d\n", result1 < result2 ? result1 : result2);
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 | #include <stdio.h> #define Min -10000000 #define Max 10000000 void shouldBeGreater(int a, int& prev, int& result) { if(prev < a) { prev = a; } else //>= { prev = Max; result++; } } void shouldBeLower(int a, int& prev, int& result) { if(prev > a) { prev = a; } else //<= { prev = Min; result++; } } int main() { int n=0; scanf("%d ", &n); int result1 = 0; int result2 = 0; int prev1 = Min; //t0 is greater (than prev, t1) int prev2 = Max; //t0 is lower for(int t=0; t < n; t++) { int a; scanf("%d ", &a); if(t % 2 == 0) { shouldBeGreater(a, prev1, result1); shouldBeLower(a, prev2, result2); } else{ shouldBeLower(a, prev1, result1); shouldBeGreater(a, prev2, result2); } } printf("%d\n", result1 < result2 ? result1 : result2); return 0; } |
English