牛客OI赛制测试赛1

本文详细解析了牛客OI比赛中的五道编程题目,涉及斐波那契数列、序列划分、城市巡查费用计算、树直径问题和旅行青蛙路线规划。通过思路分享和代码展示,帮助参赛者理解和解决问题。

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

A:

斐波那契

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

时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 32768K,其他语言65536K
64bit IO Format: %lld

题目描述

设f[i]表示斐波那契数论的第i项
f[1]=1,f[2] =1,f[i] = f[i - 1] + f[i - 2]
给定一个n

 

输入描述:

一个整数n

输出描述:

一个整数,表示答案

 

示例1

输入

复制

4

输出

复制

1

备注:

对于的数据,
对于的数据,
对于的数据,
对于的数据,

思路:

一道推理题,多往下写几个例子就会发现n为偶数就是1,奇数为-1

代码:

#include<bits/stdc++.h>
using namespace std;
const int maxn = (int)1e6 + 10;
char s[maxn];
int main()
{
	scanf("%s",s);
	int len = strlen(s);
	if ((s[len - 1] - '0') & 1)
		printf("-1\n");
	else
		printf("1\n");
	return 0;
}

B:

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

送分题

时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 32768K,其他语言65536K
64bit IO Format: %lld

题目描述

对于一套题来说,没有一道送分题,就很不符合常理,但是我又懒得写送分题,所以你可以直接复制以下代码,即可ac本题.

 
  1. #include<cstdio>#include<iostream>  using namespace std; int a,b,c;  int main(){long long l=1,r=int(1e9)<<1:cin》a>>b;while(r-l>1){c=(l+r)>>1;if(c-b<a)l=c;else if(c-b>a)r=c;else return printf("%d\n",c); }     if(l!=r)return printf("%d\n",r);      }

输入描述:

输入共一行,两个整数a和b,范围在int之间

输出描述:

输出一个整数表示答案

 

示例1

输入

复制

5 123

输出

复制

128

思路:

果然是OI题,给出的是一个有点毛病的a + b的二分写法,如果只是单纯的纠错只能过大部分数据,对于负数是过不了的,所以还需要手写a + b

代码;

#include<cstdio>
#include<iostream> 
using namespace std;
int a,b,c;
 int main()
{
 	long long sum;
	scanf("%d %d",&a,&b);
	sum = a + (long long)b;
	printf("%lld\n",sum);
	return 0;
} 

C:

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

序列

时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 32768K,其他语言65536K
64bit IO Format: %lld

题目描述

小a有n个数,他想把他们划分为连续的权值相等的k段,但他不知道这是否可行。

每个数都必须被划分

这个问题对他来说太难了,于是他把这个问题丢给了你。

输入描述:

第一行为两个整数n,q,分别表示序列长度和询问个数。
第二行有n个数,表示序列中的每个数。
接下来的q行,每行包含一个数k,含义如题所示。

输出描述:

输出q行,每行对应一个数Yes或者No,分别表示可行/不可行

 

示例1

输入

复制

5 3
2 1 3 -1 4
3
2
1

输出

复制

Yes
No
Yes

备注:

 

对于的数据,

对于的数据,

对于的数据,

设ai表示数列中的第i个数,保证

保证数据完全随机

思路:

刚看这道题就想到了二分,然而二分是明显爆时间的,其实没必要,因为是划分成等值的k段,所以每段分到的值就是sum  / k;

对于sum不能整除k的,一定不能分,注意因为有负数,所以即便一段的值大于sum / k,也不能跳出循环

代码:

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int maxn = (int)1e5 + 10;
ll a[maxn],n;
int solve(ll x)
{
	int cnt = 0,num = 0;
	for (int i = 0;i < n;i ++)
	{
		cnt += a[i];
		if (cnt == x)
		{
			num ++;
			cnt = 0;
		}
	}
	return num;
}
int main()
{
	int q;
	ll sum = 0;
	scanf("%d %d",&n,&q);
	for (int i = 0;i < n;i ++)
	{
		scanf("%lld",&a[i]);
		sum += a[i];
	}
	ll k;
	while (q --)
	{
		scanf("%lld",&k);
		if (sum % k)
			printf("No\n");
		else
		{
			if (solve(sum / k) == k)
				printf("Yes\n");
			else
				printf("No\n"); 
		}
	}
	return 0;
} 

