Codeforces Round #513 by Barcelona Bootcamp (rated, Div. 1 + Div. 2) (A B C)

2023-05-16

链接: http://codeforces.com/contest/1060

Codeforces Round #513

  • A. Phone Numbers(水题)
  • B. Maximum Sum of Digits(打表找规律、思维贪心)
  • C. Maximum Subrectangle(思维卡时间)

A. Phone Numbers(水题)

题意: 第一位是字符 ′ 8 ′ '8' 8 并且长度正好 11 11 11 位的字符串是合法的。给你一个字符串,问有多少个合法的子串(子串可以相同)。

#include <bits/stdc++.h>
using namespace std;

int main(int argc, char const *argv[])
{
	int n;
	string s;
	cin>>n>>s;
	if(n < 11) {
		cout<<0<<endl;
		return 0;
	}
	int cnt = 0;
	for (int i = 0; i < n; ++i) {
		if(s[i] == '8') cnt++;
	}
	cout<<min(cnt, n/11)<<endl;
	return 0;
}

B. Maximum Sum of Digits(打表找规律、思维贪心)

题意: 定义一个函数 S ( x ) S(x) S(x) , 它的值为 x x x 的每一位数相加,比如 S ( 123 ) = 1 + 2 + 3 S(123) = 1+2+3 S(123)=1+2+3 。现在给你一个数 n ( 1 ≤ n ≤ 1 0 12 ) n (1 \leq n \leq 10^{12}) n(1n1012), 求最大的 S ( a ) + S ( b )    ( a + b = n   , 0 ≤ a , b ≤ n ) S(a) + S(b) \ \ (a+b=n \ ,0 \leq a,b \leq n) S(a)+S(b)  (a+b=n ,0a,bn)
思路:
思路1 : 思考后可以发现,两个数 a , b a,b ab,因为和为定值, a a a 1 1 1 b b b 1 1 1,在没有进位、退位的情况下,答案是不变的,直到 a a a 变成全是 9 9 9 这种数(比如 9999999 9999999 9999999),进位之后答案变小了,所以分解成 ( 99999999 ) (99999999) 99999999 一定是最优的 。
思路2: 还有一种思路,打表观察发现 t t t 9 9 9 的这种数(比如 9999999 9999999 9999999), 最大值一定是 t ∗ 9 t*9 t9,那么我们假设就尽可能的分出 9 9 9 ,这样分解最大。验证后确实如此。
打表代码:

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;

int main(int argc, char const *argv[])
{
    for (LL n = 1; n <= 10000; ++n) {
        LL ans = -1;
        for (LL i = 0; i <= (n+1)/2; ++i) {
            LL tmp = dig(i) + dig(n-i);
            //cout<<i<<" "<<tmp<<endl;
            ans = max(ans, tmp);
        }
        cout<<ans<<endl;
    }
    return 0;
}

AC代码:

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
LL dig(LL x) {
	LL res = 0;
	while(x) {
		res += x%10;
		x /= 10;
	}
	return res;
}
int main(int argc, char const *argv[])
{
	LL n;
	cin>>n;
	if(n < 10) {
		cout<<n<<endl;
		return 0;
	}
	LL tmp = n;
	LL cnt = 0;
	while(tmp) {
		cnt++;
		tmp /= 10;
	}
	tmp = 0;
	for (int i = 0; i < cnt-1; ++i) {
		tmp *= 10;
		tmp += 9;
	}
	LL ans = dig(tmp) + dig(n-tmp);
	cout<<ans<<endl;
	return 0;
}

C. Maximum Subrectangle(思维卡时间)

题意 : 给出数组 a , b a, b a,b ,定义矩阵 c c c c [ i ] [ j ] = a [ i ] ∗ b [ j ] c[i][j] = a[i] * b[j] c[i][j]=a[i]b[j]。现在想让你找一个子矩阵 ( 1 ≤ x 1 ≤ x 2 ≤ n   , 1 ≤ y 1 ≤ y 2 ≤ m ) (1 \leq x1 \leq x2 \leq n\ , 1 \leq y1 \leq y2 \leq m) (1x1x2n ,1y1y2m)使得 ∑ i = x 1 x 2 ∑ j = y 1 y 2 c [ i ] [ j ] ≤ x \sum _{i=x1} ^{x2} \sum _{j=y1} ^{y2} c[i][j] \leq x i=x1x2j=y1y2c[i][j]x, 求最大的 s = ( y 2 − y 1 + 1 ) ∗ ( x 2 − x 1 + 1 ) s = (y2-y1+1) * (x2-x1+1) s=(y2y1+1)(x2x1+1)
思路:
∑ i = x 1 x 2 ∑ j = y 1 y 2 c [ i ] [ j ] = ∑ i = x 1 x 2 ∑ j = y 1 y 2 a [ i ] ∗ b [ j ] = ( ∑ i = x 1 x 2 a [ i ] ) ∗ ( ∑ j = y 1 y 2 b [ j ] ) \sum _{i=x1} ^{x2} \sum _{j=y1} ^{y2} c[i][j] = \sum _{i=x1} ^{x2} \sum _{j=y1} ^{y2} a[i]*b[j] = (\sum _{i=x1} ^{x2} a[i])* (\sum _{j=y1} ^{y2} b[j]) i=x1x2j=y1y2c[i][j]=i=x1x2j=y1y2a[i]b[j]=(i=x1x2a[i])(j=y1y2b[j])
矩阵的和就是横向所有数的和乘以纵向所有数的和。
思路1:
预处理一个方向(比如横向)的长度为 i i i 的最小子段和,然后排序。暴力另一个方向(纵向)所有子段,二分找合法的最大横向子段和。复杂度 O ( m a x ( m 2 l o g n , n 2 ) ) O(max(m^2 logn, n^2)) O(max(m2logn,n2))
思路2:
分别预处理 a , b a, b a,b 的长度为 i i i 的最小子段和。然后分别枚举长度。复杂度 O ( m a x ( n ∗ m , n 2 , m 2 ) ) O(max(n*m, n^2, m^2)) O(max(nm,n2,m2))

