This commit is contained in:
HuangHai
2025-08-30 18:35:01 +08:00
commit 1f397eca87
6258 changed files with 1487241 additions and 0 deletions

View File

@@ -0,0 +1,213 @@
# 语言: None, Cpp, Java, JavaScript, ObjC, Proto, TableGen, TextProto
Language: Cpp
BasedOnStyle: LLVM
# 访问说明符(public、private等)的偏移
AccessModifierOffset: -4
# 开括号(开圆括号、开尖括号、开方括号)后的对齐: Align, DontAlign, AlwaysBreak(总是在开括号后换行)
AlignAfterOpenBracket: Align
# 连续赋值时,对齐所有等号
AlignConsecutiveAssignments: false
# 连续声明时,对齐所有声明的变量名
AlignConsecutiveDeclarations: false
# 右对齐逃脱换行(使用反斜杠换行)的反斜杠
AlignEscapedNewlines: Right
# 水平对齐二元和三元表达式的操作数
AlignOperands: true
# 对齐连续的尾随的注释
AlignTrailingComments: true
# 不允许函数声明的所有参数在放在下一行
AllowAllParametersOfDeclarationOnNextLine: false
# 不允许短的块放在同一行
AllowShortBlocksOnASingleLine: true
# 允许短的case标签放在同一行
AllowShortCaseLabelsOnASingleLine: true
# 允许短的函数放在同一行: None, InlineOnly(定义在类中), Empty(空函数), Inline(定义在类中,空函数), All
AllowShortFunctionsOnASingleLine: None
# 允许短的if语句保持在同一行
AllowShortIfStatementsOnASingleLine: true
# 允许短的循环保持在同一行
AllowShortLoopsOnASingleLine: true
# 总是在返回类型后换行: None, All, TopLevel(顶级函数,不包括在类中的函数),
# AllDefinitions(所有的定义,不包括声明), TopLevelDefinitions(所有的顶级函数的定义)
AlwaysBreakAfterReturnType: None
# 总是在多行string字面量前换行
AlwaysBreakBeforeMultilineStrings: false
# 总是在template声明后换行
AlwaysBreakTemplateDeclarations: true
# false表示函数实参要么都在同一行,要么都各自一行
BinPackArguments: true
# false表示所有形参要么都在同一行,要么都各自一行
BinPackParameters: true
# 大括号换行,只有当BreakBeforeBraces设置为Custom时才有效
BraceWrapping:
# class定义后面
AfterClass: false
# 控制语句后面
AfterControlStatement: false
# enum定义后面
AfterEnum: false
# 函数定义后面
AfterFunction: false
# 命名空间定义后面
AfterNamespace: false
# struct定义后面
AfterStruct: false
# union定义后面
AfterUnion: false
# extern之后
AfterExternBlock: false
# catch之前
BeforeCatch: false
# else之前
BeforeElse: false
# 缩进大括号
IndentBraces: false
# 分离空函数
SplitEmptyFunction: false
# 分离空语句
SplitEmptyRecord: false
# 分离空命名空间
SplitEmptyNamespace: false
# 在二元运算符前换行: None(在操作符后换行), NonAssignment(在非赋值的操作符前换行), All(在操作符前换行)
BreakBeforeBinaryOperators: NonAssignment
# 在大括号前换行: Attach(始终将大括号附加到周围的上下文), Linux(除函数、命名空间和类定义,与Attach类似),
# Mozilla(除枚举、函数、记录定义,与Attach类似), Stroustrup(除函数定义、catch、else,与Attach类似),
# Allman(总是在大括号前换行), GNU(总是在大括号前换行,并对于控制语句的大括号增加额外的缩进), WebKit(在函数前换行), Custom
# 注:这里认为语句块也属于函数
BreakBeforeBraces: Custom
# 在三元运算符前换行
BreakBeforeTernaryOperators: false
# 在构造函数的初始化列表的冒号后换行
BreakConstructorInitializers: AfterColon
#BreakInheritanceList: AfterColon
BreakStringLiterals: false
# 每行字符的限制,0表示没有限制
ColumnLimit: 0
CompactNamespaces: true
# 构造函数的初始化列表要么都在同一行,要么都各自一行
ConstructorInitializerAllOnOneLineOrOnePerLine: false
# 构造函数的初始化列表的缩进宽度
ConstructorInitializerIndentWidth: 4
# 延续的行的缩进宽度
ContinuationIndentWidth: 4
# 去除C++11的列表初始化的大括号{后和}前的空格
Cpp11BracedListStyle: true
# 继承最常用的指针和引用的对齐方式
DerivePointerAlignment: false
# 固定命名空间注释
FixNamespaceComments: true
# 缩进case标签
IndentCaseLabels: false
IndentPPDirectives: None
# 缩进宽度
IndentWidth: 4
# 函数返回类型换行时,缩进函数声明或函数定义的函数名
IndentWrappedFunctionNames: false
# 保留在块开始处的空行
KeepEmptyLinesAtTheStartOfBlocks: false
# 连续空行的最大数量
MaxEmptyLinesToKeep: 1
# 命名空间的缩进: None, Inner(缩进嵌套的命名空间中的内容), All
NamespaceIndentation: None
# 指针和引用的对齐: Left, Right, Middle
PointerAlignment: Right
# 允许重新排版注释
ReflowComments: true
# 允许排序#include
SortIncludes: false
# 允许排序 using 声明
SortUsingDeclarations: false
# 在C风格类型转换后添加空格
SpaceAfterCStyleCast: false
# 在Template 关键字后面添加空格
SpaceAfterTemplateKeyword: true
# 在赋值运算符之前添加空格
SpaceBeforeAssignmentOperators: true
# SpaceBeforeCpp11BracedList: true
# SpaceBeforeCtorInitializerColon: true
# SpaceBeforeInheritanceColon: true
# 开圆括号之前添加一个空格: Never, ControlStatements, Always
SpaceBeforeParens: ControlStatements
# SpaceBeforeRangeBasedForLoopColon: true
# 在空的圆括号中添加空格
SpaceInEmptyParentheses: false
# 在尾随的评论前添加的空格数(只适用于//)
SpacesBeforeTrailingComments: 1
# 在尖括号的<后和>前添加空格
SpacesInAngles: false
# 在C风格类型转换的括号中添加空格
SpacesInCStyleCastParentheses: false
# 在容器(ObjC和JavaScript的数组和字典等)字面量中添加空格
SpacesInContainerLiterals: true
# 在圆括号的(后和)前添加空格
SpacesInParentheses: false
# 在方括号的[后和]前添加空格,lamda表达式和未指明大小的数组的声明不受影响
SpacesInSquareBrackets: false
# 标准: Cpp03, Cpp11, Auto
Standard: Cpp11
# tab宽度
TabWidth: 4
# 使用tab字符: Never, ForIndentation, ForContinuationAndIndentation, Always
UseTab: Never

