【CSDN竞赛第11期】 Python 题解

第11期比赛页面:第11期编程竞赛
第12期和第13期比赛报名已开启,有机会赢取热门实体书还有CSDN周边等!,欢迎点击参与报名:
报名12期比赛:第12期比赛点击报名
报名13期比赛:第13期比赛点击报名

前言

本次比赛题目还是比较简单的,就是要注意些数组下标等小问题。我是边吃早餐边写的代码,没有专心,导致出现了小问题,调试耽搁了点时间。
先晒晒我的早餐,奥尔良鸡肉饼+油条+水煮蛋+豆浆,9.9美滋滋
在这里插入图片描述

题解

1、圆小艺

在这里插入图片描述

最近小艺酱渐渐变成了一个圆滑的形状-球! !
小艺酱开始变得喜欢上球!
小艺酱得到n个同心圆。
小艺酱对着n个同心圆进行染色。
相邻的圆范围内不能有相同的颜色。 相隔一层的圆颜色相同。
小艺酱想知道圆最外层的那种颜色全部染了多少?

输入描述

第一行输入整数n.(1<=n<=1000)表示圆的数量。
第二行输入n个圆的半径。(1<=r<=1000)

输出描述

输出染色面积,保留小数点后3位。

输入样例

3
1 2 3

输出样例

18.849

分析

乍一看题目比较复杂,其实仔细想想就是两种颜色相间,求最外层那种颜色的面积。
如下图示,就是求蓝色部分的面积。
在这里插入图片描述
圆周率可以用3.1415926535或 a c o s ( − 1 ) acos(-1) acos(1)
来跟我一起背:
山巅一寺一壶酒,尔乐苦煞吾,把酒吃,酒杀尔杀不死,乐尔乐
3.1415926535897932384626

源码

class Solution:
    def __init__(self) -> None:
        pass
    
    def solution(self, n, arr):
        result = None
        
        # TODO: 请在此编写代码
        PI=3.141592653589793
        arr.sort(reverse=True)
        result=0
        for i in range(0,n,2):
            result+=(PI*arr[i]*arr[i]-PI*arr[i+1]*arr[i+1])
            
        return result
    
if __name__ == "__main__":
    
    n = int(input().strip())
    
    arr = [int(item) for item in input().strip().split()]
    arr.append(0)
    sol = Solution()
    result = sol.solution(n, arr)
    
    print('%.3f'%result)

2、K皇把妹

在这里插入图片描述
存在n个节点, 目标节点在m。
每个节点有自己的权值a。
在权值k内(含k值) 选择一个权值非0节点且与目标节点距离最近。
节点i与节点j的距离为abs(i-j)。

输入描述

第一行输入整数n,m,k.(1<=n,m,k<=100)
第二行输入n个整数的权值。(1<=a<=1000)

输出描述

输出最小距离

输入样例

7 3 50
62 0 0 0 99 33 22

输出样例

3

源码

class Solution:
    def __init__(self) -> None:
        pass
    
    def solution(self, n, m, k, arr):
        result = None
        
        # TODO: 请在此编写代码
        for i in range(0,n+1):
            l=m-i
            r=m+i
            if l>0:
                if arr[l]!=0 and arr[l]<=k:
                    result=abs(l-m)
                    break
            if r<=n:
                if arr[r]!=0 and arr[r]<=k:
                    result=abs(r-m)
                    break
        
        return result
    
if __name__ == "__main__":
    
    arr_temp = [int(item) for item in input().strip().split()]
    
    n = int(arr_temp[0])
    m = int(arr_temp[1])
    k = int(arr_temp[2])
    
    arr = [int(item) for item in input().strip().split()]
    arr.insert(0,0)
    
    sol = Solution()
    result = sol.solution(n, m, k, arr)
    
    print(result)

3、筛选宝物

在这里插入图片描述
已知存在n个宝物, 每个宝物都有自己的质量m和价值v, 在考虑选择宝物时只能选择总质量小于等于M的方案, 请问在最优方案下选择宝物, 能获取到最大价值V是多少?

输入描述

第一行输入宝物的数量n(1<n<100)和可选择宝物的总质量M(0<=M<=1000)。
以下n行每行输入两个数m和v(1<m<100, 1<v<100),表示这n个宝物其各自的重量和价值。

输出描述

输出最优方案下能获取的最大价值V。
说明:

v代表每个宝物自己的价值

m代表每个宝物的质量

