Codeforces Round #594 (Div. 2) D1 - The World Is Just a Programming Task (贪心)

 思路:枚举换的位置i,j 然后我们要先判断改序列能否完全匹配 如果可以 那我们就需要把差值最大的位置换过来 然后直接判断就行

#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
const double eps = 1e-6;
const int N = 1e5+7;
typedef long long ll;
const ll mod = 1e9+7;
using namespace std;
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    int n; string s;
     cin>>n; cin>>s;
     int ans=0;
     int l,r;
     l=r=0;
     for(int i=0;i<n;i++)
         for(int j=i;j<n;j++){
             swap(s[i],s[j]);
             int cnt=0;
             int minn=0;
             for(int k=0;k<n;k++){
                 if(s[k]=='(') cnt++;
                else cnt--;
                minn=min(minn,cnt);    
            }
            int res=0;
            if(cnt==0){
                for(int k=0;k<n;k++){
                    if(s[k]=='(') cnt++;
                    else cnt--;
                    if(cnt==minn) res++;
                }
            }
            if(res>ans){
                ans=res;
                l=i; r=j;
            }
            swap(s[i],s[j]);
         }
        cout<<ans<<endl;
        cout<<l+1<<" "<<r+1<<endl;
    return 0;
} 
View Code

猜你喜欢

转载自www.cnblogs.com/wmj6/p/11709992.html