Maintain a priority queue. The key value is the difference value. If the difference value is the same, the queue is sorted by binary groups.
Then maintain a chain-like object, record the next element and the previous element.
[Cpp] # include <iostream>
# Include <cstdio>
# Include <algorithm>
# Include <cstring>
# Include <cmath>
# Include <set>
# Include <vector>
# Include <queue>
# Define pb (a) push_back ()
# Define mp (a, B) make_pair (a, B)
Using namespace std;
Const int n= 200005;
Struct Node {
Int l, r, d;
Node (){}
Node (int _ l, int _ r, int _ d): l (_ l), r (_ r), d (_ d ){}
Bool operator <(const Node n) const {
Return d! = N. d? D> n. d: l> n. l;
}
};
Priority_queue <Node> que;
Int n, a [N], flag [N] = {0 };
Int next [N], pre [N];
Char str [N];
Vector <pair <int, int> ans;
Int main (){
Scanf ("% d % s", & n, str + 1 );
For (int I = 1; I <= n; I ++ ){
Scanf ("% d", & a [I]);
Pre [I] = I-1; next [I] = I + 1;
}
For (int I = 1; I <n; I ++ ){
If (str [I]! = Str [I + 1])
Que. push (Node (I, I + 1, abs (a [I]-a [I + 1]);
}
While (! Que. empty ()){
Node cur = que. top (); que. pop ();
If (flag [cur. l] | flag [cur. r]) continue;
Ans. pb (mp (cur. l, cur. r ));
Int l = pre [cur. l], r = next [cur. r];
Flag [cur. l] = flag [cur. r] = 1;
Next [l] = r; pre [r] = l;
If (l <1 | r> n) continue;
If (str [l]! = Str [r]) {
Que. push (Node (l, r, abs (a [l]-a [r]);
}
}
Printf ("% d \ n", ans. size ());
For (int I = 0; I <ans. size (); I ++)
Printf ("% d \ n", ans [I]. first, ans [I]. second );
Return 0;
}
# Include <iostream>
# Include <cstdio>
# Include <algorithm>
# Include <cstring>
# Include <cmath>
# Include <set>
# Include <vector>
# Include <queue>
# Define pb (a) push_back ()
# Define mp (a, B) make_pair (a, B)
Using namespace std;
Const int n= 200005;
Struct Node {
Int l, r, d;
Node (){}
Node (int _ l, int _ r, int _ d): l (_ l), r (_ r), d (_ d ){}
Bool operator <(const Node n) const {
Return d! = N. d? D> n. d: l> n. l;
}
};
Priority_queue <Node> que;
Int n, a [N], flag [N] = {0 };
Int next [N], pre [N];
Char str [N];
Vector <pair <int, int> ans;
Int main (){
Scanf ("% d % s", & n, str + 1 );
For (int I = 1; I <= n; I ++ ){
Scanf ("% d", & a [I]);
Pre [I] = I-1; next [I] = I + 1;
}
For (int I = 1; I <n; I ++ ){
If (str [I]! = Str [I + 1])
Que. push (Node (I, I + 1, abs (a [I]-a [I + 1]);
}
While (! Que. empty ()){
Node cur = que. top (); que. pop ();
If (flag [cur. l] | flag [cur. r]) continue;
Ans. pb (mp (cur. l, cur. r ));
Int l = pre [cur. l], r = next [cur. r];
Flag [cur. l] = flag [cur. r] = 1;
Next [l] = r; pre [r] = l;
If (l <1 | r> n) continue;
If (str [l]! = Str [r]) {
Que. push (Node (l, r, abs (a [l]-a [r]);
}
}
Printf ("% d \ n", ans. size ());
For (int I = 0; I <ans. size (); I ++)
Printf ("% d \ n", ans [I]. first, ans [I]. second );
Return 0;
}