View File

@@ -0,0 +1,25 @@
{
"configurations": [
{
"name": "Win32",
"compilerArgs": [
"-fexec-charset=GBK"
],
"includePath": [
"${workspaceFolder}/**",
"C:/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/10.3.0/**",
"C:/TDM-GCC-64/x86_64-w64-mingw32/include/**"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"compilerPath": "C:/TDM-GCC-64/bin/gcc.exe",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "gcc-x64"
}
],
"version": 4
}

View File

@@ -0,0 +1,5 @@
{
"recommendations": [
"MS-CEINTL.vscode-language-pack-zh-hans"
]
}

View File

@@ -0,0 +1,28 @@
// https://github.com/Microsoft/vscode-cpptools/blob/master/launch.md
{
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch", // 配置名称,将会在启动配置的下拉菜单中显示
"type": "cppdbg", // 配置类型,cppdbg对应cpptools提供的调试功能;可以认为此处只能是cppdbg
"request": "launch", // 请求配置类型,可以为launch(启动)或attach(附加)
"program": "${fileDirname}\\${fileBasenameNoExtension}.exe", // 将要进行调试的程序的路径
"args": [], // 程序调试时传递给程序的命令行参数,一般设为空即可
"stopAtEntry": false, // 设为true时程序将暂停在程序入口处,相当于在main上打断点
"externalConsole": false,
"cwd": "${fileDirname}\\", // 调试程序时的工作目录,此为工作区文件夹;改成${fileDirname}可变为文件所在目录
"environment": [], // 环境变量
"internalConsoleOptions": "neverOpen", // 如果不设为neverOpen,调试时会跳到“调试控制台”选项卡,你应该不需要对gdb手动输命令吧?
"MIMode": "gdb", // 指定连接的调试器,可以为gdb或lldb。但我没试过lldb
"miDebuggerPath": "gdb.exe", // 调试器路径,Windows下后缀不能省略,Linux下则不要
"setupCommands": [
{ // 模板自带,好像可以更好地显示STL容器的内容,具体作用自行Google
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": false
}
],
"preLaunchTask": "Compile" // 调试会话开始前执行的任务,一般为编译程序。与tasks.json的label相对应
}
]
}

View File