个人推荐思路2

思 路 1 A C 代 码 思路1AC代码 1AC

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const LL MAXN = 2100;
const int INF = 0x3f3f3f3f;
LL lena[MAXN*MAXN];
struct node {
	LL val;
	LL num;
	friend bool operator < ( node a, node b ) {
          return a.val<b.val;
    }
};

int main(int argc, char const *argv[])
{
	LL n, m, a[MAXN], b[MAXN], x;
	cin>>n>>m;
	for (LL i = 1; i <= n; ++i) {
		cin>>a[i];
	}
	for (LL i = 1; i <= m; ++i) {
		cin>>b[i];
	}
	cin>>x;
	LL suma[MAXN], sumb[MAXN];
	suma[0] = 0;
	for (LL i = 1; i <= n; ++i) {
		suma[i] = suma[i-1] + a[i];
	}
	sumb[0] = 0;
	for (LL i = 1; i <= m; ++i) {
		sumb[i] = sumb[i-1] + b[i];
	}
	std::vector<node > sumaz; 
	memset(lena, INF, sizeof(lena));
	for (LL i = 1; i <= n; ++i) {
		for (LL j = i; j <= n; ++j) {
			lena[j-i+1] = min(lena[j-i+1], suma[j] - suma[i-1]);
		}
	}
	for (LL i = 1; i <= n; ++i) {
		sumaz.push_back((node){lena[i], i});
	}
	sort(sumaz.begin(), sumaz.end());
	LL s = 0;
	for (LL i = 1; i <= m; ++i) {
		for (LL j = i; j <= m; ++j) {
			node tmp;
			tmp.val = x/(sumb[j] - sumb[i-1]);
			vector<node >::iterator it = lower_bound(sumaz.begin(), sumaz.end(), tmp);
			if(it != sumaz.end()) {
				if(((*it).val)*(sumb[j] - sumb[i-1]) <= x)
					s = max(s, (j-i+1)*((*it).num));
				else {
					if(it != sumaz.begin()){
						it--;
						s = max(s, (j-i+1)*((*it).num));	
					}
				}
			}
			else {
				it--;
				s = max(s, (j-i+1)*((*it).num));
			}
		}
	}
	cout<<s<<endl;
	return 0;
}

思 路 2 A C 代 码 思路2AC代码 2AC

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int MAXN = 2100;
const int INF = 0x3f3f3f3f;
LL lena[MAXN*MAXN];
LL lenb[MAXN*MAXN];

