C. LIS or Reverse LIS?(情况分类)

原题链接:

You are given an array aa of nn positive integers.

Let LIS(a)LIS(a) denote the length of longest strictly increasing subsequence of aa. For example,

  • LIS([2,1–,1,3–])LIS([2,1_,1,3_]) = 22.
  • LIS([3–,5–,10–––,20–––])LIS([3_,5_,10_,20_]) = 44.
  • LIS([3,1–,2–,4–])LIS([3,1_,2_,4_]) = 33.

We define array a′a′ as the array obtained after reversing the array aa i.e. a′=[an,an−1,…,a1]a′=[an,an−1,…,a1].

The beauty of array aa is defined as min(LIS(a),LIS(a′))min(LIS(a),LIS(a′)).

Your task is to determine the maximum possible beauty of the array aa if you can rearrange the array aa arbitrarily.

Input

The input consists of multiple test cases. The first line contains a single integer tt (1≤t≤104)(1≤t≤104)  — the number of test cases. Description of the test cases follows.

The first line of each test case contains a single integer nn (1≤n≤2⋅105)(1≤n≤2⋅105)  — the length of array aa.

The second line of each test case contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤109)(1≤ai≤109)  — the elements of the array aa.

It is guaranteed that the sum of nn over all test cases does not exceed 2⋅1052⋅105.

Output

For each test case, output a single integer  — the maximum possible beauty of aa after rearranging its elements arbitrarily.

Example

input

Copy

3
3
6 6 6
6
2 5 4 5 2 4
4
1 3 2 2

output

Copy

1
3
2

Note

In the first test case, aa = [6,6,6][6,6,6] and a′a′ = [6,6,6][6,6,6]. LIS(a)=LIS(a′)LIS(a)=LIS(a′) = 11. Hence the beauty is min(1,1)=1min(1,1)=1.

In the second test case, aa can be rearranged to [2,5,4,5,4,2][2,5,4,5,4,2]. Then a′a′ = [2,4,5,4,5,2][2,4,5,4,5,2]. LIS(a)=LIS(a′)=3LIS(a)=LIS(a′)=3. Hence the beauty is 33 and it can be shown that this is the maximum possible beauty.

In the third test case, aa can be rearranged to [1,2,3,2][1,2,3,2]. Then a′a′ = [2,3,2,1][2,3,2,1]. LIS(a)=3LIS(a)=3, LIS(a′)=2LIS(a′)=2. Hence the beauty is min(3,2)=2min(3,2)=2 and it can be shown that 22 is the maximum possible beauty.

思路:

1,每个数有用的《=2,从中间开始向左右排序,

2,开始时,我只考虑了中间向两边从大到小,但是其实可以从小到大,然后max或者min谁是奇数谁应该在中间,eg,2,5,4,5,4》5,4,2,4,5是3而不是2.

代码:

#include<bits/stdc++.h>
using namespace std;
const int maxj=2e5+100;
int a[maxj];
void solve(){
    int n;int cnt=0;
    cin>>n;map<int,int>mp;
    for(int i=1;i<=n;++i){
        cin>>a[i];
        mp[a[i]]++;
        if(mp[a[i]]<=2)cnt++;
    }
    cout<<(cnt+1)/2<<'\n';
}
int main(){
    int t;
    cin>>t;
    while(t--)solve();
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值