目录调整

This commit is contained in:
2025-03-17 19:45:51 +08:00
parent c797380b32
commit b28d6fef87
17 changed files with 58 additions and 0 deletions

View File

@@ -0,0 +1,12 @@
#include<bits/stdc++.h>
using namespace std;
int main(){
string s = "hello world";
char *str = "hello world";
char *str_1 = &str[2];
str++;
cout << str << endl;
return 0;
}

View File

@@ -0,0 +1,44 @@
// 主元素寻找
#include <iostream>
#include <vector>
using namespace std;
int findMajorityElement(const vector<int>& nums) {
int candidate = -1;
int count = 0;
// 第一次遍历:找出候选主元素
for (int num : nums) {
if (count == 0) {
candidate = num;
}
count += (num == candidate) ? 1 : -1;
}
// 第二次遍历:验证候选主元素
count = 0;
for (int num : nums) {
if (num == candidate) {
count++;
}
}
// 如果候选元素出现次数超过数组长度的一半,则为所求主元素
if (count > nums.size() / 2) {
return candidate;
} else {
return -1;
}
}
int main() {
vector<int> nums = {0, 5, 3, 5, 5, 7, 5, 5};
int result = findMajorityElement(nums);
if (result != -1) {
cout << "主元素是: " << result << endl;
} else {
cout << "没有主元素" << endl;
}
return 0;
}

View File

@@ -0,0 +1,34 @@
#include<bits/stdc++.h>
using namespace std;
vector<vector<int>> results;
void backtrack(vector<int>& current, vector<bool>& used, int n){
if(current.size() == n){
results.push_back(current);
return;
}
for(int i = 1; i <= n; i++){
if(!used[i]){
current.push_back(i);
used[i] = true;
backtrack(current, used, n);
current.pop_back();
used[i] = false;
}
}
}
int main(){
int n; cin >> n;
vector<int> current;
vector<bool> used(n+1, false);
backtrack(current, used, n);
for(const auto& perm:results){
for(int num:perm){
cout << num << " ";
}
cout << endl;
}
return 0;
}

View File

@@ -0,0 +1,31 @@
#include<bits/stdc++.h>
using namespace std;
const int N = 1e2 + 10; // 定义最大数组大小
int a[N]; // 存储排列结果
bool vis[N]; // 标记数字是否被访问过
int n; // 排列的大小
// 回溯函数dep表示当前处理到第几个数字
void dfs(int dep) {
if (dep == n + 1) { // 如果已经处理完所有数字
for (int i = 1; i <= n; i++) { // 输出排列
cout << a[i] << " \n"[i==n];
}
return;
}
for (int i = 1; i <= n; i++) { // 尝试将1到n的每个数字放入当前位置
if (vis[i]) continue; // 如果该数字已经被使用,跳过
vis[i] = true; // 标记该数字为已使用
a[dep] = i; // 将数字放入当前位置
dfs(dep + 1); // 递归处理下一个位置
vis[i] = false; // 回溯,恢复状态
}
}
int main() {
cin >> n; // 输入排列的大小
dfs(1); // 从第一个位置开始递归
return 0;
}

View File

