j
∣
∣
y
i
−
y
j
∣
d(i,j)=|x_{i}-x_{j}|+|y_{i}-y_{j}|
d(i,j)=∣xi−xj∣+∣yi−yj∣
矩阵中的数字是蛇形排列,难点是求出数字的坐标,我们可以借鉴二维数组,将矩阵中的所有数-1,便于求我们的行号:
原来1的坐标是(1, 1),现在是(0, 1),1的行号:1 / 6 = 0,设这个两数为n和m,w是行的宽度,行号 :n / w, m / w
列号怎么求呢?我们看一下排序正常的二维数组:
列号:n % w, m % w
但题目是蛇形排列的,也就是所有的奇数行我们应该把坐标翻转一下,这里特判一下就可以。
模拟
时间复杂度
O
(
1
)
O(1)
O(1)
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int w = sc.nextInt();
int m = sc.nextInt() - 1;
int n = sc.nextInt() - 1; // 起始位置从0开始 便于计算行列坐标
int x1 = m / w, x2 = n / w; // 行号
int y1 = m % w, y2 = n % w; // 列号
if (x1 % 2 != 0) y1 = w - 1 - y1; // 对奇数列进行特判 翻转y坐标
if (x2 % 2 != 0) y2 = w - 1 - y2;
System.out.print(Math.abs(x1 - x2) + Math.abs(y1 - y2)); // 曼哈顿距离公式
}
}
第八届2017年蓝桥杯真题
AcWing 1229. 日期问题
JavaB组第7题
枚举+模拟
我们枚举 19600101~20591231
之间的所有数:① 判断是否合法 ② 判断是否可能是给定的表示
import java.util.Scanner;
public class Main {
static int[] days = new int[]{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; // 平年每个月的天数
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str = sc.next();
String[] data = str.split("/");
int a = Integer.parseInt(data[0]);
int b = Integer.parseInt(data[1]);
int c = Integer.parseInt(data[2]);
for (int date = 19600101; date <= 20591231; date++) {
int year = date / 10000;
int month = date % 10000 / 100;
int day = date % 100;
if (check\_valid(year, month, day)) {
if (year % 100 == a && month == b && day == c || // 年/月/日
month == a && day == b && year % 100 == c || // 月/日/年
day == a && month == b && year % 100 == c) { // 日/月/年
System.out.printf("%d-%02d-%02d\n", year, month, day); // 格式化输出 记得加上'-'
}
}
}
}
private static boolean check\_valid(int year, int month, int day) {
if (month == 0 || month > 12) return false;
if (day == 0 || month != 2 && day > days[month]) return false;
if (month == 2) {
int leap = 0;
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) leap = 1;
if (day > 28 + leap) return false;
}
return true;
}
}
第九届2018年蓝桥杯真题
AcWing 1231. 航班时间
JavaA组第6题
从北京飞到中东,我们不知道与中东的时差,但我们先整理飞行时间的公式:首先,飞机从北京飞到中东,降落时间减去起飞时间还需要加上时差:end1 - start1 + time
,飞机从中东飞回北京,降落时间减去起飞时间还需要减去时差:end2 - start2 - time
,我们求的是飞机一趟的飞行时间,因为飞机来回的飞行时间相同,我们可以将来回的飞行时间相加:得到end1 - start1 + end2 - start2
,我们发现时差time
被抵消掉了,所以本题跟时差没有关系,飞机的飞行时间:
t
=
(
e
n
d
1
−
s
t
a
r
t
1
e
n
d
2
−
s
t
a
r
t
2
)
/
2
t = (end1 - start1 + end2 - start2) / 2
t=(end1−start1+end2−start2)/2
公式我们已经推出,接下来要处理的就是字符串的输入输出问题了。
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
public class Main {
static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
public static void main(String[] args) throws IOException {
String[] input = in.readLine().split(" ");
int n = Integer.parseInt(input[0]);
while (n-- > 0) {
int time = (get\_time() + get\_time()) / 2;
int hour = time / 3600, minute = time % 3600 / 60, second = time % 60;
System.out.printf("%02d:%02d:%02d\n", hour, minute, second);
}
}
private static int get\_time() throws IOException {
String[] line = in.readLine().split(":| "); // 分隔:和空格
int d = 0;
if (line.length == 7) d = line[6].charAt(2) - '0'; // 如果长度为7说明字符串后面有(+n) 记得转为int
int h1 = Integer.parseInt(line[0]);
int m1 = Integer.parseInt(line[1]);
int s1 = Integer.parseInt(line[2]);
int h2 = Integer.parseInt(line[3]);
int m2 = Integer.parseInt(line[4]);
int s2 = Integer.parseInt(line[5]);
return get\_seconds(h2, m2, s2) - get\_seconds(h1, m1, s1) + d \* 24 \* 3600;
}
// 将所有时间转化成距离当天00:00:00的秒数
private static int get\_seconds(int h, int m, int s) {
return h \* 3600 + m \* 60 + s;
}
}
第十届2019年蓝桥杯真题
AcWing 1241. 外卖店优先级
JavaB组第7题
分析第一个样例:
最终在6时刻时,只有外卖2在优先缓存中,答案输出1。
暴力枚举(内存超限)
时间复杂度
O
(
N
2
)
O(N^2)
O(N2)
枚举每一个时刻的外卖订单
AcWingMLE,10个数据过了7个,蓝桥杯满分100分拿到了90分,最后一个数据内存超限,但也是很可观的分数了。
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt(), m = sc.nextInt(), t = sc.nextInt();
// 数组定义在全局变量 节省内存
int[][] a = new int[t + 1][n + 1]; // 订单 i为时刻 j为外卖编号
int[] p = new int[n + 1]; // 优先级
for (int i = 0; i < m; i++) a[sc.nextInt()][sc.nextInt()]++;
for (int i = 1; i < t + 1; i++) {
for (int j = 1; j < n + 1; j++) {
if (a[i][j] > 0) {
a[i][j] = a[i - 1][j] + a[i][j] \* 2; // 加上此订单的优先级
if (a[i][j] > 5)
p[j] = 1; // 将此时刻的外卖置入优先级
} else {
if (a[i - 1][j] > 0) {
a[i][j] = a[i - 1][j] - 1;
if (a[i][j] <= 3)
p[j] = 0;
}
}
}
}
int res = 0;
for (int i = 0; i < p.length; i++) res += p[i];
System.out.println(res);
}
}
优化
我们每个外卖获取到订单的时刻都是离散的,中间可能过了好久才有第二个订单,其实可以把中间这一部分压缩掉,把连续的没有订单的这一段时间统一到下一次有订单的时刻来处理,或者是放在T时刻来处理:
这样做的好处是我们在去做每一个时刻的时候,只需要考虑有订单的店即可,没有订单的店就可以不用考虑了。
此时我们可以不用枚举时刻,将所有订单按时间顺序排序,每次处理一批相同的订单,只要订单的时间点和店铺id相同,我们就把它看成一个。
score[i]
表示第i
个店铺当前的优先级
last[i]
表示第i
个店铺上一次有订单的时刻
st[i]
表示第i
个店铺当前是否处于优先缓存中
伪代码
处理t时刻前的内容:
这样就可以把中间没有订单的时刻压缩:
score[id] -= t - last[id] - 1; // 假如第3和第6时刻都有订单 没卖东西的时间应该是4和5 所以需要减一 6-3-1=2
还要加两个判断:
if (score[id] < 0) score[id] = 0;
if (score[id] <= 3) st[id] = false; // 移出优先缓存
更新一下last:last[id] = t
处理t时刻的内容:
score[id] += cnt \* 2; // 加上优先级
if (score[id] > 5) st[id] = true; // 置于优先缓存
每一个店铺最后一段时间可能都没有订单,我们还要算一下最后一段时间,last[id] ~ T
之间有多长时间没有卖东西:
for (int i = 1; i <= n; i++) {
if (last[i] < T) {
score[i] -= T - last[i]; // 此时不用-1,因为T时刻没有订单
if (score[i] <= 3) st[i] = false;
}
}
求得结果:
int res = 0;
for (int i = 1; i <= n; i++) {
if (st[i]) res++;
}
sout(res);
完整代码
import java.util.Scanner;
import java.util.Arrays;
public class Main {
static final int N = 100010;
static int[] score = new int[N]; // 店铺的优先级
static int[] last = new int[N]; // 上一次有订单的时刻
static boolean[] st = new boolean[N]; // 表示店铺是否处于优先缓存中
static PII[] order = new PII[N]; // 订单
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt(), m = sc.nextInt(), T = sc.nextInt();
for (int i = 0; i < m; i++) order[i] = new PII(sc.nextInt(), sc.nextInt());
Arrays.sort(order, 0, m);
for (int i = 0; i < m;) { // 循环为了找到相同订单
int j = i;
while (j < m && order[j].ts == order[i].ts && order[j].id == order[i].id) j++;
int t = order[i].ts, id = order[i].id;
int cnt = j - i; // 同批订单的数量
i = j;
// 处理t时刻之前的信息
score[id] -= t - last[id] - 1; // 中间没有订单的数量
if (score[id] < 0) score[id] = 0;
if (score[id] <= 3) st[id] = false; // 移出优先缓存
// 处理t时刻的信息
score[id] += cnt \* 2; // 加上优先级
if (score[id] > 5) st[id] = true; // 置于优先缓存
last[id] = t;
}
for (int i = 1; i <= n; i++) {
if (last[i] < T) {
score[i] -= T - last[i]; // 此时不用-1,因为T时刻没有订单
if (score[i] <= 3) st[i] = false;
}
}
int res = 0;
for (int i = 1; i <= n; i++) if (st[i]) res++;
System.out.println(res);
}
static class PII implements Comparable<PII> {
int ts;
int id;
public PII(int ts, int id) {
this.ts = ts;
this.id = id;
}
@Override
public int compareTo(PII o) {
if(this.ts > o.ts) return 1;
if(this.ts == o.ts) {
if (this.id > o.id) return 1;
return -1;
}
return -1;
}
}
}
有对代码不理解的地方可以在下方评论
如何自学黑客&网络安全
黑客零基础入门学习路线&规划
初级黑客
1、网络安全理论知识(2天)
①了解行业相关背景,前景,确定发展方向。
②学习网络安全相关法律法规。
③网络安全运营的概念。
④等保简介、等保规定、流程和规范。(非常重要)
2、渗透测试基础(一周)
①渗透测试的流程、分类、标准
②信息收集技术:主动/被动信息搜集、Nmap工具、Google Hacking
③漏洞扫描、漏洞利用、原理,利用方法、工具(MSF)、绕过IDS和反病毒侦察
④主机攻防演练:MS17-010、MS08-067、MS10-046、MS12-20等
3、操作系统基础(一周)
①Windows系统常见功能和命令
②Kali Linux系统常见功能和命令
③操作系统安全(系统入侵排查/系统加固基础)
4、计算机网络基础(一周)
①计算机网络基础、协议和架构
②网络通信原理、OSI模型、数据转发流程
③常见协议解析(HTTP、TCP/IP、ARP等)
④网络攻击技术与网络安全防御技术
⑤Web漏洞原理与防御:主动/被动攻击、DDOS攻击、CVE漏洞复现
5、数据库基础操作(2天)
①数据库基础
②SQL语言基础
③数据库安全加固
6、Web渗透(1周)
①HTML、CSS和JavaScript简介
②OWASP Top10
③Web漏洞扫描工具
④Web渗透工具:Nmap、BurpSuite、SQLMap、其他(菜刀、漏扫等)
恭喜你,如果学到这里,你基本可以从事一份网络安全相关的工作,比如渗透测试、Web 渗透、安全服务、安全分析等岗位;如果等保模块学的好,还可以从事等保工程师。薪资区间6k-15k
到此为止,大概1个月的时间。你已经成为了一名“脚本小子”。那么你还想往下探索吗?
如果你想要入坑黑客&网络安全,笔者给大家准备了一份:282G全网最全的网络安全资料包评论区留言即可领取!
7、脚本编程(初级/中级/高级)
在网络安全领域。是否具备编程能力是“脚本小子”和真正黑客的本质区别。在实际的渗透测试过程中,面对复杂多变的网络环境,当常用工具不能满足实际需求的时候,往往需要对现有工具进行扩展,或者编写符合我们要求的工具、自动化脚本,这个时候就需要具备一定的编程能力。在分秒必争的CTF竞赛中,想要高效地使用自制的脚本工具来实现各种目的,更是需要拥有编程能力.
如果你零基础入门,笔者建议选择脚本语言Python/PHP/Go/Java中的一种,对常用库进行编程学习;搭建开发环境和选择IDE,PHP环境推荐Wamp和XAMPP, IDE强烈推荐Sublime;·Python编程学习,学习内容包含:语法、正则、文件、 网络、多线程等常用库,推荐《Python核心编程》,不要看完;·用Python编写漏洞的exp,然后写一个简单的网络爬虫;·PHP基本语法学习并书写一个简单的博客系统;熟悉MVC架构,并试着学习一个PHP框架或者Python框架 (可选);·了解Bootstrap的布局或者CSS。
8、超级黑客
这部分内容对零基础的同学来说还比较遥远,就不展开细说了,附上学习路线。
网络安全工程师企业级学习路线
如图片过大被平台压缩导致看不清的话,评论区点赞和评论区留言获取吧。我都会回复的
视频配套资料&国内外网安书籍、文档&工具
当然除了有配套的视频,同时也为大家整理了各种文档和书籍资料&工具,并且已经帮大家分好类了。
一些笔者自己买的、其他平台白嫖不到的视频教程。
网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!