CF--Uniqueness--正难则反思想

B. Uniqueness

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

You are given an array a1,a2,…,ana1,a2,…,an. You can remove at most one subsegment from it. The remaining elements should be pairwise distinct.

In other words, at most one time you can choose two integers ll and rr (1≤l≤r≤n1≤l≤r≤n) and delete integers al,al+1,…,aral,al+1,…,arfrom the array. Remaining elements should be pairwise distinct.

Find the minimum size of the subsegment you need to remove to make all remaining elements distinct.

Input

The first line of the input contains a single integer nn (1≤n≤20001≤n≤2000) — the number of elements in the given array.

The next line contains nn spaced integers a1,a2,…,ana1,a2,…,an (1≤ai≤1091≤ai≤109) — the elements of the array.

Output

Print a single integer — the minimum size of the subsegment you need to remove to make all elements of the array pairwise distinct. If no subsegment needs to be removed, print 00.

Examples

input

Copy

3
1 2 3

output

Copy

0

input

Copy

4
1 1 2 2

output

Copy

2

input

Copy

5
1 4 1 4 9

output

Copy

2

Note

In the first example all the elements are already distinct, therefore no subsegment needs to be removed.

In the second example you can remove the subsegment from index 22 to 33.

In the third example you can remove the subsegments from index 11 to 22, or from index 22 to 33, or from index 33 to 44.

 

要求的是删除连续k个数,使得剩余的数两两不同。

枚举可以左边可以连续选的个数,然后扫一下后面可以连续选的个数。最后取最大值。然后用n减去,就是需要删除的最小长度。

#include <algorithm>    //STL通用算法
#include <bitset>     //STL位集容器
#include <cctype>
#include <cerrno>
#include <clocale>
#include <cmath>
#include <complex>     //复数类
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>      //STL双端队列容器
#include <exception>    //异常处理类
#include <fstream>
#include <functional>   //STL定义运算函数(代替运算符)
#include <limits>
#include <list>      //STL线性列表容器
#include <map>       //STL 映射容器
#include <iomanip>
#include <ios>      //基本输入/输出支持
#include<iosfwd>     //输入/输出系统使用的前置声明
#include <iostream>
#include <istream>     //基本输入流
#include <ostream>     //基本输出流
#include <queue>      //STL队列容器
#include <set>       //STL 集合容器
#include <sstream>    //基于字符串的流
#include <stack>      //STL堆栈容器    
#include <stdexcept>    //标准异常类
#include <streambuf>   //底层输入/输出支持
#include <string>     //字符串类
#include <utility>     //STL通用模板类
#include <vector>     //STL动态数组容器
#include <cwchar>
#include <cwctype>
#define ll long long
using namespace std;
//priority_queue<int,vector<int>,less<int> >q;
int dx[]= {-1,1,0,0,-1,-1,1,1};
int dy[]= {0,0,-1,1,-1,1,1,-1};
const int maxn = 2000+66;
const ll mod=998244353;
const int inf=999999;
int a[maxn];
map<int,int>mmp;
int main()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        for(int i=1; i<=n; i++)
        {
            scanf("%d",&a[i]);
        }
        int ans=0;
        for(int i=0; i<=n; i++)
        {
            mmp.clear();
            int maxx=0;
            for(int j=1; j<=i; j++)
            {
                if(mmp[a[j]])
                    break;
                mmp[a[j]]++;
                maxx++;
            }
            for(int j=n; j>i; j--)
            {
                if(mmp[a[j]])
                    break;
                mmp[a[j]]++;
                maxx++;
            }
            ans=max(ans,maxx);
        }
        printf("%d\n",n-ans);
    }
}

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
### 回答1: 最小生成树的独特性在于,它是连接所有节点的最小权重的树形结构。这意味着,对于给定的图,最小生成树是唯一的,因为它是基于图中边的权重和拓扑结构的。如果有两个不同的最小生成树,那么它们的权重和拓扑结构必须是相同的,否则它们就不是最小生成树。因此,最小生成树的独特性是由图的特性所决定的。 ### 回答2: 最小生成树(MST)是一个有权无向图中的一种最小权重生成树。在图中只存在一个MST集,这就是MST的唯一性。MST的唯一性是由两个基本性质决定的:割定理和权重不相等原则。 割定理:一条边e是图G的一组定理的一部分,当且仅当e在图的任意MST中是轻边。根据割定理,我们可以证明一个图的MST集合中包含着一组定理,这个定理可以将图划分成两块(S, V-S),其中S是V的子集,也就是说,将图分成了一个项集S和一个项集V-S。而出现在子集Hi和Hj中的边,一定不会同时出现在MST中。因为这会导致环的出现,并使得生成树的权值不是最小。 权重不相等原则:如果两条边的权重不相等,一条较轻的边拥有优先权。在图的MST中,比较轻的边先被选中,而重边则被放弃。因此,生成的树具有唯一性,它不受选择顺序的影响。 总之,MST的唯一性可通过两个基本性质来解释。MST的唯一性对于很多算法问题来说都是非常重要的,因为它确保了结果的确性和可重复性。例如,在电信网络普及的时代,MST常被用来寻找通信网络中的最优路径,因为可以通过生成树来解决这个问题。 ### 回答3: 最小生成树是一种表示连接无向带权图中所有顶点的最小边集的算法。它的法是选择连接这些节点的最小权重边而不形成环。 在一个图形中,可能存在多个不同的生成树。这是由于生成树只是图形中可能的许多树之一。但是最小生成树具有独特性,这意味着一个给定图形的任何两个最小生成树都将包含相同数量的边,具有相同的总权重和相同的结构。 证明最小生成树的唯一性可以通过矛盾法来证明。设存在两个不同的最小生成树,可以假设它们之间存在一组边不同的点组合。我们选择这些边可以作为一组割边从一个树中删除,并添加到另一个树中去。这样得到的新图仍然要求连接所有的节点,但是总权重不会因此改变。根据割边的定义,新图中的每一组割边必须包含一条链接树之间的边。因此,我们可以在原来的树和新的树之间找到一条边,它只出现在一个树中,不会出现在另一个树中。这条边可以用来创建一个环,进而将此环内的所有边从新树中进行剪切。这样就得到了一棵比原来的树更小的,但仍然是最小生成树,这与前提相矛盾。因此我们可以得出最小生成树的唯一性。 最小生成树的唯一性是它的一个重要性质,它让我们不用担心算法的输出结果可能不止一个。它也为一些具有证明要求的应用提供了支持,例如在网络设计和路由算法中。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值