ZOJ4107-Singing Everywhere

Singing Everywhere
Time Limit: 2 Seconds Memory Limit: 65536 KB
Baobao loves singing very much and he really enjoys a game called Singing Everywhere, which allows players to sing and scores the players according to their performance.

Consider the song performed by Baobao as an integer sequence , where indicates the -th note in the song. We say a note is a “voice crack” if , and . The more voice cracks BaoBao sings, the lower score he gets.

To get a higher score, BaoBao decides to delete at most one note in the song. What’s the minimum number of times BaoBao sings a voice crack after this operation?

Input
There are multiple test cases. The first line of the input contains an integer (about 100), indicating the number of test cases. For each test case:

The first line contains one integer (), indicating the length of the song.

The second line contains integers (), indicating the song performed by BaoBao.

It’s guaranteed that at most 5 test cases have .

Output
For each test case output one line containing one integer, indicating the answer.

Sample Input
3
6
1 1 4 5 1 4
7
1 9 1 9 8 1 0
10
2 1 4 7 4 8 3 6 4 7
Sample Output
1
0
2
Hint
For the first sample test case, BaoBao does not need to delete a note. Because if he deletes no note, he will sing 1 voice crack (the 4th note), and no matter which note he deletes, he will also always sing 1 voice crack.

For the second sample test case, BaoBao can delete the 3rd note, and no voice cracks will be performed. Yay!

For the third sample test case, BaoBao can delete the 4th note, so that only 2 voice cracks will be performed (4 8 3 and 3 6 4).

题意,给定一个序列,让你消除某个数,所得序列最高峰最少是多少
例如样例 1 1 4 5 1 4 无论如何消除一个数,最后结果都是1,
但是样例 1 9 1 9 8 1 0 只要把第三个数 1 消除,得到1 9 9 8 1 0,就没有最高峰了答案为0。
实际上直接暴力就行了,首先求出原本有多少个峰,然后每次模拟消除一个数,看最多能消除多少个,最后拿原本的减去消除最多的就可以了

#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5+10;
int a[maxn],sum,T,n,ans,ans1,ans2;
int main()
{
   cin >> T;
   while(T--)
   {
       sum=0;
       cin >> n;
       for(int i=1;i<=n;i++)
       {
           cin >> a[i];
       }
       for(int i=2;i<=n-1;i++)
       {
           if(a[i]>a[i-1]&&a[i]>a[i+1])
            sum++;
       }
       ans=-1;
       for(int i=1;i<=n;i++)
       {
           ans1=0,ans2=0;
           if(i==1)
           {
               ans1=0;
               if(a[2]>a[1]&&a[2]>a[3])
                ans2=1;
               ans=max(ans,ans2-ans1);
           }
           else if(i==2)
           {
              if(a[3]>a[1]&&a[3]>a[4])
                ans1=1;
                if(a[2]>a[1]&&a[2]>a[3])
                    ans2++;
                if(a[3]>a[2]&&a[3]>a[4])
                    ans2++;
                ans=max(ans,ans2-ans1);

           }
           else if(i==n-1)
           {
               if(a[n-2]>a[n-3]&&a[n-2]>a[n])
                ans1++;
               if(a[n-1]>a[n-2]&&a[n-1]>a[n])
                ans2++;
               if(a[n-2]>a[n-3]&&a[n-2]>a[n-1])
                ans2++;
               ans=max(ans,ans2-ans1);
           }
           else if(i==n)
           {
               ans1=0;
               if(a[n-1]>a[n]&&a[n-1]>a[n-2])
                ans2++;
               ans=max(ans,ans2-ans1);
           }
           else
           {
               if(a[i-1]>a[i+1]&&a[i-1]>a[i-2])
                ans1++;
               if(a[i+1]>a[i-1]&&a[i+1]>a[i+2])
                ans1++;
               if(a[i]>a[i-1]&&a[i]>a[i+1])
                ans2++;
               if(a[i-1]>a[i-2]&&a[i-1]>a[i])
                ans2++;
               if(a[i+1]>a[i]&&a[i+1]>a[i+2])
                ans2++;
               ans=max(ans,ans2-ans1);
           }
       }
       cout << sum-ans << endl;
   }

    return 0;
}

猜你喜欢

转载自blog.csdn.net/w1304636468/article/details/89810259