CF 45C Dancing Lessons (priority queue)

Source: Internet
Author: User

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;
}


 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.