http://codeforces.com/contest/680/problem/E

题目大意:给你一个n*n的图,然后图上的 . (我们下面都叫做‘点’)表示可以走,X表示不能走,你有如下的操作,每次你可以选择一个k*k的框,把其中的所有的X都变成 ‘点’,问在该操作后点相连的数目最多是多少?

没看别人代码和思路自己思考并优化了好久,于是敲了两个多小时。。。还是代码能力太差了

思路:当然最最单纯的做法就是O(n^4)。为了优化,起初想用二维树状数组+并查集的,结果发现窗口中的‘点‘’容易造成重复计算,于是换了种方法,每次事先统计‘点’和X,然后再每次暴力枚举边就好了。复杂度O(n*n*4*4*k*log)

//看看会不会爆int!数组会不会少了一维!
//取物问题一定要小心先手胜利的条件
#include <bits/stdc++.h>
using namespace std;
#define LL long long
#define ALL(a) a.begin(), a.end()
#define pb push_back
#define mk make_pair
#define fi first
#define se second
#define haha printf("haha\n")
const int maxn = + ;
int par[maxn * maxn], cnt[maxn * maxn];
bool vis[maxn * maxn];
int n, k;
char ch[maxn][maxn];
int dx[] = {, , -, };
int dy[] = {, , , -}; int bfs(int x, int y, int fa){
queue<pair<int, int> > que;
que.push(mk(x, y));
int cnt = ;
while (!que.empty()){
pair<int, int> u = que.front(); que.pop();
for (int i = ; i < ; i++){
int nx = u.fi + dx[i], ny = u.se + dy[i];
if (nx <= || ny <= || nx > n || ny > n) continue;
if (ch[nx][ny] != '.' || vis[nx * n + ny]) continue;
vis[nx * n + ny] = true;
cnt++;
par[nx * n + ny] = fa;
que.push(mk(nx, ny));
}
}
return cnt;
} int pointcnt[maxn * maxn];
inline int init(int i){
for (int x = ; x <= n; x++)
for (int y = ; y <= n; y++)
pointcnt[x * n + y] = vis[x * n + y] = ; int cntx = ;
for (int x = i; x <= k + i - ; x++)///take care of border
for (int y = ; y < k; y++){///最后一列先不放进去
if (ch[x][y] == '.') pointcnt[par[x * n + y]]++;
if (ch[x][y] == 'X') cntx++;
}
return cntx;
} int solve(){
int ans = ;
for (int i = ; i <= n - k + ; i++){
int cntx = init(i);
for (int y = ; y <= n - k + ; y++){///四个方向的遍历
///clear上一列的,insert目前列的
for (int x = i; x <= i + k - ; x++){
if (ch[x][y - ] == '.') pointcnt[par[x * n + y - ]]--;
else if (ch[x][y - ] == 'X') cntx--;
if (ch[x][y + k - ] == '.') pointcnt[par[x * n + y + k - ]]++;
else if (ch[x][y + k - ] == 'X') cntx++;
}
int pin = ;
vector<int> v;
///左右列, 变行
for (int x = i; x <= i + k - ; x++){
for (int j = ; j < ; j++){
int nx = x + dx[j], ny = y + dy[j];
if (nx <= || ny <= || nx > n || ny > n) continue;
if (ch[nx][ny] != '.') continue;
int pos = par[nx * n + ny];
if (vis[pos]) continue;
vis[pos] = true; v.push_back(pos); pin += pointcnt[pos];
}
for (int j = ; j < ; j++){
int nx = x + dx[j], ny = y + k - + dy[j];
if (nx <= || ny <= || nx > n || ny > n) continue;
if (ch[nx][ny] != '.') continue;
int pos = par[nx * n + ny];
if (vis[pos]) continue;
vis[pos] = true; v.push_back(pos); pin += pointcnt[pos];
}
}
///上下行,变列
for (int yy = y; yy <= y + k - ; yy++){
for (int j = ; j < ; j++){
int nx = i + dx[j], ny = yy + dy[j];
if (nx <= || ny <= || nx > n || ny > n) continue;
if (ch[nx][ny] != '.') continue;
int pos = par[nx * n + ny];
if (vis[pos]) continue;
vis[pos] = true; v.push_back(pos); pin += pointcnt[pos];
}
for (int j = ; j < ; j++){
int nx = i + k - + dx[j], ny = yy + dy[j];
if (nx <= || ny <= || nx > n || ny > n) continue;
if (ch[nx][ny] != '.') continue;
int pos = par[nx * n + ny];
if (vis[pos]) continue;
vis[pos] = true; v.push_back(pos); pin += pointcnt[pos];
}
}
int len = v.size();
int tmp = k * k - pin;
for (int i = ; i < len; i++){
tmp += cnt[v[i]];
vis[v[i]] = false;
}
ans = max(ans, tmp);
}
}
return ans;
} int main(){
scanf("%d%d", &n, &k);
for (int i = ; i <= n; i++) scanf("%s", ch[i] + );
for (int i = ; i <= n; i++)
for (int j = ; j <= n; j++)
par[i * n + j] = i * n + j;
for (int i = ; i <= n; i++){
for (int j = ; j <= n; j++){
if (ch[i][j] == '.' && !vis[i * n + j]){
vis[i * n + j] = true;
cnt[i * n + j] = bfs(i, j, i * n + j);
}
}
}
printf("%d\n", solve());
return ;
}

