codeforces 3D - Least Cost Bracket Sequence(贪心)

题目大意给你一串不确定的括号以及‘?‘,其中‘?‘可以替换成为‘(’与‘)’,并且不同的‘(’需要支付一定的价格,在保证括号合法性的情况下保证最少的价格



分析:

有两个要点:合法性与最少的价格

所以可以在最少价格情况下修正合法性,也可以在保证合法性的情况下修正最小的价格

首先,计算合法性的要点:对于一个括号串,‘(‘即+1,‘)‘即-1,然后把所有的‘?‘都变成‘)‘,如果数值变成负数,则从前面找一个可以使当前括号串价格最小的一个位置变成‘(‘,用优先队列处理。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
#define MAX_NUM            50007
typedef long long LL;
char set[MAX_NUM];
LL fir[MAX_NUM];
LL las[MAX_NUM];
struct node{
    LL number;
    LL stp;
    friend bool operator< (node n1, node n2)
    {
        return n1.number < n2.number;
    }
    node(LL a,LL b):number(a),stp(b){}
};

priority_queue<node> que;
int main(int argc, char const *argv[])
{
    gets(set+1);
    int strl = strlen(set+1);
    int cnt = 0;
    for (int i = 1; i <= strl ; ++i)
    {
        if(set[i] == ‘?‘)
            cnt++;
    }
    for (int i = 1; i <= cnt; ++i)
    {
        scanf("%lld%lld",&fir[i],&las[i]);
    }
    int col = 1;
    cnt = 0;
    LL ans = 0;
    for (int i = 1; i <= strl ; ++i)
    {
        if(set[i] == ‘(‘)
            cnt++;
        else if(set[i] == ‘)‘)
            cnt--;
        else{
            cnt--;
            set[i] = ‘)‘;
            ans += las[col];
            que.push(node(las[col] - fir[col],i));
            col++;
        }
        if(cnt < 0)
        {
            if(que.empty()){
                printf("-1\n");
                return 0;
            }
            node tem = que.top();
            que.pop();
            ans-=tem.number;
            set[tem.stp] = ‘(‘;
            cnt +=2;
        }
    }
    if(cnt==0)
        printf("%lld\n%s\n",ans,set+1);
    else
        printf("-1\n");
    return 0;
}
时间: 2024-11-03 19:11:24

codeforces 3D - Least Cost Bracket Sequence(贪心)的相关文章

cf3D Least Cost Bracket Sequence

This is yet another problem on regular bracket sequences. A bracket sequence is called regular, if by inserting "+" and "1" into it we get a correct mathematical expression. For example, sequences "(())()", "()" and

【Codeforces 3D】Least Cost Bracket Sequence

Codeforces 3 D 题意:有一个括号序列,其中一些位置是问号,把第\(i\)个问号改成(需要\(a_i\)的代价,把它改成)需要\(b_i\)的代价. 问使得这个括号序列成立所需要的最小代价. 思路1: 这个是正统的贪心. 首先我们假设所有的位置上都是),那么我们在从左向右扫描的途中会发现一些问题. 比如:我们原来的序列是(??),现在假设成了())),那么在第三个字符处就会发现我们的打开的左括号数量为\(-1\),这是肯定不行的,所以我们必须把第二个字符或者第三个字符改成左括号以把左

Codeforces Beta Round #3 D. Least Cost Bracket Sequence

看来最不擅长的就是贪心,这种方法都想不起来是不是专题刷多了?   也没见得专题做得有多好啊~ 题目大意: 给出一个字符串,包括三种字符'('.')'.'?',每个问号可以变成其他两种符号,但是需要费用. 要求组成一个符合条件的字符串,使括号匹配,求最小费用. 解题思路: 贪心(发现他比动态规划都难). 不需要在意哪个括号和哪个括号匹配,只需要注意数量就行. 下面是代码: #include <set> #include <map> #include <queue> #in

CodeForces 5C Longest Regular Bracket Sequence

题意:给定一串括号,求最长的规则('(())'或‘(()())’)的字串及最长字串的个数: 思路:使用栈保存左括号,与最近的右括号匹配,使用递推推出每个位置最长字串长度: #include<cstdio> #include<cstring> #include<algorithm> #include<cmath> #include<stack> using namespace std; stack<int> t; int n,m,len

CodeForces - 3D

CodeForces - 3D D. Least Cost Bracket Sequence time limit per test1 second memory limit per test64 megabytes inputstandard input outputstandard output This is yet another problem on regular bracket sequences. A bracket sequence is called regular, if

贪心+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 Round #350 (Div. 2) E. Correct Bracket Sequence Editor

E. Correct Bracket Sequence Editor Recently Polycarp started to develop a text editor that works only with correct bracket sequences (abbreviated as CBS). Note that a bracket sequence is correct if it is possible to get a correct mathematical express

Codeforces Round #350 (Div. 2) E. Correct Bracket Sequence Editor 线段树模拟

E. Correct Bracket Sequence Editor Recently Polycarp started to develop a text editor that works only with correct bracket sequences (abbreviated as CBS). Note that a bracket sequence is correct if it is possible to get a correct mathematical express