ZOJ4107 Singing Everywhere
给出n个数 最多可以消除一个数 问这个n或n-1个数中满足a[i] > a[i - 1] && a[i] > a[i + 1]的数最少
直接模拟就行了
AC代码:
1 #include<bits/stdc++.h> 2 #define pi acos(-1) 3 #define INF 0x3f3f3f3f 4 typedef long long ll; 5 typedef unsigned long long ull; 6 using namespace std; 7 8 namespace io { 9 const int SIZE = 1e7 + 10; 10 char inbuff[SIZE]; 11 char *l, *r; 12 inline void init() { 13 l = inbuff; 14 r = inbuff + fread(inbuff, 1, SIZE, stdin); 15 } 16 inline char gc() { 17 if (l == r) init(); 18 return (l != r) ? *(l++) : EOF; 19 } 20 void read(int &x) { 21 x = 0; char ch = gc(); 22 while(!isdigit(ch)) ch = gc(); 23 while(isdigit(ch)) x = x * 10 + ch - '0', ch = gc(); 24 } 25 } using io::read; 26 27 bool cmp(const int &a, const int &b){ 28 return a > b; 29 } 30 31 int main(){ 32 ios::sync_with_stdio(false); 33 int t; 34 cin>>t; 35 ll a[100005]; 36 int n; 37 while (t--){ 38 cin>>n; 39 memset(a, 0, sizeof(a)); 40 for (int i = 1; i <= n; i++) cin>>a[i]; 41 a[0] = 9223372036854775807, a[n + 1] = 9223372036854775807; 42 int cnt = 0; 43 for (int i = 2; i < n; i++) 44 if (a[i] > a[i - 1] && a[i] >a[i + 1]) 45 cnt++; 46 int ans = 0; 47 int ans1, ans2; 48 for (int i = 2; i < n; i++){ 49 ans1 = 0, ans2 = 0; 50 if (a[i - 1] > a[i + 1] && a[i - 1] > a[i - 2]) ans1++; 51 if (a[i + 1] > a[i - 1] && a[i + 1] > a[i + 2]) ans1++; 52 if (a[i - 1] > a[i - 2] && a[i - 1] > a[i]) ans2++; 53 if (a[i + 1] > a[i + 2] && a[i + 1] > a[i]) ans2++; 54 if (a[i] > a[i - 1] && a[i] > a[i + 1]) ans2++; 55 ans = max(ans, ans2 - ans1); 56 } 57 cout<<cnt - ans<<endl; 58 } 59 return 0; 60 }