Codeforces Beta Round #3 D. Least Cost Bracket Sequence

看来最不擅长的就是贪心,这种方法都想不起来是不是专题刷多了?   也没见得专题做得有多好啊~

题目大意:

给出一个字符串,包括三种字符‘(‘、‘)‘、‘?‘,每个问号可以变成其他两种符号,但是需要费用。

要求组成一个符合条件的字符串,使括号匹配,求最小费用。

解题思路:

贪心(发现他比动态规划都难)。

不需要在意哪个括号和哪个括号匹配,只需要注意数量就行。

下面是代码:

#include <set>
#include <map>
#include <queue>
#include <math.h>
#include <vector>
#include <string>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <cctype>
#include <algorithm>

#define eps 1e-10
#define pi acos(-1.0)
#define inf 107374182
#define inf64 1152921504606846976
#define lc l,m,tr<<1
#define rc m + 1,r,tr<<1|1
#define zero(a) fabs(a)<eps
#define iabs(x)  ((x) > 0 ? (x) : -(x))
#define clear1(A, X, SIZE) memset(A, X, sizeof(A[0]) * (min(SIZE,sizeof(A))))
#define clearall(A, X) memset(A, X, sizeof(A))
#define memcopy1(A , X, SIZE) memcpy(A , X ,sizeof(X[0])*(SIZE))
#define memcopyall(A, X) memcpy(A , X ,sizeof(X))
#define max( x, y )  ( ((x) > (y)) ? (x) : (y) )
#define min( x, y )  ( ((x) < (y)) ? (x) : (y) )

using namespace std;

char s[50005];

struct node
{
    int p;
    long long w;
    bool operator <(const node a)const
    {
        return a.w<w;
    }
} temp;

int main()
{
    scanf("%s",s);
    long long ans=0,l,r;
    int n=strlen(s),cnt=0;
    priority_queue <node ,vector <node> , less <node> >q;
    for(int i=0; i<n; i++)
    {
        if(s[i]=='(')
        {
            cnt++;
        }
        else if(s[i]==')')
        {
            if(cnt==0)
            {
                if(q.empty())
                {
                    ans=-1;
                    break;
                }
                else
                {
                    temp=q.top();
                    q.pop();
                    ans+=temp.w;
                    s[temp.p]='(';
                    cnt++;
                }
            }
            else cnt--;
        }
        else
        {
            scanf("%I64d%I64d",&l,&r);
            if(cnt==0)
            {
                if(q.empty())
                {
                    ans+=l;
                    s[i]='(';
                    cnt++;
                }
                else
                {
                    temp.p=i;
                    temp.w=l-r;
                    ans+=r;
                    s[i]=')';
                    q.push(temp);
                    cnt--;
                    temp=q.top();
                    q.pop();
                    ans+=temp.w;
                    s[temp.p]='(';
                    cnt+=2;
                }
            }
            else
            {
                temp.p=i;
                temp.w=l-r;
                ans+=r;
                s[i]=')';
                q.push(temp);
                cnt--;
            }
        }
    }
    if(cnt>1)ans=-1;

    printf("%I64d\n",ans);
    if(ans!=-1)
    puts(s);
    return 0;
}
时间: 2024-10-14 01:25:27

Codeforces Beta Round #3 D. Least Cost Bracket Sequence的相关文章

贪心+stack Codeforces Beta Round #5 C. Longest Regular Bracket Sequence

题目传送门 1 /* 2 题意:求最长括号匹配的长度和它的个数 3 贪心+stack:用栈存放最近的左括号的位置,若是有右括号匹配,则记录它们的长度,更新最大值,可以在O (n)解决 4 详细解释:http://blog.csdn.net/taoxin52/article/details/26012167 5 */ 6 #include <cstdio> 7 #include <algorithm> 8 #include <cstring> 9 #include <

Codeforces Beta Round #5 C. Longest Regular Bracket Sequence 栈/dp

C. Longest Regular Bracket Sequence Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset/problem/5/C Description This is yet another problem dealing with regular bracket sequences. We should remind you that a bracket sequence

Codeforces Beta Round #5 C. Longest Regular Bracket Sequence

经过了一个多月的时间,今天终于可以回到正轨了,继续开始刷CF. 题目大意: 给出一个只有括号的字符串,求最长"匹配"子串的长度和数量. 解题思路: 设置数组记录匹配括号段的开头. 下面是代码: #include <set> #include <map> #include <queue> #include <math.h> #include <vector> #include <string> #include &l

Codeforces Beta Round #91 (Div. 1 Only) E. Lucky Array

E. Lucky Array Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467are not. Petya has an arra

Codeforces Beta Round #85 (Div. 1 Only) C (状态压缩或是数学?)

C. Petya and Spiders Little Petya loves training spiders. Petya has a board n × m in size. Each cell of the board initially has a spider sitting on it. After one second Petya chooses a certain action for each spider, and all of them humbly perform it

CodeForces Beta Round #1

Codeforces Beta Round #1 A. Theatre Square [题意]一个n*m的矩形广场,用a*a的方形石板铺设,问最少需要多少块石板能铺满广场. [思路]水题,从n方向来看能能够铺设ceil(n/a)块,从m方向来看能能够铺设ceil(m/a)块,总共有ceil(n/a)*ceil(m/a)块. 1 /* 2 ** CodeForces 1A Theatre Square 3 ** Created by Rayn @@ 2014/05/18 4 */ 5 #inclu

暴力/DP Codeforces Beta Round #22 (Div. 2 Only) B. Bargaining Table

题目传送门 1 /* 2 题意:求最大矩形(全0)的面积 3 暴力/dp:每对一个0查看它左下的最大矩形面积,更新ans 4 注意:是字符串,没用空格,好事多磨,WA了多少次才发现:( 5 详细解释:http://www.cnblogs.com/cszlg/p/3217478.html 6 */ 7 #include <cstdio> 8 #include <algorithm> 9 #include <cstring> 10 #include <cmath>

Codeforces Beta Round #6 (Div. 2 Only) B. President&#39;s Office

题目大意 给出一个n*m的矩阵 ,描述桌子的布局.总统的桌子和他的副手的桌子相邻,每一个人的桌子有它独有的颜色.问总统有多少个副手. 解题思路 搜出总统的桌子在矩阵中的边界后判断边界外的其它颜色桌子的数量. 题目代码 #include <set> #include <map> #include <queue> #include <math.h> #include <vector> #include <string> #include

图论/暴力 Codeforces Beta Round #94 (Div. 2 Only) B. Students and Shoelaces

题目传送门 1 /* 2 图论/暴力:这是个连通的问题,每一次把所有度数为1的砍掉,把连接的点再砍掉,总之很神奇,不懂:) 3 */ 4 #include <cstdio> 5 #include <cstring> 6 #include <algorithm> 7 #include <cmath> 8 using namespace std; 9 10 const int MAXN = 1e2 + 10; 11 const int INF = 0x3f3f3