ACM-ICPC 2018 南京赛区网络预赛 B The writing on the wall(思维)
https://nanti.jisuanke.com/t/30991
题意
一个n*m的方格矩阵,有的格子被涂成了黑色,问该矩阵中有多少个子矩阵,子矩阵不包含黑色格子。
分析
参考https://blog.csdn.net/Sirius_han/article/details/82313029
n=1e5,m=100。首先思考一下没有黑点的话,子矩阵总数怎么算。
现有长为L的大矩阵,对于固定高度h,其子矩阵的个数是这样计算的
for(int i=; i<=L; i++){
for(int j=i; j>; j--){
count+=h;
}
}
那么对于不同高度,只需要再加一维循环就好。
解决有黑点的问题,当存在黑点时
先看高为H(4)的子矩阵个数:以(4, 7)为右下角的高为H的子矩阵个数为3个,由L=4处再向左,就只能构成高为2的子矩阵了;
那么怎么该上边的代码才能得出答案呢?如下:
for(int i=; i<=H; i++){
for(int j=; j<=L; j++){
h=i;
for(int k=j; k>; k--){
h=min(h, i-p[k]);
count+=h;
}
}
}
//p[k]表示第k列中在i行上边的第一个黑点的位置,
那么维护p数组就是这题的重点了
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int b[][], up[];
int main(){
int T, cas=;
scanf("%d", &T);
while(T--){
int n, m, K;
scanf("%d%d%d", &n, &m, &K);
for(int i=; i<=n; i++){
for(int j=; j<=m; j++){
b[i][j]=;
up[j]=;
}
}
for(int i=; i<K; i++){
int x, y;
scanf("%d%d", &x, &y);
b[x][y]=;
}
ll ans=;
for(int i=; i<=n; i++){
for(int j=; j<=m; j++){
if(b[i][j]){
up[j]=i;
}
}
for(int j=; j<=m; j++){
ll minn=0x7f7f7f7f7f7f7f7f;
for(int k=j; k>; k--){
minn=min(minn, (ll)(i-up[k]));
ans+=minn;
}
}
}
printf("Case #%d: %lld\n", ++cas, ans);
}
return ;
}
- #include <bits/stdc++.h>
- using namespace std;
- typedef long long ll;
- int b[100010][110], up[110];
- int main(){
- int T, cas=0;
- scanf("%d", &T);
- while(T--){
- int n, m, K;
- scanf("%d%d%d", &n, &m, &K);
- for(int i=0; i<=n; i++){
- for(int j=0; j<=m; j++){
- b[i][j]=0;
- up[j]=0;
- }
- }
- for(int i=0; i<K; i++){
- int x, y;
- scanf("%d%d", &x, &y);
- b[x][y]=1;
- }
- ll ans=0;
- for(int i=1; i<=n; i++){
- for(int j=1; j<=m; j++){
- if(b[i][j]){
- up[j]=i;
- }
- }
- for(int j=1; j<=m; j++){
- ll minn=0x7f7f7f7f7f7f7f7f;
- for(int k=j; k>0; k--){
- minn=min(minn, (ll)(i-up[k]));
- ans+=minn;
- }
- }
- }
- printf("Case #%d: %lld\n", ++cas, ans);
- }
- return 0;
- }
ACM-ICPC 2018 南京赛区网络预赛 B The writing on the wall(思维)的更多相关文章
- ACM-ICPC 2018 南京赛区网络预赛 B. The writing on the wall
题目链接:https://nanti.jisuanke.com/t/30991 2000ms 262144K Feeling hungry, a cute hamster decides to o ...
- ACM-ICPC 2018 南京赛区网络预赛 B. The writing on the wall (暴力)
题意:一个n*m的方格矩阵,有的格子被涂成了黑色,问该矩阵中有多少个子矩阵,子矩阵不包含黑色格子; 思路:对于一个长为L, 高为H的无黑点矩阵中包含的高为H的子矩阵个数为L+(L-1)+(L-2)+. ...
- ACM-ICPC 2018 南京赛区网络预赛 J.sum
A square-free integer is an integer which is indivisible by any square number except 11. For example ...
- ACM-ICPC 2018 南京赛区网络预赛 E题
ACM-ICPC 2018 南京赛区网络预赛 E题 题目链接: https://nanti.jisuanke.com/t/30994 Dlsj is competing in a contest wi ...
- ACM-ICPC 2018 南京赛区网络预赛B
题目链接:https://nanti.jisuanke.com/t/30991 Feeling hungry, a cute hamster decides to order some take-aw ...
- 计蒜客 30999.Sum-筛无平方因数的数 (ACM-ICPC 2018 南京赛区网络预赛 J)
J. Sum 26.87% 1000ms 512000K A square-free integer is an integer which is indivisible by any squar ...
- 计蒜客 30996.Lpl and Energy-saving Lamps-线段树(区间满足条件最靠左的值) (ACM-ICPC 2018 南京赛区网络预赛 G)
G. Lpl and Energy-saving Lamps 42.07% 1000ms 65536K During tea-drinking, princess, amongst other t ...
- 计蒜客 30990.An Olympian Math Problem-数学公式题 (ACM-ICPC 2018 南京赛区网络预赛 A)
A. An Olympian Math Problem 54.28% 1000ms 65536K Alice, a student of grade 66, is thinking about a ...
- ACM-ICPC 2018 南京赛区网络预赛
轻轻松松也能拿到区域赛名额,CCPC真的好难 An Olympian Math Problem 问答 只看题面 54.76% 1000ms 65536K Alice, a student of g ...
随机推荐
- SSL加速卡调研的原因及背景
SSL加速卡调研的原因及背景 SSL加速卡调研的原因及背景 网络信息安全已经成为电子商务和网络信息业发展的一个瓶颈,安全套接层(SSL)协议能较好地解决安全处理问题,而SSL加速器有效地提高了网络安全 ...
- jsp配置
jsp.server.xml <Host name="localhost" appBase="webapps" unpackWARs="true ...
- vsftpd 安装与配置
下载安装vsftpd服务,db4用来支持文件数据库yum install -y vsftpd db4-utils ftp 建立宿主用户 vsftpduseradd -s /sbin/nologin - ...
- python3 文件和流
流: 打开文件: open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, ...
- Android 开发中 SQLite 数据库的使用
SQLite 介绍 SQLite 一个非常流行的嵌入式数据库,它支持 SQL 语言,并且只利用很少的内存就有很好的性能.此外它还是开源的,任何人都可以使用它.许多开源项目((Mozilla, PHP, ...
- [luogu3246][bzoj4540][HNOI2016]序列【莫队+单调栈】
题目描述 给定长度为n的序列:a1,a2,...,an,记为a[1:n].类似地,a[l:r](1<=l<=r<=N)是指序列:al,al+1,...,ar-1,ar.若1<= ...
- PHP-FPM安装报错解决
PHP源码安装 setenforce 0--------------------------------------------------------------------安装php时的报错che ...
- LOJ#6285. 数列分块入门 9
有点难..... 要求区间众数,所以我可以先把区间分块,然后我预处理出从第 i 块到第 j 块的众数,用dp[i][j]记录下来. 因为需要知道众数的num值, 所以我可以用一个vector来保存每个 ...
- A.01.03-模块的输入—模拟量输入
模拟量输入在使用过程中也十分常见,它在很多场合都应用到,但其用法又各有不同,下面列举一些常见的类型进行说明. 第一种为采用模拟口读取离散量的状态,如某开关可能有高.低.悬空三种状态均需能准确判断,这种 ...
- Building Microservices with Spring Boot and Apache Thrift. Part 2. Swifty services
http://bsideup.blogspot.com/2015/04/spring-boot-thrift-part2.html In previous article I showed y ...