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 ...
随机推荐
- 2010 SD - ICPC D - Emergency
D - Emergency Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu Submit ...
- 【Vijos】lxhgww的奇思妙想(长链剖分)
题面 给定一棵树,每次询问一个点的\(k\)次祖先,强制在线. Vijos 题解 长链剖分. 链接暂时咕咕咕了. 现在可以戳链接看题解了 #include<iostream> #inclu ...
- [TJOI2015]线性代数(最小割)
题目描述 给出一个N*N的矩阵B和一个1*N的矩阵C.求出一个1*N的01矩阵A.使得 D=(A*B-C)*A^T最大.其中A^T为A的转置.输出D 题解 观察上面那个式子发现,当一个bij有贡献时当 ...
- Python数据结构之栈的实现
一图胜千言,看图! 代码code: #coding:utf-8 #常见数据结构之栈的实现 class Stack(): #创建Stack类 def __init__(st,size): st.stac ...
- webpack入门(六) API in modules
A quick summary of all methods and variables available in code compiled with webpack. 用webpack编译的一些变 ...
- javascript之奇淫技巧
最近准备面试,复习一下javascript,整理了一些javascript的奇淫技巧~ //为兼容ie的模拟Object.keys() Object.showkeys = function(obj) ...
- Codeforces Round #493 (Div. 2)
C - Convert to Ones 给你一个01串 x是反转任意子串的代价 y是将子串全部取相反的代价 问全部变成1的最小代价 两种可能 一种把1全部放到一边 然后把剩下的0变成1 要么把所有的 ...
- BZOJ 1912: [Apio2010]patrol 巡逻 (树的直径)(详解)
题目: https://www.lydsy.com/JudgeOnline/problem.php?id=1912 题解: 首先,显然当不加边的时候,遍历一棵树每条边都要经过两次.那么现在考虑k==1 ...
- Nginx宣布正式支持gRPC,附示例代码
原创 2018-03-20 薛命灯 聊聊架构 作者|Owen Garrett编辑|薛命灯 NGINX 官方博客正式宣布 NGINX 支持原生的 gPRC,现在就可以从代码仓库拉取快照版本.该特性将会被 ...
- Injection with CDI (Part I)
官方参考:http://docs.jboss.org/weld/reference/latest/en-US/html/index.html https://antoniogoncalves.org/ ...