@@ -0,0 +1,112 @@
{
"files.defaultLanguage": "c", // ctrl+N新建文件后默认的语言
"editor.formatOnType": true, // 输入分号(C/C++的语句结束标识)后自动格式化当前这一行的代码
"editor.suggest.snippetsPreventQuickSuggestions": false, // clangd的snippets有很多的跳转点,不用这个就必须手动触发Intellisense了
"editor.acceptSuggestionOnEnter": "off", // 我个人的习惯,按回车时一定是真正的换行,只有tab才会接受Intellisense
"code-runner.runInTerminal": true, // 设置成false会在“输出”中输出,无法输入
"code-runner.saveFileBeforeRun": true, // run code前保存
"code-runner.preserveFocus": true, // 若为false,run code后光标会聚焦到终端上。如果需要频繁输入数据可设为false
"code-runner.clearPreviousOutput": false, // 每次run code前清空属于code runner的终端消息,默认false
"code-runner.ignoreSelection": true, // 默认为false,效果是鼠标选中一块代码后可以单独执行,但C是编译型语言,不适合这样用
"C_Cpp.clang_format_sortIncludes": true,
"files.associations": {
"array": "cpp",
"atomic": "cpp",
"*.tcc": "cpp",
"cctype": "cpp",
"clocale": "cpp",
"cmath": "cpp",
"cstdarg": "cpp",
"cstddef": "cpp",
"cstdint": "cpp",
"cstdio": "cpp",
"cstdlib": "cpp",
"cwchar": "cpp",
"cwctype": "cpp",
"deque": "cpp",
"unordered_map": "cpp",
"vector": "cpp",
"exception": "cpp",
"algorithm": "cpp",
"memory": "cpp",
"memory_resource": "cpp",
"optional": "cpp",
"string": "cpp",
"string_view": "cpp",
"system_error": "cpp",
"tuple": "cpp",
"type_traits": "cpp",
"utility": "cpp",
"fstream": "cpp",
"initializer_list": "cpp",
"iosfwd": "cpp",
"iostream": "cpp",
"istream": "cpp",
"limits": "cpp",
"new": "cpp",
"ostream": "cpp",
"sstream": "cpp",
"stdexcept": "cpp",
"streambuf": "cpp",
"typeinfo": "cpp",
"valarray": "cpp",
"cassert": "cpp",
"cerrno": "cpp",
"cfloat": "cpp",
"chrono": "cpp",
"ciso646": "cpp",
"climits": "cpp",
"complex": "cpp",
"csetjmp": "cpp",
"csignal": "cpp",
"cstring": "cpp",
"ctime": "cpp",
"cuchar": "cpp",
"numeric": "cpp",
"ratio": "cpp",
"version": "cpp",
"bitset": "cpp",
"regex": "cpp",
"random": "cpp",
"unordered_set": "cpp",
"list": "cpp",
"any": "cpp",
"functional": "cpp",
"mutex": "cpp",
"shared_mutex": "cpp",
"thread": "cpp",
"variant": "cpp",
"map": "cpp",
"scoped_allocator": "cpp",
"future": "cpp",
"iomanip": "cpp",
"forward_list": "cpp",
"bit": "cpp",
"ccomplex": "cpp",
"cfenv": "cpp",
"charconv": "cpp",
"cinttypes": "cpp",
"codecvt": "cpp",
"condition_variable": "cpp",
"cstdalign": "cpp",
"cstdbool": "cpp",
"ctgmath": "cpp",
"set": "cpp",
"iterator": "cpp",
"filesystem": "cpp",
"ios": "cpp",
"locale": "cpp",
"queue": "cpp",
"stack": "cpp",
"typeindex": "cpp",
"compare": "cpp",
"concepts": "cpp",
"coroutine": "cpp",
"numbers": "cpp",
"ranges": "cpp",
"span": "cpp",
"stop_token": "cpp"
}, // 格式化时调整include的顺序(按字母排序)
}

View File

@@ -0,0 +1,40 @@
// https://code.visualstudio.com/docs/editor/tasks
{
"version": "2.0.0",
"tasks": [
{
"label": "Compile", // 任务名称,与launch.json的preLaunchTask相对应
"command": "g++", // 要使用的编译器,C++用g++
"args": [
"${file}",
"-o", // 指定输出文件名,不加该参数则默认输出a.exe,Linux下默认a.out
"${fileDirname}\\${fileBasenameNoExtension}.exe",
"-g", // 生成和调试有关的信息
"-Wall", // 开启额外警告
"-static-libgcc", // 静态链接libgcc,一般都会加上
"-fexec-charset=GBK", // 生成的程序使用GBK编码,不加这一条会导致Win下输出中文乱码
"-std=c++17", // C++最新标准为c++17,或根据自己的需要进行修改
], // 编译的命令,其实相当于VSC帮你在终端中输了这些东西
"type": "process", // process是vsc把预定义变量和转义解析后直接全部传给command;shell相当于先打开shell再输入命令,所以args还会经过shell再解析一遍
"group": {
"kind": "build",
"isDefault": true // 不为true时ctrl shift B就要手动选择了
},
"presentation": {
"echo": true,
"reveal": "always", // 执行任务时是否跳转到终端面板,可以为always,silent,never。具体参见VSC的文档
"focus": false, // 设为true后可以使执行task时焦点聚集在终端,但对编译C/C++来说,设为true没有意义
"panel": "shared" // 不同的文件的编译信息共享一个终端面板
},
},
{
"label": "commit gitee",
"type": "shell",
"command": "D:\\python\\一键推拉.cmd",
"group": {
"kind": "build",
"isDefault": true
},
}
]
}