然后敲完之后跑出来是2600ms,然后有人是300ms左右就过了的,具体思路差不多,大概就是我初始化+vector多了一堆没用的东西导致复杂度变得很高了。

不过我也看了一下他们的代码发现,果然差距还是好大

代码来源:http://blog.csdn.net/Fsss_7/article/details/51706546#reply

#include<bits/stdc++.h>
using namespace std;
const int N=;
typedef long long ll;
typedef unsigned long long ull;
char s[N];
int n,a[*N][*N],b[N][N];
int g=,q[N*N],f[N*N],xm[N*N],ym[N*N],xx[N*N],yx[N*N]; ///这里应该就是单纯的跑一下,然后知道目前联通块的边界
void dfs(int x,int y) {
b[x][y]=g;f[g]++;///标记是第几个联通快,和记录着联通块的数目
xm[g]=min(xm[g],x);xx[g]=max(xx[g],x);
ym[g]=min(ym[g],y);yx[g]=max(yx[g],y);
if (x>&&b[x-][y]==-) dfs(x-,y);
if (x<n&&b[x+][y]==-) dfs(x+,y);
if (y>&&b[x][y-]==-) dfs(x,y-);
if (y<n&&b[x][y+]==-) dfs(x,y+); } int main(){
int i,j,h,k,tot,ans=;
scanf("%d%d", &n, &k);
for (i=;i<=n;i++) {
scanf("%s", s);
for (j=;j<=n;j++)
if (s[j-]=='.') b[i][j]=-;///存在点
else {
b[i][j]=;///不是点
a[i][j]+=;a[i][j+k]+=-;///然后标记障碍物所对应的方框
a[i+k][j]+=-;a[i+k][j+k]+=;
}
}
memset(xm,0x7f,sizeof(xm));
memset(ym,0x7f,sizeof(ym));
for (i=;i<=n;i++)
for (j=;j<=n;j++)
if (b[i][j]==-) {
g++,dfs(i,j);
if (xx[g]-xm[g]+<=k&&yx[g]-ym[g]+<=k) {///边界超过K的就不会被k*k包围啦,然后对边界进行处理
a[xx[g]][yx[g]]+=f[g]; a[xm[g]+k][ym[g]+k]+=f[g];//左上右下
a[xx[g]][ym[g]+k]-=f[g]; a[xm[g]+k][yx[g]]-=f[g];//左下右上
}
} for (int i = ; i <= n; i++){
for (int j = ; j <= n; j++){
printf("%d ", a[i][j]);
}
cout << endl;
} for (i=;i<=n;i++)///a[i][j]是统计(i,j)~(i-k, j-k)这个矩形中,只在内部的‘点’和X的数目
for (j=;j<=n;j++)
a[i][j]+= a[i][j-] + a[i-][j] - a[i-][j-];
for (i=k;i<=n;i++)
for (j=k;j<=n;j++) {///枚举(k,k)开始的端点
tot=;
for (h=j-k+;h<=j;h++)///左列
if (!q[b[i-k][h]]) q[b[i-k][h]]=,tot+=f[b[i-k][h]];///用q标记该连通块是否被访问过,然后b所记录的是第几个块的标号
for (h=j-k+;h<=j;h++)///右列
if (!q[b[i+][h]]) q[b[i+][h]]=,tot+=f[b[i+][h]];
for (h=i-k+;h<=i;h++)///上行
if (!q[b[h][j-k]]) q[b[h][j-k]]=,tot+=f[b[h][j-k]];
for (h=i-k+;h<=i;h++)///下行
if (!q[b[h][j+]]) q[b[h][j+]]=,tot+=f[b[h][j+]];
ans=max(ans,tot+a[i][j]);
printf("tot = %d a[%d][%d] = %d\n", tot, i, j, a[i][j]);
///printf("a[%d][%d] = %d\n", i, j, a[i][j]);
///初始化
for (h=j-k+;h<=j;h++) q[b[i-k][h]]=;
for (h=j-k+;h<=j;h++) q[b[i+][h]]=;
for (h=i-k+;h<=i;h++) q[b[h][j-k]]=;
for (h=i-k+;h<=i;h++) q[b[h][j+]]=;
}
printf("%d\n", ans);
return ;
}

