Merge remote-tracking branch 'gitee/master'

This commit is contained in:
pronting 2024-06-19 15:36:07 +08:00
commit 4e20145709
11 changed files with 538 additions and 0 deletions

View File

@ -0,0 +1,30 @@
package priv.pront.code.mati.national.practice;
import java.util.Scanner;
/**
* @Author: Pronting
* @Description:
* @DateTime: 2023/12/1 12:39
**/
public class B3638_T1三角形面积 {
static int[][] arr = new int[3][2];
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
for (int i = 0; i < 3; i++) {
arr[i][0] = scanner.nextInt();
arr[i][1] = scanner.nextInt();
}
double a = Math.sqrt(Math.pow(arr[0][1] - arr[1][1], 2) + Math.pow(arr[0][0] - arr[1][0], 2));
double b = Math.sqrt(Math.pow(arr[0][1] - arr[2][1], 2) + Math.pow(arr[0][0] - arr[2][0], 2));
double c = Math.sqrt(Math.pow(arr[2][1] - arr[1][1], 2) + Math.pow(arr[2][0] - arr[1][0], 2));
double s = 0.5 * (a + b + c);
double p = Math.sqrt(s * (s - a) * (s - b) * (s - c));
int ans = (int)Math.round(p);
System.out.println(ans);
}
}

View File

@ -0,0 +1,57 @@
package priv.pront.code.mati.national.practice;
import java.util.Scanner;
/**
* @Author: Pronting
* @Description:
* @DateTime: 2023/12/1 13:02
**/
public class P1098_字符串的展开 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int p1 = scanner.nextInt();
int p2 = scanner.nextInt();
int p3 = scanner.nextInt();
scanner.nextLine(); // Consume the newline
String input = scanner.nextLine();
StringBuilder output = new StringBuilder();
for (int i = 0; i < input.length(); i++) {
char c = input.charAt(i);
if (c == '-' && i > 0 && i < input.length() - 1) {
char start = input.charAt(i - 1);
char end = input.charAt(i + 1);
if (start + 1 == end) { // Case 5
continue;
}
if (start >= end || !(Character.isDigit(start) && Character.isDigit(end)) &&
!(Character.isLetter(start) && Character.isLetter(end))) {
output.append(c);
continue;
}
StringBuilder expansion = new StringBuilder();
for (char j = (char) (start + 1); j < end; j++) {
for (int k = 0; k < p2; k++) { // p2: repeat count
char ch = (p1 == 3) ? '*' : j;
if (p1 == 2 && Character.isLetter(j)) {
ch = Character.toUpperCase(ch);
}
expansion.append(ch);
}
}
if(p3 == 2){
output.append(expansion.reverse().toString());
}else{
output.append(expansion.toString());
}
} else {
output.append(c);
}
}
System.out.println(output.toString());
}
}

View File

@ -0,0 +1,46 @@
package priv.pront.code.mati.national.practice;
import java.util.Scanner;
/**
* @Author: Pronting
* @Description: TODO
* @DateTime: 2023/12/1 15:08
**/
public class P1596_Lake_Counting_S {
static int n, m, count = 0;
static char[][] g;
static boolean[][] st;
static int[] dx = {-1, -1, 0, 1, 1, 1, 0, -1}, dy = {0, 1, 1, 1, 0, -1, -1, -1};
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
n = scanner.nextInt();
m = scanner.nextInt();
g = new char[n][m];
st = new boolean[n][m];
for (int i = 0; i < n; i++) {
g[i] = scanner.next().toCharArray();
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if(!st[i][j] && g[i][j] == 'W'){
dfs(i, j);
count++;
}
}
}
System.out.println(count);
}
private static void dfs(int row, int col){
for (int i = 0; i < 8; i++) {
int nx = dx[i] + row, ny = dy[i] + col;
if(nx < 0 || nx >= n || ny < 0 || ny >= m || st[nx][ny] || g[nx][ny] == '.') continue;
st[nx][ny] = true;
dfs(nx, ny);
}
}
}

View File

@ -0,0 +1,47 @@
package priv.pront.code.mati.national.practice;
import java.util.Scanner;
/**
* @Author: Pronting
* @Description:
* @DateTime: 2023/12/2 14:27
**/
public class P1638_逛画展 {
static int N, M, L, R, i = 0, num, ansL, ansR;
static int[] n = new int[1000000];
static int[] m = new int[2001];
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
N = scanner.nextInt(); // 博览馆内的图画总数
M = scanner.nextInt(); // 这些图画是由多少位名师的画所绘画的
for (int i = 0; i < N; i++) {
n[i] = scanner.nextInt();
}
i = 0;
while (num != M) {
if (m[n[i]] == 0) num++;
m[n[i]]++;
R++;
i++;
}
while (m[n[L]] > 1)
m[n[L++]]--;
ansL = L;
ansR = R;
while (i < N) {
m[n[i]]++;
R++;
i++;
while (m[n[L]] > 1)
m[n[L++]]--;
if (ansR - ansL > R - L) {
ansR = R;
ansL = L;
}
}
System.out.println(ansL + 1 + " " + ansR);
}
}

