A. One and Two
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
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.
Example
input
Copy
3
6
2 2 1 2 1 2
3
1 2 1
4
1 1 1 1
output
Copy
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
#include<iostream>
#include<cmath>
#include<cstring>
#include<cstdio>
#include<stack>
#include<string>
#include<algorithm>
#include<map>
#include<cstring>
#include<queue>
#include<set>
#include<stdlib.h>
#define dbug cout<<"hear!"<<endl;
using namespace std;
typedef long long ll;
typedef long double ld;
const int N = 2e5 + 5, INF = 0x3f3f3f3f;
ll gcdd(ll a, ll b)
{
if (b) while ((a %= b) && (b %= a));
return a + b;
}
ll t,n,m,a,b,cnt,ans,ant;
ll arr[N],brr[N];
int main()
{
int t;
cin>>t;
while(t--)
{
cin>>n;
cnt=0;
ant=0;
for(int i=1;i<=n;i++)
{
cin>>arr[i];
if(arr[i]==2)
{
cnt++;
}
}
if(cnt%2==1)
{
cout<<-1<<endl;
}
else if(cnt==0)
{
cout<<1<<endl;
}
else{
for(int j=1;j<=n;j++)
{
if(arr[j]==2)
{
ant++;
if(ant==cnt/2)
{
ans=j;
break;
}
}
}
cout<<ans<<endl;
}
}
}