Advertisement
Guest User

Untitled

a guest
Jun 19th, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.41 KB | None | 0 0
  1. #include<iostream>
  2. #include<stdio.h>
  3. #include<algorithm>
  4. #include<cmath>
  5. #include<vector>
  6. #include<stack>
  7. #include<queue>
  8. #include<set>
  9. #include<map>
  10. #include<unordered_map>
  11. #define x first
  12. #define y second
  13. #define all(x) x.begin(), x.end()
  14. #define pb push_back
  15. #define mp make_pair
  16. #define pii pair<int,int>
  17. // #define int long long
  18. using namespace std;
  19. signed main(){
  20. int m,n;
  21. cin >> m >> n;
  22. vector<int>a(int(3e5 + 7));
  23. vector<pii>pos(int(3e5 + 7),mp(1e9,-1));
  24. for(int i = 0;i < m;i++){
  25. cin >> a[i];
  26. pos[a[i]].x = min(i,pos[a[i]].x);
  27. pos[a[i]].y = max(i,pos[a[i]].y);
  28. }
  29. stack<int>s;
  30. set<int>ss;
  31. for(int i = 0;i < m;i++){
  32. if(ss.count(a[i]) == 0){
  33. if(pos[a[i]].y == i){
  34. continue;
  35. }
  36. ss.insert(a[i]);
  37. s.push(a[i]);
  38. }else{
  39. if(s.top() != a[i]){
  40. cout << "-1";
  41. return 0;
  42. }else{
  43. s.pop();
  44. if(i != pos[a[i]].y){
  45. s.push(a[i]);
  46. }else{
  47. ss.erase(a[i]);
  48. }
  49. }
  50. }
  51. }
  52. int cnt = 0;
  53. for(int i = 0;i < m;i++){
  54. if(pos[a[i]].x == i){
  55. cnt++;
  56. // cout << a[i] << ' ' << pos[a[i]].x + 1 << ' ' << pos[a[i]].y + 1<< endl;
  57. }
  58. }
  59. cout << cnt << endl;
  60. for(int i = 0;i < m;i++){
  61. if(pos[a[i]].x == i){
  62. // cnt++;
  63. cout << a[i] << ' ' << pos[a[i]].x + 1 << ' ' << pos[a[i]].y + 1<< endl;
  64. }
  65. }
  66. return 0;
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement