华中农业大学第五届程序设计大赛网络同步赛题解
A.Little Red Riding Hood
B.Choosy in Food
•F[i]:从第I个点到终点的期望步数
•F[i] = (F[i + k] + k ) * P[k]
•F[ed] = 0
•高斯消元求解
•注意存在从起点不能到达终点的情况
C.Friends
•F[rt][len] :节点rt的子孙里,距离rt的为len的节点个数
•ans[rt][len] : 整棵树上,距离rt为len的节点个数
•F值一次简单的深搜可以得到
•而ans[rt][len] = F[rt][len] + ans[fa(rt)][len – 1] – F[fa(rt)][len – 1]
D.GCD
E.One Stroke
•在每一个根到叶子结点的路径上用双指针即可求出一笔能画出的最多结点。
•双指针思路:枚举起点i,向右不断移动终点j,直到ij之间的和不大于k,复杂度O(nlogn)
•如果暴力优美也能过(T^T),复杂度O(nlognlogn)
F.Escape from the Darkness
G.Sequence Number
•这是一道排序可以过的题,也可以rmq+二分
•最快的写法可以用单调栈做到O(n)
H.Mathematical Game
I.Candies
•线段树中查询一个区间里,最长的连续B区间的长度
•区间合并时考虑左间的右部连续B加上右区间的左部连续B区间可能会更新这个区间的MAX_B值
•同时更新区间的左部连续B长度时,考虑左区间全部为B时,应加上右区间的左B长度
•更新区间的右部连续长度同理
•查询时,同样要根据更新的原理来更新查询的答案
J.Color Circle
• 搜索,DFS即可
K.Deadline
•这题只要想到思路还是很简单的,假设所有工程师每天都在修复bug,那么对天数记录bug的前缀和,O(n)得到答案max(pre[i]+i-1)/i)
L.Happiness
Description
Chicken brother is very happy today, because he attained N pieces of biscuits whose tastes are A or B. These biscuits are put into a box. Now, he can only take out one piece of biscuit from the box one time. As we all know, chicken brother is a creative man. He wants to put an A biscuit and a B biscuit together and eat them. If he take out an A biscuit from the box and then he take out a B biscuit continuously, he can put them together and eat happily. Chicken brother’s happiness will plus one when he eat A and B biscuit together one time.
Now, you are given the arrangement of the biscuits in the box(from top to bottom) ,please output the happiness of Chicken Brother when he take out all biscuit from the box.
Input
The first line is an integer indicates the number of test cases.
In each case, there is one line includes a string consists of characters ‘A’ and ‘B’.
The length of string is not more than 1000000.
Output
For each test case:
The first line output “Case #k:", k indicates the case number.
The second line output the answer.
Sample Input
1
ABABBBA
Sample Output
Case #1:
2
HINT
Source
题解:
•求字符串中AB出现的次数
•遍历一次字符串即可
特别提醒:此题出题人已挖好大坑等着萌新们入坑,我也是第一次遇到这种100W就TL的情况,原因我给你们分析一下,就是这个strlen的应用,之前我将strlen放在for循环内,复杂度为100W*100W,O(n^2)必然超时,只要把strlen提出来,复杂度降为100W+100W,O(n+n),神TM知道这个函数调用也会TL!
下面附上AC代码:
1 #include <bits/stdc++.h>
2 using namespace std;
3 char s[1000010];
4 int main()
5 {
6 int n;
7 while(cin>>n)
8 {
9 int ans=0;
10 for(int i=1;i<=n;i++)
11 {
12 scanf("%s",s);
13 printf("Case #%d:n",i);
14 int len=strlen(s);
15 int ans=0;
16 for(int j=0;j<len;j++)
17 {
18 if(s[j]=='A'&&s[j+1]=='B')
19 ans++;
20 }
21 printf("%dn",ans);
22 }
23 }
24 return 0;
25 }
- What is aspnet.config
- qq侧滑
- Novell 发布Mono 1.2 推动.NET跨平台
- 【Java SE】Java NIO系列教程(十一) Pipe
- 仿qq最新侧滑菜单
- 【高能】用PyMC3进行贝叶斯统计分析(代码+实例)
- 【Java SE】Java NIO系列教程(十二)Java NIO与IO
- 【贝叶斯系列】预测未来回报的交易算法基于Bayesian cone
- android viewgroup事件分发机制
- Rafy 领域实体框架示例(1) - 转换传统三层应用程序
- android view事件分发机制
- Rafy 领域实体框架演示(2) - 新功能展示
- ormlite介绍一
- 从Encoder到Decoder实现Seq2Seq模型(算法+代码)
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- 基于Java图形界面的IPV4与网址的地址解析器
- 如何在千里之外能访问自己的电脑?(FRP)
- 三分钟Docker-镜像、容器实战篇
- 看懂今天这个!你就是个真正的javaer!
- 猿进化系列7——一文搞懂IO
- 猿进化系列13——一文搞懂MVC相关框架套路
- 猿进化系列16——实战之一学会SQL开发正确姿势
- 猿进化系列17——实战之一文学会前后端分离套路
- 基于Java的模拟写字板的设计与实现
- 猿思考系列2——一文搞懂同步并发套路
- 猿思考系列3——一文搞懂单例和思考的套路
- 猿思考系列3——一文学会思考的正确姿势
- 猿思考系列4——一文学会java的斗转星移动
- 猿思考系列5——一文明白java和微商那点儿事儿
- 猿思考系列8——缓存的套路也就这些