标签:printf can include set str algo min i++ max
尺取法,这里的边界是知识点总数量,看代码就明白
#include <iostream> #include <cstring> #include <cstdio> #include <algorithm> #include <map> #include <set> using namespace std; const int maxn=1e7+10; int n; int a[maxn]; set<int> aa; int chiqu() { int tt=aa.size(); map<int,int> bb;//知识点以及他们对应的数量 int num=0,t=0,s=0;//num是知识点数量 int ans=n+1; while(1) { while(t<n&&num<tt) { if(bb[a[t++]]++==0) num++; } if(num<tt) break; ans=min(ans,t-s); if(--bb[a[s++]]==0) num--;//如果是只出现过一次的知识点,那么总知识点减一,否则不减,但s依旧会++ } return ans; } int main() { scanf("%d",&n); for(int i=0;i<n;i++) { scanf("%d",&a[i]); aa.insert(a[i]); } printf("%d\n",chiqu()); return 0; }
标签:printf can include set str algo min i++ max
原文地址:http://www.cnblogs.com/Wangwanxiang/p/6833353.html