都是用Python实现
知识点整理:
python
fruits = {'apple': 2, 'banana': 3, 'orange': 1}
# 遍历键
for fruit in fruits:
print(fruit, fruits[fruit])
# 遍历值
for quantity in fruits.values():
print(quantity)
# 遍历键值对
for fruit, quantity in fruits.items():
print(f"{fruit} -> {quantity}")
2.sorted()默认升序
reverse=True降序
sorted 的应用,也可以通过 key 的值来进行数组/字典的排序,比如:
(1)
python
array = [{"age":20,"name":"a"},{"age":25,"name":"b"},{"age":10,"name":"c"}]
array = sorted(array,key=lambda x:x["age"])
print(array)
{'age': 10, 'name': 'c'}, {'age': 20, 'name': 'a'}, {'age': 25, 'name': 'b'}
(2)
先按照成绩降序排序,相同成绩的按照名字升序排序:
python
d1 = [{'name':'alice', 'score':38}, {'name':'bob', 'score':18}, {'name':'darl', 'score':28}, {'name':'christ', 'score':28}]
l = sorted(d1, key=lambda x:(-x['score'], x['name']))
print(l)
{'name': 'alice', 'score': 38}, {'name': 'christ', 'score': 28}, {'name': 'darl', 'score': 28}, {'name': 'bob', 'score': 18}
3.用Python怎么实现树结构和图呢?
而且要用树和图构建来实现运算的,比如图的边上有值,(?)
4.还有个类似滑动窗口的题,窗口长度固定但要在窗口里找Min,max,sum,第P个,而且不能对窗口内容排序这咋做?
题干
题目描述:
训练集由多个审核源重复提交审核结果。需要先保留每个来源的最新审核,再汇总样本置信信息,发现有效来源之间的标签不一致。
输入结构保证合法;数值字段为合法整数字面量或NA。
输入记录为 sample source version confidence label,前3列保证是正整数;后2列允许NA及范围异常。
先按(sample,source)选version最大的记录;同版本取输入行号较大的记录。
然后过滤:confidence为NA或不在0,100,或label为NA或不在{0,1},整条记录删除。不得回退旧版本。
按sample汇总有效记录。一个来源最多贡献一条。
依次计算来源数、置信总和、最小置信、最大置信、置信整数均值(总和除以来源数向下取整)。
有效标签均相同则类别为该标签0或1,否则为CONFLICT。没有有效来源的样本不输出。
【输入格式】
第一行N;后续N行是上述记录,字段用空白分隔。
【输出格式】
第一行输出三个整数:有有效来源的样本数、有效来源记录总数、不一致样本数。
随后按sample升序,每行输出:sample 来源数 置信总和 最小置信 最大置信 整数均值 类别。
无有效记录时仅输出0 0 0。
【约束】
0≤N≤100000;sample、source、version≤1000000000。异常confidence、label的数值绝对值≤1000000000。无需处理非整数且非NA的token。
【答题建议】
- 在solve内解析并选择每个(sample,source)的最新行。
- 过滤最新行,按样本汇总计数、总和、极值及标签集合。
- 按样本编号排序,输出汇总及不一致样本数。
【接口与填空量】
只补全def solve(text):函数体,返回完整输出字符串;非空输出以换行结束。main已提供,读取和类型转换必须在solve内部完成。目标约30行,建议28至34行;不以精确行数替代正确性评判。
【复杂度建议】
时间O(N+U log U),U为有效样本数;空间O(N),包括读取的输入文本、解析结果及输出。建议Python 3,5秒、256MiB;实际平台以统一验证为准。
示例:
【样例输入】
7
10 1 1 80 0
10 1 2 90 1
10 2 1 70 0
20 1 1 100 1
20 1 1 60 1
30 1 1 50 0
30 1 2 NA 0
【样例输出】
2 3 1
10 2 160 70 90 80 CONFLICT
20 1 60 60 60 60 1
python
def solve(text):
tokens = text.split()
if not tokens:
return "0 0 0\n"
#用 split() 一次性切分全部输入,避免逐行读取的开销。
#latest 字典以 (sample, source) 为键,存该来源的最新记录。idx 作为游标手动前进。
n = int(tokens[0])
latest = {}
idx = 1
for _ in range(n):
sample = int(tokens[idx])
source = int(tokens[idx + 1])
version = int(tokens[idx + 2])
conf_tok = tokens[idx + 3]
label_tok = tokens[idx + 4]
idx += 5
key = (sample, source)
old = latest.get(key)
if old is None or version >= old[0]:
latest[key] = (version, conf_tok, label_tok)
agg = {}
total_records = 0
for (sample, source), (version, conf_tok, label_tok) in latest.items():
if conf_tok == "NA" or label_tok == "NA":
continue
conf = int(conf_tok)
label = int(label_tok)
if conf < 0 or conf > 100 or label not in (0, 1):
continue
total_records += 1
print(total_records)
if sample not in agg:
agg[sample] = [1, conf, conf, conf, {label}]
else:
item = agg[sample]
item[0] += 1
item[1] += conf
item[2] = min(item[2], conf)
item[3] = max(item[3], conf)
item[4].add(label)
if not agg:
return "0 0 0\n"
lines = []
conflict_count = 0
for sample in sorted(agg):
cnt, total_conf, min_conf, max_conf, labels = agg[sample]
if len(labels) == 1:
category = str(next(iter(labels)))
else:
category = "CONFLICT"
conflict_count += 1
mean_conf = total_conf // cnt
lines.append(
f"{sample} {cnt} {total_conf} {min_conf} {max_conf} {mean_conf} {category}"
)
lines.insert(0, f"{len(agg)} {total_records} {conflict_count}")
print(lines)
return "\n".join(lines) + "\n"
题目2
标注平台保留任务的多次复核记录。请恢复最终有效任务,并按类别通过率形成质量排名。
先以任务编号去重,保留版本号最大的记录;同版本并列保留输入行号较大的记录。类别以该记录为准,任务可能迁移类别。再检查分数:NA表示缺失;整数不在0,100内为异常,整条记录丢弃,不回退旧版本。分数>=60为通过。没有有效任务的类别不输出。
每类别统计有效总数、通过数、通过率万分值(通过数*10000//总数)。
排名先按真实通过率降序(整数交叉相乘比较,不可用截断后的万分值排序),再按总数降序,再按类别字典序升序。
【输入】
首行n,随后n行:任务编号 版本号 类别 分数。
输入结构保证合法;数值字段为整数字面量或NA,其中只有分数允许NA。任务编号、类别为1至20位ASCII字母数字串;比较按Python字符串字典序。0<=版本号<=10^9,分数整数在-1000,1000。0<=n<=100000。
【输出】
首行有效类别数量;接下来按排名每行:类别 总数 通过数 通过率万分值。全无有效记录仅输出0。所有输出末尾换行。
【答题建议】
- 在solve中解析、转换版本并选择最新记录。
- 过滤缺失和异常分数,按最终类别累计总数与通过数。
- 精确比较通过率、输出排名及整数万分值。
【接口与填空量】
只补全def solve(text):的完整函数体;接收完整输入字符串,返回完整输出字符串。框架不提供预处理。目标约30行,建议28至34行;不以精确行数替代正确性评判。
【复杂度与资源建议】
设有效类别数为g,建议时间O(n+g log g),含输入文本及行列表的空间O(n)。Python3建议5秒、256MiB,须以组织方目标环境实测为准,不代表已验证性能。
示例:
【输入】
9
a1 0 X 80
a1 1 Y 50
a2 0 X 60
a3 0 X 20
a4 0 Y 90
a5 0 Z NA
a6 0 Z 101
a7 0 A 60
a8 0 A 59
【输出】
3
A 2 1 5000
X 2 1 5000
Y 2 1 5000
python
from functools import cmp_to_key
def solve(text):
data = text.split()
n = int(data[0])
idx = 1
best = {}
# 1. 按任务编号去重:版本最大;同版本保留行号较大者(后出现的覆盖)
for _ in range(n):
tid, ver, cat, score = data[idx], int(data[idx + 1]), data[idx + 2], data[idx + 3]
idx += 4
prev = best.get(tid)
if prev is None or ver >= prev[0]:
best[tid] = (ver, cat, score)
# 2. 过滤分数,按最终类别累计 [总数, 通过数]
agg = {}# agg = { 类别: [总数, 通过数] } 值是 list,不是 tuple
for _, (ver, cat, score) in best.items():
if score == "NA":
continue
s = int(score)
if s < 0 or s > 100:
continue
rec = agg.get(cat)# 拿到的是引用,不是副本
if rec is None:
agg[cat] = [1, 1 if s >= 60 else 0]
else:
rec[0] += 1
if s >= 60:
rec[1] += 1
if not agg:
return "0\n"
# 3. 排名比较器:真实通过率降序(交叉相乘)→ 总数降序 → 类别字典序升序
def cmp(a, b):
ta, pa = agg[a]
tb, pb = agg[b]
lhs, rhs = pa * tb, pb * ta
if lhs != rhs:
return -1 if lhs > rhs else 1
if ta != tb:
return -1 if ta > tb else 1
return -1 if a < b else (1 if a > b else 0)
#简洁版--降序,降序,升序
#from fractions import Fraction
#sorted(agg, key=lambda c: (-Fraction(agg[c][1], agg[c][0]), -agg[c][0], c))
cats = sorted(agg, key=cmp_to_key(cmp))
lines = [str(len(cats))]
for c in cats:
t, p = agg[c]
lines.append(f"{c} {t} {p} {p * 10000 // t}")
return "\n".join(lines) + "\n"
if __name__ == '__main__':
# result = solve(sys.stdin.buffer.read().decode('utf-8'))
# sys.stdout.write(result)
text=input()
print(solve(text))
以上不算是简洁化的写法,用Python可以在很多步骤上简洁化,但以我熟练度想不到T-T