View File

@ -0,0 +1,81 @@
package priv.pront.code.mati.national.practice;
import java.util.*;
/**
* @Author: Pronting
* @Description:
* @DateTime: 2023/12/1 13:54
**/
public class P1825_Corn_Maze_S {
static int n, m;
static char[][] g;
static int[][][] position = new int[26][2][2];
static Set<Character> containsSet = new HashSet<>();
static Queue<int[]> queue = new LinkedList<>();
static int[] dx = new int[]{-1, 1, 0, 0}, dy = new int[]{0, 0, -1, 1};
static boolean[][] st;
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
n = scanner.nextInt();
m = scanner.nextInt();
g = new char[n][m];
st = new boolean[n][m];
int startX = -1, startY = -1;
for (int i = 0; i < n; i++) g[i] = scanner.next().toCharArray();
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (g[i][j] >= 'A' && g[i][j] <= 'Z') {
if(!containsSet.contains(g[i][j])){
position[g[i][j] - 'A'][0][0] = i;
position[g[i][j] - 'A'][0][1] = j;
}else{
position[g[i][j] - 'A'][1][0] = i;
position[g[i][j] - 'A'][1][1] = j;
}
containsSet.add(g[i][j]);
}
if(g[i][j] == '@'){
startX = i;
startY = j;
}
}
}
st[startX][startY] = true;
queue.add(new int[]{startX, startY, 0});
while (!queue.isEmpty()) {
int[] poll = queue.poll();
if(g[poll[0]][poll[1]] == '='){
System.out.println(poll[2]);
return;
}
char cur = g[poll[0]][poll[1]];
if (containsSet.contains(cur)) {
int[] ans = jump(poll[0], poll[1]);
poll[0] = ans[0];
poll[1] = ans[1];
}
for (int i = 0; i < 4; i++) {
int nx = dx[i] + poll[0];
int ny = dy[i] + poll[1];
if (nx < 0 || nx >= n || ny < 0 || ny >= m || st[nx][ny] || g[nx][ny] == '#') continue;
st[nx][ny] = true;
queue.add(new int[]{nx, ny, poll[2] + 1});
}
}
}
private static int[] jump(int row, int col){
char cur = g[row][col];
int jumpX , jumpY;
if (position[cur - 'A'][0][0] == row && position[cur - 'A'][0][1] == col) {
jumpX = position[cur - 'A'][1][0];
jumpY = position[cur - 'A'][1][1];
} else {
jumpX = position[cur - 'A'][0][0];
jumpY = position[cur - 'A'][0][1];
}
return new int[]{jumpX, jumpY};
}
}

View File

@ -0,0 +1,69 @@
package priv.pront.code.mati.national.practice;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StreamTokenizer;
import java.util.Scanner;
/**
* @Author: Pronting
* @Description:
* @DateTime: 2023/12/1 15:29
**/
public class P2004_领地选择 {
static int n, m, c, x, y;
static int max = Integer.MIN_VALUE;
static int[][] a;
static int[][] preSum;
static StreamTokenizer st = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
public static void main(String[] args) throws IOException {
st.nextToken();
n = (int) st.nval;
st.nextToken();
m = (int) st.nval;
st.nextToken();
c = (int) st.nval;
a = new int[n][m];
preSum = new int[n][m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
st.nextToken();
a[i][j] = (int) st.nval;
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
preSum[i][j] = a[i][j];
if(j > 0) preSum[i][j] += preSum[i][j - 1];
if(i > 0) preSum[i][j] += preSum[i - 1][j];
if(j > 0 && i > 0) preSum[i][j] -= preSum[i - 1][j - 1];
}
}
for (int i = c - 1 ; i < n; i++) {
for (int j = c - 1; j < m; j++) {
int value;
if(i == c - 1 && j == c - 1){
value = preSum[i][j];
}else if(i > c - 1 && j == c - 1){
value = preSum[i][j] - preSum[i - c][j];
}else if(i == c - 1 && j > c - 1){
value = preSum[i][j] - preSum[i][j - c];
}else{
value = preSum[i][j] - preSum[i - c][j] - preSum[i][j - c] + preSum[i - c][j - c];
}
if(value > max){
max = value;
x = i; y = j;
}
}
}
System.out.print(x - c + 2 + " ");
System.out.print(y - c + 2);
}
}

View File

