首页 > 其他分享 >Codeforces Round #632 (Div. 2) / 1333C】- C. Eugene and an array

Codeforces Round #632 (Div. 2) / 1333C】- C. Eugene and an array

时间:2022-10-27 18:36:21浏览次数:65  
标签:632 elements Codeforces good cnt subarray array Eugene

题面

Eugene likes working with arrays. And today he needs your help in solving one challenging task.

An array cc is a subarray of an array bb if cc can be obtained from bb by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end.

Let's call a nonempty array good if for every nonempty subarray of this array, sum of the elements of this subarray is nonzero. For example, array [-1, 2, -3][−1,2,−3] is good, as all arrays [-1][−1], [-1, 2][−1,2], [-1, 2, -3][−1,2,−3], [2][2], [2, -3][2,−3], [-3][−3] have nonzero sums of elements. However, array [-1, 2, -1, -3][−1,2,−1,−3] isn't good, as his subarray [-1, 2, -1][−1,2,−1] has sum of elements equal to 00.

Help Eugene to calculate the number of nonempty good subarrays of a given array aa.

题意

给一个两个一维数组组成的矩阵,问其中用1组成的面积为k的矩阵有多少个

分析

枚举矩形的边长x,再统计每行每列的连续x个1的个数即可

统计个数的处理方式可以是输入时预处理f[i] = f[i - 1] + 1;
也可以是遍历到超过k就计数,不是1就归零

代码

#include <iostream>
#include <cstdio>
#include <cmath>
#define ll long long
using namespace std;
inline int read() {
	register int x = 0, f = 1;
	register char c = getchar();
	while (c < '0' || c > '9') {if (c == '-') f = -1; c = getchar();}
	while (c >= '0' && c <= '9') x = (x << 3) + (x << 1) + c - '0',c = getchar();
	return x * f;
}
const int MAXN = 4e4 + 1;
int n, m, k, cnt;
ll ans;
int a[MAXN], b[MAXN], fac[MAXN];
int main() {
	n = read(), m = read(), k = read();
	for (int i(1); i <= n; i++) a[i] = read();
	for (int i(1); i <= m; i++) b[i] = read();
	for (int i(1); i <= sqrt(k); i++){
		if (!(k % i)){
			fac[++cnt] = i;
			if (i * i == k) break;
			fac[++cnt] = k / i;
		}
	}
	for (int i(1); i <= cnt; i++){
		int row = fac[i], col = k / fac[i];
		int sum = 0, cnt_row = 0, cnt_col = 0;
		for (int j(1); j <= n; j++){
			if (a[j]) sum++;
			else sum = 0;
			if (sum >= row) cnt_row++;
		}
		sum = 0;
		for (int j(1); j <= m; j++){
			if (b[j]) sum++;
			else sum = 0;
			if (sum >= col) cnt_col++; 
		}
		ans += cnt_row * cnt_col;
	}
	printf("%lld\n", ans);
	return 0;
}

标签:632,elements,Codeforces,good,cnt,subarray,array,Eugene
From: https://www.cnblogs.com/cancers/p/16833262.html

相关文章

  • Leetcode第1822题:数组元素积的符号(Sign of product of an array)
    解题思路一个数组所有元素的乘积为正返回1,为零返回0,为负返回-1.变量pos统计正元素的个数,变量neg统计负元素的个数,遍历数组。遍历过程中如果有元素为零,直接返回0;遍历结束......
  • Codeforces Round #725 (Div. 3) ABC(双指针)F
    https://codeforces.com/contest/1538AB都没啥好说的,C卡了半天,F挺好写的,找到规律了就直接ac1300的题目卡半天,1500的题目写的顺顺利利,真呆啊我A.StoneGame#include<......
  • Codeforces Round #619 (Motarack's Birthday)
    题面DarkisgoingtoattendMotarack’sbirthday.DarkdecidedthatthegiftheisgoingtogivetoMotarackisanarrayaofnnon-negativeintegers.Darkcr......
  • LeetCode_Array_75. Sort Colors 颜色分类 (C++)
    目录​​1,题目描述​​​​2,思路​​​​3,代码【C++】​​1,题目描述Givenanarraywithnobjectscoloredred,whiteorblue,sortthemin-placesothatobjectsoft......
  • LeetCode_Array_73. Set Matrix Zeroes (C++)
    目录​​1,题目描述​​​​2,思路​​​​3,代码【C++】​​​​4,测试效果​​1,题目描述Givenamxnmatrix,ifanelementis0,setitsentirerowandcolumnto0.Do......
  • LeetCode_Array_64. Minimum Path Sum (C++)
    目录​​1,题目描述​​​​2,思路​​​​3,代码【C++】​​​​4,测试效果​​1,题目描述Givenamxngridfilledwithnon-negativenumbers,findapathfromtopleftt......
  • LeetCode_Array_63. Unique Paths II (C++)
    目录​​1,题目描述​​​​2,思路​​​​3,代码​​​​4,测试结果​​1,题目描述Arobotislocatedatthetop-leftcornerofamxngrid(marked'Start'inthediagra......
  • LeetCode_Array_56. Merge Intervals (C++)
    目录​​1,题目描述​​​​2,解题思路​​​​3,代码【C++】​​​​4,运行比较​​1,题目描述Givenacollectionofintervals,mergealloverlappingintervals.Example1:I......
  • LeetCode_Array_62. Unique Paths (C++)
    目录​​1,题目描述​​​​2,思路​​​​思路一:排列组合​​​​思路二:动态规划​​​​方法一:空间复杂度O(m*n)​​​​方法二:空间复杂度O(2n)​​​​方法三:空间复杂度O(n......
  • LeetCode_Array_55. Jump Game (C++)
    目录​​1,题目描述​​​​2,解题思路​​​​3,代码【C++】​​​​4,运行结果​​1,题目描述Givenanarrayofnon-negativeintegers,youareinitiallypositionedatthe......