【贪心】51Nod 1476 括号序列的最小代价
2017-10-20 20:12
127 查看
题面在这里
这题不太容易想到……
其实还是很简单的
先把所有的?换成’)’,然后一路扫过去
如果不符合配对的要求就在之前的?中找修正代价最小的
用优先队列维护一下,然后就好了
示例程序:
这题不太容易想到……
其实还是很简单的
先把所有的?换成’)’,然后一路扫过去
如果不符合配对的要求就在之前的?中找修正代价最小的
用优先队列维护一下,然后就好了
示例程序:
#include<cstdio> #include<cstring> #include<queue> using namespace std; typedef long long ll; const int maxn=50005; int n,m,a[maxn],b[maxn]; ll ans; char s[maxn]; priority_queue<int,vector<int>,greater<int> > Q; int main(){ scanf("%s",s);n=strlen(s); for (int i=0;i<n;i++) if (s[i]=='?') m++; for (int i=1;i<=m;i++) scanf("%d%d",&a[i],&b[i]); int S=0; for (int i=0,t=0;i<n;i++){ if (s[i]=='?'){ t++;ans+=b[t];Q.push(a[t]-b[t]);S--; }else S+=(s[i]=='(')?1:-1; while (!Q.empty()&&S<0) S+=2,ans+=Q.top(),Q.pop(); if (S<0) return printf("-1"),0; } if (S>0) return printf("-1"),0; printf("%lld",ans); return 0; }
相关文章推荐
- 51nod 1476 括号序列的最小代价【贪心】【堆】
- 51nod 1476 括号序列的最小代价(贪心)
- 51Nod-1476-括号序列的最小代价
- 51nod 1476 括号序列的最小代价
- [贪心] 51Nod1476 括号序列的最小代价
- 51node1476 括号序列的最小代价(贪心)
- 【codeforces 13 C】【DP + 离散化 + 贪心+滚动数组 】C. Sequence【用最小代价把序列变成非严格递增序列】
- 51NOD 1125(交换机器最小代价) (贪心) 思想 !思想!
- 优先队列+括号配对 51Nod1476 括号序列的最小代价
- 51nod 1125 机器交换最小代价(贪心)
- 51nod 1255 字典序最小的子序列【贪心】
- 51nod 1255 字典序最小的子序列 []【贪心】
- 51nod 1255 字典序最小的子序列(贪心)
- 【51Nod1476】括号序列的最小代价
- [贪心 置换] 51Nod 1125 交换机器的最小代价
- 51NOD 1255 字典序最小的子序列 【贪心】
- 51nod 1255 字典序最小的子序列 贪心
- 51nod 1255 字典序最小的子序列(贪心)
- 括号序列的最小代价
- 51nod -1255 字典序最小的子序列(贪心)