新生赛
每日一句诗词
loading...
loading...
题 A
最大化网格图中正方形空洞的面积
给你一个网格图,由 n + 2
条 横线段 和 m + 2
条 竖线段 组成,一开始所有区域均为 1 x 1 的单元格。
所有线段的编号从 1 开始。给你两个整数 n
和 m
。
同时给你两个整数数组 hBars
和 vBars
。
hBars
包含区间 [2, n + 1] 内 互不相同 的横线段编号。vBars
包含 [2, m + 1] 内 互不相同的 竖线段编号。
如果满足以下条件之一,你可以 移除 两个数组中的部分线段:
如果移除的是横线段,它必须是 hBars
中的值。
如果移除的是竖线段,它必须是 vBars
中的值。
两个正整数 1 ≤ n ≤ 10^9 ,1 ≤ m ≤ 10^9 。
接下来两个数组
1 <= hBars.length <= 100
2 <= hBars[i] <= n + 1
1 <= vBars.length <= 100
2 <= vBars[i] <= m + 1
请你输出移除一些线段后(可能不移除任何线段),剩余网格图中 最大正方形 空洞的面积,正方形空洞的意思是正方形 内部 不含有任何线段。
- 案例
1
2输入:n = 2, m = 1, hBars = [2,3], vBars = [2]
输出:4
思路:统计横竖的最宽间隔, 然后取小后平方;
1 | import java.util.Scanner; |
题 B
所有路径
给定一个 n * m 矩阵 ,角色只能往下或者往右
从左上角走到右下角的所有路径
两个正整数 n 和 m 分别表示 矩阵的行和列
2 <= n <= 1000
2 <= n <= 1000
返回到达右下角的所有路径
- 案例
1
2
3
4输入
2 8
输出
45
思路:本题在新生赛中数据量非常小,所有下面三种方法都可以解答
1.DFS 2.组合数 3. dp
就给出2的解法,
当数据量在 10^9时 只有2的解法可以
1 | import java.util.Scanner; |
题 C
小红的01连续段
小红定义一个01串的“连续段”为:连续相同字符的极大长度。例如:”110001111”有一个长度为2的连续段,有一个长度为3的连续段,有一个长度为4的连续段。
小红拿到了一个01串,但其中有一些字符不可见了(用’?’表示)。小红想知道,这个01串的连续段长度的最大值最多能达到多少?
一个仅由’0’、’1’、’?’组成的字符串,长度不超过200000。
一个正整数,代表连续段长度的最大长度。
- 案例
1
2
3
4
5
6输入
1?0?1?
输出
3
说明
该字符串可以是"100011",最大的连续段长度为3。
思路:可以想到,当最长连续段是 0 时 把所有的?变成0为最优解 ,1同理
1 | import java.util.*; |
题 D
小红的01串构造
小红希望你构造一个长度为n的01串,其中恰好有
k个1,且恰好有t对相邻字符都是1。你能帮帮她吗?
三个正整数n,k,t,用空格隔开。
1 ≤ n ≤ 10^5
0 ≤ k,t ≤ n
如果无法完成构造,请输出-1。否则输出任意一个满足条件01串即可。
- 案例
1
2
3
4
5
6
7输入
3 2 1
输出
110
说明
"110"为长度为3的01串,恰好有2个1,恰好有1对相邻数字是1。满足要求。
输出"011"也是可以通过本样例的。
思路:依照思路构造即可
1 | import java.util.Scanner; |