CodeForces - 3C (暴力)

题目链接

D. Least Cost Bracket Sequence

time limit per test

1 second

memory limit per test

64 megabytes

input

standard input

output

standard 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

Copy

(??)
1 2
2 8

output

Copy

4
()()

AC代码:

#include<cstdio>
using namespace std;
char e[4][4];
inline int check(char ch){
    int cnt = 0;
    bool flag1, flag2;
    for(int i = 0; i < 3; i++){
        flag1 = false, flag2 = false;
        for(int j = 0; j < 3; j++){
            if(e[i][j] != ch) flag1 = true;
            if(e[j][i] != ch) flag2 = true;
        }
        if(!flag1) cnt++;
        if(!flag2) cnt++;
    }
    flag1 = false, flag2 = false;
    for(int i = 0; i < 3; i++){
        if(e[i][i] != ch) flag1 = true;
        if(e[i][2 - i] != ch) flag2 = true;
    }
    if(!flag1) cnt++;
    if(!flag2) cnt++;
   // printf("%d\n",cnt);
    return cnt;
}
int main(){
    #ifdef ONLINE_JUDGE
    #else
        freopen("in.txt", "r", stdin);
    #endif // ONLINE_JUDGE
    for(int i = 0; i < 3; i++){
        scanf("%s", e[i]);
    }
    int a = 0, b = 0, c = 0;
    for(int i = 0; i < 3; i++){
        for(int j = 0; j < 3; j++){
            if(e[i][j] == 'X') a++;
            else if(e[i][j] == '0') b++;
            else if(e[i][j] == '.') c++;
        }
    }
    int ans1 = check('X');
    int ans2 = check('0');
    if(b > a || (a - b) > 1 || (ans1 && ans2)){
        printf("illegal\n");
        return 0;
    }
    if(ans1 >= 1 && ans2 == 0){
        if(a == b) printf("illegal");
        else printf("the first player won\n");
    }
    else if(ans1 == 0 && ans2 >= 1){
        if(a == b + 1) printf("illegal\n");
        else printf("the second player won\n");
    }
    else{
        if(a == b) printf("first\n");
        else if(a == b + 1){
            if(c == 0) printf("draw\n");
            else printf("second\n");
        }
        else printf("illegal\n");
    }
    return 0;
}

 

### Codeforces Problem 976C Solution in Python For solving problem 976C on Codeforces using Python, efficiency becomes a critical factor due to strict time limits aimed at distinguishing between efficient and less efficient solutions[^1]. Given these constraints, it is advisable to focus on optimizing algorithms and choosing appropriate data structures. The provided code snippet offers insight into handling string manipulation problems efficiently by customizing comparison logic for sorting elements based on specific criteria[^2]. However, for addressing problem 976C specifically, which involves determining the winner (&#39;A&#39; or &#39;B&#39;) based on frequency counts within given inputs, one can adapt similar principles of optimization but tailored towards counting occurrences directly as shown below: ```python from collections import Counter def determine_winner(): for _ in range(int(input())): count_map = Counter(input().strip()) result = "A" if count_map[&#39;A&#39;] > count_map[&#39;B&#39;] else "B" print(result) determine_winner() ``` This approach leverages `Counter` from Python’s built-in `collections` module to quickly tally up instances of &#39;A&#39; versus &#39;B&#39;. By iterating over multiple test cases through a loop defined by user input, this method ensures that comparisons are made accurately while maintaining performance standards required under tight computational resources[^3]. To further enhance execution speed when working with Python, consider submitting codes via platforms like PyPy instead of traditional interpreters whenever possible since they offer better runtime efficiencies especially important during competitive programming contests where milliseconds matter significantly.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kunsir_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值