• pipioj 1452: 括号匹配Ⅱ(贪心)


     1 #define bug(x) cout<<#x<<" is "<<x<<endl
     2 #define IO std::ios::sync_with_stdio(0)
     3 #include <bits/stdc++.h>
     4 using namespace  std;
     5 typedef long long ll;
     6 #define mk make_pair
     7 #define pb push_back
     8 const int inf=2147483647;
     9 const int N=1e5+10;
    10 int n,m;
    11 
    12 char s[N];
    13 
    14 priority_queue<ll>q;
    15 
    16 int main(){
    17 
    18     scanf("%s",s+1);
    19     n=strlen(s+1);
    20     int k=0;
    21     ll ans=0;
    22     for(int i=1;i<=n;i++){
    23         if(s[i]=='(')k++;
    24         else if(s[i]==')'){
    25             k--;
    26             if(k<0){
    27                 if(q.empty()){
    28                     printf("-1
    ");
    29                     return 0;
    30                 }
    31                 else{
    32                     ans-=q.top();
    33                     q.pop();
    34                     k+=2;
    35                 }
    36             }
    37         }
    38         else{
    39             ll x,y;
    40             scanf("%lld%lld",&x,&y);
    41             if(k>0){
    42                 k--;
    43                 ans+=y;
    44                 q.push(y-x);
    45             }
    46             else if(!q.empty()&&q.top()>y-x){
    47                 k++;
    48                 ans-=q.top();
    49                 q.pop();
    50                 ans+=y;
    51                 q.push(y-x);
    52             }
    53             else{
    54                 k++;
    55                 ans+=x;
    56             }
    57         }
    58     }
    59     if(k)ans=-1;
    60     printf("%lld
    ",ans);
    61 }
  • 相关阅读:
    Excel表格函数逻辑排错
    MobaXterm体验最好的SSH客户端
    Excel中的常用函数
    Shell 知识点2020
    Linux 知识点2020
    Python知识点2020
    es6 模版字符串
    es6对象定义简写
    es6解构赋值
    ES6 let const关键字
  • 原文地址:https://www.cnblogs.com/ccsu-kid/p/14270176.html
Copyright © 2020-2023  润新知