牛客假日团队赛11 部分题解【为了抽奖闲来做题】

本文精选牛客网算法竞赛题目,涵盖级数求和、路径计算、数的变换等,提供清晰的解题思路及AC代码示例,深入探讨算法设计与优化技巧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

链接:https://ac.nowcoder.com/acm/contest/1077/A
来源:牛客网

A.级数求和

题目描述

已知:Sn= 1+1/2+1/3+…+1/n。显然对于任意一个整数K,当n足够大的时候,Sn大于K。
现给出一个整数K(1<=k<=15),要求计算出一个最小的n;使得Sn>K。

输入描述:
输入k

输出描述:
输出n

输入

1

输出

2

Solution

在这里插入图片描述

AC Code

#include <iostream>
using namespace std;
int t[]={1,2,4,11,31,83,227,616,1674,4550,12367,33617,91380,248397,675214};
int main(){
    int n; cin >> n; cout<< t[n] << endl;
    return 0;
}
k = int(input())
res = 0.0
for i in range(1, 5000000):
    res = res+1.0/i
    if res > k:
        print(i)
        exit(0)


过河卒

链接:https://ac.nowcoder.com/acm/contest/1077/H
来源:牛客网

题目描述

如图,A 点有一个过河卒,需要走到目标 B 点。卒行走规则:可以向下、或者向右。同时在棋盘上的任一点有一个对方的马(如上图的C点),该马所在的点和所有跳跃一步可达的点称为对方马的控制点。例如上图 C 点上的马可以控制 9 个点(图中的P1,P2 … P8 和 C)。卒不能通过对方马的控制点。

棋盘用坐标表示,A 点(0,0)、B 点(n,m)(n,m 为不超过 20 的整数,并由键盘输入),同样马的位置坐标是需要给出的(约定: C<>A,同时C<>B)。现在要求你计算出卒从 A 点能够到达 B 点的路径的条数。

输入:
输入B点的坐标(n,m)以及对方马的坐标(X,Y){不用判错}

输出描述:
输出一个整数(路径的条数)。

输入

6 6 3 2

输出

17

Solution

 卒只能往右边和下面走,因此排除掉危险的点,直接dp即可

AC Code

#  Copyright (c) 2019 Ng Kimbing, HNU, All rights reserved. May not be used, modified, or copied without permission.
#  @Author:NgKimbing
#  @LastModified:2019-08-18 T 21:43:35.914 +08:00
from myIo import *

dx = [-2, -2, -1, 1, 2, 2, 1, -1]
dy = [-1, 1, 2, 2, 1, -1, -2, -2]
vis = getArray2D(25, 25)
f = getArray2D(25, 25)
f[0][0] = 1
if __name__ == '__main__':
    n = nextInt()
    m = nextInt()
    x = nextInt()
    y = nextInt()
    vis[x][y] = 1
    for i in range(8):
        nx = x + dx[i]
        ny = y + dy[i]
        if nx < 0 or nx > n or ny < 0 or ny > m:
            continue
        vis[nx][ny] = 1
    for i in range(n + 1):
        for j in range(m + 1):
            if not vis[i][j]:
                if i == 0 and j == 0:
                    continue
                if i != 0:
                    f[i][j] = f[i - 1][j]
                if j != 0:
                    f[i][j] = f[i][j] + f[i][j - 1]
    print(f[n][m])

链接:https://ac.nowcoder.com/acm/contest/1077/C
来源:牛客网

产生数

题目描述

给出一个整数 n(n<1030) 和 k 个变换规则(k<=15)。
规则:
一位数可变换成另一个一位数:规则的右部不能为零。
例如:n=234。有规则(k=2):
2-> 5
3-> 6
上面的整数 234 经过变换后可能产生出的整数为(包括原数):
234
534
264
564
共 4 种不同的产生数
问题:
给出一个整数 n 和 k 个规则。
求出:

经过任意次的变换(0次或多次),能产生出多少个不同整数。仅要求输出个数。