@ -0,0 +1,43 @@
package priv.pront.code.mati.national.practice;
import java.io.*;
import java.util.*;
/**
* @Author: Pronting
* @Description: TODO
* @DateTime: 2023/12/2 13:55
**/
public class P2032_扫描 {
static StreamTokenizer st = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
static PrintWriter out = new PrintWriter(System.out);
public static void main(String[] args) throws IOException {
st.nextToken();
int n = (int) st.nval;
st.nextToken();
int k = (int) st.nval;
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
st.nextToken();
arr[i] = (int) st.nval;
}
Deque<Integer> deque = new LinkedList<>();
for (int i = 0; i < n; i++) {
while (!deque.isEmpty() && deque.peekLast() < arr[i]) {
deque.pollLast();
}
deque.offerLast(arr[i]);
if (i >= k - 1) {
if (i - k >= 0 && deque.peekFirst() == arr[i - k]) {
deque.pollFirst();
}
out.println(deque.peekFirst());
out.flush();
}
}
}
}

View File

@ -0,0 +1,49 @@
package priv.pront.code.mati.national.practice;
import java.util.Scanner;
/**
* @Author: Pronting
* @Description: TODO
* @DateTime: 2023/12/1 13:34
**/
public class P2615_神奇的幻方 {
static int n;
static int[][] a;
static int[] x, y;
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
n = scanner.nextInt();
a = new int[n][n];
x = new int[n * n];
y = new int[n * n];
a[0][n / 2] = 1;
x[0] = 0;
y[0] = n / 2;
for (int k = 1; k < n * n; k++) {
if(x[k - 1] == 0 && y[k - 1] != n - 1) {
x[k] = n -1; y[k] = y[k - 1] + 1;
}else if(x[k - 1] != 0 && y[k - 1] == n - 1){
x[k] = x[k - 1] - 1; y[k] = 0;
}else if(x[k - 1] == 0 && y[k - 1] == n - 1){
x[k] = x[k - 1] + 1; y[k] = y[k - 1];
}else{
if(a[x[k - 1] - 1][y[k - 1] + 1] == 0){
x[k] = x[k - 1] - 1; y[k] = y[k - 1] + 1;
}else{
x[k] = x[k - 1] + 1; y[k] = y[k - 1];
}
}
a[x[k]][y[k]] = k + 1;
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
System.out.print(a[i][j] + " ");
}
System.out.println();
}
}
}

View File

@ -0,0 +1,56 @@
package priv.pront.code.mati.national.practice;
import java.io.*;
/**
* @Author: Pronting
* @Description:
* @DateTime: 2023/12/1 16:39
**/
//FIXME TLE
public class P3397_地毯 {
static int[][] a, b;
static StreamTokenizer st = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
static PrintWriter out = new PrintWriter(System.out);
public static void main(String[] args) throws IOException {
st.nextToken();
int n = (int) st.nval;
st.nextToken();
int m = (int) st.nval;
a = new int[n + 2][n + 2];
b = new int[n + 2][n + 2];
for (int i = 0; i < m; i++) {
st.nextToken();
int x1 = (int) st.nval;
st.nextToken();
int y1 = (int) st.nval;
st.nextToken();
int x2 = (int) st.nval;
st.nextToken();
int y2 = (int) st.nval;
add(x1, y1, x2, y2);
}
out.flush();
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
b[i][j] += b[i - 1][j] + b[i][j - 1] - b[i - 1][j - 1];
out.print(b[i][j] + " ");
out.flush();
}
out.println();
out.flush();
}
}
public static void add(int x1, int y1, int x2, int y2) {
b[x1][y1]++;
b[x1][y2 + 1]--;
b[x2 + 1][y1]--;
b[x2 + 1][y2 + 1]++;
}
}

View File

@ -0,0 +1,33 @@
package priv.pront.code.mati.national.practice;
import java.util.Scanner;
/**
* @Author: Pronting
* @Description: TODO
* @DateTime: 2023/12/1 12:56
**/
public class P5723_质数口袋 {
static int n, size = 0;
static int INF = (int) 1e9;
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
n = scanner.nextInt();
for (int i = 2; i < INF; i++) {
if(check(i)) System.out.println(i);
}
System.out.println(size);
}
private static boolean check(int num){
if(n - num < 0) return false;
for (int i = 2; i * i <= num; i++) {
if(num % i == 0) return false;
}
n -= num;
size++;
return true;
}
}

View File

@ -0,0 +1,27 @@
package priv.pront.code.mati.national.practice;
import java.util.Arrays;
import java.util.Scanner;
/**
* @Author: Pronting
* @Description:
* @DateTime: 2023/12/1 12:52
**/
public class P5726_打分 {
static int n;
static int[] a;
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
n = scanner.nextInt();
a = new int[n];
for (int i = 0; i < n; i++) a[i] = scanner.nextInt();
Arrays.sort(a);
int sum = 0;
for (int i = 1; i < n - 1; i++) {
sum += a[i];
}
System.out.printf("%.2f", sum * 1.0 / (n - 2));
}
}