intmain(){ int t; cin >> t; while (t--){ ll maxc = INF; int n,s; cin >> n >> s; for (int i = 0; i < n; i ++ )cin >> a[i]; ll st = 0,ed = 0; ll ans = INF, sum = 0; while (1){ while (ed < n && sum < s) sum += a[ed++];//只要当前状态不满足题目要求,尾指针就一直右移 if (sum < s) break;//退出条件 ans = min(ans, ed -st);//更新答案 sum -= a[st++];//头指针右移,尝试更新答案 } if (ans == INF) ans = 0;//如果答案没有被更新过,那么答案为0 cout << ans << endl; } return0; }
#include<algorithm> #include<stdio.h> #include<iostream> #include<string.h> #include<cstring> #include<stdlib.h> #include<math.h> #include<cmath> #include<list> #include<vector> #include<queue> #include<stack> #include<map> #include<set> usingnamespace std; typedeflonglong ll; typedefdouble ff; typedef pair<int,int> PII; constint INF = 0x3f3f3f3f; constdouble pi = acos(-1.0); constint N = 1e6 +7; int a[N]; intmain(){ int r,n,m; cin >> r >> n >> m; ff d = (ff)((ff)2*(ff)pi*(ff)r*1.0)/(ff)n*1.0; int s = 0; int ans = INF; map<int,int> Q,q; for (int i = 0; i < n; i ++ ) scanf("%d",&a[i]); for (int i = n; i < 2 * n; i++) a[i] = a[i-n];//环形问题,拼接成一条链 for (int i = 0; i < m; i ++ ) { int x,y; cin >> x >> y; s += y; Q[x] = y;//map映射 } int st = 0 ,ed = 0; int sum = 0; while (1){ while ( ed < 2*n && sum < s ) //需要注意区间最大长度为2n if (++q[a[ed]] <= Q[a[ed++]]) sum++;//map映射,当前元素个数小于等于所需才会对当前状态有贡献 if ( sum < s ) break;//退出 ans = min(ans, ed -st);//更新答案 if (--q[a[st]] < Q[a[st++]]) sum--;//如果删去这个元素,该元素个数就不满足条件,对当前状态的贡献为-1 } ff res; if (ans == INF) res = 0; else res = (ff)(ans-1)*d; res += r;//加上最开始走的距离 printf("%.5lf\n",res); return0; }
intmain(){ int t; cin >> t; map<int,int> q; set<int> Q; for (int i = 0 ; i< t; i++ ) { scanf("%d",&a[i]);Q.insert(a[i]);//使用set去重 } int s= Q.size();//元素 int st = 0 ,ed = 0; int ans = INF, sum = 0; while (1){ while (ed < t && sum < s ) if(q[a[ed++]]++ == 0) sum++;//如果这个元素没有被加入 if (sum < s) break;//退出 ans = min(ans , ed -st);//更新答案 if (--q[a[st++]] == 0) sum--;//如果指针所指向的元素后,这个元素就不在区间里的话 } if (ans == INF) ans = 0; cout << ans << endl; return0; }