输入描述:
输入格式为:
n k
x1 y1
x2 y2
… …
xn yn

输出描述:
一个整数(满足条件的个数)
示例1

输入

234 2
2 5
3 6

输出

4

Solution

 大数

AC Code

核心代码

def count(currId: int) -> int:
    global cnt
    if visited[currId] == 1:
        return cnt
    cnt = cnt + 1
    visited[currId] = 1
    for i in range(10):
        if matrix[currId][i] == 1:
            count(i)
    return cnt
    
if __name__ == '__main__':
    ans = 1
    s = next()
    n = int(s)
    k = nextInt()
    if k == 0:
        print(1)
        exit(0)
    for i in range(k):
        a, b = nextInt(), nextInt()
        matrix[a][b] = 1
    for c in s:
        visited, cnt = [0] * 10, 0
        foo = count(int(c))
        # print("foo {}".format(foo))
        ans = ans * foo
    print(ans)

链接:https://ac.nowcoder.com/acm/contest/1077/B
来源:牛客网

produced much more milk than usual, surely because contented cows make more milk.
The cows disagree, though, on which is the best game console. One cow wanted to buy the Xbox 360 to play Halo 3; another wanted to buy the Nintendo Wii to play Super Smash Brothers Brawl; a third wanted to play Metal Gear Solid 4 on the PlayStation 3. FJ wants to purchase the set of game consoles (no more than one each) and games (no more than one each – and within the constraints of a given budget) that helps his cows produce the most milk and thus nourish the most children.
FJ researched N (1 <= N <= 50) consoles, each with a console price Pi (1 <= Pi <= 1000) and a number of console-specific games Gi (1 <= Gi <= 10). A cow must, of course, own a console before she can buy any game that is specific to that console. Each individual game has a game price GPj (1 <= GPj price <= 100) and a production value (1 <= PVj <= 1,000,000), which indicates how much milk a cow will produce after playing the game. Lastly, Farmer John has a budget V (1 <= V <= 100,000) which is the maximum amount of money he can spend. Help him maximize the sum of the production values of the games he buys.
Consider one dataset with N=3 consoles and a V=$800 budget. The first console costs $300 and has 2 games with cost $30 and $25 and production values as shown:

Game # Cost Production Value
1 $30 50
2 $25 80

The second console costs $600 and has only 1 game:
Game # Cost Production Value
1 $50 130

The third console costs $400 and has 3 games:
Game # Cost Production Value
1 $40 70
2 $30 40
3 $35 60

Farmer John should buy consoles 1 and 3, game 2 for console 1, and games 1 and 3 for console 3 to maximize his expected production at 210:
Production Value
Budget: $800
Console 1 -$300
Game 2 -$25 80
Console 3 -$400
Game 1 -$40 70
Game 3 -$35 60
-------------------------------------------
Total: 0 (>= 0) 210

输入描述:

  • Line 1: Two space-separated integers: N and V
  • Lines 2…N+1: Line i+1 describes the price of and the games ?available for console i; it contains: Pi, Gi, and Gi pairs of space-separated integers GPj, PVj

输出描述:

  • Line 1: The maximum production value that Farmer John can get with his budget.

输入

3 800
300 2 30 50 25 80
600 1 50 130
400 3 40 70 30 40 35 60

输出

210

Solution

 这题比较难搞,也比较有趣
根据题意,应该是个背包问题,只不过选取物品时有前提条件
做法是先假设去满足这个前提条件,更新最值,判断这个前提条件是否应该被满足

AC Code

python超时了,只能改为cpp

#  Copyright (c) 2019 Ng Kimbing, HNU, All rights reserved. May not be used, modified, or copied without permission.
#  @Author:NgKimbing
#  @LastModified:2019-08-18 T 21:43:35.914 +08:00
from myIO import *

