202006-1 线性分类器
解答:
n,m = map(int,input().split())
input_point = [""]*n
# positive,negetive = "A","B"
result = [""]*m
for i in range(n):
input_point[i] = input()
for i in range(m):
t1,t2,t3 = map(int,input().split())
positive, negetive = "A", "B"
for j in range(n):
x,y,symbol = input_point[j].split()
flag = t1 + t2 * int(x) + t3 * int(y)
y_or_n = 1
if j == 0:
if flag > 0 and symbol == "B":
positive,negetive = "B","A"
elif flag < 0 and symbol == "A":
positive,negetive = "B","A"
if flag < 0 and symbol != negetive:
result[i] = "No"
y_or_n = 0
break
if flag > 0 and symbol == negetive:
result[i] = "No"
y_or_n = 0
break
if y_or_n == 1:
result[i] = "Yes"
for i in result:
print(i)
202006-2 稀疏向量
解答:
n,a,b = map(int,input().split())
a_dict = {}
b_dict = {}
num = 0
for i in range(a):
key,value = map(int,input().split())
a_dict[key] = value
for i in range(b):
key,value = map(int,input().split())
b_dict[key] = value
for key in a_dict.keys():
if key in b_dict.keys():
num += a_dict[key]*b_dict[key]
print(num)
202009-1 称检测点查询
解答:
n,X,Y = map(int,input().split())
distance = [0]*n
index_list = [i+1 for i in range(n)] #记录下表位置
for i in range(n):
x,y = map(int,input().split())
distance[i] = (X-x)**2 + (Y-y)**2
for i in range(3):
min_dis = min(distance)
index = index_list[distance.index(min_dis)]
print(index) #查找对应下标
distance.remove(min_dis)
index_list.remove(index)
202009-2 风险人群筛查
解答:
n,k,t,xl,yd,xr,yu = map(int,input().split()) #n位居民,k个点临界,t时间,左下右上
result_1 = result_2 = 0
for i in range(n):
list_t = list(map(int,input().split()))
num1 = 0
num2 = 0
flag = 0
for x in range(0,2*t,2):
if list_t[x] in range(xl,xr+1) and list_t[x+1] in range(yd,yu+1):
num1 += 1
num2 += 1
if num2 >= k:
flag = 1
else:
num2 = 0
if num1 != 0:
result_1 += 1
if flag == 1:
result_2 += 1
print(result_1)
print(result_2)
202012-1 期末预测之安全指数
n = eval(input())
x = 0
for i in range(n):
w,score = map(int,input().split())
x += w * score
y = max(0,x)
print(y)
202012-2 期末预测之最佳阈值
解答:
70分暴力解答,运行超时。
m = eval(input())
y = [0]*m
result = [0]*m
for i in range(m):
y[i],result[i] = map(int,input().split())
y_test = set(y)
temp_num = -1
temp_index = -1
for i in y_test:
num = 0
for x in range(m):
if y[x] >= i and result[x] == 1:
num += 1
elif y[x] < i and result[x] == 0:
num += 1
if num > temp_num:
temp_num = num
temp_index = i
if num == temp_num and i > temp_index:
temp_index = i
print(temp_index)
晚上想到老师讲贪心算法画的二维表格,此题可以利用前面的值,减少后面的循环吗?
以样例1为例,最大值7,只需要判断小于它的例子为0的值,和自己本身的值是否为1,就是预测正确的值。然后是5,相对于7,需要重新判断的只有5是否为1,为1则预测正确的值加一,为0则减一。
所以:7:预测正确:2+1 =3
5:预测正确:3+1=4
3:预测正确:4+1=5
1:预测正确:5+1-1 =5
0:预测正确:5-1=4
依照这个思想写代码:
因为1.阈值要排序。2.阈值的预测值要记录
所以我选字典。
import sys
m = eval(sys.stdin.readline())
test = {}
number_true = 0
for i in range(m):
key,value = map(int,sys.stdin.readline().split())
#顺便计算输入的0值,最大值可以和其他值的情况合并了(7,【0,1】)
if value == 0:
number_true += 1
if key in test.keys():
test[key].append(value)
else:
test[key] = [value]
test_true_sorted = sorted(test.items(),key=lambda x:x[0],reverse=True) #元素值为元组的列表
# print(test_true_sorted)
#计算预测值,并记录最大
max_true = 0
max_index = 0
for tuple_x in test_true_sorted:
for i in tuple_x[1]:
if i == 1:
number_true += 1
else:
number_true -= 1
if number_true > max_true:
max_true = number_true
max_index = tuple_x[0]
print(max_index)
过程中,字典的值为列表的追加有几次错误尝试:
正确的是: