Codeforces Round #594 (Div. 2) D1

D1. The World Is Just a Programming Task (Easy Version)

  • 思路:当时读错题了 没理解到题意 在那一直瞎写 暴力随便搞搞过了

  • AC代码


#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
typedef long long ll;
typedef unsigned long long ull;
using namespace std;

ll mult_mod(ll x, ll y, ll mod){
    return (x * y - (ll)(x / (long double)mod * y + 1e-3) * mod + mod) % mod;
}

ll pow_mod(ll a, ll b, ll p){
    ll res = 1;
    while (b){
        if (b & 1)
            res = mult_mod(res, a, p);
        a = mult_mod(a, a, p);
        b >>= 1;
    }
    return res % p;
}

ll gcd(ll a, ll b){
    return b ? gcd(b, a % b) : a;
}

int n, res, ans, cnt1, cnt2, pos1, pos2;
string s;

int calc() {
    res = 0;
    int cnt = 0;
    for (int i = 0; i < s.length(); i ++ ){
        if (s[i] == '(')
            cnt ++ ;
        else
            cnt -- ;
        if (cnt == 0)
            res ++ ;
        if (cnt < 0){
            res = 0;
            cnt = 0;
        }
    }
    if (cnt > 0)
        res ++ ;
    return res;
}

int main(){
#ifndef ONLINE_JUDGE
    freopen("my_in.txt", "r", stdin);
#endif
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    ans = -1;
    cin >> n >> s;
    for (int i = 0; i < s.length(); i ++ ){
        if (s[i] == '(')
            cnt1 ++ ;
        else
            cnt2 ++ ;
    }
    if (cnt1 != cnt2){
        cout << "0
1 1
";
        return 0;
    }
    ans = calc();
    for (int i = 0; i < s.size(); i ++ ){
        for (int j = i; j < s.size(); j ++ ){
            if (s[i] != s[j]){
                swap(s[i], s[j]);
                res = calc();
                if (res > ans){
                    ans = res;
                    pos1 = i;
                    pos2 = j;
                }
                swap(s[i], s[j]);
            }
        }
    }
    cout << ans << "
" << pos1 + 1 << " " << pos2 + 1 << "
";
    return 0;
}
原文地址:https://www.cnblogs.com/Misuchii/p/11733232.html