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 by inserting "+" and "1" into it we get a correct mathematical expression. For example, sequences "(())()", "()" and "(()(()))" are regular, while ")(", "(()" and "(()))(" are not. You have a pattern of a bracket sequence that consists of characters "(", ")" and "?". You have to replace each character "?" with a bracket so, that you get a regular bracket sequence.

For each character "?" the cost of its replacement with "(" and ")" is given. Among all the possible variants your should choose the cheapest.

Input
The first line contains a non-empty pattern of even length, consisting of characters "(", ")" and "?". Its length doesn‘t exceed 5·104. Then there follow m lines, where m is the number of characters "?" in the pattern. Each line contains two integer numbers ai and bi (1?≤?ai,??bi?≤?106), where ai is the cost of replacing the i-th character "?" with an opening bracket, and bi — with a closing one.

Output
Print the cost of the optimal regular bracket sequence in the first line, and the required sequence in the second.

Print -1, if there is no answer. If the answer is not unique, print any of them.

Examples
input
(??)
1 2
2 8
output
4
()()
AC代码:

#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
typedef long long ll;
const int N = 1e5+10;
struct node{
    int num, id;
    bool operator < (const node & a) const{
        return num > a.num;
    }
};
char s[N];
int main(){
    #ifdef ONLINE_JUDGE
    #else
        freopen("in.txt", "r", stdin);
    #endif // ONLINE_JUDGE
    int x, y;
    while(~scanf("%s", s)){
        int len = strlen(s);
        int l = 0, r = 0;
        ll ans = 0;
        bool flag = false;
        priority_queue<node> Q;
        for(int i = 0; i < len; i++){
            if(s[i] == '(') l++;
            else if(s[i] == ')') r++;
            else{
                scanf("%d%d", &x, &y);
                ans += y;
                Q.push((node){x - y, i});
                r++;
            }
            if(l < r){
                if(Q.empty()) flag = true;
                else {
                    ans += Q.top().num;
                    s[Q.top().id] = '(';
                    Q.pop();
                    l++;
                    r--;
                }
            }
        }
        if(flag || l != r) printf("-1");
        else{
            printf("%lld\n", ans);
            for(int i = 0; i < len; i++){
                if(s[i] == '?') printf(")");
                else printf("%c", s[i]);
            }
        }
        printf("\n");
    }
    return 0;
}

原文地址:https://www.cnblogs.com/kun-/p/10259055.html

时间: 2024-11-02 22:38:59

CodeForces - 3D的相关文章

【Codeforces 3D】Least Cost Bracket Sequence

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

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

题目大意给你一串不确定的括号以及'?',其中'?'可以替换成为'('与')',并且不同的'('需要支付一定的价格,在保证括号合法性的情况下保证最少的价格 分析: 有两个要点:合法性与最少的价格 所以可以在最少价格情况下修正合法性,也可以在保证合法性的情况下修正最小的价格 首先,计算合法性的要点:对于一个括号串,'('即+1,')'即-1,然后把所有的'?'都变成')',如果数值变成负数,则从前面找一个可以使当前括号串价格最小的一个位置变成'(',用优先队列处理. #include <iostre

括号序列问题

湘潭邀请赛 codeforces#3D. Least Cost Bracket Sequence 题意 给一个未完全填好的括号序列,每一个可填充的位置,填(的花费为a,填)的花费为b,求使其成合法序列的最小花费  分析 合法的括号序列有一个特点:对于任意位置 i ,它的左括号的数量不能少于 ceil ((i+1)/2).首先强制将所有都变为),然后从前往后模拟这个过程,用一个优先队列维护即可. #include <bits/stdc++.h> #define ll long long usin

Codeforces 346A Alice and Bob 博弈

http://codeforces.com/problemset/problem/346/A 题意:A和B两个人进行游戏,每人轮流操作,每次从集合(集合元素个数n<=100)取出两个数x,y将|x-y|放入集合中 (|x-y|不存在集合中)不能操作则输 最终游戏结束的标志是无法取出两个数字,他们的差值不在序列中.也就是说,最终状态是一个首项等于公差的等差序列.求出这个等差数列的项数-n,就是游戏进行的回合. 所以我们要先求出首项.设首项为d,接下来就是d+d,d+2d-.后面几项都是首项的倍数

Codeforces - 346A - Alice and Bob - 简单数论

http://codeforces.com/problemset/problem/346/A 观察了一下,猜测和他们的最大公因数有关,除以最大公因数前后结果是不会变的. 那么怎么证明一定是有n轮呢?我猜就是因为现在至少有几个是互质的,所以总是可以构造出1?具体怎么证明呢?还是看看别人的思路吧-- 首先最终停止的状态一定是一个等差数列,这个是毫无疑问的.设首项为d,那么肯定停止于d,2d,3d,...,n,那么很显然d就是他们的最大公因数啊--对哦?! #include<bits/stdc++.h

【codeforces 718E】E. Matvey&#39;s Birthday

题目大意&链接: http://codeforces.com/problemset/problem/718/E 给一个长为n(n<=100 000)的只包含‘a’~‘h’8个字符的字符串s.两个位置i,j(i!=j)存在一条边,当且仅当|i-j|==1或s[i]==s[j].求这个无向图的直径,以及直径数量. 题解:  命题1:任意位置之间距离不会大于15. 证明:对于任意两个位置i,j之间,其所经过每种字符不会超过2个(因为相同字符会连边),所以i,j经过节点至多为16,也就意味着边数至多

Codeforces 124A - The number of positions

题目链接:http://codeforces.com/problemset/problem/124/A Petr stands in line of n people, but he doesn't know exactly which position he occupies. He can say that there are no less than a people standing in front of him and no more than b people standing b

Codeforces 841D Leha and another game about graph - 差分

Leha plays a computer game, where is on each level is given a connected graph with n vertices and m edges. Graph can contain multiple edges, but can not contain self loops. Each vertex has an integer di, which can be equal to 0, 1 or  - 1. To pass th

Codeforces Round #286 (Div. 1) A. Mr. Kitayuta, the Treasure Hunter DP

链接: http://codeforces.com/problemset/problem/506/A 题意: 给出30000个岛,有n个宝石分布在上面,第一步到d位置,每次走的距离与上一步的差距不大于1,问走完一路最多捡到多少块宝石. 题解: 容易想到DP,dp[i][j]表示到达 i 处,现在步长为 j 时最多收集到的财富,转移也不难,cnt[i]表示 i 处的财富. dp[i+step-1] = max(dp[i+step-1],dp[i][j]+cnt[i+step+1]) dp[i+st