目的:只是为了帮助一些上课的同学整理笔记,有什么错误也欢迎指出。
课程详情:活动 - AcWing
争取六月十日之前整理复习到dp
4.差分
一维差分 —— 模板题 AcWing 797. 差分 给区间[l, r]中的每个数加上c:B[l] += c, B[r + 1] -= c
思路(前缀和的逆运算,把原数组看成是前缀和的S[i])
一维差分
//步骤可以合并
import java.util.Scanner;
public class 差分1 {
static int[] b = new int[100010];
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
//输入m n
int n = sc.nextInt();//长度
int m = sc.nextInt();
//创建数组
int[] a = new int[n + 2];
for (int i = 1; i <= n; i++) {
//插入原数组
a[i] = sc.nextInt();
}
//初始化差分数组
for (int i = 1; i <= n; i++) {
//插入原数组
insert(i, i, a[i]);
}
//根据输入来计算差分数组
while(m-- > 0) {
int l = sc.nextInt();
int r = sc.nextInt();
int c = sc.nextInt();
insert(l,r,c);
}
//通过差分数组来复原原数组
for (int i = 1; i <= n; i++) {
if (i == 1) {
a[i] = b[i];
}else {
a[i] = b[i] + a[i - 1];
}
}
//循环遍历 不能用foreach
for (int x : a) {
System.out.print(x + " ");
}
}
private static void insert(int l, int r, int c) {
b[l] += c;
b[r + 1] -= c;
}
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
/*
* 前缀和数组需要比原数组多两位,
* 第0个是为了符合前缀和的s = a[i] + s[i - 1]
* 最后一个是为了符合差分的s[j+1] += c;
*/
public class Main{
public static void main(String args[]) throws NumberFormatException, IOException {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
int T = Integer.parseInt(bf.readLine());
while (T-- > 0) {
int n = Integer.parseInt(bf.readLine());
String[] s = bf.readLine().split(" ");
int[] arr = new int[n]; //前缀和数组
for (int i = 0; i < n; i++) {
arr[i] = Integer.parseInt(s[i]);
}
long[] dir = new long[n + 2];
for (int j = 1; j < n + 1; j++) {
if (arr[j-1] != 0) {
int c = arr[j-1];
int l = Math.max(1, j - c + 1);
int r = j + 1;
dir[l] += c;
dir[r] -= c;
}
}
//合成新数组,差分,不用再开数组
for (int j = 1; j < n + 1; j++) {
dir[j] += dir[j-1];
System.out.print(Math.min(dir[j], 1) + " ");
}
System.out.print("\n");
}
}
}
二维差分
x1,y1的前缀和是指其与其的右下矩阵
—— 模板题 AcWing 798. 差分矩阵 给以(x1, y1)为左上角,(x2, y2)为右下角的子矩阵中的所有元素加上c: S[x1, y1] += c, S[x2 + 1, y1] -= c, S[x1, y2 + 1] -= c, S[x2 + 1, y2 + 1] += c
5.双指针
归并排序的时候需要将两个子序列合并的时候用的是双指针算法。(快排也是双指针);
核心思想是:暴力写法是O(n2)的。将朴素算法使用双指针算法优化到O(n)。
package 双指针;
import java.util.*;
import java.io.*;
/*
* 两种算法:
* 1、开辟一个数组用来记录出现次数。类似于哈希表
* 2、直接使用hashmap,利用其键值对,无序不重复的性质
*/
public class 最长连续不重复子序列799 {
static int N = 100010;
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(reader.readLine());
int[] nums = new int[n];
String[] strs = reader.readLine().split(" ");
for (int i = 0; i < n; i++) {
nums[i] = Integer.parseInt(strs[i]);
}
int count = 0;
int[] arr = new int[N];
//1.哈希做法:双指针
for (int i = 0, j = 0; i < n; i++) {//i比j先走
arr[nums[i]]++;
while (arr[nums[i]] > 1 && j < i) {
arr[nums[j]]--;
j++;
}
count = Math.max(count, i - j + 1);
}
//2.直接使用hashmap
Map<Integer,Integer> map = new HashMap<>();
for (int i = 0, j = 0; i < n; i++) {//i比j先走
map.put(nums[i], map.getOrDefault(nums[i],0)+1); //注意这里要设置当值为null时的默认值
while (map.get(nums[i]) > 1 && j < i) {
map.put(nums[j], map.get(nums[j])-1);
j++;
}
count = Math.max(count, i - j + 1);
}
System.out.println(count);
reader.close(); // 记得关闭
}
}
常见问题分类: (1) 对于一个序列,两个指针维护一段区间 (2) 对于两个序列,维护某种次序,比如归并排序中合并两个有序序列的操作。
6.位运算
位运算 —— 模板题 AcWing 801. 二进制中1的个数
n >> k & 1:
一般:求n的二进制表示中第k位是几?
思路:
1.先把第k为移到最后一位:n >> k;
2.看个位的数字:x & 1;
so:n >> k & 1!!
lowbit(n) = n & -n(返回n的最后一位1 ):
一般:求二进制一共有几个1
7.离散化
特指升序整数
一般思路:
1.去重(存在重复元素的时候咋办)
2.快速的映射(如何算出a[i]离散化之后的值)(使用二分)
import java.util.*;
class Pair{
int first;
int second;
public Pair(int first, int second){
this.first = first;
this.second = second;
}
}
public class Main{
public static void main(String[] args){
var in = new Scanner(System.in);
int n = in.nextInt();
int m = in.nextInt();
List<Integer> alls = new ArrayList<Integer>();
int[] ori = new int[300010];
int[] sum = new int[300010];
//读取数据
for(int i = 0; i < n; i++){
int a = in.nextInt();
int b = in.nextInt();
add.add(new Pair(a, b));
alls.add(a);
}
for(int j = 0; j < m; j++){
int l = in.nextInt();
int r = in.nextInt();
query.add(new Pair(l, r));
alls.add(l);
alls.add(r);
}
//排序出重。为啥这么做呢?是需要做前缀和。
Collections.sort(alls);
int unique = unique(alls);
alls = alls.subList(0, unique);
for(Pair item : add){
int tag = find(item.first, alls);
ori[tag] += item.second;
}
//前缀和
for(int i = 1; i <= alls.size(); i++) sum[i] = sum[i - 1] + ori[i];
for(Pair item : query){
int l = find(item.first, alls);
int r = find(item.second, alls);
System.out.println(sum[r] - sum[l - 1]);
}
}
public static int unique(List<Integer> list){
int j = 0;
for(int i = 0; i < list.size(); i++){
if(i == 0 || list.get(i) != list.get(i - 1)){
list.set(j, list.get(i));
j++;
}
}
return j;
}
public static int find(int x, List<Integer> list){
int l = 0;
int r = list.size() - 1;
while(l < r){
int mid = l + r >> 1;
if(list.get(mid) >= x){
r = mid;
}else{
l = mid + 1;
}
}
return l + 1;
}
}
8.区间合并
—— 模板题 AcWing 803. 区间合并
// 将所有存在交集的区间合并
void merge(vector<PII> &segs)
{
vector<PII> res;
sort(segs.begin(), segs.end());
int st = -2e9, ed = -2e9;
for (auto seg : segs)
if (ed < seg.first)
{
if (st != -2e9) res.push_back({st, ed});
st = seg.first, ed = seg.second;
}
else ed = max(ed, seg.second);
if (st != -2e9) res.push_back({st, ed});
segs = res;
}