A
签到题
/*
* @author :Changersh
* @date : 2022/11/15
*/
import java.io.*;
import java.util.*;
import java.lang.*;
public class Main {
public static void main(String[] args) {
int a = sc.nextInt();
int b = sc.nextInt();
int x = sc.nextInt();
out.println((x - b) / a);
out.close();
}
static class FastScanner{
// sc.xxx;
// out.print();
// out.flush();
// out.close();
BufferedReader br;
StringTokenizer st;
public FastScanner(InputStream in) {
br=new BufferedReader( new InputStreamReader(System.in));
eat("");
}
public void eat(String s) {
st=new StringTokenizer(s);
}
public String nextLine() {
try {
return br.readLine();
}catch(IOException e) {
return null;
}
}
public boolean hasNext() {
while(!st.hasMoreTokens()) {
String s=nextLine();
if(s==null)return false;
eat(s);
}
return true;
}
public String next() {
hasNext();
return st.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
}
static FastScanner sc=new FastScanner(System.in);
static PrintWriter out=new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
}
B
分别统计每一个房间联通的出口个数,劫匪在某个房间的时候,该房间联通的出口都不能用了。
用出口的总数,减去这些出口即是可以逃跑的出口
/*
* @author :Changersh
* @date : 2022/11/15
*/
import java.io.*;
import java.util.*;
import java.lang.*;
public class Main {
private static int N = 100005;
public static void main(String[] args) {
int n = sc.nextInt(), m = sc.nextInt(), q = sc.nextInt();
int[] st = new int[N];
for (int i = 1; i <= n; i++) {
int x = sc.nextInt();
st[x]++;
}
while (q-- > 0) {
int x = sc.nextInt();
out.println(n - st[x]);
}
out.close();
}11
static class FastScanner{
// sc.xxx;
// out.print();
// out.flush();
// out.close();
BufferedReader br;
StringTokenizer st;
public FastScanner(InputStream in) {
br=new BufferedReader( new InputStreamReader(System.in));
eat("");
}
public void eat(String s) {
st=new StringTokenizer(s);
}
public String nextLine() {
try {
return br.readLine();
}catch(IOException e) {
return null;
}
}
public boolean hasNext() {
while(!st.hasMoreTokens()) {
String s=nextLine();
if(s==null)return false;
eat(s);
}
return true;
}
public String next() {
hasNext();
return st.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
}
static FastScanner sc=new FastScanner(System.in);
static PrintWriter out=new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
}
C
经典dp
/*
* @author :Changersh
* @date : 2022/11/15
*/
import java.io.*;
import java.util.*;
import java.lang.*;
public class Main {
private static int N = 4005, n, m;
private static int[] st = new int[N];
public static void main(String[] args) {
n = sc.nextInt(); m = sc.nextInt();
for (int i = 0; i < n; i++) {
st[i] = sc.nextInt();
}
int[] dp = new int[N];
dp[0] = st[0];
for (int i = 1; i < n; i++) {
if (i <= m) dp[i] = Math.max(dp[i - 1], st[i]);
else dp[i] = Math.max(dp[i - 1], dp[i - m - 1] + st[i]);
}
out.println(dp[n - 1]);
out.close();
}
static class FastScanner{
// sc.xxx;
// out.print();
// out.flush();
// out.close();
BufferedReader br;
StringTokenizer st;
public FastScanner(InputStream in) {
br=new BufferedReader( new InputStreamReader(System.in));
eat("");
}
public void eat(String s) {
st=new StringTokenizer(s);
}
public String nextLine() {
try {
return br.readLine();
}catch(IOException e) {
return null;
}
}
public boolean hasNext() {
while(!st.hasMoreTokens()) {
String s=nextLine();
if(s==null)return false;
eat(s);
}
return true;
}
public String next() {
hasNext();
return st.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
}
static FastScanner sc=new FastScanner(System.in);
static PrintWriter out=new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
}
D
因为相邻距离是1,求最短路就可以使用 bfs,就是很典型的bfs,只不过在走的时候,必须要经过一个符合条件的商店(游戏刺激度 > x)。
分别对起点和终点做一次bfs,遍历求 (刺激度 > x),的商店到二者的距离和,取最小值即可。
/*
* @author :Changersh
* @date : 2022/11/15
*/
import java.io.*;
import java.util.*;
import java.lang.*;
public class Main {
private static int N = 4005, n, m, x, sx, ex, sy, ey, ans = 0;
private static int[][] g = new int[N][N];
private static int[][] s1 = new int[N][N], s2 = new int[N][N];
private static int[] dx = {-1, 0, 1, 0}, dy = {0, 1, 0, -1};
public static void main(String[] args) {
n = sc.nextInt(); m = sc.nextInt(); x = sc.nextInt();
sx = sc.nextInt(); sy = sc.nextInt(); ex = sc.nextInt(); ey = sc.nextInt();
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
g[i][j] = sc.nextInt();
ans = Math.max(g[i][j], ans);
}
}
if (ans <= x) {
out.println("-1");
} else {
ans = Integer.MAX_VALUE;
for (int i = 0; i <= n; i++) {
Arrays.fill(s1[i], -1);
Arrays.fill(s2[i], -1);
}
bfs(s1, sx, sy);
bfs(s2, ex, ey);
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (g[i][j] > x && s1[i][j] != -1 && s2[i][j] != -1) {
ans = Math.min(ans, s1[i][j] + s2[i][j]);
}
}
}
out.println(ans == Integer.MAX_VALUE ? -1 : ans);
}
out.close();
}
private static void bfs(int[][] st, int a, int b) {
Queue<int[]> q = new LinkedList<>();
q.add(new int[]{a, b});
st[a][b] = 0;
while (!q.isEmpty()) {
int[] t = q.poll();
int x = t[0], y = t[1];
for (int i = 0; i < 4; i++) {
int nx = x + dx[i];
int ny = y + dy[i];
if (nx <= n && nx > 0 && ny <= m && ny > 0 && g[nx][ny] != -1 && st[nx][ny] == -1) {
q.add(new int[]{nx, ny});
st[nx][ny] = st[x][y] + 1;
}
}
}
}
static class FastScanner{
// sc.xxx;
// out.print();
// out.flush();
// out.close();
BufferedReader br;
StringTokenizer st;
public FastScanner(InputStream in) {
br=new BufferedReader( new InputStreamReader(System.in));
eat("");
}
public void eat(String s) {
st=new StringTokenizer(s);
}
public String nextLine() {
try {
return br.readLine();
}catch(IOException e) {
return null;
}
}
public boolean hasNext() {
while(!st.hasMoreTokens()) {
String s=nextLine();
if(s==null)return false;
eat(s);
}
return true;
}
public String next() {
hasNext();
return st.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
}
static FastScanner sc=new FastScanner(System.in);
static PrintWriter out=new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
}