SCNU省選校賽第二場B題題解

來源:互聯網
上載者:User

今晚的校賽又告一段落啦,終於“開齋”了!

AC了兩題,還算是滿意的,英語還是硬傷。

來看題目吧!

  

B. Arraytime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard output

You've got an array a, consisting ofn integers: a1, a2, ..., an. Your task is to find a minimal by inclusion segment [l, r] (1 ≤ l ≤ r ≤ n) such, that among numbers al,  al + 1,  ...,  ar there are exactlyk distinct numbers.

Segment [l, r] (1 ≤ l ≤ r ≤ n;l, r are integers) of length m = r - l + 1, satisfying the given property, is calledminimal by inclusion, if there is no segment[x, y] satisfying the property and less thenm in length, such that 1 ≤ l ≤ x ≤ y ≤ r ≤ n. Note that the segment[l, r] doesn't have to be minimal in length among all segments, satisfying the given property.

Input

The first line contains two space-separated integers:n and k (1 ≤ n, k ≤ 105). The second line containsn space-separated integers a1, a2, ..., an — elements of the arraya (1 ≤ ai ≤ 105).

Output

Print a space-separated pair of integers l and r (1 ≤ l ≤ r ≤ n) such, that the segment[l, r] is the answer to the problem. If the sought segment does not exist, print "-1 -1" without the quotes. If there are multiple correct answers, print any of them.

Sample test(s)Input
4 21 2 2 3
Output
1 2
Input
8 31 1 2 2 3 3 4 5
Output
2 5
Input
7 44 7 7 4 7 4 7
Output
-1 -1
Note

In the first sample among numbers a1 and a2 there are exactly two distinct numbers.

In the second sample segment [2, 5] is a minimal by inclusion segment with three distinct numbers, but it is not minimal in length among such segments.

In the third sample there is no segment with four distinct numbers.

題目理解的痛點在於min區間的意思,所謂的min是不能在這個區間找到更小的滿足。

這個區間的性質就是,恰好有k個不同的數字。

比如 n=5,k=3

1 1 2 3 4

滿足區間性質的有: 1 1 2 3, 1 2 3,但是前者不是最小,因為其子區間1 2 3也滿足。而1 2 3為最小的,因為沒有其他子區間有三個不同數了。當然 2 3 4也是最小區間。

這樣就有個問題了?怎麼才叫最小呢?

比如 n=6, k=3,

1 1 2 1 2 3 

我們的策略就是從第一個開始找,找到區間含有k個數,馬上就break掉,記下當前滿足k個不同的大區間(不一定是min)

之後我們就確定了一個區間[1,end],之後從end開始找,找到區間含有k個數,馬上就break掉,記下當前滿足k個不同的子區間開始beg.

那麼此時[beg,end]一定是最優的。

為什麼呢?

我們可以這樣考慮,因為區間要求是連續的,而end是第k個數,我們不能缺少這個數,所以第一次確定了end後,右邊界就確定了,之後往左走,同理,在beg找到第k個(這時候我們看end是第一個數啦),我們就不能缺少beg的數,區間就不能比[beg,end]更小了,所以的出來的結果就是最優的。

My Code:

/*******************************************************************************//* OS           : 3.2.0-58-generic #88-Ubuntu SMP Tue Dec 3 UTC 2013 GNU/Linux * Compiler     : g++ (GCC)  4.6.3 (Ubuntu/Linaro 4.6.3-1ubuntu5) * Encoding     : UTF8 * Date         : 2014-04-03 * All Rights Reserved by yaolong.*****************************************************************************//* Description: ********************************************************************************************************************************************//* Analysis: ***********************************************************************************************************************************************//*****************************************************************************/#include<iostream>#include<cstdio>#include<cstring>#include<cmath>#include<string>#include<vector>using namespace std;int mp[100005];int main(){    int n,k,i;    int ind;    vector<int> a;    while(cin>>n>>k){       memset(mp,0,sizeof(mp));       a.clear();       a.resize(n+1);       for(i=1;i<=n;i++)          cin>>a[i];       int cnt=0;       int beg=0;       for(i=1;i<=n&&cnt<k;i++){            if(mp[a[i]]==0){               mp[a[i]]=1;               cnt++;               ind=i;            }else{            }       }       if(cnt!=k){            cout<<-1<<" "<<-1<<endl;       }else{           memset(mp,0,sizeof(mp));           for(i=ind;i>=1&&cnt>=0;i--){              if(mp[a[i]]==0){                  mp[a[i]]=1;                  cnt--;                  beg=i;              }           }           cout<<beg<<" "<<ind<<endl;       }    }    return 0;}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.