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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

【CodeForces - 616D 】Longest k-Good Segment (twopointer,尺取)

發布時間:2023/12/10 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【CodeForces - 616D 】Longest k-Good Segment (twopointer,尺取) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題干:

The array?a?with?n?integers is given. Let's call the sequence of one or more consecutive elements in?a?segment. Also let's call the segment?k-good?if it contains no more than?k?different values.

Find any longest?k-good segment.

As the input/output can reach huge size it is recommended to use fast input/output methods: for example, prefer to use?scanf/printf?instead of?cin/cout?in C++, prefer to use?BufferedReader/PrintWriter?instead of?Scanner/System.out?in?Java.

Input

The first line contains two integers?n,?k?(1?≤?k?≤?n?≤?5·105) — the number of elements in?a?and the parameter?k.

The second line contains?n?integers?ai?(0?≤?ai?≤?106) — the elements of the array?a.

Output

Print two integers?l,?r?(1?≤?l?≤?r?≤?n) — the index of the left and the index of the right ends of some?k-good longest segment. If there are several longest segments you can print any of them. The elements in?a?are numbered from?1?to?n?from left to right.

Examples

Input

5 5 1 2 3 4 5

Output

1 5

Input

9 3 6 5 1 2 3 2 1 4 5

Output

3 7

Input

3 1 1 2 3

Output

1 1

題目大意:

在長度為n的序列?中找出有不超過k個不同數字的最長連續子串,輸出子串開始以及結束的位置(若有多個答案,輸出任何?一個即可)

解題報告:

? ?尺取就好了。剛開始讀錯題了,讀成了恰有k個不同數字了。。不過還好if(now==k)改成if(now<=k)即可。

AC代碼:

//剛開始讀錯題了。。簡單尺取、、 #include<cstdio> #include<cstring> #include<algorithm> #include<iostream> #define ll long long using namespace std; int a[(int)5e5 + 5],cnt[(int)1e6 + 5]; int n,k,ans,ansl,ansr; int main() {cin>>n>>k;for(int i = 1; i<=n; i++) scanf("%d",a+i);int l = 1,r = 1,now = 0;while(r <= n) {if(cnt[a[r]] == 0) now++;cnt[a[r]]++;while(now > k) {cnt[a[l]]--;if(cnt[a[l]] == 0) now--;l++;}if(now <= k) {if(r-l+1 > ans) {ansl = l;ansr = r;ans = r-l+1;}}r++;}printf("%d %d\n",ansl,ansr);return 0 ;}

?

總結

以上是生活随笔為你收集整理的【CodeForces - 616D 】Longest k-Good Segment (twopointer,尺取)的全部內容,希望文章能夠幫你解決所遇到的問題。

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