B. Uniqueness(二分+离散化)
作者:互联网
链接:http://codeforces.com/contest/1208/problem/B
题意:问去掉最小多少的一段能使所有不同数字的个数为1。
题解:以去掉的长度为基准进行二分,因为ai能到达1e9,所以还要离散化,然后直接暴力查询即可。
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<vector>
#include<stack>
#include<cstring>
#include<map>
#include<queue>
#include<cmath>
#include<set>
#define INF 0x3f3f3f3f
#define lowbit(a) ((a)&-(a))
#define speed std::ios::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL)
using namespace std;
typedef long long ll;
queue<int> q;
priority_queue<int> pq;
const int maxn = 2005;
ll n,m;
ll ans=INF,cnt=0;
ll a[maxn],b[maxn];
bool check(ll x)
{
ll c[maxn];
ll i,j,k;
for(i=1;i<=n-x+1;i++){
memset(c,0,sizeof(c));
for(j=1;j<=i-1;j++){
c[a[j]]++;
if(c[a[j]]>1)
break;
}
for(k=i+x;k<=n;k++){
c[a[k]]++;
if(c[a[k]]>1)
break;
}
if(k==n+1&&j==i)
return true;
}
return false;
}
int main()
{
cin>>n;
for(int i=1;i<=n;i++)
cin>>a[i];
for(int i=1;i<=n;i++)
b[i]=a[i];
sort(b+1,b+n+1);
for(int i=1;i<=n;i++)
if(b[i]!=b[cnt])
b[++cnt]=b[i];
for(int i=1;i<=n;i++)
a[i]=lower_bound(b+1,b+cnt+1,a[i])-b;
/*for(int i=1;i<=n;i++)
cout<<a[i]<<endl;*/
ll l=0,r=n-1,mid=(l+r)/2;;
while(l<=r){
//cout<<mid<<endl;
if(check(mid)){
r=mid-1,ans=min(mid,ans);
}
else
l=mid+1;
mid=(l+r)/2;
}
cout<<ans<<endl;
return 0;
}
标签:二分,false,int,ll,离散,Uniqueness,maxn,include,define 来源: https://blog.csdn.net/weixin_43870656/article/details/100081205