题目传送
题意:
给你一个n大小的数组,你每次可以合并俩个不同的数,问最后你能把数组压缩到的最小长度是多少?
思路:
只有1和n的区别。
什么时候为n呢?
当所有数都是一样的时候,我们不能进行合并操作,如 5 5 5,那么长度一定只能是n
什么时候为1呢?
只要一个数组中有俩个数不同,那么一定可以合并到只有一个数,如何证明?
证明:
既然有不同的数,那么数组中肯定有一个最大值,和一个最小值,且最大值与最小值不等。那么我就先让目前的最大值和最小值合并,就会又形成一个最大值,那么这个最大值现在在数组中肯定是唯一的,那么我们就一直用这个最大值去合并其他数,那么这个值一定会越来越大且唯一,到最后就只剩下了一个数
AC代码
#include <bits/stdc++.h>
inline int read(){char c = getchar();int x = 0,s = 1;
while(c < '0' || c > '9') {if(c == '-') s = -1;c = getchar();}
while(c >= '0' && c <= '9') {x = x*10 + c -'0';c = getchar();}
return x*s;}
using namespace std;
#define NewNode (TreeNode *)malloc(sizeof(TreeNode))
#define Mem(a,b) memset(a,b,sizeof(a))
#define lowbit(x) (x)&(-x)
const int N = 1e6 + 5;
const long long INFINF = 0x7f7f7f7f7f7f7f;
const int INF = 0x3f3f3f3f;
const double EPS = 1e-7;
const int mod = 998244353;
const double II = acos(-1);
const double PP = (II*1.0)/(180.00);
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<ll,ll> piil;
signed main()
{
std::ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
// freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
int t;
cin >> t;
while(t--)
{
ll n,num,ans = 0;
cin >> n;
ll arr[n+5];
for(int i = 0;i < n;i++)
{
cin >> arr[i];
if(i == 0) num = arr[i];
else if(arr[i] != num) ans = 1;
}
ans ? cout << 1 << endl : cout << n << endl;
}
}