V代表最大价值

M代表总质量

输入样例

5 10
2 3
5 3
4 5
6 2
4 2

输出样例

10

源码

典型的01背包问题

class Solution:
    def __init__(self) -> None:
        pass
    
    def solution(self, n, M, vector):
        result = None
        
        # TODO: 请在此编写代码
        V=[0]*1000
        for i in range(1,n+1):
            for j in range(M,vector[i][0]-1,-1):
                V[j]=max(V[j],V[j-vector[i][0]]+vector[i][1])
        result=V[M]
        return result

if __name__ == "__main__":
    
    arr_temp = [int(item) for item in input().strip().split()]
    
    n = int(arr_temp[0])
    M = int(arr_temp[1])
    
    vector = []
    vector.append([0,0])
    for i in range(n):
        vector.append([int(item) for item in input().strip().split()])
    sol = Solution()
    result = sol.solution(n, M, vector)
    print(result)

4、圆桌

在这里插入图片描述
有N个客人与足够多张的圆桌。 主人安排每位客人坐在一个圆桌边, 但是每位客人希望自己左右边上分别有一些空座位,不然会觉得害羞。 注意, 如果一个客人所在的圆桌只有他一个人, 那么他左边的空座位数量就是他右边的空座位数量。
试问主人需要准备多少个座位, 才能让每个客人舒适的坐下。

输入描述

第一行输入一个整数N,(1<=N<=10000),代表客人的数量
接下来N行,每行两个整数li与ri,(1<=i<=N,1<=li<=ri<=1000000000)
代表第i位客人希望左边有li个空座位,右边有ri个空座位。

输出描述

输出一个整数,代表主人需要准备的最少座位数量。

输入样例

3
1 1
1 1
1 1

输出样例

6

英文题面

Social Circles
time limit per test: 2 seconds
memory limit per test: 512 megabytes
input: standard input
output: standard output

You invited n \pmb{n} nnn guests to dinner! You plan to arrange one or more circles of chairs. Each chair is going to be either occupied by one guest, or be empty. You can make any number of circles.

Your guests happen to be a little bit shy, so the i \pmb{i} iii-th guest wants to have a least l i \pmb{l_i} lilili free chairs to the left of his chair, and at least r i \pmb{r_i} ririri free chairs to the right. The “left” and “right” directions are chosen assuming all guests are going to be seated towards the center of the circle. Note that when a guest is the only one in his circle, the l i \pmb{l_i} lilili chairs to his left and r i \pmb{r_i} ririri chairs to his right may overlap.

What is smallest total number of chairs you have to use?

Input
First line contains one integer n \pmb{n} nnn — number of guests, ( 1 ⩽ n ⩽ 1 0 5 ) (\pmb{1}⩽\pmb{n}⩽\pmb{10^5}) (111nnn105105105).

Next n lines contain n \pmb{n} nnn pairs of space-separated integers l i \pmb{l_i} lilili and r i ( 0 ⩽ l i , r i ⩽ 1 0 9 ) \pmb{r_i}(\pmb{0}⩽\pmb{l_i},\pmb{r_i}⩽\pmb{10^9}) ririri(000lilili,ririri109109109).

Output
Output a single integer — the smallest number of chairs you have to use.

Examples
input

3
1 1
1 1
1 1

output

6

input

1
5 6

output

7

Note
In the second sample the only optimal answer is to use two circles: a circle with 5 \pmb{5} 555 chairs accomodating guests 1 \pmb{1} 111 and 2 \pmb{2} 222, and another one with 10 \pmb{10} 101010 chairs accomodationg guests 3 \pmb{3} 333 and 4 \pmb{4} 444.

In the third sample, you have only one circle with one person. The guest should have at least five free chairs to his left, and at least six free chairs to his right to the next person, which is in this case the guest herself. So, overall number of chairs should be at least 6+1=7.

源码

class Solution:
    def __init__(self) -> None:
        pass

    def solution(self, n, l,r):
        result = None

        # TODO: 请在此编写代码
        l.sort()
        r.sort()
        result=0
        for i in range(n):
            result+=max(l[i],r[i])
        result+=n;

        return result

if __name__ == "__main__":

    n = int(input().strip())

    l,r=[],[]
    for i in range(n):
        a,b=map(int,input().split())
        l.append(a),r.append(b)

    sol = Solution()
    result = sol.solution(n, l,r)

    print(result)
  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Colazxk.xyz

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值