@@ -0,0 +1,44 @@
// lanqiao1508 N皇后问题
#include<bits/stdc++.h>
using namespace std;
const int N = 15;
int n, ans;
int vis[N][N];
void dfs(int dep){
if(dep == n + 1){
ans++;
return;
}
for(int i = 1; i <= n; i++){ // 为什么下面只标记同一行,因为每次搜寻时只会在同一行(深度)寻找一个皇后
if(vis[dep][i]) continue; // 所以只需要排除某行某个元素的同一列即可
// 修改状态
for(int _i = 1; _i <= n; _i++) vis[_i][i]++; // 标记同一列
// 表壳 X 形模版
for(int _i = dep, _j = i; _i >= 1 && _j >= 1; _i--, _j--) vis[_i][_j]++; // 标记左上对角线
for(int _i = dep, _j = i; _i <= n && _j >= 1; _i++, _j--) vis[_i][_j]++; // 标记右上对角线
for(int _i = dep, _j = i; _i >= 1 && _j <= n; _i--, _j++) vis[_i][_j]++; // 标记左下对角线
for(int _i = dep, _j = i; _i <= n && _j <= n; _i++, _j++) vis[_i][_j]++; // 标记右下对角线
// 搜查下一深度
dfs(dep + 1);
// 恢复现场
for(int _i = 1; _i <= n; _i++) vis[_i][i]--;
for(int _i = dep, _j = i; _i >= 1 && _j >= 1; _i--, _j--) vis[_i][_j]--;
for(int _i = dep, _j = i; _i <= n && _j >= 1; _i++, _j--) vis[_i][_j]--;
for(int _i = dep, _j = i; _i >= 1 && _j <= n; _i--, _j++) vis[_i][_j]--;
for(int _i = dep, _j = i; _i <= n && _j <= n; _i++, _j++) vis[_i][_j]--;
}
}
int main(){
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cin >> n;
dfs(1);
cout << ans << '\n';
return 0;
}
/* test samples
5
*/

View File

@@ -0,0 +1,33 @@
// lanqiao182 小朋友崇拜圈
#include<bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
int n, a[N], dfn[N], idx, mindfn;
int dfs(int x){
dfn[x] = ++idx;
if(dfn[a[x]]){
if(dfn[a[x]] >= mindfn) return dfn[x] - dfn[a[x]] + 1;
return 0;
}
return dfs(a[x]);
}
int main(){
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cin >> n;
for(int i = 1; i <= n; i++) cin >> a[i];
int ans = 0;
for(int i = 1; i <= n; i++){
if(!dfn[i]){
mindfn = idx + 1;
ans = max(ans, dfs(i));
}
}
cout << ans << endl;
return 0;
}
/* test samples
9
3 4 2 5 3 8 4 6 9
*/

View File

@@ -0,0 +1,59 @@
// lanqiao178 全球变暖
#include<bits/stdc++.h>
using namespace std;
const int N = 1e3 + 10;
char mp[N][N];
int n, scc, col[N][N];
bool vis[N*N];
// 移动向量
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
void dfs(int x, int y){
col[x][y] = scc;
for(int i = 0; i < 4; i++){
int nx = x + dx[i], ny = y + dy[i];
if( col[nx][ny] || mp[nx][ny] == '.') continue; // 不需要判断nx, ny是否在地图里面因为地图边缘都是海洋
dfs(nx ,ny);
}
}
int main(){
ios::sync_with_stdio, cin.tie(0), cout.tie(0);
cin >> n;
for(int i = 1; i <= n; i++) cin >> mp[i] + 1;
for(int i = 1; i <= n; i++){
for(int j = 1; j <= n; j++){
if(col[i][j] || mp[i][j] == '.') continue;
scc++; // scc表示当前颜色编号
dfs(i, j);
}
}
int ans = 0;
for(int i = 1; i <= n; i++){
for(int j = 1; j <= n; j++){
if(mp[i][j] == '.') continue;
bool tag = true;
for(int k = 0; k < 4; k++){
int x = i + dx[k], y = j + dy[k];
if(mp[x][y] == '.') tag = false;
}
if(tag){
if(!vis[col[i][j]]) ans++;
vis[col[i][j]] = true;
}
}
}
cout << scc - ans << '\n';
return 0;
}
/* test samples
7
.......
.##....
.##....
....##.
..####.
...###.
.......
*/

View File

@@ -0,0 +1,44 @@
// lanqiao2942 数字王国之军训排队
#include<bits/stdc++.h>
using namespace std;
const int N = 15;
int a[N], n;
vector<int> v[N];
bool dfs(int cnt, int dep){
if(dep == n+1){
// 检查当前方案的合法性
for(int i = 1; i <= cnt; i++){
for(int j = 0; j < v[i].size(); j++){
for(int k = j+1; k < v[i].size(); k++){
if(v[i][k] % v[i][j] == 0) return false;
}
}
}
return true;
}
// 枚举每个人所属的队伍
for(int i = 1; i <= cnt; i++){
v[i].push_back(a[dep]);
if(dfs(cnt, dep + 1)) return true;
// 恢复现场
v[i].pop_back();
}
return false;
}
int main(){
cin >> n;
for(int i = 1; i <= n; i++) cin >> a[i];
sort(a+1, a+1+n);
for(int i = 1; i <= n; i++){
if(dfs(i, 1)){
cout << i << '\n';
break;
}
}
return 0;
}
/* test samples
4
2 3 4 4
*/

