蓝桥杯 第二十一天 贪心的一天

目录

1.1055. 股票买卖 II - AcWing题库

2.104. 货仓选址 - AcWing题库

3.122. 糖果传递 - AcWing题库

4.112. 雷达设备 - AcWing题库

5.1235. 付账问题 - AcWing题库

(1)有精度问题

(2)改良,仍有问题

6.1239. 乘积最大 - AcWing题库

7.1247. 后缀表达式 - AcWing题库

8.1248. 灵能传输 - AcWing题库


1.1055. 股票买卖 II - AcWing题库

n=int(input())
martix=list(map(int,input().split()))
ans=0
for i in range(n-1):
    if martix[i+1]-martix[i]>0:
        ans+=martix[i+1]-martix[i]
print(ans)

2.104. 货仓选址 - AcWing题库

n=int(input())
martix=list(map(int,input().split()))
martix.sort()
ans=0
n//=2
for i in martix:
    ans+=abs(i-martix[n])
print(ans)

3.122. 糖果传递 - AcWing题库

离谱的一题

n=int(input())
martix=[]
for i in range(n):
    martix.append(int(input()))
avg=sum(martix)//n
c=[0 for i in range(n+1)]
for i in range(n-1,0,-1):
    c[i]=c[i+1]+avg-martix[i]
c[0]=0
martix=c[:n]
martix.sort()
res=0
half=martix[n//2]
for i in range(n):
    res+=abs(c[i]-half)
print(res)

4.112. 雷达设备 - AcWing题库

thread=[]
n,d=map(int,input().split())
flag=False
for _ in range(n):
    x,y=map(int,input().split())
    if y>d:
        flag=True
        break
    change=(d*d-y*y)**0.5
    thread.append((x-change,x+change))
if flag:
    print(-1)
else:
    thread.sort(key=lambda x:x[1])
    point=thread[0][1]
    ans=1
    for i in range(1,n):
        if point>=thread[i][0] and point<=thread[i][1]:
            continue
        else:
            ans+=1
            point=thread[i][1]
    print(ans)

5.1235. 付账问题 - AcWing题库

(1)有精度问题

import math
def out(martix):
    avg=sum(martix)/n
    sums=0
    for i in martix:
        sums+=pow(i-avg,2)
    sums/=n
    sums=math.sqrt(sums)
    print("{:.4f}".format(sums))
    return
n,s=map(int,input().split())
avg=s/n
m=list(map(int,input().split()))
nums=0
ans=[]
for i in m:
    if i<=avg:
        s-=i
        ans.append(i)
    else:
        nums+=1
ans+=[s/nums for i in range(nums)]
out(ans)

(2)改良,仍有问题

import math
n,s=map(int,input().split())
a=list(map(int,input().split()))
a.sort()
res=0
avg=s/n
for i in range(n):
    cur=s/(n-i)
    if a[i]

6.1239. 乘积最大 - AcWing题库

n,k=map(int,input().split())
a=[]
for i in range(n):
    a.append(int(input()))
a.sort()
res=1
l,r=0,n-1
sign=1
modss=1000000009
ans=[]
if k%2==1:
    ans.append(a[r])
    k-=1
    res=a[r]
    r-=1
    if res<0:
        sign=-1
    res=res*sign%modss
while k:
    x=a[l]*a[l+1]
    y=a[r]*a[r-1]
    if x*sign>y*sign:
        res=x%modss*(res)%modss
        ans.append(a[l])
        ans.append(a[l+1])
        l+=2
    else:
        ans.append(a[r])
        ans.append(a[r-1])
        res=(y%modss*res)%modss
        r-=2
    k-=2
print(res*sign)

7.1247. 后缀表达式 - AcWing题库

n,m=map(int,input().split())
k=n+m+1
a=list(map(int,input().split()))
res=0
if m==0:
    res=sum(a)
else:
    a.sort()
    res+=a[-1]-a[0]
    for i in range(1,k-1):
        res+=abs(a[i])
print(res)

8.1248. 灵能传输 - AcWing题库

前缀和+贪心

t=int(input())
for _ in range(t):
    n=int(input())
    a=[0]+list(map(int,input().split()))
    s=[0 for i in range(n+1)]
    for i in range(1,n+1):
        s[i]=s[i-1]+a[i]
    s0=s[0]
    sn=s[n]
    if s0>sn:
        s0,sn=sn,s0
    s.sort()
    s0=s.index(s0)
    sn=s.index(sn)
    st=[False for i in range(n+1)]
    ans=[0 for i in range(n+1)]
    l,r=0,n
    for i in range(s0,-1,-2):
        ans[l]=s[i]
        l+=1
        st[i]=True
    for i in range(sn,n+1,+2):
        ans[r]=s[i]
        r-=1
        st[i]=True
    for i in range(n+1):
        if not st[i]:
            ans[l]=s[i]
            l+=1
    res=0
    for i in range(1,n+1):
        res=max(res,abs(ans[i]-ans[i-1]))
    print(res)

你可能感兴趣的:(蓝桥杯,leetcode,职场和发展)