int main(int argc, char const *argv[])
{
	LL n, m, a[MAXN], b[MAXN], x;
	cin>>n>>m;
	for (LL i = 1; i <= n; ++i) {
		cin>>a[i];
	}
	for (LL i = 1; i <= m; ++i) {
		cin>>b[i];
	}
	cin>>x;
	LL suma[MAXN], sumb[MAXN];
	suma[0] = 0;
	for (LL i = 1; i <= n; ++i) {
		suma[i] = suma[i-1] + a[i];
	}
	sumb[0] = 0;
	for (LL i = 1; i <= m; ++i) {
		sumb[i] = sumb[i-1] + b[i];
	}
	memset(lena, INF, sizeof(lena));
	for (LL i = 1; i <= n; ++i) {
		for (LL j = i; j <= n; ++j) {
			lena[j-i+1] = min(lena[j-i+1], suma[j] - suma[i-1]);
		}
	}
	
	memset(lenb, INF, sizeof(lenb));
	for (LL i = 1; i <= m; ++i) {
		for (LL j = i; j <= m; ++j) {
			lenb[j-i+1] = min(lenb[j-i+1], sumb[j] - sumb[i-1]);
		}
	}
	LL s = 0;
	for (LL i = 1; i <= n; ++i) {
		for (LL j = 1; j <= m; ++j) {
			if(lena[i] * lenb[j] <= x) {
				s = max(s, i*j);
			}
		}
	}
	cout<<s<<endl;
	return 0;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Codeforces Round #513 by Barcelona Bootcamp (rated, Div. 1 + Div. 2) (A B C) 的相关文章

随机推荐

  • CodeForces - 954C - Matrix Walk

    坑题 题目 xff1a CodeForces 954C 题意 矩阵的每一元素可以用 Ai j 61 y i 1 43 j 来表示 xff0c xff08 就是二维数组用一维指针表示的方法 xff09 xff0c 给你一个路径序列 xff0c
  • Mathjex练习

    u k i 61 a k i k 1 j 61 1 l k j u j i u k i 61
  • 转载:全排列与next_permutation

    转载声明 xff1a 来自https blog csdn net yingyujianmo article details 52046398 感谢作者的讲解 全排列是面试笔试过程中经常遇到的一个问题 对于练习过的同学来说 xff0c 这个问
  • 转载:如何快速转载CSDN中的博客

    转载声明 xff1a 来自https blog csdn net bolu1234 article details 51867099 感谢作者的分享 前言 对于喜欢逛CSDN的人来说 xff0c 看别人的博客确实能够对自己有不小的提高 xf
  • Matlab日记

    Matlab中对clear函数赋值后如何清除变量 xff1f 方法很多 xff0c 一般用 builtin clear b u i l t i n 执 行 内 建 的 函 数 b u i l
  • QT_Windows_命令行下编译,发布

    本文所使用到的资源链接 xff1a 1 所有QT版本镜像下载 2 单文件制作封装工具Engima Virtual Box 环境配置 xff1a 报如下错 xff0c 参考这个 在我这里是因为 xff1a 系统的环境变量的目录中有几个版本不同
  • 容斥原理详解

    翻译 xff1a vici 64 cust 对容斥原理的描述 容斥原理是一种重要的组合数学方法 xff0c 可以让你求解任意大小的集合 xff0c 或者计算复合事件的概率 描述 容斥原理可以描述如下 xff1a 要计算几个集合并集的大小 x
  • 2018ACM/ICPC全国邀请赛(江苏) 总结

    抱憾打铁 整理了一下今天的思路 xff0c 记录如下 开始时我先开的A题 xff0c 我感觉是模拟 xff0c 和lqs讨论了一下 xff0c 感觉会T xff0c 就想其他方法了 开始wjj开的B xff0c 他说感觉是推个公式 xff0
  • HDU 1002 Java大数

    题意很简单输出 a 43 b a 43 b 只不过 a a 和 b b 都很大 xff0c 需要处理大数问题 Java大数解决方法 xff0c 详见代码 xff1a import java io import java util impor
  • broadcom OF-DPA

    https www broadcom com products ethernet connectivity software of dpa http broadcom switch github io of dpa doc html OFD
  • 【论文笔记】Spatial Temporal Graph Convolutional Networks for Skeleton-Based Action Recognition...

    Spatial Temporal Graph Convolutional Networks for Skeleton Based Action Recognition 2018 01 28 15 45 13 研究背景和动机 xff1a 行人
  • Windows下GCC编译环境中文乱码解决方案

    转载声明 xff1a 来自https blog csdn net MyLibs article details 27913281 在编译参数中增加以下两条指令 xff1a fexec charset 61 gbk finput charse
  • C++ 读入优化与输出优化 模板

    来自 xff1a https blog csdn net liyizhixl article details 54412459 简介 C 43 43 是一种神奇的编程语言 自然 xff0c 读入和输出也有着许多种形式 xff1a 如 xff
  • RMQ(range minimum/maximum query)即查询区间最大最小值。

    转载来自 https www cnblogs com yyxayz p 4109390 html 对于求区间最大最小值 xff0c 我们自然而然就想到了一个O xff08 n xff09 时间复杂度的算法 xff0c 但是如果询问有很多呢
  • 数论小知识点总结

    m i 61 1 g c d m i 61 d m d m d i 61 1 m g
  • 牛客国庆集训派对Day1(A C E L)

    链接 xff1a https www nowcoder com acm contest 201 question 牛客国庆集训派对Day1 A Tobaku Mokushiroku Kaiji xff08 水题 xff09 C Utawar
  • 牛客国庆集训派对Day2(A F H)

    链接 xff1a https www nowcoder com acm contest 202 question 牛客国庆集训派对Day2 A 矩阵乘法 xff08 分块 xff09 F 平衡二叉树 xff08 数据结构 xff09 H 卡
  • Git learn

    分布式版本控制系统 Git https git scm com 一 初始化 init xff0c 添加 add 到暂存区 stage xff0c 提交 commit 到版本库 master 二 工作区 xff0c 版本库 状态 status
  • 牛客国庆集训派对Day4(A D I J)

    链接 xff1a https www nowcoder com acm contest 204 question 牛客国庆集训派对Day4 A 深度学习 xff08 思维水题 xff09 D 最小生成树 xff08 思维 xff09 I 连
  • Codeforces Round #513 by Barcelona Bootcamp (rated, Div. 1 + Div. 2) (A B C)

    链接 xff1a http codeforces com contest 1060 Codeforces Round 513 A Phone Numbers xff08 水题 xff09 B Maximum Sum of Digits xf