日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

POJ 3320 Jessica's Reading Problem (尺取)

發布時間:2025/5/22 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 POJ 3320 Jessica's Reading Problem (尺取) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Description

給出一段長度為\(n\)的整數序列,求包含所有出現過的數的連續區間的最小長度。

Input

第一行給出序列長度\(n\),第二行給出\(n\)個整數,表示這個序列。\(n \leqslant 10^6\)

Output

輸出一個整數,表示滿足條件的區間的最小長度。

Sample Input

5 1 8 8 8 1

Sample Output

2

Solution

求連續區間,尺取問題。限制條件是下限,區間要包含每一種數。依次推進左端點,對于每個左端點,右端點向右直到找到第一個滿足條件的位置,更新答案。如果右端點直到最右端仍然找不到合法區間,就跳出。

Code

#include <cstdio> #include <cstring> #include <algorithm> #include <map> using namespace std; typedef long long ll; const int INF = 0x3f3f3f3f; const int N = 1e6 + 10;int a[N]; map<int, int> mp;int main() {int n;scanf("%d", &n);mp.clear();for (int i = 1; i <= n; i++) scanf("%d", a + i), mp[a[i]]++;int sum = mp.size();mp.clear();int r = 1, cnt = 0, ans = INF;for (int l = 1; l <= n; l++){while (r <= n && cnt < sum){mp[a[r]]++;if (mp[a[r]] == 1) cnt++;r++;}if (cnt < sum) break;ans = min(ans, r - l);mp[a[l]]--;if (mp[a[l]] == 0) cnt--;}printf("%d\n", ans);return 0; }

http://poj.org/problem?id=3320

轉載于:https://www.cnblogs.com/dadamrx/p/7352591.html

總結

以上是生活随笔為你收集整理的POJ 3320 Jessica's Reading Problem (尺取)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。