提交 f8885d27 编写于 作者: qq_36480062's avatar qq_36480062

c

上级 12b85ce5
......@@ -5,6 +5,29 @@ import java.util.Scanner;
/**
* https://blog.csdn.net/qq_30277239/article/details/104670312
* 请你编写一个程序,计算城堡一共有多少房间,最大的房间有多大。
* 城堡被分割成 m∗n个方格区域,每个方格区域可以有0~4面墙。
* 输入格式
* 第一行包含两个整数 m 和 n,分别表示城堡南北方向的长度和东西方向的长度。
* 接下来 m 行,每行包含 n 个整数,每个整数都表示平面图对应位置的方块的墙的特征。
* 每个方块中墙的特征由数字 P 来描述,我们用1表示西墙,2表示北墙,4表示东墙,8表示南墙,P 为该方块包含墙的数字之和。
* 例如,如果一个方块的 P 为3,则 3 = 1 + 2,该方块包含西墙和北墙。
* 城堡的内墙被计算两次,方块(1,1)的南墙同时也是方块(2,1)的北墙。
* 输入的数据保证城堡至少有两个房间。
* 输出格式
* 共两行,第一行输出房间总数,第二行输出最大房间的面积(方块数)。
* 数据范围
* 1≤m,n≤50,
* 0≤P≤15
* 输入样例:
* 4 7
* 11 6 11 6 3 10 6
* 7 9 6 13 5 15 5
* 1 10 12 7 13 7 5
* 13 11 10 8 10 12 13
* 输出样例:
* 5
* 9
* 显然寻找有多少个四联通块
* 并找出最大连通块有多少个
*/
......
package RMQ;
import java.io.*;
import java.util.StringTokenizer;
public class 洛谷ST {
public static void main(String[] args) throws IOException {
n = nextInt();
m = nextInt();
for (int i = 1; i <= n; i++) {
a[i] = nextInt();
}
init();
int a, b;
/**
* 索引从1开始!!!
*/
for (int i = 0; i < m; i++) {
a = nextInt();
b = nextInt();
bw.write(query(a, b) + "\n");
}
bw.flush();
}
static void init() {
log[1] = 0;
for (int i = 2; i <= n; i++) {
log[i] = log[i / 2] + 1;
}
for (int i = 1; i <= n; i++) {
st[i][0] = a[i];
}
for (int j = 1; 1 << j <= n; j++) {
for (int i = 1; i + (1 << j) - 1 <= n; i++) {
st[i][j] = Math.max(st[i][j - 1], st[i + (1 << j - 1)][j - 1]);
}
}
}
static int query(int l, int r) {
int k = log[r - l + 1];
return Math.max(st[l][k], st[r - (1 << k) + 1][k]);
}
static int[] a = new int[100010];
static int n, m;
static int[] log = new int[(int) 1e5];
static int[][] st = new int[100100][20];
static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static StringTokenizer stk = new StringTokenizer("");
static String next() throws IOException {
while (!stk.hasMoreTokens()) {
stk = new StringTokenizer(br.readLine());
}
return stk.nextToken();
}
static int nextInt() throws IOException {
return Integer.parseInt(next());
}
}
package basic;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.Scanner;
/**
* https://www.acwing.com/activity/content/code/content/245992/
*/
public class 蛇形矩阵 {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
int[] dx = {0, 1, 0, -1};
int[] dy = {1, 0, -1, 0};
for (int x = 0, y = 0, k = 1, d = 0; k <= n * m; k++) {
res[x][y] = k;
int a = dx[d] + x, b = dy[d] + y;
if (a < 0 || a >= n || b < 0 || b >= m || res[a][b] != 0) {
d = (d + 1) % 4;
a = x + dx[d];
b = y + dy[d];
}
x = a;
y = b;
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
bw.write(res[i][j] + "");
}
bw.write("\n");
}
bw.flush();
}
static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
static int[][] res = new int[100][100];
}
......@@ -78,7 +78,7 @@ public class 最长连续不重复子序列 {
j++;
}
ans = Math.max(ans, i - j + 1);
//实际上是枚举i,j区间,双指针,特备牛逼
//实际上是枚举i,j区间,双指针
}
System.out.println(ans);
}
......
......@@ -3,6 +3,9 @@ package dp.区间dp;
import java.util.Arrays;
import java.util.Scanner;
/**
* 破环为链
*/
public class 环形石子合并 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
......@@ -18,7 +21,7 @@ public class 环形石子合并 {
for (int i = 1; i <= n * 2; i++) {
s[i] += s[i - 1] + a[i];
}
for (int len = 1; len <= n; len++) {
for (int len = 1; len <= n; len++) {//可以len为1
for (int l = 1; l + len - 1 <= n * 2; l++) {
int r = l + len - 1;
if (len == 1) f[l][r] = g[l][r] = 0;
......@@ -32,8 +35,8 @@ public class 环形石子合并 {
}
int minv = Integer.MAX_VALUE / 2, maxv = Integer.MIN_VALUE / 2;
for (int i = 1; i <= n; i++) {
minv=Math.min(f[i][i+n-1],minv);
maxv=Math.max(g[i][i+n-1],maxv);
minv = Math.min(f[i][i + n - 1], minv);
maxv = Math.max(g[i][i + n - 1], maxv);
}
System.out.println(minv);
System.out.println(maxv);
......
......@@ -60,10 +60,10 @@ public class Spfa {
if (dis[x] != Integer.MAX_VALUE && dis[t] > dis[x] + w[i]) {
dis[t] = dis[x] + w[i];
if (!vis[t]) {
if (!q.isEmpty()&&dis[t]<dis[q.peekFirst()]){
if (!q.isEmpty() && dis[t] < dis[q.peekFirst()]) {
q.addFirst(t);
}else q.add(t);
vis[t]=true;
} else q.add(t);
vis[t] = true;
}
}
}
......
......@@ -55,7 +55,7 @@ public class dijkstra {
c = nextInt();
add(a, b, c);
}
Arrays.fill(dis, (1 << 31));
Arrays.fill(dis, Integer.MAX_VALUE);
dij(s);
}
......
......@@ -33,7 +33,6 @@ public class 连接格点 {
int res = 0;
int[] dx = {-1, 0, 1, 0};
int[] dy = {0, 1, 0, -1};
int[] dw = {1, 2, 1, 2};
//枚举横向边,边权为1
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
......
......@@ -45,7 +45,7 @@ public class 组合m个数 {
}
static int m = 3, n = 12;
static int[] arr = {23, 1, 2, 4, 7, 3, 6, 8, 1, 5, 12,13};
static int[] arr = {23, 1, 2, 4, 7, 3, 6, 8, 1, 5, 12, 13};
static int[] vis = new int[12];
//枚举到第u位 ,sum是当前选了几个,state是vis数组
......
......@@ -49,7 +49,7 @@ public class DFS {
}
System.exit(0);//结束程序,不然会出现其他答案,这里只需要一个答案
}
if (k < 0 || cur == arr.length)//剪枝
if (k < 0 || cur == arr.length)//没有候选数,或者到达最后一个位置
return;
buFenHe(arr, k, cur + 1, intS);//不要第cur个
......
......@@ -9,10 +9,11 @@ import java.util.Scanner;
* n<100万
* 思路:求出最接近n的阶乘,10的阶乘就是360万,11的阶乘就是3900万,n最大100万
* 所以求出1-10的阶乘存在数组里面,
*
* <p>
* 第一步求出最接近n的阶乘,每次找到最接近n的阶乘 执行n -= temp[i];
* 第二步重复找最接近n的阶乘数,重复第一步
* 若最终n=0,则n可以分解成阶乘之和,反之不能
* 能选的一定选上
*/
public class 阶乘之和 {
public static void main(String[] args) {
......
package matrix;
public class 矩阵 {
public static void main(String[] args) {
int[][] arr = {
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12},
{13, 14, 15, 16}
};
print(arr);
}
/**
* 顺时针打印二维数组
*
* @param arr
*/
public static void print(int[][] arr) {
int leftUpRow = 0, leftUpCol = 0, rightDownRow = arr.length - 1, rightDownCol = arr[0].length - 1;
//左上角行,左上角列,右下角行,右下角列
while (leftUpCol <= rightDownCol) {
System.out.print(arr[leftUpRow][leftUpCol++] + " ");
}
//恢复
leftUpCol = rightDownCol;
leftUpRow++;//行数往下走
while (leftUpRow <= rightDownRow)//行变列不变
{
System.out.print(arr[leftUpRow++][rightDownRow] + " ");
}
leftUpRow = rightDownRow;//恢复
leftUpCol--;
while (leftUpCol >= rightDownCol) {
}
}
public static void she(int n) {
int[][] arr = new int[n][n];
int tot = arr[0][n - 1];
while (tot < n * n) {
}
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册