D:

链接:https://www.nowcoder.com/acm/contest/181/D
来源:牛客网
 

小叶的巡查

时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 65536K,其他语言131072K
64bit IO Format: %lld

题目描述

8102年,牛客系列竞赛空前繁荣。为了更好地管理竞赛,小叶决定巡查于各大城市之间,体察民情。所以,从一个城市马不停蹄地到另一个城市成了小叶最常做的事情。小叶有一个钱袋,用于存放往来城市间的路费。

这个国家有一套优秀的交通方案,使得任何一个大城市都能从首都直接或者通过其他大城市间接到达。同时,如果不重复经过大城市,从首都到达每个大城市的方案都是唯一的。

如果不在某个城市停下来修整,在连续行进过程中,小叶所花的路费与他已走过的距离有关,在走第x-1千米到第x千米这一千米中(x是整数),他花费的路费是x+10这么多。也就是说走1千米花费11,走2千米要花费23。

因为国家力挺牛客系列竞赛,所以国家会给小叶报销全部的路费。

现在组织想知道:小叶从某一个城市出发,中间不休息,到达另一个城市,所有可能花费的路费中最多是多少呢?

输入描述:

输入的第一行包含一个整数n,表示包括首都在内的城市数
城市从1开始依次编号,1号城市为首都。
接下来n-1行,描述高速路(高速路一定是n-1条)
每行三个整数Pi, Qi, Di,表示城市Pi和城市Qi之间有一条高速路,长度为Di千米。

输出描述:

输出一个整数,表示小叶最多花费的路费是多少。

 

示例1

输入

复制

5
1 2 2
1 3 1
2 4 5
2 5 4

输出

复制

135

备注:

n<23333

思路:

一道很明显的树直径,最多也就是在计算路费的时候搞搞,其实也就是一个式子就行了

代码:

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int maxn = 233333 + 10;
vector<pair<int,int> > G[maxn];
int dis[maxn];
ll ans;
bool vis[maxn];

int bfs(int p)
{
	memset(dis,0,sizeof(dis));
	memset(vis,0,sizeof(vis));
	queue<int> que;
	que.push(p);
	vis[p] = 1;
	int point;
	while (!que.empty())
	{
		int f = que.front();
		que.pop();
		if (dis[f] > ans)
		{
			point = f;
			ans = dis[f];
		}
		pair<int,int> pr;
		for (int i = 0;i < G[f].size();i ++)
		{
			pr = G[f][i];
			if (!vis[pr.first])
			{
				vis[pr.first] = 1;
				dis[pr.first] = dis[f] + pr.second;
				que.push(pr.first);
			}
		}
	 }
	 return point;
}
int main()
{
	int n;
	scanf("%d",&n);
	int x,y,z;
	for (int i = 1;i < n;i ++)
	{
		scanf("%d %d %d",&x,&y,&z);
		G[x].push_back(make_pair(y,z));
		G[y].push_back(make_pair(x,z));
	}
	ans = -1;//ans 不能等于0,不然数据3,4段错误,具体原因未知
	int p = bfs(1);
	ans = -1;
	bfs(p);
	ll res = ans * 10 + ans * (ans + 1) / 2;
	printf("%lld\n",ans == -1 ? 0 : res);
	return 0;
}

E:

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

旅行青蛙

时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 262144K,其他语言524288K
64bit IO Format: %lld

题目描述

一只青蛙出去旅游,因为中国有一句古话说的好:“由简入奢易,由奢入俭难”,所以这只青蛙当看的当前景点比前面看过的景点差的时候,青蛙就会说“不开心”为了避免这只青蛙说“不开心”,并且使青蛙看的景点尽量的多,所以他请你帮忙给他安排一条线路,使青蛙可以看到尽量多的景点,并且不走回头路。

输入描述:

第一行为一个整数n,表示景点的数量
接下来n行,每行1个整数,分别表示第i个景点的质量

