七月九日训练总结

 

Let's call an array tt dominated by value vv in the next situation.

At first, array tt should have at least 22 elements. Now, let's calculate number of occurrences of each number numnum in tt and define it as occ(num)occ(num). Then tt is dominated (by vv) if (and only if) occ(v)>occ(v′)occ(v)>occ(v′) for any other number v′v′. For example, arrays [1,2,3,4,5,2][1,2,3,4,5,2], [11,11][11,11] and [3,2,3,2,3][3,2,3,2,3] are dominated (by 22, 1111 and 33 respectevitely) but arrays [3][3], [1,2][1,2] and [3,3,2,2,1][3,3,2,2,1] are not.

Small remark: since any array can be dominated only by one number, we can not specify this number and just say that array is either dominated or not.

You are given array a1,a2,…,ana1,a2,…,an. Calculate its shortest dominated subarray or say that there are no such subarrays.

The subarray of aa is a contiguous part of the array aa, i. e. the array ai,ai+1,…,ajai,ai+1,…,aj for some 1≤i≤j≤n1≤i≤j≤n.

Input

The first line contains single integer TT (1≤T≤10001≤T≤1000) — the number of test cases. Each test case consists of two lines.

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

The second line contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤n1≤ai≤n) — the corresponding values of the array aa.

It's guaranteed that the total length of all arrays in one test doesn't exceed 2⋅1052⋅105.

Output

Print TT integers — one per test case. For each test case print the only integer — the length of the shortest dominated subarray, or −1−1 if there are no such subarrays.

Example

Input

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

Output

-1
6
3
2

Note

In the first test case, there are no subarrays of length at least 22, so the answer is −1−1.

In the second test case, the whole array is dominated (by 11) and it's the only dominated subarray.

In the third test case, the subarray a4,a5,a6a4,a5,a6 is the shortest dominated subarray.

In the fourth test case, all subarrays of length more than one are dominated.

这个题目我一开始的思路是用两个指针,如果满足条件,左指针右移。右指针左移,这样直接模拟过程,但是由于添麻烦就一直没能ac,但下午看过题解后发现其实并没有那么麻烦,实际上,满足条件的部分,一定是一个数和上一次出现这个数位置的地方的子串,求出最短的部分,就是答案

#include <cstdio>
#include <cstring>
#include <iostream>
#include <cmath>
#include <algorithm>
 
using namespace std;
 
const int INF = 0x3f3f3f3f;
int a[200005],pre[200005];
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int n;
        cin>>n;
        int ans = INF;
        for(int i = 1;i <= n;i++)
            pre[i] = 0;
        for(int i = 1;i <= n;i++)
        {
            cin>>a[i];
            if(pre[a[i]] == 0)
                pre[a[i]] = i;
            else
            {
                ans = min(ans,i - pre[a[i]]);
                pre[a[i]] = i;
            }
        }
        if(ans == INF)
            cout<<"-1"<<endl;
        else
            cout<<ans + 1<<endl;
    }
    return 0;
}

ou play a computer game. In this game, you lead a party of mm heroes, and you have to clear a dungeon with nn monsters. Each monster is characterized by its power aiai. Each hero is characterized by his power pipi and endurance sisi.

The heroes clear the dungeon day by day. In the beginning of each day, you choose a hero (exactly one) who is going to enter the dungeon this day.

When the hero enters the dungeon, he is challenged by the first monster which was not defeated during the previous days (so, if the heroes have already defeated kk monsters, the hero fights with the monster k+1k+1). When the hero fights the monster, there are two possible outcomes:

  • if the monster's power is strictly greater than the hero's power, the hero retreats from the dungeon. The current day ends;
  • otherwise, the monster is defeated.

After defeating a monster, the hero either continues fighting with the next monster or leaves the dungeon. He leaves the dungeon either if he has already defeated the number of monsters equal to his endurance during this day (so, the ii-th hero cannot defeat more than sisi monsters during each day), or if all monsters are defeated — otherwise, he fights with the next monster. When the hero leaves the dungeon, the current day ends.

Your goal is to defeat the last monster. What is the minimum number of days that you need to achieve your goal? Each day you have to use exactly one hero; it is possible that some heroes don't fight the monsters at all. Each hero can be used arbitrary number of times.

Input

The first line contains one integer tt (1≤t≤1051≤t≤105) — the number of test cases. Then the test cases follow.

The first line of each test case contains one integer nn (1≤n≤2⋅1051≤n≤2⋅105) — the number of monsters in the dungeon.

The second line contains nn integers a1a1, a2a2, ..., anan (1≤ai≤1091≤ai≤109), where aiai is the power of the ii-th monster.

The third line contains one integer mm (1≤m≤2⋅1051≤m≤2⋅105) — the number of heroes in your party.

Then mm lines follow, each describing a hero. Each line contains two integers pipi and sisi (1≤pi≤1091≤pi≤109, 1≤si≤n1≤si≤n) — the power and the endurance of the ii-th hero.

It is guaranteed that the sum of n+mn+m over all test cases does not exceed 2⋅1052⋅105.

Output

For each test case print one integer — the minimum number of days you have to spend to defeat all of the monsters (or −1−1 if it is impossible).

Example

Input

2
6
2 3 11 14 1 8
2
3 2
100 1
5
3 5 100 2 3
2
30 5
90 1

Output

5
-1

这个题目还是非常有意思的hhh,就像奥特曼打怪兽,题目大概说的是有 n 个怪兽和 m 个奥特曼,每个怪兽有一个能力值 a,每个奥特曼有一个能力值 p 和 耐力值 s。每天都要派一个奥特曼去牢房里清理怪兽,只有当该奥特曼的能力值大于怪兽的能力值时才可以清理掉这个怪兽,每清理一个怪兽,奥特曼的耐力值会减 1 ,当耐力值减完或者不能打败这个怪兽时这一天便结束。问最少要花多少天才能打完所有的怪兽。

