Blue Bridge Cup Day 21 (Python) (Day 4 of Crazy Questions)

Question type:

1. Thinking questions/Miscellaneous questions: Mathematical formulas, analyzing the meaning of questions, finding patterns

2. BFS/DFS: wide search (recursive implementation), deep search (deque implementation)

3. Simple number theory: modulus, prime number (only need to judge to int (sqrt (n)) + 1), gcd, lcm, fast exponentiation (bit operation shift operation), large number decomposition (decomposition into the product of prime numbers)

4. Simple graph theory: shortest path (one-to-many (Dijstra, adjacent table, matrix implementation), many-to-many (Floyd, matrix implementation)), minimum spanning tree (and search set implementation)

5. Simple string processing: it is best to switch to list operations

6. DP: Linear DP, longest common subsequence, 0/1 knapsack problem, longest continuous string, largest increasing substring

7. Basic algorithms: binary, greedy, combination, permutation, prefix sum, difference

8. Basic data structures: queues, sets, dictionaries, strings, lists, stacks, trees

9. Commonly used modules: math, datetime, set the maximum recursion depth in sys (sys.setrecursionlimit(3000000)), collections.deque (queue), itertools.combinations (list, n) (combination), itertools.permutations (list, n) (permutation) heapq (small top heap)

Table of contents

Question type:

brush question:

1. Bidirectional sorting (violent, built-in functions)

2. Swiss wheel (violent, handwritten sorting function)

3. Digital sorting (custom sorting rules)

 4. Sequence evaluation (take the remainder plus cycle)

5. Yang Hui's triangle (violence, list splicing)

6. Combination number problem (violence)


brush question:

1. Bidirectional sorting (violent, built-in functions)

import sys  #设置递归深度
import collections  #队列
import itertools  # 排列组合
import heapq  #小顶堆
import math
sys.setrecursionlimit(300000)

# 暴力过60%
n,m=map(int,input().split())
a=[i for i in range(1,n+1)]
for i in range(m):
   p,q=map(int,input().split())
   if p==0:
      a=sorted(a[:q],reverse=True)+a[q:]
   if p==1:
      a=a[:q-1]+sorted(a[q-1::])
print(*a)

 Just use the built-in function, direct violence, just pass 60% of the data, note that the slicing operation cannot get the last one, and sorted can be partially sorted and return a new sequence.

2. Swiss wheel (violent, handwritten sorting function)

 

import sys  #设置递归深度
import collections  #队列
import itertools  # 排列组合
import heapq  #小顶堆
import math
sys.setrecursionlimit(300000)
import functools   # 自定义比较函数

def cmp(a,b):  # 降序,优先比大小,再比序号 1表示交换,-1不变
   if a[1]!=b[1]:  # 总分
      return -1 if a[1]>b[1] else 1
   if a[1]==b[1]:  # 序号
      return -1 if a[0]<b[0] else 1

n,r,q=map(int,input().split())
score =[0]+ list(map(int,input().split()))  # 存储初始分
power =[0]+list(map(int,input().split()))   # 存储实力值

# 给得分和实力值添加编号
for i in range(1,len(score)):
   score[i]=[i,score[i]]
for i in range(1,len(power)):
   power[i]=[i,power[i]]

for i in range(r):  # r轮比赛
   #print(score)  #打印比赛前
   for j in range(1,n+1):   # 赢的加分
         ''' 这里有问题,应该按照当前排序后得分的索引来排序'''
##      if power[2*j-1][1]>power[2*j][1]:
##         score[2*j-1][1]+=1
##      elif power[2*j-1][1]<power[2*j][1]:
##         score[2*j][1]+=1
         # 获得两个分值的序号,查他们的实力值
         index1=score[2*j-1][0]
         index2=score[2*j][0]
         if power[index1][1]>power[index2][1]:
            score[2*j-1][1]+=1
         elif power[index1][1]<power[index2][1]:
            score[2*j][1]+=1
   #print(score)  # 比赛后
   score=[0]+sorted(score[1::],key=functools.cmp_to_key(cmp))  # 拼接,升序
   #print(score)  # 排序后
   #print("-"*50) # 分隔线