输出描述:

一个整数,表示青蛙最多可以看到几个景点

 

示例1

输入

复制

10
3
18
7
14
10
12
23
30
16
24

输出

复制

6

备注:

景点质量为1到n+23的整数
10<=n<23 10%

23<=n<233 30%

233<=n<2333 60%

2333<=n<23333 100%

思路:

最长不下降子序列,二分做法O(n*logn),注意这个二分应该用upper_bound(),因为lower_bound()的话会盖住等于与当前的值相等的;

代码:

#include<bits/stdc++.h>
using namespace std;
const int maxn = 23333 + 10;
int a[maxn];
int dp[maxn];
int main()
{
	int n,pos = 1;
	scanf("%d",&n);
	for (int i = 0;i < n;i ++)
		scanf("%d",&a[i]);
	dp[0] = a[0];
	for (int i = 1;i <= n;i ++)
	{
		int temp = upper_bound(dp,dp + pos,a[i]) - dp;
		if (a[i] >= dp[pos - 1])
			dp[pos ++] = a[i];
		else
			dp[temp] = a[i];
	}
	printf("%d\n",pos);
	return 0;
}

 

### 关于牛客小白月109的信息 目前并未找到关于牛客小白月109的具体比信息或题解内容[^5]。然而,可以推测该事可能属于牛客网举办的系列算法竞之一,通常这类比会涉及数据结构、动态规划、图论等经典算法问题。 如果要准备类似的事,可以通过分析其他场次的比题目来提升自己的能力。例如,在牛客小白月13中,有一道与二叉树相关的题目,其核心在于处理树的操作以及统计最终的结果[^3]。通过研究此类问题的解决方法,能够帮助理解如何高效地设计算法并优化时间复杂度。 以下是基于已有经验的一个通用解决方案框架用于应对类似场景下的批量更新操作: ```python class TreeNode: def __init__(self, id): self.id = id self.weight = 0 self.children = [] def build_tree(n): nodes = [TreeNode(i) for i in range(1, n + 1)] for node in nodes: if 2 * node.id <= n: node.children.append(nodes[2 * node.id - 1]) if 2 * node.id + 1 <= n: node.children.append(nodes[2 * node.id]) return nodes[0] def apply_operations(root, operations, m): from collections import defaultdict counts = defaultdict(int) def update_subtree(node, delta): stack = [node] while stack: current = stack.pop() current.weight += delta counts[current.weight] += 1 for child in current.children: stack.append(child) def exclude_subtree(node, total_nodes, delta): nonlocal root stack = [(root, False)] # (current_node, visited) subtree_size = set() while stack: current, visited = stack.pop() if not visited and current != node: stack.append((current, True)) for child in current.children: stack.append((child, False)) elif visited or current == node: if current != node: subtree_size.add(current.id) all_ids = {i for i in range(1, total_nodes + 1)} outside_ids = all_ids.difference(subtree_size.union({node.id})) for idx in outside_ids: nodes[idx].weight += delta counts[nodes[idx].weight] += 1 global nodes nodes = {} queue = [root] while queue: curr = queue.pop(0) nodes[curr.id] = curr for c in curr.children: queue.append(c) for operation in operations: op_type, x = operation.split(' ') x = int(x) target_node = nodes.get(x, None) if not target_node: continue if op_type == '1': update_subtree(target_node, 1) elif op_type == '2' and target_node is not None: exclude_subtree(target_node, n, 1) elif op_type == '3': path_to_root = [] temp = target_node while temp: path_to_root.append(temp) if temp.id % 2 == 0: parent_id = temp.id // 2 else: parent_id = (temp.id - 1) // 2 if parent_id >= 1: temp = nodes[parent_id] else: break for p in path_to_root: p.weight += 1 counts[p.weight] += 1 elif op_type == '4': pass # Implement similarly to other cases. result = [counts[i] for i in range(m + 1)] return result ``` 上述代码片段展示了针对特定类型的树形结构及其操作的一种实现方式。尽管它并非直接对应小白月109中的具体题目,但它提供了一个可借鉴的设计思路。 ####
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值