View File

@@ -0,0 +1,45 @@
// lanqiao2942 数字王国之军训排队
#include<bits/stdc++.h>
using namespace std;
const int N = 15;
int a[N], n;
vector<int> v[N];
// 剪枝
bool dfs(int cnt, int dep){
if(dep == n+1){
return true;
}
// 枚举每个人所属的队伍
for(int i = 1; i <= cnt; i++){
bool tag = true;
for(const auto &j:v[i]){
if(a[dep] % j == 0){
tag = false;
break;
}
}
if(!tag) continue;
v[i].push_back(a[dep]);
if(dfs(cnt, dep + 1)) return true;
// 恢复现场
v[i].pop_back();
}
return false;
}
int main(){
cin >> n;
for(int i = 1; i <= n; i++) cin >> a[i];
sort(a+1, a+1+n);
for(int i = 1; i <= n; i++){
if(dfs(i, 1)){
cout << i << '\n';
break;
}
}
return 0;
}
/* test samples
4
2 3 4 4
*/

View File

@@ -0,0 +1,37 @@
// 蓝桥 3008 特殊的三角形
#include<bits/stdc++.h>
using namespace std;
const int N = 1e6+9;
int cnt[N], prefix[N];
void dfs(int dep, int st, int mul, int sum){
if(mul > 1e6) return;
if(dep == 4){
cnt[mul]++;
return;
}
int up = pow(1e6 / mul, 1.0/(4-dep)) + 3;
for(int i = st + 1; i < (dep == 3?sum:up); i++){
dfs(dep+1, i, mul * i, sum + i);
}
}
int main(){
dfs(1, 0, 1, 0);
for(int i = 1; i <= 1e6; i++){
prefix[i] = prefix[i-1] + cnt[i];
}
int q; cin >> q;
while(q--){
int l, r; cin >> l >> r;
cout << prefix[r] - prefix[l-1] << '\n';
}
return 0;
}
/* test samples
4
1 10
30 50
60 200
200 400
*/

View File

@@ -0,0 +1,39 @@
// 蓝桥3075 特殊的多边形
#include<bits/stdc++.h>
using namespace std;
const int N = 1e5+9;
int n, cnt[N], prefix[N];
void dfs(int dep, int st, int mul, int sum){
// 剪枝1
if(mul > 1e5) return;
if(dep == n + 1){
cnt[mul]++;
return;
}
// 剪枝2
int up = pow(1e5 / mul, 1.0/(n - dep + 1)) + 3;
for(int i = st + 1; i < (dep == n?min(sum, up):up); i++){
dfs(dep+1, i, mul * i, sum + i);
}
}
int main(){
int q; cin >> q >> n;
dfs(1, 0, 1, 0);
for(int i = 1; i <= 1e5; i++){
prefix[i] = prefix[i-1] + cnt[i];
}
while(q--){
int l, r; cin >> l >> r;
cout << prefix[r] - prefix[l-1] << '\n';
}
return 0;
}
/* test samples
4 3
1 10
30 50
60 200
200 400
*/

View File

@@ -0,0 +1,23 @@
// 斐波那契数递归优化
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
const ll p = 1e9+7;
const int inf = 1e9, N = 1e5+3;
ll dp[N];
ll f(int n){
if( n <= 2) return 1;
if(dp[n] != -1) return dp[n];
return dp[n] = (f(n -1 ) + f(n - 2)) % p;
}
int main(){
memset(dp, -1, sizeof dp);
int n; cin >> n;
cout << f(n) << endl;
return 0;
}
/* test samples 976496506
5000
*/

View File

