-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
41d34b3
commit 23d7e17
Showing
39 changed files
with
810 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<module type="JAVA_MODULE" version="4"> | ||
<component name="NewModuleRootManager" inherit-compiler-output="true"> | ||
<exclude-output /> | ||
<content url="file://$MODULE_DIR$"> | ||
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" /> | ||
</content> | ||
<orderEntry type="inheritedJdk" /> | ||
<orderEntry type="sourceFolder" forTests="false" /> | ||
</component> | ||
</module> |
Binary file added
BIN
+2.27 KB
algorithm/acwing/code/out/production/ACWing/a_BinaryAlgorithm/T789.class
Binary file not shown.
Binary file added
BIN
+1.35 KB
algorithm/acwing/code/out/production/ACWing/a_BinaryAlgorithm/T790.class
Binary file not shown.
Binary file added
BIN
+947 Bytes
algorithm/acwing/code/out/production/ACWing/a_BinaryAlgorithm/Template.class
Binary file not shown.
Binary file added
BIN
+1.8 KB
algorithm/acwing/code/out/production/ACWing/b_MergeAlgorithm/T788.class
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added
BIN
+1.55 KB
algorithm/acwing/code/out/production/ACWing/d_DifferenceMethod/T797.class
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package a_BinaryAlgorithm; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.InputStreamReader; | ||
import java.util.Scanner; | ||
|
||
/** | ||
* @problem 789数的范围 | ||
* @idea 二分法查找 | ||
*/ | ||
public class T789 { | ||
public static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | ||
public static final Scanner sc =new Scanner(System.in); | ||
public static int n, q; | ||
public static void main(String[] args) throws Exception{ | ||
n = sc.nextInt(); | ||
q = sc.nextInt(); | ||
int k; | ||
int[] arr = new int[n]; | ||
String[] nums = br.readLine().split(" "); | ||
for (int i = 0; i < n; i++) { | ||
arr[i] = Integer.parseInt(nums[i]); | ||
} | ||
for (int i = 0; i < q; i++) { | ||
k = sc.nextInt(); | ||
startToEnd(arr, k); | ||
} | ||
} | ||
|
||
public static void startToEnd(int[] arr, int k){ | ||
int l = 0, r = n - 1; | ||
while (l < r){ | ||
int mid = (l + r) / 2; | ||
if (arr[mid] >= k) r = mid; | ||
else l = mid + 1; | ||
} | ||
if(arr[l] != k) System.out.println("-1 -1"); | ||
else { | ||
System.out.println(l + " "); | ||
l = 0; | ||
r = n - 1; | ||
while (l < r){ | ||
int mid = (l + r + 1) / 2; | ||
if(arr[mid] <= k) l = mid; | ||
else r = mid - 1; | ||
} | ||
System.out.println(l); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package a_BinaryAlgorithm; | ||
|
||
import java.util.Scanner; | ||
|
||
/** | ||
* @problem 790数的三次方根 | ||
* @idea 二分法 | ||
*/ | ||
public class T790 { | ||
public static void main(String[] args) { | ||
double n = new Scanner(System.in).nextDouble(); | ||
cubeRoot(n); | ||
} | ||
|
||
private static void cubeRoot(double n) { | ||
double l, r; | ||
double abs_n = Math.abs(n); | ||
if(abs_n <= 1){ | ||
l = abs_n; | ||
r = 1; | ||
}else { | ||
l = 1; | ||
r = abs_n; | ||
} | ||
while (r - l > 1e-8){ | ||
double mid = (l + r) / 2; | ||
if(mid * mid * mid <= abs_n) l = mid; | ||
else r = mid; | ||
} | ||
System.out.println(String.format("%.6f", n >= 0 ? l : -l)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package a_BinaryAlgorithm; | ||
|
||
public class Template { | ||
/** | ||
* 检查x是够满足某种性质 | ||
* @param x | ||
* @return | ||
*/ | ||
public static<E> boolean check(E x){ | ||
/*...*/ | ||
return true; | ||
} | ||
|
||
/** | ||
* 区间[l, r]被划分成[l, mid]和[mid + 1, r]时使用。用于确定最左端的位置 | ||
* @param l | ||
* @param r | ||
* @return | ||
*/ | ||
public static int bSearchLeft(int l, int r){ | ||
while (l < r){ | ||
int mid = l + r >> 1; | ||
if(check(mid)) r = mid; | ||
else l = mid + 1; | ||
} | ||
return l; | ||
} | ||
|
||
/** | ||
* 区间[l, r]被划分成[l, mid - 1]和[mid, r]时使用。用于确定最右端的位置 | ||
* @param l | ||
* @param r | ||
* @return | ||
*/ | ||
public static int bSearchRight(int l, int r){ | ||
while (l < r){ | ||
int mid = l + r + 1 >> 1; | ||
if(check(mid)) l = mid; | ||
else r = mid - 1; | ||
} | ||
return l; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package b_MergeAlgorithm; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.IOException; | ||
import java.io.InputStreamReader; | ||
|
||
/** | ||
* @problem 788逆序对的数量 | ||
* @Idea 归并排序 | ||
*/ | ||
public class T788 { | ||
public static int n; | ||
public static int[] arrTemp = new int[100010]; | ||
public static long rp = 0; | ||
public static final BufferedReader br = new BufferedReader(new InputStreamReader((System.in))); | ||
public static void main(String[] args) throws IOException { | ||
n = Integer.parseInt(br.readLine()); | ||
int[] arr = new int[n]; | ||
String[] nums = br.readLine().split(" "); | ||
for (int i = 0; i < n; i++) { | ||
arr[i] = Integer.parseInt(nums[i]); | ||
} | ||
reversePairs(arr, 0, n - 1); | ||
System.out.println(rp); | ||
br.close(); | ||
} | ||
|
||
public static void reversePairs(int[] arr, int low, int high){ | ||
if(low < high){ | ||
int mid = (low + high) / 2; | ||
reversePairs(arr, low, mid); | ||
reversePairs(arr, mid + 1, high); | ||
for (int i = low; i <= high; i++) { | ||
arrTemp[i] = arr[i]; | ||
} | ||
int i = low, j = mid + 1; | ||
int k = low; | ||
while (i <= mid && j <=high){ | ||
if (arrTemp[i] <= arrTemp[j]) arr[k++] = arrTemp[i++]; | ||
else { | ||
arr[k++] = arrTemp[j++]; | ||
rp += mid - i + 1; | ||
} | ||
} | ||
while (i <= mid) arr[k++] = arrTemp[i++]; | ||
while (j <= high) arr[k++] = arrTemp[j++]; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package c_PrefixSum; | ||
|
||
import java.util.Scanner; | ||
|
||
/** | ||
* @problem 795.一维前缀和 | ||
* @idea 前缀和算法 | ||
*/ | ||
public class T795 { | ||
public static void main(String[] args) { | ||
Scanner sc = new Scanner(System.in); | ||
int n = sc.nextInt(); | ||
int m = sc.nextInt(); | ||
int[] num = new int[n]; | ||
int[] s = new int[n + 1]; | ||
for (int i = 0; i < n; i++) { | ||
num[i] = sc.nextInt(); | ||
s[i + 1] = s[i] + num[i]; | ||
} | ||
while (m > 0){ | ||
int l = sc.nextInt(); | ||
int r = sc.nextInt(); | ||
System.out.println(s[r] - s[l - 1]); | ||
--m; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package c_PrefixSum; | ||
|
||
import java.util.Scanner; | ||
/** | ||
* @problem 796.二维前缀和 | ||
* @idea 前缀和算法 | ||
*/ | ||
public class T796 { | ||
public static void main(String[] args) { | ||
Scanner sc = new Scanner(System.in); | ||
int n = sc.nextInt(); | ||
int m = sc.nextInt(); | ||
int q = sc.nextInt(); | ||
int[][] num = new int[n + 1][m + 1]; | ||
int[][] s = new int[n + 1][m + 1]; | ||
for (int i = 1; i <= n; i++) { | ||
for (int j = 1; j <= m; j++) { | ||
num[i][j] = sc.nextInt(); | ||
s[i][j] = s[i - 1][j] + s[i][j - 1] - s[i - 1][j - 1] + num[i][j]; | ||
} | ||
} | ||
while (q > 0){ | ||
int x1 = sc.nextInt(); | ||
int y1 = sc.nextInt(); | ||
int x2 = sc.nextInt(); | ||
int y2 = sc.nextInt(); | ||
System.out.println(s[x2][y2] - s[x2][y1 - 1] - s[x1 - 1][y2] + s[x1 - 1][y1 - 1]); | ||
--q; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package d_DifferenceMethod; | ||
|
||
import java.util.Scanner; | ||
|
||
public class T797 { | ||
public static void main(String[] args) { | ||
Scanner sc = new Scanner(System.in); | ||
int n = sc.nextInt(); | ||
int m = sc.nextInt(); | ||
int[] a = new int[n + 1]; | ||
int[] b = new int[n + 1]; | ||
for (int i = 1; i <= n; ++i){ | ||
a[i] = sc.nextInt(); | ||
b[i] = a[i] - a[i - 1]; | ||
} | ||
while (m > 0){ | ||
int l = sc.nextInt(); | ||
int r = sc.nextInt(); | ||
int c = sc.nextInt(); | ||
b[l] += c; | ||
if(r < n) b[r + 1] -= c; | ||
--m; | ||
} | ||
for (int i = 1; i <= n; i++) { | ||
a[i] = a[i - 1] + b[i]; | ||
System.out.print(a[i] + " "); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
## 前缀和 | ||
|
||
--- | ||
|
||
### 一维前缀和 | ||
|
||
#### 一维前缀和是什么 | ||
原数组: a[1], a[2], a[3], a[4], a[5], …, a[n] | ||
|
||
前缀和 S[i]为数组的前 i 项和 | ||
|
||
前缀和: S[i] = a[1] + a[2] + a[3] + … + a[i] | ||
|
||
**注意:前缀和的下标从1开始,s[0]=0;** | ||
|
||
> s[0] = 0 | ||
> s[1] = s[0] + a[1] | ||
> | ||
> s[2] = s[1] + a[2] | ||
> | ||
> ...... | ||
> | ||
> s[i] = s[i - 1] + a[i] | ||
**前缀和的作用** | ||
快速求出元素组中某段区间的和 | ||
|
||
**一维数组求解前缀和(Si)** | ||
|
||
1. for循环求出 每个S[i] (将 S[0] 定义为 0, 避免下标的转换) | ||
2. 求 [l, r]中的和, 即为 **==S[r] - S[l-1]==** | ||
```java | ||
public class Main { | ||
public static void main(String[] args) { | ||
Scanner sc = new Scanner(System.in); | ||
int n = sc.nextInt(); | ||
int m = sc.nextInt(); | ||
int[] num = new int[n]; | ||
int[] s = new int[n + 1]; | ||
s[0] = 0; | ||
for (int i = 0; i < n; i++) { | ||
num[i] = sc.nextInt(); | ||
s[i + 1] = s[i] + num[i]; | ||
} | ||
while (m > 0){ | ||
int l = sc.nextInt(); | ||
int r = sc.nextInt(); | ||
System.out.println(s[r] - s[l - 1]); | ||
--m; | ||
} | ||
} | ||
} | ||
``` | ||
|
||
### 二维前缀和 | ||
|
||
#### 二位前缀和是什么 | ||
|
||
在一维前缀和的基础上,将一维数组改为了矩阵 | ||
|
||
前缀和`S[i][j]`表示二维数组的前 i 行、前 j 列的和 | ||
|
||
 | ||
|
||
紫色面积是指(1,1)左上角到(i, j-1)右下角的矩形面积, 绿色面积是指(1,1)左上角到(i-1, j )右下角的矩形面积。 | ||
|
||
*每一个颜色的矩形面积都代表了它所包围元素的和。* | ||
|
||
从图中我们很容易看出,整个外围蓝色矩形面积s[i][j] = 绿色面积`s[i-1][j]` + 紫色面积`s[i][j-1]` - 重复加的红色的面积`s[i-1][j-1]`+小方块的面积`a[i][j]`; | ||
|
||
**注意:前缀和的下标从i=1, j=1开始,`s[0][i]=0、s[j][0]=0`;** | ||
|
||
|
||
|
||
#### 公式 | ||
|
||
==**`s[i][j] = s[i - 1][j] + s[i][j - 1] + a[i][j] - s[i - 1][j - 1]`**== | ||
|
||
以`(x1, y1)`为左上角,`(x2, y2)`为右下角的矩阵的和为:**==`s[x2][y2] - s[x1 -1][y2] - s[x2][y1 - 1] + s[x1 - 1][y1 - 1]`==** | ||
|
||
 | ||
|
||
|
||
|
||
--- | ||
|
||
**相关题目:** | ||
|
||
***AcWing 795 https://www.acwing.com/activity/content/problem/content/829/*** | ||
|
||
***AcWing 796 https://www.acwing.com/activity/content/problem/content/830/*** |
Oops, something went wrong.