cf3d
2014-05-26 17:57
169 查看
题目链接:http://www.codeforces.com/problemset/problem/3/D
思路:贪心+调整,?就改成')',如果加了')'后发现右括号比左括号多, 就将花费小的括号变成'(',(采用优先队列,默认排序是从大到小);
View Code
思路:贪心+调整,?就改成')',如果加了')'后发现右括号比左括号多, 就将花费小的括号变成'(',(采用优先队列,默认排序是从大到小);
View Code
1 #include<iostream> 2 #include<cstdio> 3 #include<queue> 4 #include<cstring> 5 using namespace std; 6 typedef long long ll; 7 #define MAXN 50005 8 9 10 int main(){ 11 char str[MAXN]; 12 while(~scanf("%s",str)){ 13 priority_queue<pair<int,int> >Q; 14 int len=strlen(str),x,y; 15 ll ans=0,count=0; 16 bool flag=true; 17 for(int i=0;i<len;i++){ 18 if(str[i]=='(')count++; 19 else if(str[i]==')')count--; 20 else { 21 scanf("%d%d",&x,&y); 22 str[i]=')'; 23 count--; 24 Q.push(make_pair(y-x,i)); 25 ans+=y; 26 } 27 pair<int,int>p; 28 if(count<0){ 29 if(Q.empty()){flag=false;continue;} 30 p=Q.top();Q.pop();//出对列的是最大元素,保证花费最小; 31 ans-=p.first; 32 str[p.second]='('; 33 count+=2; 34 } 35 } 36 (count==0&&flag)?printf("%I64d\n%s\n",ans,str):puts("-1"); 37 } 38 return 0; 39 }
相关文章推荐
- cf3d
- CF 3D Least Cost Bracket Sequence
- CF:3D City Model(小思维)
- CF_3D_LeastCostBracketSequence
- HALCON算子函數——Chapter 10 : Matching-3D
- Fusion Charts学习-----Column3D
- 3D Constraints
- cf 825E Minimal Labels 【拓扑】
- [Unity 3D] 重力感应与罗盘(二)
- 3d打印机的软件系统组成部分
- CF Good Bye 2015 B- New Year and Old Property(CF611B)
- OpenGL实现3D自由变形
- iPhone开发:建立3D界面
- libgdx 3D
- CF Good Bye 2015 E. New Year and Three Musketeers(贪心+枚举)
- CF 55D 离散化各个位数的倍数
- 最强偏振3D播放器TriDef 3D安装+全格式播放配置
- Unity 3D,开发mobile,android初学者入门,环境搭建
- Html5添加三联切换仿3D效果旋转木马jQuery插件教程
- AR Camera开发记录(二) -- 3D人脸模型