-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdelns2.cpp
136 lines (87 loc) · 2.05 KB
/
delns2.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#include "iostream"
#include "stdio.h"
#define get getchar_unlocked
using namespace std;
long int solutions[100050];
long int solution_count = 0;
inline long int scan2(){
long int n=0,s=1;
char p=get();
if(p=='-') s=-1;
while((p<'0'||p>'9')&&p!=EOF&&p!='-') p=get();
if(p=='-') s=-1,p=get();
while(p>='0'&&p<='9') { n = (n<< 3) + (n<< 1) + (p - '0'); p=get(); }
return n*s;
}
struct solution{
long int v;
long int t;
} sol[100001];
long int sol_count = 0;
int main(){
long int i,n,j,k;
long int e[100002];
long int occurances[100002];
long int jump = 1,temp_count = 0,temp_t = 0,s = 0;
//scanf("%ld",&n);
n = scan2();
for(i=0;i<n;i++){
//scanf("%ld",&e[i].num); //The actual data
e[i] = scan2();
// e[i].pos = i+1;
occurances[e[i]]++; //The occurances of each of the actual data
}
for(i=n-1;i>=0;i--){
temp_count = 0;
jump = 1;
if(occurances[e[i]] == 1){
sol[sol_count].v = i + 1; //e[i].pos
sol[sol_count++].t = 1;
occurances[e[i]]--; // it will be set to zero;
}
else {
//A system to find t , from the first two occurances of the number
if(occurances[e[i]] >= 2){
for(k=i-1;k>=0;k--){
if(e[k] == e[i]){
sol[sol_count].t = i-k;//e[i].pos - e[k].pos; //or i-k;
temp_t = i-k;
break;
}
}
occurances[e[i]]--;
}
for(s=k;e[i]== e[s] && s>=0 && (occurances[e[i]] !=0) ;s = s-temp_t)
occurances[e[i]]--;
s = s + temp_t;
sol[sol_count++].v = s + 1 ;//e[s].pos;
s++;
for(; s <= i-1 ; s++ ){
temp_count++;
if( temp_count % temp_t == 0){
//e[s].pos -= jump;
jump++;
// e[s-jump] = e[s];
temp_count = 0 ;
}
else
e[s-jump] = e[s];
// jump++;
}
// for(;s<=i;s = s + temp_t){
// for(j = s + 1;j < s + temp_t;j++ ){
// e[j-jump] = e[j];
// }
// jump++;
// }
i = i - jump;
jump = 1; //added afterwards
}
}
//Just print the solution structure here
printf("%ld\n",sol_count);
for(i=0;i<sol_count;i++){
printf("%ld %ld\n",sol[i].v,sol[i].t);
}
return 0;
}