https://codingcompetitions.wi...python
T 組數據,每組兩行,第一行 N,B 表明有 N 個待售房屋,你擁有的錢是 B。第二行是 N 個整數,表明每一個房屋的價格。
求能購買的最多房屋數量。app
T = int(input()) for t in range(T): N, B = map(int, input().split()) a = list(map(int, input().split())) a.sort() cnt = 0 for x in a: if B < x: break cnt += 1 B -= x print('Case #%d: %d' % (t+1, cnt))
T 組數據。每組數據 N + 1 行:第一行 N, K, P,表明有 N 摞盤子,每摞都是 K 個。一共須要挑出 P 個。後面 N 行每行是 K 個整數表明這摞盤子中每一個盤子的顏值,順序是從上到下。測試
須要保證挑出的盤子的顏值的和最大。google
T = int(input()) for t in range(T): N, K, P = map(int, input().split()) s = [] for _ in range(N): s.append(list(map(int, input().split()))) for i in range(N): for j in range(1, K): s[i][j] += s[i][j-1] s[i].append(0) dp = [[0] * (P+1) for _ in range(N+1)] for i in range(1, N+1): for j in range(1, P+1): for x in range(0, min(j, K)+1): dp[i][j] = max(dp[i][j], dp[i-1][j-x] + s[i-1][x-1]) print('Case #%d: %d' % (t+1, dp[N][P]))
T 組數據,每組兩行,第一行 N,K 表明一個嚴格遞增數列中有 N 個數,咱們能夠向其中插入 K 個任意數字。第二行是這 N 個整數。
插入數字後咱們但願數列的相鄰數字差的最大值儘量小,求這個最小值。code
import math T = int(input()) for t in range(T): N, K = map(int, input().split()) s = list(map(int, input().split())) d = [] for i in range(1, len(s)): d.append(s[i]-s[i-1]) maxv = max(d) if maxv < 2: print('Case #%d: %d' % (t+1, maxv)) else: d.sort(reverse=True) l, r = 1, maxv while l < r: m = l + r >> 1 ks = 0 for dd in d: if dd <= m: break ks += math.ceil(dd/m)-1 if ks > K: l = m + 1 else: r = m print('Case #%d: %d' % (t+1, r))
把 N 個字符串分紅 K 個組(N 必定被 K 整除)。每組的分數是這一組字符串最長公共前綴的和。求全局分數最大遞歸
T = int(input()) for tt in range(T): N, K = map(int, input().split()) ss = [] for _ in range(N): ss.append(input()) base = ord('A') class TrieNode: def __init__(self): self.cnt = 0 self.children = [None] * 26 def add(self, s): self.cnt += 1 if not s: return if not self.children[i]: self.children[i] = TrieNode() self.children[i].add(s[1:]) root = TrieNode() for s in ss: t = root for ch in s: t.cnt += 1 i = ord(ch) - base if not t.children[i]: t.children[i] = TrieNode() t = t.children[i] t.cnt += 1 cnt = 0 root.cnt = 0 stack = [root] while stack: t = stack.pop() cnt += t.cnt // K for c in t.children: if c and c.cnt >= K: stack.append(c) print('Case #%d: %d' % (tt+1, cnt))
T = int(input()) for t in range(T): N, K = map(int, input().split()) ss = [] for _ in range(N): ss.append(input()) ss.sort() cnt = [0] def find(i, j, k): s = i while s < j and k >= len(ss[s]): s += 1 x = s while s < j: while x < j and ss[x][k] == ss[s][k]: x += 1 if x - s >= K: cnt[0] += (x - s) // K find(s, x, k+1) s = x find(0, len(ss), 0) print('Case #%d: %d' % (t+1, cnt[0]))
歡迎來個人博客: https://codeplot.top/
個人博客刷題分類:https://codeplot.top/categories/%E5%88%B7%E9%A2%98/字符串