View File

@@ -0,0 +1,25 @@
#include <bits/stdc++.h>
using namespace std;
const int N = 110;
char a[N];
int n;
int c0, c1, c2;
/*
6
p q p p q q
*/
int main() {
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
// p右 q左 0,1,2
for (int i = 1; i <= n; i++) {
int cnt = 0;
if (a[i - 1] == 'p') cnt++;
if (a[i + 1] == 'q') cnt++;
if (cnt == 0) c0++;
if (cnt == 1) c1++;
if (cnt == 2) c2++;
}
cout << c0 << " " << c1 << " " << c2 << endl;
return 0;
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 245 KiB

View File

@@ -0,0 +1,44 @@
#include <bits/stdc++.h>
/*
测试用例I:
5 3 3
答案:
2 4 5 7 8
测试用例II:
5 4 3
5 7 8 1 2
这个测试用例可以看出由小到大排序的意义。
*/
using namespace std;
const int N = 1010;
int a[N], al;
int main() {
int x, y, k;
cin >> x >> y >> k;
queue<int> q;
int cnt = 0;
for (int i = 1; i <= x + y; i++) q.push(i);
while (q.size() > x) {
auto t = q.front();
q.pop();
cnt++;
if (cnt % k > 0)
q.push(t);
else
cnt = 0;
}
while (q.size()) {
auto t = q.front();
q.pop();
a[al++] = t;
}
sort(a, a + al);
for (int i = 0; i < al; i++) cout << a[i] << " ";
return 0;
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 171 KiB

View File

@@ -0,0 +1,49 @@
### 题目分析
* 一共$n$个木桩,青蛙现在在第$1$个木桩上。
* 每个木桩上面有一个数字,表示从此木桩出发,最远可以跳几个木桩。比如有数字$2$,表示一次最远可以跳$2$个木桩。
* 从$1$号木桩出发,跳到$n$号木桩上,问**最少**需要几步?
$$\large [2,1,5,1,3]$$
* 不能只看眼前,不看将来,不是本次跳的越远越好,挑最远的跳,可能就略过了**以后更有利**的木桩。
<br>
* 从当前木桩($x$)出发,可以有几种选择呢?$1<=?<=x$种选择。
<br>
* **可不可以向后跳呢**?
* 可能需要一个状态记录的数组$st$,装$st[3]=1$
* 最短路径采用广度优先遍历$bfs$
---
(1)输入$n$,$N=110$,$a[N]$
(2) int st[N];
(3)
```c++
queue<PII> q;
//初始化,老大入队列
//老大已使用过
while(q.size()){
auto t=q.front();
q.pop();
if(目标位置是终点){
输出t.second
break;
}
从1到当前木桩上的数字进行遍历
{
for (从 1 到 t.first+1) {
下一个位置 x = t.first + i;
如果x走过了 continue;
//填充到队列中,做为下一跳的起点
q.push({x, 当前步数 + 1});
标识x已使用过
}
}
}
```

Binary file not shown.

After

Width:  |  Height:  |  Size: 171 KiB

View File

@@ -0,0 +1,43 @@
#include <bits/stdc++.h>
/*
5
2 1 5 1 3
答案 2
*/
using namespace std;
struct Node {
int pos;
int step;
};
const int N = 110;
int a[N];
int st[N];
int n;
void bfs() {
queue<Node> q;
q.push({1, 0});
st[1] = 1;
while (q.size()) {
auto t = q.front();
q.pop();
for (int i = 1; i <= a[t.pos]; i++) {
int x = t.pos + i;
// 先判断下一跳是不是终点
if (x == n) {
cout << t.step + 1 << endl;
exit(0);
}
// 正常入队列
if (st[x]) continue;
q.push({x, t.step + 1});
st[x] = 1;
}
}
}
int main() {
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
bfs();
return 0;
}

View File

@@ -0,0 +1,33 @@
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> PII;
const int N = 110;
int a[N];
int st[N];
int n;
void bfs() {
queue<PII> q; // first:当前木桩号 sencond:从起点走了几步到达了当前的木桩
q.push({1, 0});
st[1] = 1;
while (q.size()) {
auto t = q.front();
q.pop();
if (t.first == n) {
cout << t.second << endl;
exit(0);
}
for (int i = 1; i <= a[t.first]; i++) {
int x = t.first + i;
if (st[x]) continue;
q.push({x, t.second + 1});
st[x] = 1;
}
}
}
int main() {
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
bfs();
return 0;
}

View File

@@ -0,0 +1,36 @@
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
int main() {
int N; // 木桩的数量
cin >> N;
vector<int> nums(N); // 每个木桩上的数字
for (int i = 0; i < N; i++) {
cin >> nums[i];
}
vector<int> steps(N, -1); // 每个木桩的步数,初始值设为-1
steps[0] = 0; // 第一个木桩的步数为0
queue<int> q; // 存储已经遍历了的木桩
q.push(0);
while (!q.empty()) {
int cur = q.front();
q.pop();
for (int i = cur + 1; i <= cur + nums[cur] && i < N; i++) {
if (steps[i] == -1 || steps[i] > steps[cur] + 1) {
steps[i] = steps[cur] + 1;
q.push(i);
}
}
}
cout << steps[N - 1] << endl; // 输出最后一个木桩的步数
return 0;
}

View File

@@ -0,0 +1,54 @@
#include <bits/stdc++.h>
/**
3
A B B
A B A
B A B
*/
using namespace std;
const int N = 110;
typedef pair<int, int> PII;
char a[N][N], b[N][N];
int n;
int dx[] = {-1, 0, 1, 0}; //上右下左
int dy[] = {0, 1, 0, -1}; //上右下左
int res;
void bfs(int x, int y) {
memcpy(b, a, sizeof a);
queue<PII> q;
int cnt = 0;
// b[x][y] = 'A';
q.push({x, y});
while (q.size()) {
auto t = q.front();
q.pop();
cnt++;
for (int i = 0; i < 4; i++) {
int tx = t.first + dx[i], ty = t.second + dy[i];
if (tx == 0 || tx > n || ty == 0 || ty > n) continue;
if (b[tx][ty] == 'A') {
b[tx][ty] = 'B';
q.push({tx, ty});
}
}
}
res = max(res, cnt);
}
int main() {
cin >> n;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
cin >> a[i][j];
//每个b牌
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
if (a[i][j] == 'B')
bfs(i, j);
printf("%d\n", res);
return 0;
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 183 KiB

View File

@@ -0,0 +1,62 @@
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
int n; // 矩阵行数和列数
vector<vector<char>> cards; // 卡片状态
vector<vector<bool>> visited; // 访问标记
int ans = 0; // 相邻的A面卡片数量最大值
// 计算以 (x0, y0) 作为 B 面卡片翻转后相邻的 A 面卡片数量
int bfs(int x0, int y0) {
queue<pair<int, int>> q;
int cnt = 0; // 相邻 A 面卡片数量
vector<pair<int, int>> dirs{{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
q.push({x0, y0});
visited[x0][y0] = true;
if (cards[x0][y0] == 'A')
cnt++;
while (!q.empty()) {
int x = q.front().first;
int y = q.front().second;
q.pop();
// 枚举四个方向
for (auto &dir : dirs) {
int nx = x + dir.first;
int ny = y + dir.second;
if (nx >= 0 && nx < n && ny >= 0 && ny < n && !visited[nx][ny] && cards[nx][ny] == 'A') {
visited[nx][ny] = true;
cnt++;
q.push({nx, ny});
}
}
}
return cnt;
}
int main() {
cin >> n;
cards.resize(n, vector<char>(n));
visited.resize(n, vector<bool>(n, false));
int ans = 0; // 相邻的A面卡片数量最大值
// 读入卡片状态
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
cin >> cards[i][j];
}
}
// 枚举每个 B 面卡片
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
if (cards[i][j] == 'B') {
ans = max(ans, bfs(i, j));
visited = vector<vector<bool>>(n, vector<bool>(n, false));
}
}
}
// 输出相邻 A 面卡片最多的数量
cout << ans << endl;
return 0;
}

View File

@@ -0,0 +1,66 @@
#include <bits/stdc++.h>
using namespace std;
// 时间复杂度:10000*10000=1e8,C++一秒可过
/**
测试用例I:
3 2
3 6 1
答案
3
测试用例II:
4 2
2 6 11 18
答案:
4
解释:
将2调到6,需要4步。
也可以把2调到3,1步。
把6降到3,3步,共4步。
测试用例III:
5 3
2 6 11 18 14
答案:
7
解释:
将11调到14,需要3步
将18调到14,需要4步,共7步。
测试用例IV
5 3
1 2 7 8 12
答案:5
解释:
最终结果是8,
7+1=8,1步
8不需要动,0步
12-8=4,4步
共1+0+4=5步
*/
const int INF = 0x3f3f3f3f;
const int N = 1010;
int a[N];
int n, k;
int res = INF;
int main() {
cin >> n >> k;
for (int i = 0; i < n; i++) cin >> a[i];
for (int i = 0; i <= n - k; i++) { // 枚举起点
vector<int> b;
for (int j = i; j <= i + k - 1; j++) b.push_back(a[j]); // 将每个可用范围复制到数组b中
sort(b.begin(), b.end()); // 排序
int cnt = 0; // 变更次数
for (int j = 0; j < b.size(); j++) cnt += abs(b[b.size() / 2] - b[j]); // 枚举每个数字与中位数对比
res = min(res, cnt); // 记录最少变更次数
}
printf("%d\n", res);
return 0;
}

View File

@@ -0,0 +1,94 @@
![](http://dsideal.obs.cn-north-1.myhuaweicloud.com/HuangHai/BlogImages/2023/02/8e9859f774149cd1dfc90c107377b745.png)
### 解题思路
首先猜想最终相等的元素`t`的范围,最终应为数组中的某个元素。
* 若`t`小于数组中所有的元素,则此时增大`t`,那么所有元素变为`t`的次数将减小,可见`t`并非最优解;
* 若`t`大于数组中所有的元素,则此时减小`t`,那么所有元素变为`t`的次数将减小,可见`t`并非最优解;
可见`t`应该位于数组最大与最小值之间,下面证明最优的`t`可以取为数组中的某个元素,从而遍历数组元素即可。
<center><img src='https://img-blog.csdnimg.cn/706d85a45dc54ccbba1c956734aa40e7.png'></center>
如上图所示,设t为数组最大与最小值之间的一个值,若`t`不为数组中的某个元素,则设此时大于`t`的元素有`x`个,小于t的元素为`y`个。
若`x>y`,则上移一格`t`,此时操作数变化量为`-x+y<0`,将会更优;
若`x<y`,则下移一格`t`,此时操作数变化量为`+x-y<0`,将会更优;
若`x=y`,则无论上移与下移,操作数不变。
因此,可以看出若`t`不取数组中的某个元素值,则总可以通过调整`t`,使得操作数更少。因此`t`应该取数组中的某个值。
则**问题转化**为:
遍历数组中的数,找到某个元素`t`,使得`t`到其他元素值的距离之和最小。
由**绝对值不等式**可知,当`t`取数组**中位数**时,该**距离之和最小**。
中位数+绝对值不等式 $AcWing$ $104$. [货仓选址](https://www.cnblogs.com/littlehb/p/15459501.html)
排序之后取中位数,中位数的性质,所有数到他的和是最小的。
```cpp {.line-numbers}
#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const int N = 1010;
int a[N];
int n, k;
int res = INF;
// 时间复杂度:10000*10000=1e8,C++一秒可过
/**
测试用例I:
3 2
3 6 1
答案
3
测试用例II:
4 2
2 6 11 18
答案:
4
解释:
将2调到6,需要4步。
也可以把2调到3,1步。
把6降到3,3步,共4步。
测试用例III:
5 3
2 6 11 18 14
答案:
7
解释:
将11调到14,需要3步
将18调到14,需要4步,共7步。
测试用例IV
5 3
1 2 7 8 12
答案:5
解释:
最终结果是8,
7+1=8,1步
8不需要动,0步
12-8=4,4步
共1+0+4=5步
*/
int main() {
cin >> n >> k;
for (int i = 0; i < n; i++) cin >> a[i];
for (int i = 0; i < n - k + 1; i++) { // 枚举起点
vector<int> b;
for (int j = i; j <= i + k - 1; j++) b.push_back(a[j]); // 将每个可用范围复制到数组b中
sort(b.begin(), b.end()); // 排序
int cnt = 0; // 变更次数
for (int j = 0; j < b.size(); j++) cnt += abs(b[b.size() / 2] - b[j]); // 枚举每个数字与中位数对比
res = min(res, cnt); // 记录最少变更次数
}
printf("%d\n", res);
return 0;
}
```

Binary file not shown.

After

Width:  |  Height:  |  Size: 160 KiB

View File

@@ -0,0 +1,38 @@
#include <bits/stdc++.h>
using namespace std;
const int N = 15;
typedef pair<int, int> PII;
int n, m;
int res;
int dx[] = {-1, 0, 1, 0}; // 上右下左
int dy[] = {0, 1, 0, -1}; // 上右下左
int st[N][N];
void dfs(int x, int y, int cnt) {
if (x == 1 && y == 1 && cnt == m * n) {
res++;
return;
}
for (int i = 0; i < 4; i++) {
int tx = x + dx[i], ty = y + dy[i];
if (tx == 0 || tx > n || ty == 0 || ty > m) continue;
if (!st[tx][ty]) {
st[tx][ty] = 1;
dfs(tx, ty, cnt + 1);
st[tx][ty] = 0;
}
}
}
int main() {
cin >> n >> m;
dfs(1, 1, 0);
printf("%d\n", res);
return 0;
} /**
测试数据:
3 4
答案:
4
*/

Binary file not shown.

After

Width:  |  Height:  |  Size: 193 KiB

View File

@@ -0,0 +1,45 @@
#include <iostream>
using namespace std;
int dfs(int i, int j, int visited[][10], int cnt, int m, int n) {
// 终止条件:当遍历到最后一个格子时,如果已经遍历了mn个格子,则符合条件,数量加1
if (i == m && j == n) {
if (cnt == m * n)
return 1;
else
return 0;
}
// 非终止条件,继续递归
int res = 0;
if (i > 1 and !visited[i - 2][j - 1]) {
visited[i - 2][j - 1] = 1;
res += dfs(i - 1, j, visited, cnt + 1, m, n);
visited[i - 2][j - 1] = 0;
}
if (j > 1 and !visited[i - 1][j - 2]) {
visited[i - 1][j - 2] = 1;
res += dfs(i, j - 1, visited, cnt + 1, m, n);
visited[i - 1][j - 2] = 0;
}
if (i < m and !visited[i][j - 1]) {
visited[i][j - 1] = 1;
res += dfs(i + 1, j, visited, cnt + 1, m, n);
visited[i][j - 1] = 0;
}
if (j < n and !visited[i - 1][j]) {
visited[i - 1][j] = 1;
res += dfs(i, j + 1, visited, cnt + 1, m, n);
visited[i - 1][j] = 0;
}
return res;
}
int main() {
int m, n;
cin >> m >> n;
int visited[m][10] = {0}; // 二维数组表示已经遍历的格子
visited[0][0] = 1;
int cnt = 1; // 已经遍历的格子数目
cout << dfs(1, 1, visited, cnt, m, n) << endl;
return 0;
}

View File

@@ -0,0 +1,6 @@
#include <bits/stdc++.h>
using namespace std;
int main() {
return 0;
}

View File

@@ -0,0 +1 @@
<mxfile host="Electron" modified="2022-06-15T11:07:23.017Z" agent="5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) draw.io/17.2.4 Chrome/96.0.4664.174 Electron/16.1.0 Safari/537.36" etag="p5LVy9aHAXfcujLshn3e" version="17.2.4" type="device"><diagram id="3P9wmF2lPSN8qlaqJUDf" name="第 1 页">1ZvZcuI4FIafJpdJWZuXywBJejJLZTrdM91XUwYr4BpjUbYIME8/crxKMuAQG5QrrCNZxp9+HR0tvkLj5fYh8VeL31lAoytoBdsrNLmCEEEbiZ/MssstBHu5YZ6EQW4CteE5/I8WRquwrsOAplJBzljEw5VsnLE4pjMu2fwkYRu52AuL5Keu/DnVDM8zP9Ktf4cBX+RWFzq1/QsN54vyycAu3m/pl4WLN0kXfsA2DRO6u0LjhDGeXy23Yxpl8Eou+X33e3KrP5bQmHe54a/Hxebfr970y5/397/Ev756k9vkuqjl1Y/W8gunfFciENUI2iIx2ixCTp9X/izL2YgGF7YFX0YiBcSln67yJngJt1Q8dfTCYl40KfBEWv/L5fNpwum2YSpe4YGyJeXJThQpcqFd4NzJOtnUjYML06LRLqXNL+QwryquiYmLAto7AEINIBwQ4EsYRWMWsUQYYhbTfphiJDN1LswUaUyR2aIklgyw6vOXIog1gsRsggKhRBDjCxMkGkHHbIIKQGhdGKCtAXTNBujJAN0L8/M0fthsfthRuvClnWAZGjQQ2mYjVCSILq1BoIeHOsA4uM3ibJGaRX6ahjOZW8LWcZABm1gt0Og25D+Kktn1z6zcDSlSk21x21tiVyZi8W4/momfzUR901uqvGtv86RsnczoIQp5Oe4nc8qPh4I0kGYUemM3WpO0tGZpS2jk8/BVDsvbmrh4whMLxZtVWqrEU4oJKCrJ37u4qzlvUCpSo8MqAi8rysFoFQlV+LtGsVVWINU0WYH5gEz1IPwMMgUdRZqreXiZwo4yRWbJVFEXOVGlRFWpKvd3q3TPc5RQH3jSrFtc5DX2K3B9RjSQwGvfa6LEUUeJe0ZJHEN8AzAEiNgQOcIP27KCSHv2u/00cVuV2XsPgEpPc87RA/QZ7VA94JRIBDT1DwbTv9dR/9gs/bsH9Q/3dI9361+JnmFH/fcmUX3JwFiJNjx27yLFHUVaDtCmqFSdfKGT42X3kNyRQ25c4rg2Ro4NbdfDH/TSvetYX7npW8d7AofhHGeptaOiJGZp0vJatVKusFj9eE6kLr6q0h/aczqmKu4Uj3u6SklHkdpGiVTo7sYmLrE8SLCDHcXfZdGvByyAsAUwthyLnCZSiA5GEcf6wtASds8n4XMN5HbXgdwoOUJlgo7UMLCr4ABQ4knrvF4R6eOwvjefLvxVdkm3guIoYOvpW44sJ3B8dXuAxexrZaeelOmGDuwDOuh/W1kfZvROaiJQYwDqTk7fmDcR4FaGZQzPLnt8BvME0DCg5UbYwXMPJgN1TQOqbwDqW6gGA622Ro0BCjWg+rkSg4EiyzSg+taIfs7EZKCmDUpYX2n3PhNQbNygpK8LlzOLT0LUuFGpwwqlwTyJcYNSh/U3g3naxo1JHRaDTOZp3JCkz5M+E0/HtBGJ6EGoxrP7YmUbtT6Pkwy3K4S6rnBis7aF1CC8Onn74Y0fqFTU05ERZWvVls9M6Uu4ygDpgMPlscLDdvs9kfJ18ni3nj56374/XP+Tgokz/v5Hy4c/e33SzI8itubHnc+KJqH4XzSpb3qqTaMgTOiMhywW2aKJM6WN9jijlg6xf6EWOEjeWARWy+cXbouggSqYU3xUK159Wnc74yzZy3i9jPICaJTBCAW+3/wpjZ5YGhbIpoxztmwUuI3CeZbBmdIOgm0UxnRcfVNo9UQaYvXzNbvljDdqIa0euu0AWiTrrwtz2dffaKK7/wE=</diagram></mxfile>

View File

@@ -0,0 +1,51 @@
#### 1、约瑟夫环可以使用队列来模拟。
> 测试用例:
5 3 3
> 答案:
2 4 5 7 8
#### 2、输入 x,y,k 怎么理解?x:男生人数,y:女生人数,k:走到几出列,比如3
```c++
cin>>x>>y>>k;
```
#### 3、队列初始化
```c++
queue<int> q;
balabala
```
#### 4、操作队列
```c++
int cnt=0;
while(剩余x个位置){
auto t=q.front();
q.pop();
cnt++;
balabala
是k的倍数,则干掉,不重新入队列,否则重新入队列 balabala
}
```
#### 5、输出男同学的位置
```c++
while(队列不空){
auto t=q.front();
q.pop();
输出弹出的位置号
balabala
}
```
#### 6、需要排序
```c++
int a[N],idx;
while(q.size()){
auto t=q.front();
q.pop();
a[++idx]=t;
}
sort(a+1,a+1+x);
for(int i=1;i<=x;i++>)
cout<<a[i]<<" ";
```

View File

@@ -0,0 +1,9 @@
https://blog.csdn.net/Youtey/article/details/125029198
https://blog.csdn.net/Youtey?type=blog
2022年5月29日第十三届蓝桥杯python国赛第五题
https://www.bilibili.com/video/BV1A94y1m7Fu
2022年5月29日第十三届蓝桥杯python国赛第六题
https://www.bilibili.com/video/BV1tU4y1y7fn

Binary file not shown.

After

Width:  |  Height:  |  Size: 83 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 82 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 72 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 88 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 87 KiB

View File

@@ -0,0 +1,20 @@
### 2、青蛙跳木桩
$Q$:为什么写的伪代码与真正实现的代码,检查是不是终点的位置不一样?
(1)在检查下一个可达位置时,直接检查下一个位置是不是终点
(2)在检查下一个可达位置时,不管下一个位置是不是终点,直接再次入队列,直到它从队列中弹出时再检查。
这两种作法,从效率上讲,肯定是第1种更佳。参看代码进行理解。
### 3、翻卡片
为什么存在某种特定的情况比如
B B A A A
B B A A A
A A A A A
A A A A A
Q:此时,我们选择了左上角的B,然后以此点为起点,进行洪水覆盖,结果,发现只能覆盖掉1个点,可是,此时整个图形的最大覆盖值应该是 16 个!
这不就错了吗?
A: 不用怕,因为我们的程序还在继续,会走到第2个B点,此时就能获取到17个,比刚才的肯定要大,这个结果最终会替换掉刚才被封成边缘的B。也算是一种贪心策略吧。当然,我们也可以在假定某个位置是A以后,再全局洪水覆盖一次,找出连通块的数量最大值,也是一样的。