七月九日训练总结
发布日期:2021-05-04 19:39:57 浏览次数:40 分类:精选文章

本文共 6293 字,大约阅读时间需要 20 分钟。

 

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

41161 2 3 4 5 194 1 2 4 5 4 3 2 143 3 3 3

Output

-1632

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 
#include
#include
#include
#include
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"<

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

262 3 11 14 1 823 2100 153 5 100 2 3230 590 1

Output

5-1

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

这其实很明显就是一倒贪心的题目,上午没能ac出是因为并没有使用二分算法来节省这道题目的时间复杂度

贪心策略就是记录每个奥特曼耐力值,奥特曼的最大能力值。为了快速清理完所有的怪兽,就应该优先选择能力值大且耐力高的高特曼,这就是所谓的能力越大责任越大。
当有一个怪兽的能力值,比所有的奥特曼能力值都大的时候,就输出 -1;
每次找最大的能力值(一个奥特曼可以清的怪数)

 

#include
#include
#include
#include
#define ll long long#define inf 0x3f3f3f3fusing 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
>a[i]; } cin>>m; for(int i=0; i
>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
mx[tmp-pos+1]) break; ++tmp; } if(tmp==pos) { flag=false; break; } pos=tmp; } if(!flag) day=-1; cout<
<

 

上一篇:七月十日训练总结
下一篇:七月八号训练总结

发表评论

最新留言

网站不错 人气很旺了 加油
[***.192.178.218]2025年04月07日 08时45分45秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章