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. Application.DoEvents()的作用

    记得第一次使用Application.DoEvents()是为了在加载大量数据时能够有一个数据加载的提示,不至于系统出现假死的现象,当时也没有深入的去研究他的原理是怎样的,结果在很多地方都用上了App ...

  2. Linux常用使用技巧

    一.如何实时查看linux下的日志 cat /var/log/*.log 如果日志在更新,如何实时查看 tail -f /var/log/messages 还可以使用 watch -d -n 1 ca ...

  3. HTTP Basic Authentication认证的各种语言 后台用的

    访问需要HTTP Basic Authentication认证的资源的各种语言的实现 无聊想调用下嘀咕的api的时候,发现需要HTTP Basic Authentication,就看了下. 什么是HT ...

  4. 大数据时代之hadoop(六):hadoop 生态圈(pig,hive,hbase,ZooKeeper,Sqoop)

    hadoop是有apache基金会所开发的分布式系统基础架构,其主要提供了两方面的功能:分布式存储和分布式计算. 其中分布式存储是分布式计算的基础,在hadoop的实现里面,提供了分布式存储的接口,并 ...

  5. 详解Bootstrap 定义按钮的样式(CSS)

    以下样式可用于<a>, <button>, 或 <input> 元素上: 更多关于Bootstrap 定义CSS样式的可查看:http://v2.bootcss.c ...

  6. java 操作redis

    使用Java操作Redis需要jedis-2.1.0.jar,如果需要使用Redis连接池的话,还需commons-pool-1.5.4.jar package com.test; import ja ...

  7. hdu_5877_Weak Pair(离散+DFS+树状数组)

    题目链接:hdu_5877_Weak Pair 题意: 给你一棵树,让你找有多少对满足那两个条件的weak pair 题解: 有人用Treap,我不会,然后我用树状数组+离散来替代Treap,用DFS ...

  8. C程序浅议

    文件FILE是程序设计中的一个重要概念.所谓“文件”一般是指存储在外部介质上的数据的集合.操作系统是以文件为单位对数据进行管理的,而文件是以文件名为标识的.操作系统对文件实行“按名存取”. C语言把文 ...

  9. 转 gl_VertexID的含义

    #version //layout(location = 0) in vec4 VERTEX; uniform mat4 MODEL_MATRIX; uniform mat4 VIEW_MATRIX; ...

  10. Java常用集合类(1)

    一.HashMap 参考文章: http://yikun.github.io/2015/04/01/Java-HashMap%E5%B7%A5%E4%BD%9C%E5%8E%9F%E7%90%86%E ...