这其实很明显就是一倒贪心的题目,上午没能ac出是因为并没有使用二分算法来节省这道题目的时间复杂度
贪心策略就是记录每个奥特曼耐力值,奥特曼的最大能力值。为了快速清理完所有的怪兽,就应该优先选择能力值大且耐力高的高特曼,这就是所谓的能力越大责任越大。
当有一个怪兽的能力值,比所有的奥特曼能力值都大的时候,就输出 -1;
每次找最大的能力值(一个奥特曼可以清的怪数)

 

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#define ll long long
#define inf 0x3f3f3f3f
using namespace std;
const int N=2e5+10;
int t,n,m;
int a[N],p[N],s[N];
int mx[N];
int main()
{
    cin>>t;
    while(t--)
    {
        memset(mx,0,sizeof(mx));
        scanf("%d",&n);
        for(int i=0; i<=n; i++) mx[i]=0;
        for(int i=0; i<n; i++)
        {
            cin>>a[i];
        }
        cin>>m;
        for(int i=0; i<m; i++)
        {
            cin>>p[i]>>s[i];
            mx[s[i]]=max(mx[s[i]],p[i]);
        }
        for(int i=n-1; i>=0; i--)
        {
            mx[i]=max(mx[i],mx[i+1]);
        }
        int day=0,pos=0;
        bool flag=true;
        while(pos<n)
        {
            ++day;
            int tmp=pos;
            int maxn=0;
            while(true)
            {
                maxn=max(maxn,a[tmp]);
                if(maxn>mx[tmp-pos+1])
                    break;
                ++tmp;
            }
            if(tmp==pos)
            {
                flag=false;
                break;
            }
            pos=tmp;
        }
        if(!flag)
            day=-1;
        cout<<day<<endl;
    }
    return 0;
}

 

非常高兴能够帮您回答这个问题。 以下是我为您生成的五十首赞美秋天的七言绝句: 1. 秋来莫忘山水情,万木丛中鹤舞翎。 2. 秋啸凉风瑟瑟起,满地黄叶舞纷飞。 3. 远山如翠翁媚女,晴日似火映池塘。 4. 高处秋风瑟瑟微,霜华铺洒夜漫漫。 5. 瑶草含香秋霁日,流水潺湲夕阳时。 6. 银霜凋零水痕迹,极目苍茫日色移。 7. 江畔秋色画屏障,瑞雪凝冰月色寒。 8. 西园枫叶红似火,战士扬帆起故乡。 9. 常山太行红叶合,小蹄八哥迎秋归。 10. 林莽苍茫秋色起,万里千翠遂相宜。 11. 望稗山黄叶缤纷,飞燕穿林相与闲。 12. 烟波画楼秋色秀,故园扶疏木叶红。 13. 天绮飘风落叶残,大雁南飞怀故园。 14. 草黄花落燕归时,碧峰如翠晓凝眸。 15. 岁华虚度秋芳散,北风吹瘦水声喧。 16. 野径穿过山峦远,夕阳西下九秋寒。 17. 朱雀祠前今夜月,夜半钟声到客船。 18. 风拂枫林叶落红,凉风习习思归人。 19. 晨露滋新落莲叶,寒鸦鸣道秋日归。 20. 飞花落醉斜阳里,寒鸭穿村暮色归。 21. 洛阳汴河秋色利,鸟翅泛落远山起。 22. 瑶池仙客居桂林,砧杵成婚秋色深。 23. 金秋送夕阳未央,碧浪潮生深巷扬。 24. 清风吹绕涟漪碧,江水东流月份扬。 25. 清秋遐想近高楼,情绪铺零催白头。 26. 纤云一缕映山岚,古木参差耐啜寒。 27. 天清波平闲望外,叠岸嫣然依水栏。 28. 彩斑随风楼台影,流水莹莹屋漏闻。 29. 古庙属墟芝兰香,风摆莲叶舞长江。 30. 峰回路转秋风怒,借杯酒旧追忆中。 31. 风举葱翠东海岸,夕阳余晖激浪滔。 32. 燕归花尽钟声绝,愁闻鹤舞江南宿。 33. 青松翠柏树峰抱,碧水涟漪雁阵拂。 34. 芙蓉如鬟玉如许,片片残红映衣裾。 35. 铁衣飘动追秋色,酒肆歇脚种菊中。 36. 秋韵萧疏绿苔红,簇簇霜草飞古空。 37. 汀洲秋梦碧波里,细雨横风两相依。 38. 招隐飞来碧水瀑,高枝古韵秋花蓝。 39. 喜虞九日起莲池,干树苍苔带泉痕。 40. 窗前秋雨细如丝,愁思万千寄江碧。 41. 翠峰定眸凝秋水,陇头群鹿美晴天。 42. 光风草露思潇洒,假山山水尽秋色。 43. 芳洲木兰映水影,青峰紫霄涌秋意。 44. 碧瓦朱楼临水寺,红于滩渡碧莙垂。 45. 秋光秦楼黄叶路,山色空闺独卧时。 46. 珠泉琼舞愁惆怅,古涧河声激春光。 47. 金陵庙里翠微红,飞鸟牵引远云漾。 48. 山涧清来隐鹭声,千古风流月廿四。 49. 小舟随转金山谷,百鸟鸣歇云峰起。 50. 风急铜驼万里辰,落叶千层故乡知。 希望我的回答能够满足您的需求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值