http://codeforces.com/contest/1248/problem/D1
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int>pi;
ll beauty(string &s)
{
int n = s.size();
ll veg = 1;
ll ob = 0, pc = 0;
for(int i = 0; i < n; i++)
{
if(s[i] == '(')
ob++;
else
ob--;
if(ob == pc)
{
veg++;
}
else if(ob < pc)
{
pc = ob;
veg = 1;
}
}
if(ob != 0)
return 0;
else if(pc == 0)
return veg-1;
else
return veg;
}
int main()
{
ios_base::sync_with_stdio(false); cin.tie(NULL); cout.precision(20);
int n;
cin >> n;
string s;
cin >> s;
ll res = beauty(s), ib = 0, jb = 0;
for(int i = 0 ; i< n; i++)
{
for(int j = i + 1; j < n; j++)
{
swap(s[i] , s[j]);
ll c = beauty(s);
swap(s[i],s[j]);
if(c > res)
{
res = c;
ib = i;
jb = j;
}
}
}
cout << res << endl;
cout << ib +1 << " "<< jb + 1 <<endl;
}

本文解析了Codeforces竞赛中编号为1248的D1题,通过定义字符串的美丽值并使用C++实现了一个算法,该算法计算字符串的最大美丽值,并找到交换两个字符以获得最大美丽值的位置。
&spm=1001.2101.3001.5002&articleId=102727885&d=1&t=3&u=8f74a3dee8274747ba50e0d8f3c91ce3)
423

被折叠的 条评论
为什么被折叠?



