You are given an array aa consisting of nn integers. For each ii (1≤i≤n1≤i≤n) the following inequality is true: −2≤ai≤2−2≤ai≤2.
You can remove any number (possibly 00) of elements from the beginning of the array and any number (possibly 00) of elements from the end of the array. You are allowed to delete the whole array.
You need to answer the question: how many elements should be removed from the beginning of the array, and how many elements should be removed from the end of the array, so that the result will be an array whose product (multiplication) of elements is maximal. If there is more than one way to get an array with the maximum product of elements on it, you are allowed to output any of them.
The product of elements of an empty array (array of length 00) should be assumed to be 11.
Input
The first line of input data contains an integer tt (1≤t≤1041≤t≤104) —the number of test cases in the test.
Then the descriptions of the input test cases follow.
The first line of each test case description contains an integer nn (1≤n≤2⋅1051≤n≤2⋅105) —the length of array aa.
The next line contains nn integers a1,a2,…,ana1,a2,…,an (|ai|≤2|ai|≤2) — elements of array aa.
It is guaranteed that the sum of nn over all test cases does not exceed 2⋅1052⋅105.
Output
For each test case, output two non-negative numbers xx and yy (0≤x+y≤n0≤x+y≤n) — such that the product (multiplication) of the array numbers, after removing xx elements from the beginning and yy elements from the end, is maximal.
If there is more than one way to get the maximal product, it is allowed to output any of them. Consider the product of numbers on empty array to be 11.
Example
input
Copy
5 4 1 2 -1 2 3 1 1 -2 5 2 0 -2 2 -1 3 -2 -1 -1 3 -1 -2 -2
output
Copy
0 2 3 0 2 0 0 1 1 0
Note
In the first case, the maximal value of the product is 22. Thus, we can either delete the first three elements (obtain array [2][2]), or the last two and one first element (also obtain array [2][2]), or the last two elements (obtain array [1,2][1,2]). Thus, in the first case, the answers fit: "3 0", or "1 2", or "0 2".
In the second case, the maximum value of the product is 11. Then we can remove all elements from the array because the value of the product on the empty array will be 11. So the answer is "3 0", but there are other possible answers.
In the third case, we can remove the first two elements of the array. Then we get the array: [−2,2,−1][−2,2,−1]. The product of the elements of the resulting array is (−2)⋅2⋅(−1)=4(−2)⋅2⋅(−1)=4. This value is the maximum possible value that can be obtained. Thus, for this case the answer is: "2 0".
思路:
1,三目运算符有优先级等问题,调了好久才发现
2,关键在+-2的个数,以零为分界,统计|2|的个数,和负号的个数,循环的判断更新.(分段)
代码:
#include<bits/stdc++.h>
using namespace std;
#define rep(i,m,n) for(int i=m;i<=n;++i)
const int maxj =2e5+100,mod = 998244353;
int a[maxj];
int main(){
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int t ;
cin>>t ;
while(t--){
int sum[maxj]={0},sum2[maxj]={0},ans=0,ansl=1,ansr=0;
int n;
cin>>n;
sum[0]=0,sum2[0]=0;
rep(i,1,n){//前缀和,循环处理
cin>>a[i];
int l=a[i]<0?1:0;
sum[i]=sum[i-1]+l;//
// cout<<sum[i]<<' ';
int r=(a[i]==2||a[i]==-2)?1:0;
sum2[i]=sum2[i-1]+r;//
// cout<<sum2[i]<<' ';
}
int l=1,r=0;//类似指针操作
while(l<=n){
while(a[l]==0&&l<=n) //排除前段零
l++;
if(l>n)break;
r=l;
while(r<=n&&a[r]!=0) //以零为界
r++;
r--;
if((sum[r]-sum[l-1])%2==0){
if(sum2[r]-sum2[l-1]>ans){
ans=sum2[r]-sum2[l-1];ansl=l,ansr=r;
}
}else{
int pre=l,las=r;
while(pre<=n&&a[pre]>0) pre++;
while(las<=n&&a[las]>0) las--;
if(sum2[r]-sum2[pre]>ans){ ans=sum2[r]-sum2[pre],ansl=pre+1;ansr=r;}
if(sum2[las-1]-sum2[l-1]>ans){ ans=sum2[las-1]-sum2[l-1];ansl=l,ansr=las-1;}
}
l=r+1;
}
cout<<ansl-1<<' '<<n-ansr<<'\n';
}
return 0;
}