Codeforces Global Round 10 A. Omkar and Password(思维)

题目传送
题意:
给你一个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;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值