You are given a sequence �1,�2,…,��a1,a2,…,an. Each element of �a is 11 or 22.
Find out if an integer �k exists so that the following conditions are met.
- 1≤�≤�−11≤k≤n−1, and
- �1⋅�2⋅…⋅��=��+1⋅��+2⋅…⋅��a1⋅a2⋅…⋅ak=ak+1⋅ak+2⋅…⋅an.
If there exist multiple �k that satisfy the given condition, print the smallest.
Input
Each test contains multiple test cases. The first line contains the number of test cases �t (1≤�≤1001≤t≤100). Description of the test cases follows.
The first line of each test case contains one integer �n (2≤�≤10002≤n≤1000).
The second line of each test case contains �n integers �1,�2,…,��a1,a2,…,an (1≤��≤21≤ai≤2).
Output
For each test case, if there is no such �k, print −1−1.
Otherwise, print the smallest possible �k.
Sample 1
Inputcopy | Outputcopy |
---|---|
3 6 2 2 1 2 1 2 3 1 2 1 4 1 1 1 1 | 2 -1 1 |
Note
For the first test case, �=2k=2 satisfies the condition since �1⋅�2=�3⋅�4⋅�5⋅�6=4a1⋅a2=a3⋅a4⋅a5⋅a6=4. �=3k=3 also satisfies the given condition, but the smallest should be printed.
For the second test case, there is no �k that satisfies �1⋅�2⋅…⋅��=��+1⋅��+2⋅…⋅��a1⋅a2⋅…⋅ak=ak+1⋅ak+2⋅…⋅an
For the third test case, �=1k=1, 22, and 33 satisfy the given condition, so the answer is 11.
题解:
1:确定2的个数是奇数还是偶数。
2:如果为偶数,从左到右寻找2的个数,当为总的2的个数的一半时,记录此时i的数字
3:奇数的话就直接输出-1.
代码:
#include<bits/stdc++.h>
using namespace std;
int main()
{
int a,b,c[10009];
int n,d;
cin>>n;
while(n--)
{
cin>>b;
int ct=0;
for(int i=0;i<b;i++)
{
cin>>c[i];
if(c[i]%2==0) ct++;
}
if(ct%2==0)
{
if(ct==0) cout<<"1"<<endl;
else{d=ct/2;
for(int i=0;i<b;i++)
{
if(c[i]%2==0) d--;
if(d==0) {cout<<i+1<<endl;break;}
}}
}
else cout<<"-1"<<endl;
}
}