cf之 前缀和差分

给定一个n×n的WB矩阵,给定一个k∗k的能把B变成W的橡皮擦,求橡皮擦作用一次后,全为W的行、列总数最大值

连接:http://codeforces.com/contest/1200/problem/D

#include<bits/stdc++.h>
using namespace std;
int A[2004][2004];
char C[2004][2004];
int B[2004][2004];
int E[2004][2004];
int  F[2004][2004];
int n,k;
int main()
{
    scanf("%d%d",&n,&k);
    int ans=0;
    for(int i=1; i<=n; i++)
    {
        getchar();
        for(int j=1; j<=n; j++)
        {
            C[i][j]=getchar();
            A[i][j]=A[i][j-1]+(C[i][j]==‘B‘);
        }
        if(A[i][n]==0)ans++;
    }
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            B[j][i]=B[j-1][i]+(C[j][i]==‘B‘);
        }
        if(B[n][i]==0)ans++;
    }
    //cout<<ans<<‘\n‘;
    int t=0,ma=0;
    for(int i=1;i<=n;i++){
        for(int j=1;j+k-1<=n;j++){
            E[i][j]=E[i-1][j]+(A[i][j+k-1]-A[i][j-1]==A[i][n]&&A[i][n]);
        }
    }
    for(int i=1;i<=n;i++){
        for(int j=1;j+k-1<=n;j++){
            F[j][i]=F[j][i-1]+(B[j+k-1][i]-B[j-1][i]==B[n][i]&&B[n][i]);
        }
    }
    for(int i=1;i+k-1<=n;i++){
        for(int j=1;j+k-1<=n;j++){
            t=E[i+k-1][j]-E[i-1][j];
            t+=F[i][j+k-1]-F[i][j-1];
            ma=max(t,ma);
        }

    }
    cout<<ans+ma<<‘\n‘;

}

  

原文地址:https://www.cnblogs.com/hgangang/p/11830719.html

时间: 2024-08-30 15:39:16

cf之 前缀和差分的相关文章

Codeforces 479E Riding in a Lift:前缀和/差分优化dp

题目链接:http://codeforces.com/problemset/problem/479/E 题意: 有一栋n层的房子. 还有一个无聊的人在玩电梯,每次玩电梯都会从某一层坐到另外一层. 他初始在a层,然后要玩k次电梯. 这栋楼里还有一个神秘实验室,在b层. 这让他每次坐电梯受到了限制: 当前在x层,然后要坐到y层,则必须满足|x-y|<|x-b| 问你共有多少种坐电梯的方案. 题解: 表示状态: dp[i][j] = numbers 表示当前在第i层,已经坐了j次电梯,此时的方案数.

HDU-6514 Monitor(二维前缀和+差分)

http://acm.hdu.edu.cn/showproblem.php?pid=6514 Problem Description Xiaoteng has a large area of land for growing crops, and the land can be seen as a rectangle of n×m. But recently Xiaoteng found that his crops were often stolen by a group of people,

Starting a Scenic Railroad Service(前缀和+差分)

Starting a Scenic Railroad Service 时间限制: 2 Sec  内存限制: 128 MB提交: 59  解决: 21[提交] [状态] [讨论版] [命题人:admin] 题目描述 Jim, working for a railroad company, is responsible for planning a new tourist train service. He is sure that the train route along a scenic va

前缀和&amp;差分

一:差分数组概念 一.差分数组的定义及用途 1.定义:对于已知有n个元素的数列d,建立记录它每项与前一项差值的差分数组f:显然,f[1]=d[1]-0=d[1];对于整数i∈[2,n],我们让f[i]=d[i]-d[i-1].//f[i]数组为差分数组,d[i]数组为原数组 2.简单性质:(1)计算数列各项的值:观察d[2]=f[1]+f[2]=d[1]+d[2]-d[1]=d[2]可知,d[i]=f[i]的前缀和.(2)计算数列每一项的前缀和:第i项的前缀和即为数列前i项的和,那么推导可知 /

CF 19E Fairy——树上差分

题目:http://codeforces.com/contest/19/problem/E 去掉一条边,使无向图变成二分图. 该边应该被所有奇环经过,且不被偶环经过. 因为一条非树边一定只在一个环里.所以一条既被所有奇环经过又被偶环经过的边是树边.如果把它去掉,将无法处理包含它的那个偶环的非树边和包含它的某个奇环的非树边加上两段树边所构成的奇环. 找这样的边,弄一个边上的树上差分就行了. 可以模仿kruscal用并查集弄一个生成树.不过dfs其实也行. 注意图可能不连通. #include<io

二维前缀和差分+离散化

/* 二维前缀和求法 a[i][j]+=a[i][j-1]+a[i-1][j]-a[i-1][j-1]; 构建前缀和 int n,m; scanf("%d%d",&n,&m); for(int i=1;i<=n;i++){//初始化 for(int j=1;j<=m;j++){ int x; scanf("%d",&x); a[i][j]+=a[i][j-1]+a[i-1][j]-a[i-1][j-1]+x; } } 输入nm 以

前缀和和差分模板(AcWing 795-798)

前缀和分一维前缀和和二维前缀和,前缀和可以帮我们快速统计一段范围内的合. 需要简单的理解 一维前缀和 —— 模板题 AcWing 795. 前缀和S[i] = a[1] + a[2] + ... a[i]:a[l] + ... + a[r] = S[r] - S[l - 1]: 二维前缀和 —— 模板题 AcWing 796. 子矩阵的和S[i, j] = 第i行j列格子左上部分所有元素的和以(x1, y1)为左上角,(x2, y2)为右下角的子矩阵的和为:S[x2, y2] - S[x1 -

1926: [Sdoi2010]粟粟的书架

1926: [Sdoi2010]粟粟的书架 Time Limit: 30 Sec  Memory Limit: 552 MBSubmit: 807  Solved: 321[Submit][Status][Discuss] Description 幸福幼儿园 B29 班的粟粟是一个聪明机灵.乖巧可爱的小朋友,她的爱好是画画和读书,尤其喜欢 Thomas H. Co rmen 的文章.粟粟家中有一个 R行C 列的巨型书架,书架的每一个位置都摆有一本书,上数第i 行.左数第j 列 摆放的书有Pi,j

HDU4609 3-idiots(母函数 + FFT)

题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=4609 Description King OMeGa catched three men who had been streaking in the street. Looking as idiots though, the three men insisted that it was a kind of performance art, and begged the king to fre