@@ -0,0 +1,67 @@
// 蓝桥3820 混境之地
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
const ll p = 1e9 + 7;
const int inf = 1e9, N = 1e3 + 3;
int n, m, k, sx, sy, fx, fy, h[N][N];
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
int dp[N][N][2];
bool inmp(int x, int y){
return x >= 1 && x <= n && y >= 1 && y <= m;
}
// 返回值表示能否到达终点(fx, fy), t表示当前使用的喷气背包的次数
bool dfs(int x, int y, int t){
if(x == fx && y == fy) return true;
if(dp[x][y][t] != -1) return dp[x][y][t];
for(int i = 0; i < 4; i++){
int nx = x + dx[i], ny = y + dy[i];
if(!inmp(nx, ny)) continue;
if(!t){ // 没用过背包
// 不用
if(h[x][y] > h[nx][ny] && dfs(nx, ny, 0)) return dp[x][y][t] = true;
// 用
if(h[x][y] + k > h[nx][ny] && dfs(nx, ny, 1)) return dp[x][y][t] = true;
}else{ // 已经用过一次背包了
// 不用
if(h[x][y] > h[nx][ny] && dfs(nx, ny, 1)) return dp[x][y][t] = true;
}
}
return dp[x][y][t] = false;
}
int main(){
memset(dp, -1, sizeof dp); // 记忆化搜索
cin >> n >> m >> k;
cin >> sx >> sy >> fx >> fy;
for(int i = 1; i <= n; i++){
for(int j = 1; j <= m; j++){
cin >> h[i][j];
}
}
cout << (dfs(sx, sy, 0)?"Yes":"No") << '\n';
return 0;
}
/* test samples 1 -> YES
5 5 30
1 1 5 5
3 20 13 12 11
19 17 33 72 10
12 23 12 23 9
21 43 23 12 2
21 34 23 12 1
*/
/* test samples 1 -> NO
5 5 10
1 1 5 5
3 2 13 12 11
1 17 33 72 10
12 23 12 23 9
21 43 23 12 2
21 34 23 12 1
*/

View File

@@ -0,0 +1,49 @@
// lanqiao216 地宫寻宝
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
const ll p = 1e9 + 7;
const int N = 55;
int n, m, k, c[N][N];
int dx[] = {0, 1};
int dy[] = {1, 0};
int dp[N][N][15][15];
bool inmp(int x, int y){
return x >= 1 && x <= n && y >= 1 && y <= m;
}
// 表示从(1,1)到(x, y), 总共有cnt件宝贝, 且最大值为max的方案数
ll dfs(int x, int y, int mx, int cnt){
if(x == n && y == m) return (ll)(cnt == k);
if(dp[x][y][mx][cnt] != -1) return dp[x][y][mx][cnt];
ll res = 0;
for(int i = 0; i < 2; i++){
int nx = x + dx[i], ny = y + dy[i];
if(!inmp(nx, ny)) continue;
// 拿宝贝
if(c[nx][ny] > mx && cnt < k) res = (res + dfs(nx, ny, c[nx][ny], cnt + 1)) % p;
//不拿宝贝
res = (res + dfs(nx, ny, mx, cnt)) % p;
}
return dp[x][y][mx][cnt] = res;
}
int main(){
memset(dp, -1, sizeof dp);
cin >> n >> m >> k;
for(int i = 1; i <= n; i++){
for(int j = 1; j <= m; j++){
cin >> c[i][j];
c[i][j]++;
}
}
cout << (dfs(1,1,0,0) + dfs(1,1,c[1][1],1)) % p << '\n';
return 0;
}
/* test samples 1 -> 2
2 2 2
1 2
2 1
*/

View File

