2025第四届大学生算法挑战赛 赛前测试赛 题解

前言


题解

测试考场【算法编程赛道】2025第四届大学生算法挑战赛

继续用Deepseek进行求解,还是非常丝滑。


A. 追债之旅

思路: 图论 + bfs题

利用deepseek,直接给出答案

感觉deepseek在输入输出上,显得有些啰嗦。

python3 复制代码
# coding=utf-8
import heapq

def solve():
    import sys
    input = sys.stdin.read
    data = input().split()
    idx = 0
    n = int(data[idx])
    idx += 1
    m = int(data[idx])
    idx += 1
    k = int(data[idx])
    idx += 1
    
    adj = [[] for _ in range(n + 1)]
    for _ in range(m):
        u = int(data[idx])
        idx += 1
        v = int(data[idx])
        idx += 1
        w = int(data[idx])
        idx += 1
        adj[u].append((v, w))
        adj[v].append((u, w))
    
    a = list(map(int, data[idx:idx + k]))
    idx += k
    
    INF = float('inf')
    dp = [[INF] * (n + 1) for _ in range(k + 1)]
    dp[0][1] = 0
    
    heap = []
    heapq.heappush(heap, (0, 0, 1))  # (total_cost, day, city)
    
    found = False
    min_total = INF
    
    while heap:
        total_cost, day, u = heapq.heappop(heap)
        if u == n:
            min_total = min(min_total, total_cost)
            found = True
            continue
        if day == k:
            continue
        if total_cost > dp[day][u]:
            continue
        for (v, w) in adj[u]:
            new_day = day + 1
            if new_day > k:
                continue
            new_total = total_cost + w + a[day]
            if new_total < dp[new_day][v]:
                dp[new_day][v] = new_total
                heapq.heappush(heap, (new_total, new_day, v))
    
    if found:
        print(min_total)
    else:
        print(-1)

solve()

B. 单词分类

思路: 对每个单词的字母频率进行统计,构建一个feature key

python3 复制代码
# coding=utf-8


n = int(input())

from collections import Counter

st = set()
for _ in range(n):
    s = input()
    cnt = Counter(s)
    z = list(cnt.items())
    z.sort(key=lambda x: (x[0], x[1]))
    k = ""
    for (i, j) in z:
        k += str(i) + ":" + str(j) + ","
    st.add(k)

print (len(st))

C. 梯形面积

S = ( u + d ) ∗ h / 2 S = (u + d) * h / 2 S=(u+d)∗h/2

python3 复制代码
u, d, h = list(map(int, input().split()))

print ("%.3f" % ((u + d)/ 2.0 * h))

D. 好串

等价变化:堆栈模拟, a等价push,b等价于pop

保证栈不存在pop空的操作,同时最后栈为空

python3 复制代码
s = input()

def solve(s):
    r = 0
    for c in s:
        if c == 'a': r+=1
        else:
            r-=1
            if r < 0: return False
    return r == 0


print ("Good" if solve(s) else "Bad")

E. 展览

省流题意: 给你一个数组,任意取几个元素,使得其异或值最大

线性基的板子题

想到了线性基,但代码还是借助deepseek来提供。

python3 复制代码
# coding=utf-8

def insert(x, basis):
    for i in range(63, -1, -1):
        if (x >> i) & 1:
            if basis[i] == 0:
                basis[i] = x
                break
            x ^= basis[i]
    return basis

n = int(input())
arr = list(map(int, input().split()))
basis = [0] * 64

for v in arr:
    insert(v, basis)

def get_max_xor(basis):
    res = 0
    for i in range(63, -1, -1):
        if (res ^ basis[i]) > res:
            res ^= basis[i]
    return res

print (get_max_xor(basis))

写在最后

相关推荐
糖葫芦君16 分钟前
Policy Gradient【强化学习的数学原理】
算法
向阳@向远方2 小时前
第二章 简单程序设计
开发语言·c++·算法
github_czy3 小时前
RRF (Reciprocal Rank Fusion) 排序算法详解
算法·排序算法
许愿与你永世安宁3 小时前
力扣343 整数拆分
数据结构·算法·leetcode
爱coding的橙子3 小时前
每日算法刷题Day42 7.5:leetcode前缀和3道题,用时2h
算法·leetcode·职场和发展
满分观察网友z4 小时前
从一次手滑,我洞悉了用户输入的所有可能性(3330. 找到初始输入字符串 I)
算法
YuTaoShao4 小时前
【LeetCode 热题 100】73. 矩阵置零——(解法二)空间复杂度 O(1)
java·算法·leetcode·矩阵
Heartoxx4 小时前
c语言-指针(数组)练习2
c语言·数据结构·算法
大熊背5 小时前
图像处理专业书籍以及网络资源总结
人工智能·算法·microsoft
满分观察网友z5 小时前
别怕树!一层一层剥开它的心:用BFS/DFS优雅计算层平均值(637. 二叉树的层平均值)
算法