print(score[q][0])  # 排名第Q
2 4 2
7 6 6 7
10 5 20 15
[0, [1, 7], [2, 6], [3, 6], [4, 7]]
[0, [1, 8], [2, 6], [3, 7], [4, 7]]
[0, [1, 8], [3, 7], [4, 7], [2, 6]]
--------------------------------------------------
[0, [1, 8], [3, 7], [4, 7], [2, 6]]
[0, [1, 8], [3, 8], [4, 8], [2, 6]]
[0, [1, 8], [3, 8], [4, 8], [2, 6]]
--------------------------------------------------
[0, [1, 8], [3, 8], [4, 8], [2, 6]]
[0, [1, 8], [3, 9], [4, 9], [2, 6]]
[0, [3, 9], [4, 9], [1, 8], [2, 6]]
--------------------------------------------------
[0, [3, 9], [4, 9], [1, 8], [2, 6]]
[0, [3, 10], [4, 9], [1, 9], [2, 6]]
[0, [3, 10], [1, 9], [4, 9], [2, 6]]
--------------------------------------------------
1

Handwritten sorting function, through functools, the sorting function uses functools.cmp_to_key(cmp) ,

def cmp() remember -1 is unchanged, 1 swaps places

3. Digital sorting (custom sorting rules)

 

 Note the following properties

n=123
print(str(n))
print(list(str(n)))
print(list(map(int,str(n))))

"""
123
['1', '2', '3']
[1, 2, 3]

"""

So the code can be written as

import sys  #设置递归深度
import collections  #队列
import itertools  # 排列组合
import heapq  #小顶堆
import math
sys.setrecursionlimit(300000)
import functools   # 自定义比较函数

def cmp(a,b):  #自定义排序规则 1表示交换,-1不变
   sum1=sum(map(int,list(a)))
   sum2=sum(map(int,list(b)))
   if sum1!=sum2 :   # 和从小到大
      return -1 if sum1<sum2 else 1
   else:    # 字典序排序
      return -1 if a>b else 1

n=int(input())
m=int(input())
save=[str(i) for i in range(1,n+1)]
save.sort(key=functools.cmp_to_key(cmp))
save=[0]+save   # 也可以不拼接,索引取m-1
print(save[m])  # 打印第m个元素

Send points, familiarize yourself with the writing method of custom sorting rules, pay attention to the splitting of numbers, convert to str type, and then directly convert to a list.

 4. Sequence evaluation (take the remainder plus cycle)

import sys  #设置递归深度
import collections  #队列
import itertools  # 排列组合
import heapq  #小顶堆
import math
sys.setrecursionlimit(300000)
import functools   # 自定义比较函数  -1不变,1交换

a=1
b=1
c=1
d=3
for i in range(4,20190325):
   d=(a+b+c)%10000
   a,b,c=b,c,d
print(d)



 It is enough to simply take the remainder cycle, pay attention to take the remainder, otherwise the memory error will occur if the number is too large.

5. Yang Hui's triangle (violence, list splicing)

 

import os
import sys
 
# 骗分写法
n=int(input())
a=[[1],[1,1]]
for i in range(1,500): # 50-1+2行
  b=[]
  temp=0
  for j in range(i): # 根据上一行i计算
    temp=a[i][j]+a[i][j+1]
    b.append(temp)
  a.append([1]+b+[1])
# print(a)
b=[]
for i in range(501):  #进行队列拼接
    b=b+a[i]
print(b.index(n)+1)  # 直接通过队列值找索引
 

 Violently calculate the first 500 Yang Hui triangles and then splice them into one row, and then calculate the index value.

6. Combination number problem (violence)

import os
import sys
 
# 请在此输入您的代码
def f(i,j):  # 5 3  5*4*3/3*2*1
  a,b=1,1
  for z in range(i,i-j,-1):  # 5,2
    a*=z
  for z in range(1,j+1):
    b*=z
  return a//b
 
 
t,k = map(int,input().split())
ans=0
for _ in range(t):
  n,m=map(int,input().split())
  for i in range(1,n+1):
    for j in range(0,min(i,m)+1):
      if f(i,j)%k==0:
        print(i,j)
        ans+=1
print(ans%(10**9+7))

Just run violently according to the meaning of the title

Guess you like

Origin blog.csdn.net/weixin_52261094/article/details/129910370