#include <iostream> #include <vector> #include <set> #include <algorithm> #include <math.h> #include <map> #include <fstream> #include <stdio.h> #include <stdlib.h> #include <string> #include <ctime> #include <stdlib.h> #include <stdio.h> #include <climits> #include <iomanip> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n; cin >> n; int min = -1000000000; int max = 1000000000; int pr = 0; int lw = 0; int ca = 0; int cb = 1; int ap, al, bp, bl; cin >> ap; al = ap; for (int i = 1; i < n; i++) { cin >> bp; bl = bp; if(ca == 1){ if (ap >= bp) { pr++; bp = max; } ca = 0; } else { if (ap <= bp) { pr++; bp = min; }ca = 1; } if (cb == 1) { if (al >= bl) { lw++; bl = max; } cb = 0; } else { if (al <= bl) { lw++; bl = min; }cb = 1; } ap = bp; al = bl; } if (pr > lw) cout << lw; else cout << pr; 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 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 | #include <iostream> #include <vector> #include <set> #include <algorithm> #include <math.h> #include <map> #include <fstream> #include <stdio.h> #include <stdlib.h> #include <string> #include <ctime> #include <stdlib.h> #include <stdio.h> #include <climits> #include <iomanip> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n; cin >> n; int min = -1000000000; int max = 1000000000; int pr = 0; int lw = 0; int ca = 0; int cb = 1; int ap, al, bp, bl; cin >> ap; al = ap; for (int i = 1; i < n; i++) { cin >> bp; bl = bp; if(ca == 1){ if (ap >= bp) { pr++; bp = max; } ca = 0; } else { if (ap <= bp) { pr++; bp = min; }ca = 1; } if (cb == 1) { if (al >= bl) { lw++; bl = max; } cb = 0; } else { if (al <= bl) { lw++; bl = min; }cb = 1; } ap = bp; al = bl; } if (pr > lw) cout << lw; else cout << pr; return 0; } |