# 前i个console,有j的budget的 最大production
dp = visited = [[0] * 100005 for i in range(50)]
if __name__ == '__main__':
    n, budget = nextInt(), nextInt()
    for i in range(1, n + 1):
        console_cost, game_num = nextInt(), nextInt()
        # print(dp[1][500])
        for j in range(budget, console_cost - 1, -1):
            dp[i][j] = dp[i - 1][j - console_cost]
        for _ in range(game_num):
            game_cost, production = nextInt(), nextInt()
            for j in range(budget, game_cost + console_cost - 1, -1):
                dp[i][j] = max(dp[i][j], dp[i][j - game_cost] + production)
        for j in range(budget + 1):
            dp[i][j] = max(dp[i - 1][j], dp[i][j])
    print(dp[n][budget])
    # print(dp)
#include<bits/stdc++.h>
using namespace std;
 
//# 前i个console,有j的budget的 最大production
int dp[55][100005];
int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n, budget;
    cin >> n >> budget;
    for(int i = 1; i <= n; ++i){
        int console_cost, game_num;
        cin >> console_cost >> game_num;
//        # print(dp[1][500])
        for(int j = budget; j >= console_cost; --j)
            dp[i][j] = dp[i - 1][j - console_cost];
        for(int _ = 0; _ < game_num; ++_){
            int game_cost, production;
            cin >> game_cost >> production;
            for(int j = budget; j >= game_cost + console_cost; --j)
                dp[i][j] = max(dp[i][j], dp[i][j - game_cost] + production);
        }
        for(int j = 0; j <= budget; ++j)
            dp[i][j] = max(dp[i - 1][j], dp[i][j]);
    }
    cout << dp[n][budget];
//    print(dp[n][budget])
//    # print(dp)
    return 0;
}

链接:https://ac.nowcoder.com/acm/contest/1077/E
来源:牛客网

Lonesome Partners

题目描述

Bessie and the rest of the herd totaling N (2 <= N <= 500) cows have gone to the dance. For the cows-only part of the dance, two cows are chosen as the “Belles of the Ball”. Farmer John records the X,Y coordinates (0 <= X_iX
i

<= 5,000; 0 <= Y_iY
i

<= 5,000) of all the cows in the dance hall and then asks you to determine the indices of the two cows who are farthest apart (which, happily, is guaranteed to be unique). Distance is the normal cartesian distance calculated as the square root of the sum of the squares of the differences in the row and column coordinates.
In a dance with just eight cows:
8 | . C . . . . . . . .
7 | . . . . . . . . . .
6 | . C . . . . . . . .
5 | . . . C C . C . . .
4 | . . . . C . . . . .
3 | . . C . . . . . . .
2 | . . . . . . . . . .
1 | . . . . . . . . . C
0 ±-------------------
0 1 2 3 4 5 6 7 8 8 9
Farmer John hopes you would choose the cows at 2,8 and 9,1 as farthest apart.

输入描述:

  • Line 1: A single integer: N
    Lines 2…N+1: Line i+1 contains the integer coordinate location of cow i: X_i
    and Y_i

输出描述:

  • Line 1: Two sorted integers that are the indices of the two cows that are located farthest apart.

输入

8
2 6
3 3
2 8
4 5
7 5
5 5
9 1
5 4

输出

3 7

说明
Cow #3 and Cow #7 are the cow numbers of the cows from the example in the text.

Solution

在这里插入图片描述
就不解释了

AC Code

队友:OuJian

#include <iostream>
using namespace std;
const int maxn = 1e3+5;
int X[maxn],Y[maxn],n,an,a,b;
int main(){
    cin>>n;
    for(int i=1;i<=n;i++)
        cin>>X[i]>>Y[i];
    for(int i=1;i<=n;i++){
        for(int j=i+1;j<n;j++) if(abs(X[i]-X[j])+abs(Y[i]-Y[j])>an){
            an=abs(X[i]-X[j])+abs(Y[i]-Y[j]);
            a=i;b=j;
        }
    }
    cout<<a<<" "<<b<<"\n";
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值