并查集+bfs+暴力滑窗 Codeforces Round #356 (Div. 2) E的更多相关文章

  1. Codeforces Round #356 (Div. 1) D. Bear and Chase 暴力

    D. Bear and Chase 题目连接: http://codeforces.com/contest/679/problem/D Description Bearland has n citie ...

  2. Codeforces Round #356 (Div. 2)-A

    A. Bear and Five Cards 题目链接:http://codeforces.com/contest/680/problem/A A little bear Limak plays a ...

  3. Codeforces Round #356 (Div. 2)-B

    B. Bear and Finding Criminals 链接:http://codeforces.com/contest/680/problem/B There are n cities in B ...

  4. Codeforces Round #356 (Div. 2) C. Bear and Prime 100(转)

    C. Bear and Prime 100 time limit per test 1 second memory limit per test 256 megabytes input standar ...

  5. Codeforces Round #356 (Div. 2)B. Bear and Finding Criminals(水题)

    B. Bear and Finding Criminals time limit per test 2 seconds memory limit per test 256 megabytes inpu ...

  6. Codeforces Round #356 (Div. 2)A. Bear and Five Cards(简单模拟)

    A. Bear and Five Cards time limit per test 2 seconds memory limit per test 256 megabytes input stand ...

  7. dfs Codeforces Round #356 (Div. 2) D

    http://codeforces.com/contest/680/problem/D 题目大意:给你一个大小为X的空间(X<=m),在该空间内,我们要尽量的放一个体积为a*a*a的立方体,且每 ...

  8. Codeforces Round #356 (Div. 1) C. Bear and Square Grid

    C. Bear and Square Grid time limit per test 3 seconds memory limit per test 256 megabytes input stan ...

  9. Codeforces Round #356 (Div. 2) E. Bear and Square Grid 滑块

    E. Bear and Square Grid 题目连接: http://www.codeforces.com/contest/680/problem/E Description You have a ...

随机推荐

  1. 转:Web网站性能测试分析及调优实例

    1.背景 前段时间,性能测试团队经历了一个规模较大的门户网站的性能优化工作,该网站的开发和合作涉及多个组织和部门,而且网站的重要性不言而喻,同时上线时间非常紧迫,关注度也很高,所以对于整个团队的压力也 ...

  2. android -- 加载gif 防止oom

    项目中涉及到gif图片的展示,原来使用的是gifview,但是当频繁的,加载过大图片的时候会出现OOM的问题,后来去github上面找相关的库: https://github.com/koral--/ ...

  3. android ScrollView中嵌套listview listview可点击处理,可展开

    public class MyListView extends ListView { public MyListView(Context context, AttributeSet attrs, in ...

  4. JavaScript的正则表达式

    1.文本框只能输入数字(不包括小数点) <input onkeyup="this.value=this.value.replace(/\D/g,'')" onafterpas ...

  5. CF 602C The Two Routes(dij+邻接矩阵)

    ( ̄▽ ̄)" #include<iostream> #include<cstdio> #include<cmath> #include<algori ...

  6. MongoDB高级操作

    参考MongoDB菜鸟教程 一.$type操作符 MongoDB 中可以使用的类型如下表所示: 类型 数字 备注 Double 1   String 2   Object 3   Array 4   ...

  7. webpack-hot-middleware 用于 livereload

    https://github.com/glenjamin/webpack-hot-middleware Webpack hot reloading using only webpack-dev-mid ...

  8. Chapter 16_2 继承

    类也是对象,所有它们也可以从其他类获得方法.这就是“继承”,可以在Lua中表示: Account = { balance = } function Account:new(o) o = o or {} ...

  9. 图片拉伸:resizableImageWithCapInsets

    iOS 5.0 在iOS 5.0中,UIImage有一个新方法可以处理图片的拉伸问题 - (UIImage *)resizableImageWithCapInsets:(UIEdgeInsets)ca ...

  10. java的String类型为什么是final

    (转自:http://www.cnblogs.com/ikuman/archive/2013/08/27/3284410.html) 最佳答案: 主要是为了“效率” 和 “安全性” 的缘故.若 Str ...