摘要: 前缀和差分 一维前缀和 主要的作用是可以快速的求出数组中任意一段区间的和 核心代码 S[i] = a[1] + a[2] + ... a[i]//S是前缀和数组,下边从1开始,方便边界的计算 a[l] + ... + a[r] = S[r] - S[l - 1] 题目链接 二维前缀和 类似于一维前缀 阅读全文
posted @ 2021-01-13 18:27 Lngstart 阅读(79) 评论(0) 推荐(0) 编辑
摘要: AcWing 788.逆序对的数量 原题链接 思路 暴力的思路 暴力是最容易想到的思路,但是时间复杂度有点高,当数组中数比较多的时候一般会超时 代码如下: ll res = 0; for(int i = 0; i < n; ++ i){ for(int j = i + 1; j < n; ++ j) 阅读全文
posted @ 2021-01-11 10:41 Lngstart 阅读(99) 评论(0) 推荐(0) 编辑
摘要: 原题链接 题解 要明确一个东西,他们两个人行动不是同步的,所以只要分别对他们两个人跑一边BFS,最后统计最小的时间即可 代码如下 #include <iostream> #include <cstring> #include <algorithm> #include <queue> #include 阅读全文
posted @ 2020-08-19 08:52 Lngstart 阅读(149) 评论(0) 推荐(0) 编辑
摘要: 原题链接 题解 题目的意思是给你两个素数a,b,问你每次只能改变一个数字a-->b的素数路径最小是多少。先利用素数筛把素数10000以内的素数选出来,方便计算。然后直接利用BFS枚举所有的情况。 代码如下 #include <iostream> #include <cstring> #include 阅读全文
posted @ 2020-08-11 09:38 Lngstart 阅读(100) 评论(0) 推荐(0) 编辑
摘要: 原题链接 题解 这个题目貌似是个模拟题,直接模拟就是了 代码如下 #include <iostream> #include <cstring> #include <algorithm> #include <set> using namespace std; set<string> s; int ma 阅读全文
posted @ 2020-08-11 09:33 Lngstart 阅读(63) 评论(0) 推荐(0) 编辑
摘要: 原题链接 题解 题目中给了两个杯子,可以利用BFS直接搜索枚举6种情况就可以了,每个杯子中的水量就是他的状态。这里要记录他的操作路径,可以使用两个数组,一个记录他是由哪个点拓展来得,一个记录这个点的操作(也可以直接在结构体中加个字符串记录操作) 代码如下 #include <iostream> #i 阅读全文
posted @ 2020-08-08 08:46 Lngstart 阅读(248) 评论(0) 推荐(0) 编辑
摘要: 原题链接 题意解释 一个人从数轴上的某一点x出发,要去数轴上的另一个点y,只有每分钟可以选择步行x + 1 、x - 1和传送x * 2这三种走法,问最少需要多少时间才能走到y点 题解 使用一个BFS直接跑就可以了,用一个数组标记一下这个点是否到达过,如果到达过,这个点就不能再走了,因为BFS第一次 阅读全文
posted @ 2020-08-06 10:44 Lngstart 阅读(66) 评论(0) 推荐(0) 编辑
摘要: 原题链接 题解 直接手动模拟过去,时间复杂度为O(n) 代码如下 class Solution { public: vector<int> distributeCandies(int candies, int num_people) { vector<int> res(num_people, 0); 阅读全文
posted @ 2020-07-19 14:36 Lngstart 阅读(85) 评论(0) 推荐(0) 编辑
摘要: 原题链接 题解 用BFS直接套就行 代码如下 class Solution { public: int st[11][11]; int orangesRotting(vector<vector<int>>& grid) { int lenx = grid.size(); if(lenx == 0) 阅读全文
posted @ 2020-07-19 14:30 Lngstart 阅读(129) 评论(0) 推荐(0) 编辑
摘要: 原题链接 题解 方式一:利用额外的空间 开一个额外的空间存放数据,最后再放回去 代码如下 class Solution { public: void merge(vector<int>& A, int m, vector<int>& B, int n) { int i = 0, j = 0; int 阅读全文
posted @ 2020-07-19 14:24 Lngstart 阅读(92) 评论(0) 推荐(0) 编辑