洛谷P1878 舞蹈课 贪心 堆

洛谷P1878 舞蹈课
贪心 堆

 1 #include 
 2 #define LL long long
 3 #define GG int
 4 #define For(i, j, k) for(register int i=j; i<=k; i++)
 5 #define Dow(i, j, k) for(register int i=j; i>=k; i--)
 6 using namespace std;
 7 inline GG read() {
 8     GG x = 0, f = 1;
 9     char ch = getchar();
10     while(ch<'0'||ch>'9') { if(ch=='-') f = -1; ch = getchar(); }
11     while(ch>='0'&&ch<='9') { x = x*10+ch-48; ch = getchar(); }
12     return x * f;
13 }
14 void write(GG x) {
15     if(x<0) putchar('-'), x = -x;
16     if(x>9) write(x/10);
17     putchar(x%10+48);
18 }
19 inline void writeln(GG x) { write(x); putchar('\n'); }
20 
21 const int N = 2e5+11; 
22 struct node{
23     int l, r, del; 
24     friend bool operator <(node a, node b) {
25         if(a.del != b.del) return a.del > b.del; 
26         return a.l > b.l; 
27     }
28 };
29 priority_queue  Q; 
30 int n, tot;
31 int val[N], vis[N], L[N], R[N];  
32 char s[N]; 
33 
34 inline void work() {
35     while(!Q.empty()) {
36         node p = Q.top(); Q.pop(); 
37         if(vis[p.l] || vis[p.r]) continue; 
38         vis[p.l] = 1; vis[p.r] = 1; 
39         L[++tot] = p.l; R[tot] = p.r; 
40         
41         int l = p.l-1, r = p.r+1; 
42         while(l>=1 && vis[l]) --l; 
43         while(r<=n && vis[r]) ++r;
44         if(l>=1 && r<=n && s[l]!=s[r]) 
45             Q.push((node){l, r, abs(val[l]-val[r]) });  
46     }
47 }
48 
49 int main() {
50     n = read(); 
51     scanf("%s", s+1); 
52     For(i, 1, n) val[i] = read(); 
53     For(i, 1, n-1) 
54         if(s[i] != s[i+1]) 
55             Q.push((node){i, i+1, abs(val[i]-val[i+1])} ); 
56     work(); 
57     writeln(tot); 
58     For(i, 1, tot) {
59         write(L[i]); putchar(' '); writeln(R[i]); 
60     }
61 }

 

转载于:https://www.cnblogs.com/third2333/p/8476276.html

你可能感兴趣的:(洛谷P1878 舞蹈课 贪心 堆)