鏈接
Problem - 620C - Codeforces
思路 :?
貪心 : 對于每一段區間,從前往后貪,如果前面一段區間有重復數字,那么就直接合并成答案的一段區間,然后繼續尋找下一段區間,對于最后一段,如果沒有匹配的話,就直接合并到已經加入到答案的最后一段區間里面;
代碼 :?
#include<bits/stdc++.h>
#define IOS ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define endl '\n'
#define lowbit(x) (x&(-x))
#define sz(a) (int)a.size()
#define pb push_back
#define all(a) a.begin(), a.end()
#define int long long
#define PII pair<int,int>
typedef long long LL;
const int mod = 1e9+7;
const int N = 3e5+10;
using namespace std;int n ;
int a[N];inline void solve(){cin >> n;vector<PII> b;int ans = 0;unordered_map<int,int> mp;int pre = 1;for(int i=1;i<=n;i++){cin >> a[i];}for(int i=1;i<=n;i++){mp[a[i]]++;if(mp[a[i]]>1){ans ++;mp.clear();b.push_back({pre,i});pre = i + 1;}}int x,y;if(!b.size()) cout << -1 << endl;else{cout << ans << endl;int len = b.size();for(int i=0;i<len-1;i++) cout << b[i].first << " " << b[i].second << endl;cout << b[len-1].first << " " << n << endl;}return ;
}signed main()
{IOSint _ = 1;// cin >> _;while(_ --) solve();return 0;
}