CCF 推荐系统问题
源代码如下
from heapq import *
MAXINT = 30001
MAX = 51
def opt1_add(product, category, community, score):
product[category].append((score, MAXINT - community))
def opt2_delete(product, category, community):
for value in range(len(product[category])):
if product[category][value][1] == MAXINT - community:
product[category].pop(value)
break
# 数据存储格式为类名+分数,分数中存储响应的序号
def opt3_select(product, opt, m):
global MAX
global MAXINT
constraint = opt[2:].copy()
num = opt[1]
result = []
for i in range(len(constraint)):
temp = nlargest(constraint[i], product[i])
for key in temp:
result.append((MAX - i, 30001 - key[1]))
temp = {}
for key in nlargest(num, result):
try:
temp[MAX - key[0]].append(key[1])
except KeyError:
temp[MAX - key[0]] = []
temp[MAX - key[0]].append(key[1])
keys = list(product.keys())
keys.sort()
for key in keys:
try:
temp2 = temp[key].copy()
except:
print(-1)
continue
temp2.sort()
for i in range(len(temp2) - 1):
print(temp2[i], end=" ")
try:
print(temp2[len(temp2) - 1])
except IndexError:
pass
temp = list(input().split())
m = int(temp[0])
n = int(temp[1])
product = {}
for i in range(1, n + 1):
temp = input().split()
for j in range(m):
try:
product[j].append((int(temp[1]), MAXINT - int(temp[0])))
except KeyError:
product[j] = []
product[j].append((int(temp[1]), MAXINT - int(temp[0])))
num = int(input())
for i in range(num):
opt = list(input().split())
for j in range(len(opt)):
opt[j] = int(opt[j])
if opt[0] == 1:
opt1_add(product, opt[1], opt[2], opt[3])
if opt[0] == 2:
opt2_delete(product, opt[1], opt[2])
if opt[0] == 3:
# print(product)
opt3_select(product, opt, m)
答案与测试用例相同但是一直报错误,0分,求问什么wen'ti