@@ -0,0 +1,65 @@
// 树形结构的前中后序遍历以及bfs层级遍历
#include<bits/stdc++.h>
using namespace std;
const int N = 20;
int ls[N], rs[N];
// 先序遍历
void dfs1(int x){
cout << x << ' ';
if(ls[x]) dfs1(ls[x]);
if(rs[x]) dfs1(rs[x]);
}
// 中序遍历
void dfs2(int x){
if(ls[x]) dfs2(ls[x]);
cout << x << ' ';
if(rs[x]) dfs2(rs[x]);
}
// 后序遍历
void dfs3(int x){
if(ls[x]) dfs3(ls[x]);
if(rs[x]) dfs3(rs[x]);
cout << x << ' ';
}
// 层序遍历
void bfs(){
queue<int> q;
q.push(1);
while(q.size()){
int x = q.front(); q.pop();
cout << x << ' ';
if(ls[x]) q.push(ls[x]);
if(rs[x]) q.push(rs[x]);
}
}
int main(){
int n; cin >> n;
for(int i = 1; i <= n; i++) cin >> ls[i] >> rs[i];
dfs1(1); cout << endl;
dfs2(1); cout << endl;
dfs3(1); cout << endl;
bfs(); cout << endl;
return 0;
}
/* test samples
10
2 3
4 5
6 7
8 0
0 9
0 0
10 0
0 0
0 0
0 0
out:
1 2 4 8 5 9 3 6 7 10
8 4 2 5 9 1 6 3 10 7
8 4 9 5 2 6 10 7 3 1
1 2 3 4 5 6 7 8 9 10
*/

View File

@@ -0,0 +1,52 @@
// lanqiao3029 卖树
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 1e5 + 9;
vector<int> g[N];
int dep1[N], depU[N], depV[N];
void dfs(int x, int fa, int dep[]){
dep[x] = dep[fa] + 1;
for(const auto &y:g[x]){
if(y == fa) continue;
dfs(y, x, dep);
}
}
void solve(){
ll n, k, c; cin >> n >> k >> c;
for(int i = 1; i < n; i++){
int u, v; cin >> u >> v;
g[u].push_back(v), g[v].push_back(u);
}
dep1[0] = depU[0] = depV[0] = -1;
dfs(1, 0, dep1);
// 得到U
int U = 1;
for(int i = 1; i <= n; i++) if(dep1[i] > dep1[U]) U = i;
dfs(U, 0, depU);
int V = 1;
for(int i = 1; i <= n; i++) if(depU[i] > depU[V]) V = i;
dfs(V, 0, depV);
// 枚举所有点计算价值和代价
ll ans = 0;
for(int i = 1; i <= n; i++){
// 价值 = 以 i 为根的树的所有点的最大深度, 即max(depU[i], depV[i])
// 代价 = 从1走到i的代价
ans = max(ans, max(depU[i], depV[i])*k - dep1[i]*c);
}
cout << ans << endl;
// 多组测试数据,需要重置
for(int i = 1; i <= n; i++) g[i].clear();
}
int main(){
int t; cin >> t;
while(t--) solve();
return 0;
}
/* test samples -> 4
1
3 4 5
1 2
1 3
*/

View File

@@ -0,0 +1,58 @@
// lanqiao4385 最近公共祖先LCA查询
#include<bits/stdc++.h>
using namespace std;
const int N = 1e5 + 9;
int dep[N], fa[N][24];
vector<int> g[N];
void dfs(int x, int p){
dep[x] = dep[p] + 1;
fa[x][0] = p;
for(int i = 1; i <= 20; i++) fa[x][i] = fa[fa[x][i-1]][i-1];
for(const auto&y:g[x]){
if(y == p) continue;
dfs(y, x);
}
}
int lca(int x, int y){
if(dep[x] < dep[y]) swap(x, y);
for(int i = 20; i >= 0; i--){
if(dep[fa[x][i]] >= dep[y]){
x = fa[x][i];
}
}
if(x == y) return x;
for(int i = 20; i >= 0; i--){
if(fa[x][i] != fa[y][i]){
x = fa[x][i], y = fa[y][i];
}
}
return fa[x][0];
}
int main(){
int n; cin >> n;
for(int i = 1; i < n; i++){
int u, v; cin >> u >> v;
g[u].push_back(v); g[v].push_back(u);
}
dfs(1, 0);
int m; cin >> m;
while(m--){
int x, y; cin >> x >> y;
cout << lca(x, y) << '\n';
}
return 0;
}
/* test samples -> 2 1 1
5
1 2
1 3
2 4
2 5
3
4 5
3 4
3 5
*/