首页 > 其他分享 >codeforces 1257C Dominated Subarray

codeforces 1257C Dominated Subarray

时间:2023-02-03 12:33:29浏览次数:43  
标签:cin int codeforces pos Dominated ans 1257C INF include


codeforces 1257C Dominated Subarray_ci

codeforces 1257C Dominated Subarray_#include_02

题意就是找到一个最小的子区间使得这个区间中只有一个数的个数为2.

AC代码:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector>
#include<stdlib.h>
#include<queue>
#include<map>
#include<set>
#include<iomanip>
#include<math.h>
using namespace std;
typedef long long ll;
typedef double ld;
const int INF = 0x3f3f3f3f;
int i,j,k;
int ans,res,cnt,temp;
int n,m;
const int N = 2e5 + 10;
int a[N];
set<int>s;
int pos[N];
int main()
{
int t;
cin>>t;
while(t--)
{
s.clear();
memset(pos,0,sizeof pos );
cin>>n;
for(i=1;i<=n;i++)
cin>>a[i];
ans=INF;
for(i=1;i<=n;i++)
{
if(s.count(a[i])==1)///已经出现过一次
{
ans=min(ans,i-pos[a[i]]+1);
pos[a[i]]=i;///更新位置
continue;
}
s.insert(a[i]);
pos[a[i]]=i;///更新位置
}
if(ans==INF)
cout<<-1<<endl;
else
cout<<ans<<endl;
}
return 0;
}

 

标签:cin,int,codeforces,pos,Dominated,ans,1257C,INF,include
From: https://blog.51cto.com/u_15952369/6035772

相关文章

  • Codeforces1151B-Dima and a Bad XOR(构造)
    这道题真的想复杂了,作为div2的B题肯定不算难。只要构造出任意一种异或和大于1就行,如果第一列的值异或和>1,就直接全输入1即可,如果等于0,我们只要在任意一行中找到一个不等于......
  • Codeforces Round #661 (Div. 3)
    A.RemoveSmallest题意:给定一个序列,每次操作可以任选两个差的绝对值小于等于排序后计算相邻数的差,只要有大于AC代码:constintN=2e5+50;intn,m;inta[N];intmain......
  • Codeforces Round #662 (Div. 2)
    A.RainbowDash,FluttershyandChessColoring题意:有手动写几个找找规律。AC代码:intn,m;intmain(){intT;sd(T);while(T--){sd(n);pd(n/2+1);......
  • Codeforces Round #658 (Div. 2)
    ACommonSubsequence只要找到有一个相同的元素输出即可。AC代码:constintN=1010;inta[N],b[N];intans;intcnt[N];intmain(){intt;sd(t);while(t--){......
  • Codeforces Round #657 (Div. 2)
    A.AcaciusandString题意:给你一个串,你可以把换成任意字符,使得这个串最后只出现一次暴力枚举AC代码:strings;intn;stringT="abacaba";boolcheck(string&a){int......
  • Codeforces Round #656 (Div. 3)
    A.ThreePairwiseMaximums题意:给你三个正整数和,请你找到正整数和,使得,或者确定不可能找到这样的和AC代码:intmain(){intt;sd(t);while(t--){int......
  • Codeforces Round #655 (Div. 2)
    AOmkarandCompletion只要找两个相加不等的数交叉构造即可。AC代码:intmain(){intt;sd(t);while(t--){sd(n);rep(i,1,n){if(i&1)......
  • Codeforces 1360 D. Buying Shovels
    题意:要买个铲子,商店中有中不同的卖法,依次每一次卖到个铲子,现在只能选择其中的一种买法,问最少买几次同一种的买法,使得刚好买到直接选择小于的AC代码:intn,m,k;......
  • Codeforces 1360 E. Polygon
    题意:在一个的网格上方和左边都有一排大炮,每次可以发射一个,遇到边界和都会停下来,有没有一种发射频率可以组成给出的大炮的位置在左和上,所以每个非右边界或者下边界的......
  • Codeforces 1358 C. Celex Update
    题意:一个矩形内有多个方格,每个方格都按照顺序填写了一些数。给两个坐标,求这两个坐标间路径经过的数字和不同的路线总数。可以看出比如要从走到,这两种走法和第二个比......