Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<stdio.h>
- #include<algorithm>
- #include<cmath>
- #include<vector>
- #include<stack>
- #include<queue>
- #include<set>
- #include<map>
- #include<unordered_map>
- #define x first
- #define y second
- #define all(x) x.begin(), x.end()
- #define pb push_back
- #define mp make_pair
- #define pii pair<int,int>
- // #define int long long
- using namespace std;
- signed main(){
- int m,n;
- cin >> m >> n;
- vector<int>a(int(3e5 + 7));
- vector<pii>pos(int(3e5 + 7),mp(1e9,-1));
- for(int i = 0;i < m;i++){
- cin >> a[i];
- pos[a[i]].x = min(i,pos[a[i]].x);
- pos[a[i]].y = max(i,pos[a[i]].y);
- }
- stack<int>s;
- set<int>ss;
- for(int i = 0;i < m;i++){
- if(ss.count(a[i]) == 0){
- if(pos[a[i]].y == i){
- continue;
- }
- ss.insert(a[i]);
- s.push(a[i]);
- }else{
- if(s.top() != a[i]){
- cout << "-1";
- return 0;
- }else{
- s.pop();
- if(i != pos[a[i]].y){
- s.push(a[i]);
- }else{
- ss.erase(a[i]);
- }
- }
- }
- }
- int cnt = 0;
- for(int i = 0;i < m;i++){
- if(pos[a[i]].x == i){
- cnt++;
- // cout << a[i] << ' ' << pos[a[i]].x + 1 << ' ' << pos[a[i]].y + 1<< endl;
- }
- }
- cout << cnt << endl;
- for(int i = 0;i < m;i++){
- if(pos[a[i]].x == i){
- // cnt++;
- cout << a[i] << ' ' << pos[a[i]].x + 1 << ' ' << pos[a[i]].y + 1<< endl;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement