# TopCoder——SRM 521 DIV 2

Score 250. 枚举分割点，取最小值. ( 比赛时挂掉了，好郁闷，思考问题还是欠缺严谨呐 )

class RedAndGreen
{
public:
int minPaints(string row) {
int Len = (int)row.length();
int ans = 0x7fffffff;
for(int i = 0; i <= Len; ++i) {
int tot = 0;
for(int j = 0; j < i; ++j)
if( row[j] == 'G' ) ++tot;
for(int j = i; j < Len; ++j)
if( row[j] == 'R' ) ++tot;
ans = min(ans, tot);
}
return ans;
}

};

Score 500. 当右括号比左括号数目多的时候，表明需要添加二者差值个左括号，然后重新计数，最后加上需要添加的右括号数目.

class MissingParentheses
{
public:

int countCorrections(string par) {
int Len = (int)par.length();
int ans = 0, L = 0, R = 0;
for(int i = 0; i < Len; ++i) {
if( par[i] == '(' ) ++L;
else ++R;

if( R > L ) {
ans += (R - L);
L = R = 0;
}
}
return ans + (L - R);
}

};

Score 1000. 需要离散化预处理，枚举合法的矩形<(x, y), (x + n, y + n)>，不知道枚举左下脚标有什么要求 (x,y)，猜测会在(_x, _y) 的基础上，可能会是

1. 保持不变

2. 增量为 +1  /  -1

3. 增量为 +n  /  -n

4. 增量为 + ( n + 1 )   /  - ( n + 1 )

class SquaredSubsets
{
public:
bool inSide(int &x, int &y, int &_x, int &_y, int &n) {
return (x >= _x) && (x <= _x + n) && (y >= _y) && (y <= _y + n);
}

long long countSubsets(int n, vector <int> x, vector <int> y) {
int Len = (int)x.size();
n <<= 0x1;
for(int i = 0; i < Len; ++i) x[i] <<= 0x1, y[i] <<= 0x1;

vector<int> _x, _y;
for(int i = 0; i < Len; ++i) {
_x.push_back( x[i] );
_x.push_back( x[i] + 1 );
_x.push_back( x[i] + n );
_x.push_back( x[i] + 1 + n );
_x.push_back( x[i] - 1 );
_x.push_back( x[i] - n );
_x.push_back( x[i] - 1 - n );

_y.push_back( y[i] );
_y.push_back( y[i] + 1 );
_y.push_back( y[i] + n );
_y.push_back( y[i] + 1 + n );
_y.push_back( y[i] - 1 );
_y.push_back( y[i] - n );
_y.push_back( y[i] - 1 - n );
}

int _Len = (int)_x.size();

set<long long> d;

long long v;
for(int i = 0, j, k, tx, ty; i < _Len; ++i) {
for(j = 0; j < _Len; ++j) {
tx = _x[i], ty = _y[j];
v = 0;
for(k = 0; k < Len; ++k)
if( inSide(x[k], y[k], tx, ty, n) ) v |= (1LL << k);
if( v ) d.insert(v);
}
}

return (long long)d.size();
}
};

• 本文已收录于以下专栏：

举报原因： 您举报文章：TopCoder——SRM 521 DIV 2 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)