- 包含本次模拟赛的10道题题解
- 能过样例,应该可以AC
- 若有错误,欢迎评论区指出
- 本次题目除了最后两题有些难度,其余题目较为简单,我只将代码和结果给出,如果不能理解欢迎私信我,我会解答滴。
start = 2022
def check(num) :
s = str(hex(num))[2:]
for i in range(len(s)) :
if ord(s[i]) < ord('a') or ord(s[i]) > ord('f') : return False
return True
while 1 :
start += 1
if check(start) : break
print(start)
#没代码,直接草稿纸手推就行了
import datetime
start = datetime.date(1900,1,1)
end = datetime.date(9999,12,31)
diff = datetime.timedelta(1)
def check(num) :
year, month, day = num.split('-')
if sum(list(map(int,year))) == sum(list(map(int,month))) + sum(list(map(int,day))) : return True
return False
ans = 0
while start <= end :
if check(str(start)) : ans += 1
if start == end : break
start += diff
print(ans)
s = '99,22,51,63,72,61,20,88,40,21,63,30,11,18,99,12,93,16,7,53,64,9,28,84,34,96,52,82,51,77'
lst = list(map(int,s.split(',')))
ans = 0
for i in range(29) :
for j in range(i + 1, 30) :
if lst[i] * lst[j] >= 2022 : ans += 1
print(ans)
s = '110010000011111110101001001001101010111011011011101001111110\
010000000001010001101100000010010110001111100010101100011110\
001011101000100011111111111010000010010101010111001000010100\
101100001101011101101011011001000110111111010000000110110000\
010101100100010000111000100111100110001110111101010011001011\
010011011010011110111101111001001001010111110001101000100011\
101001011000110100001101011000000110110110100100110111101011\
101111000000101000111001100010110000100110001001000101011001\
001110111010001011110000001111100001010101001110011010101110\
001010101000110001011111001010111111100110000011011111101010\
011111100011001110100101001011110011000101011000100111001011\
011010001101011110011011111010111110010100101000110111010110\
001110000111100100101110001011101010001100010111110111011011\
111100001000001100010110101100111001001111100100110000001101\
001110010000000111011110000011000010101000111000000110101101\
100100011101011111001101001010011111110010111101000010000111\
110010100110101100001101111101010011000110101100000110001010\
110101101100001110000100010001001010100010110100100001000011\
100100000100001101010101001101000101101000000101111110001010\
101101011010101000111110110000110100000010011111111100110010\
101111000100000100011000010001011111001010010001010110001010\
001010001110101010000100010011101001010101101101010111100101\
001111110000101100010111111100000100101010000001011101100001\
101011110010000010010110000100001010011111100011011000110010\
011110010100011101100101111101000001011100001011010001110011\
000101000101000010010010110111000010101111001101100110011100\
100011100110011111000110011001111100001110110111001001000111\
111011000110001000110111011001011110010010010110101000011111\
011110011110110110011011001011010000100100101010110000010011\
010011110011100101010101111010001001001111101111101110011101'
def check(x, y) :
res = 0
queue = [(x, y)]
while queue :
nx, ny = queue.pop(0)
res += 1
st[nx][ny] = False
for i, j in [(1,0), (-1,0), (0,1), (0,-1)] :
px = nx + i ; py = ny + j
if 0 <= px < 30 and 0 <= py < 60 and Map[px][py] == '1' and st[px][py] :
queue.append((px,py))
return res
Map = [[0] * 60 for i in range(30)]
for i in range(30) :
for j in range(60) :
Map[i][j] = s[i*30+j]
st = [[True] * 60 for i in range(30)]
ans = 0
for i in range(30) :
for j in range(60) :
if Map[i][j] == '1' and st[i][j] :
ans = max(ans, check(i, j))
print(ans)
x = int(input())
n = int(input())
x = x + (n % 7)
print(x if x <= 7 else x - 7)
from math import sqrt, pi
W,H,n,r = map(int,input().split())
def get_dist(x1, y1, x2, y2) :
dx = x1 - x2
dy = y1 - y2
return sqrt(dx * dx + dy * dy)
def check(x, y) :
for i, j in loc :
if get_dist(x, y, i, j) <= r : return True
return False
loc = []
for i in range(n) :
x,y = map(int,input().split())
loc.append((x,y))
ans = 0
for i in range(W + 1) :
for j in range(H + 1) :
if check(i, j) : ans += 1
print(ans)
N,M = map(int,input().split())
T = int(input())
Map = [[True] * (M + 1) for i in range(N + 1)]
for _ in range(T) :
r1,c1,r2,c2 = map(int,input().split())
for i in range(r1, r2 + 1) :
for j in range(c1, c2 + 1) :
Map[i][j] = False
ans = 0
for i in range(1, N + 1) :
ans += Map[i][1:].count(True)
print(ans)
这道题是经典的滑雪道记忆化搜索的题目,我写过一篇很详细的题解,请自行参考。
蓝桥杯/洛谷 : 最长滑雪道/滑雪 + 扩展题目(记忆化搜索 Python)_蓝桥杯滑雪_正在黑化的KS的博客-CSDN博客
N,M = map(int,input().split())
Map = []
for i in range(N) :
Map.append(list(map(int,input().split())))
dist = [[-1] * M for i in range(N)]
def find(x, y) :
if dist[x][y] != -1 : return dist[x][y]
dist[x][y] = 0
for i,j in [(1,0),(0,1),(-1,0),(0,-1)] :
px = x + i ; py = y + j
if 0 <= px < N and 0 <= py < M and Map[px][py] < Map[x][y] :
dist[x][y] = max(dist[x][y], find(px, py))
dist[x][y] += 1
return dist[x][y]
ans = 0
for i in range(N) :
for j in range(M) :
ans = max(ans, find(i, j))
print(ans)
重点题目来了!(敲黑板
有几位小伙伴私信问我这道题目该怎么解,我先说明我是用线段树解决的,但我认为应该还有别的解法,只是我目前还没有想到,如果有与我解法不同的方法可以放在评论区,大家一起讨论哈。
题目大意就是说,对于每一个点i而言,它的前 k 个点到后 k 个点这段 2k + 1 的区间中,最小数是多少。(区间边界是 0 ~ n - 1)
首先先说下超时做法,我们很容易想到,可以依次枚举每个点i,对每个点i再枚举它的
2k + 1 这个区间,维护一个最小值。这样的做法最坏情况下的时间复杂度是,对于这道题而言,n最大有10^6,因此肯定会超时。
正解如下,其实本质上这道题目是一道线段树的简化版,因为只涉及区间查询,不涉及区间修改,所以建树之后每次按要求查询区间就可以了。线段树的理论有些复杂,这里就不再给大家详细讲了,网上有很多篇关于线段树优秀的讲解。大家可以自行翻阅,如果已经理解了线段树还是不能看懂我的代码的话,可以来私信我。
N = 10 ** 6 + 10
INF = float('inf')
n = int(input())
w = list(map(int,input().split()))
class Node :
def __init__(self, l, r, v) :
self.l = l
self.r = r
self.v = v
tr = [None] * (4 * N)
def pushup(u) :
tr[u].v = min(tr[u << 1].v, tr[u << 1 | 1].v)
def build(u, l, r) :
if l == r : tr[u] = Node(l, r, w[r])
else :
tr[u] = {l, r, INF}
mid = l + r >> 1
build(u << 1, l, mid) ; build(u << 1 | 1, mid + 1, r)
pushup(u)
def query(u, l, r) :
if l <= tr[u].l and r >= tr[u].r : return tr[u].v
mid = tr[u].l + tr[u].r >> 1
res = INF
if l <= mid : res = min(res, query(u << 1, l, r))
if r > mid : res = min(res, query(u << 1 | 1, l, r))
return res
build(1, 0, n - 1)
k = int(input())
for i in range(n) :
print(query(1, max(0, i - k), min(n - 1, i + k)), end = ' ')
Tip: 考虑到很多小伙伴是C++组选手,我这里也贴一份C++的代码,以供参考。
Python写线段树真的有点反人类..
#include
#include
#include
using namespace std ;
typedef long long LL ;
const int N = 1e6 + 10 ;
int n, k ;
int w[N] ;
struct Node
{
int l, r, mn ;
}tr[4 * N] ;
void pushup(int u)
{
tr[u].mn = min(tr[u << 1].mn, tr[u << 1 | 1].mn) ;
}
void build(int u, int l, int r)
{
if (l == r) tr[u] = {l, r, w[r]} ;
else
{
tr[u] = {l, r} ;
int mid = l + r >> 1 ;
build(u << 1, l, mid), build(u << 1 | 1, mid + 1, r) ;
pushup(u) ;
}
}
int query(int u, int l, int r)
{
if (l <= tr[u].l && r >= tr[u].r) return tr[u].mn ;
int mid = tr[u].l + tr[u].r >> 1 ;
int res = 0x3f3f3f3f ;
if (l <= mid) res = min(res, query(u << 1, l, r)) ;
if (r > mid) res = min(res, query(u << 1 | 1, l, r)) ;
return res ;
}
int main()
{
ios::sync_with_stdio(false) ;
cin >> n ;
for (int i = 0 ; i < n ; i ++ )
cin >> w[i] ;
build(1, 0, n - 1) ;
cin >> k ;
for (int i = 0 ; i < n ; i ++ )
cout << query(1, max(0, i - k), min(n - 1, i + k)) << " " ;
return 0 ;
}
还有一个月左右省赛就要开始了,预祝大家能有个圆满且理想的成绩。
如果我的文章对你有一些帮助,可以留下你的
谢谢你的阅读。