zoukankan      html  css  js  c++  java
  • 洛谷P1878 舞蹈课 贪心 堆

    洛谷P1878 舞蹈课
    贪心 堆

     1 #include <bits/stdc++.h>
     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('
    '); }
    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 <node> 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 }
  • 相关阅读:
    安装 node-sass 的不成功
    input标签附带提示文字(bootstrap里面输入框的两侧同时添加额外元素)
    更改bootstrap的默认样式
    属性font-family:Font property font-family does not have generic default
    let与const命令
    vue之监听事件
    组件复用传值(待解决问题)
    vue之组件注册
    vue之组件理解(一)
    学习整理与细化(2)——HTML VS XHTML
  • 原文地址:https://www.cnblogs.com/third2333/p/8476276.html
Copyright © 2011-2022 走看看