A. Omkar and Password
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
Lord Omkar has permitted you to enter the Holy Church of Omkar! To test your worthiness, Omkar gives you a password which you must interpret!
A password is an array a of n positive integers. You apply the following operation to the array: pick any two adjacent numbers that are not equal to each other and replace them with their sum. Formally, choose an index i such that 1≤i<n and ai≠ai+1, delete both ai and ai+1 from the array and put ai+ai+1 in their place.
For example, for array [7,4,3,7] you can choose i=2 and the array will become [7,4+3,7]=[7,7,7]. Note that in this array you can’t apply this operation anymore.
Notice that one operation will decrease the size of the password by 1. What is the shortest possible length of the password after some number (possibly 0) of operations?
Input
Each test contains multiple test cases. The first line contains the number of test cases t (1≤t≤100). Description of the test cases follows.
The first line of each test case contains an integer n (1≤n≤2⋅105) — the length of the password.
The second line of each test case contains n integers a1,a2,…,an (1≤ai≤109) — the initial contents of your password.
The sum of n over all test cases will not exceed 2⋅105.
Output
For each password, print one integer: the shortest possible length of the password after some number of operations.
Example
input
2
4
2 1 3 1
2
420 420
output
1
2
My Answer Code:
#include<bits/stdc++.h>
using namespace std;
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t;
cin>>t;
vector<int>num(t);
vector<int>ans(t);
for(int i=0;i<t;i++)
{
int flag=0;
cin>>num[i];
vector<int>a(num[i]);
for(int j=0;j<num[i];j++)cin>>a[j];
for(int k=0;k<num[i]-1;k++)
{
if(a[k]!=a[k+1]){flag=1;break;}
}
if(flag)ans[i]=1;
else ans[i]=num[i];
}
for(int i=0;i<t;i++)cout<<ans[i]<<'\n';
return 0;
}
本文介绍了一种通过特定操作缩短数组长度的方法,即选择不相等的相邻元素并将其替换为它们的和,直至无法继续操作。文章提供了示例及解决方案。
347

被折叠的 条评论
为什么被折叠?



