Initial commit

This commit is contained in:
2025-03-14 22:39:32 +08:00
commit 7b1e0f329e
38 changed files with 1153 additions and 0 deletions

BIN
.DS_Store vendored Normal file

Binary file not shown.

28
14lanqiao/test1.cpp Normal file
View File

@@ -0,0 +1,28 @@
#include<bits/stdc++.h>
using namespace std;
const int N = 1e2 + 10;
int a[N] = { 0,5,6,8,6,9,1,6,1,2,4,9,1,9,8,2,3,6,4,7,7,5,9,5,0,3,8,7,5,8,1,5,8,6,1,8,3,0,3,7,9,2,
7,0,5,8,8,5,7,0,9,9,1,9,4,4,6,8,6,3,3,8,5,1,6,3,4,6,7,0,7,8,2,7,6,8,9,5,6,5,6,1,4,0,1,
0,0,9,4,8,0,9,1,2,8,5,0,2,5,3,3 }; // 示例数组
int day[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int main(){
int ans = 0;
for(int i = 1; i <=12; i++){
for(int j = 1; j <= day[i]; j++){
int date[] = {0, 2, 0, 2, 3, i/10, i%10, j/10, j%10 };
int id = 1;
for(int i = 1; i<=100; i++){
if(a[i] == date[id]){
id++;
if(id>8){
ans++;
break;
}
}
}
}
}
cout << ans << endl;
return 0;
}

13
14lanqiao/test2.cpp Normal file
View File

@@ -0,0 +1,13 @@
#include<bits/stdc++.h>
using namespace std;
int main(){
double n = 23333333;
double res = 11625907.5798;
for(int a = 0; a <= n; a++){ // a为0出现的次数
int b = n - a; // b为1出现的次数
if(a>=b) continue;
double ans = (-1)*a*(a/n)*log2(a/n) + (-1)*b*(b/n)*log2(b/n);
if(fabs(ans - res) < 1e-4) cout << a << endl;
}
return 0;
}

49
14lanqiao/test3-1.cpp Normal file
View File

@@ -0,0 +1,49 @@
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N = 1e4 + 10;
int a[N], b[N];
//枚举法
signed main(){
int n; cin >> n;
int mxv = LLONG_MIN, miv = LLONG_MAX;
for(int i = 1; i <= n; i++){
cin >> a[i] >> b[i];
if(b[i] != 1) mxv = max(mxv, a[i]/(b[i] - 1));
miv = min(miv, a[i]/(b[i] + 1));
}
for(int v = miv; v <= mxv; v++){
bool flag = true;
for(int i = 1; i <= n; i++){
if(a[i] / v != b[i]){
flag = false;
break;
}
}
if(flag){
cout << v << " ";
break;
}
}
for(int v = mxv; v >= miv; v--){
bool flag = true;
for(int i = 1; i <= n; i++){
if(a[i] / v != b[i]){
flag = false;
break;
}
}
if(flag){
cout << v << endl;
break;
}
}
return 0;
}
/* test samples
3
75 3
53 2
59 2
*/

26
14lanqiao/test3-2.cpp Normal file
View File

@@ -0,0 +1,26 @@
#include<bits/stdc++.h>
using namespace std;
int main(){
int n; cin >> n;
int mxv = INT_MIN; int miv = INT_MAX;
for(int i = 1; i <= n; i++){
int a, b; cin >> a >> b;
miv = min(miv, a/b);
mxv = max(mxv, a/(b+1)+1);
}
cout << mxv << " " << miv << endl;
return 0;
}
/*
数学优化
通过floor(A/V) = B可以推出 A/V =B,B+1
进而得到B <= A/V < B+1三个表达式同时被A除
得到关于V的不等式即 A/B >= V > A/(B+1)
*/
/* test samples
3
75 3
53 2
59 2
*/

50
14lanqiao/test4.cpp Normal file
View File

@@ -0,0 +1,50 @@
#include<bits/stdc++.h>
using namespace std;
const int N = 1e2 + 10;
struct node{
int t, d, l; // 分别代表可以降落的时刻,盘旋时间,降落花费时间
}a[N];
bool vis[N];
int t, n;
bool flag = false;
// lasttime: 之前完成降落的所有飞机中的最后一架飞机降落的时间
void dfs(int dep, int lasttime){
if(dep == n + 1){
flag = true;
return;
}
for(int i = 1; i <= n; i++){
if(!vis[i] && a[i].t + a[i].d >= lasttime){
vis[i] = 1;
dfs(dep + 1, max(lasttime, a[i].t) + a[i].l);
// 遍历同级的另一个飞机的下一个深度的飞机前,需要将该飞机设置为未安排状态
vis[i] = 0;
}
}
}
int main(){
cin >> t;
while(t--){
cin >> n;
for(int i = 1; i<=n; i++){
cin >> a[i].t >> a[i].d >> a[i].l;
}
flag = false;
memset(vis, 0, sizeof(vis));
dfs(1, 0);
if(flag) cout << "YES" << endl;
else cout << "NO" << endl;
}
return 0;
}
/* test samples
2
3
0 100 10
10 10 10
0 2 20
3
0 10 20
10 10 20
20 10 20
*/

33
14lanqiao/test5-1.cpp Normal file
View File

@@ -0,0 +1,33 @@
#include<bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
int ft[N], bk[N], dp[N];
/*
ft[i]第i个元素的首数字、bki第i个元素尾数字
类LIS问题的DP解法-时间复杂度0(n^2)
状态 dp[i] 以第主个元素作为结尾的最长接龙数列的长度
状态转移方程 dp[i] = max(dp[i], dp[j]+1);
*/
int main(){
int n; cin >> n;
string s;
for(int i = 1; i<=n; i++){
cin >> s;
ft[i] = s.front() - '0', bk[i] = s.back() - '0', dp[i] = 1;
}
int mx = 1;
for(int i = 2; i <= n; i++){
for(int j = 1; j < i; j++){
if(bk[j] == ft[i]){
dp[i] = max(dp[i], dp[j] + 1);
}
mx = max(mx, dp[i]);
}
}
cout << n - mx << endl;
return 0;
}
/* test samples 易超时
5
11 121 22 12 2023
*/

25
14lanqiao/test5-2.cpp Normal file
View File

@@ -0,0 +1,25 @@
#include<bits/stdc++.h>
using namespace std;
int dp[10];
/*
优化dp方案
状态 dp[i] 以数字i结尾的最长接龙序列的长度
状态转移方程 dp[bk]=max(dp[ft]+1, dp[bk])
*/
int main(){
int n; cin >> n;
string s;
int mx = 1;
for(int i = 0; i < n; i++){
cin >> s;
int ft = s.front() - '0', bk = s[s.size() - 1] - '0';
dp[bk] = max(dp[ft] + 1, dp[bk]);
mx = max(mx, dp[bk]);
}
cout << n - mx << endl;
return 0;
}
/* test samples
5
11 121 22 12 2023
*/

15
15lanqiao/test1-1.cpp Normal file
View File

@@ -0,0 +1,15 @@
#include<bits/stdc++.h>
using namespace std;
int main() {
int tot = (50*49) / 2, cnt = (7*6) / 2;
cout << tot - cnt << endl;
return 0;
}
/*
总共有50人参与会议如果按照常规情况即每个人与除了自己以外的所有人各握手一次
握手总数tot=49+48+...+1=(50*49)/2
现在有7个人没有互相握手这7这个人本来应该握手的次数 cnt=6+5+...+1=(7*6)/2
实际握手次数=总的握手次数tot-7人的情况cnt (tot - cnt)
*/

17
15lanqiao/test1-2.cpp Normal file
View File

@@ -0,0 +1,17 @@
#include<bits/stdc++.h>
using namespace std;
int main() {
int ans = 0;
for(int i = 1; i <=50; i++){
for(int j = i + 1; j<=50; j++){
if(!( (i >= 1 && i<= 7) && (j >= 1 && j <= 7) )) ans++;
// i和j不能同时满足都在[1, 7]范围内
}
}
cout << ans << endl;
return 0;
}
/* 枚举法
*/

19
15lanqiao/test2.cpp Normal file
View File

@@ -0,0 +1,19 @@
#include<bits/stdc++.h>
using namespace std;
int main() {
int x = 343720, y = 233333, dx = 15, dy = 17;
int p = y*dx, q = x*dy;
int g = gcd(p, q);
p /= g, q /= g;
int t = 2*p*x / dx;
double ans = t * (sqrt(15*15 + 17*17));
printf("%.2lf\n", ans);
return 0;
}
/*
思路:针对前进的方向进行分解分为x方向的运动和y方向的运动假设x方向走了p个来回y反向走了q个来回
经过了时间t小球第一次回到原点则t*dx=2px,t*dy=2qy结合1式/2式得p/q=y/x*dx/dy
利用gcd对分式p,q进行约分进而得到约分后的p,q
则时间 t = 2px/dx, 总路程 = t * sqrt(15*15 + 17*17)
*/

30
15lanqiao/test3.cpp Normal file
View File

@@ -0,0 +1,30 @@
#include<bits/stdc++.h>
using namespace std;
bool check(int n){
int b = 1; //数位
while(n){
if(b % 2){ // 奇数位
if(!(n % 10 % 2)) return false;
}else{ // 偶数位
if(n % 10 % 2) return false;
}
n /= 10;
b++;
}
return true;
}
int main() {
int n; cin >>n;
int ans = 0;
for(int i = 1; i <= n; i++){
if(i % 10 % 2 == 0) continue;
if(check(i)) ans++;
}
cout << ans << endl;
return 0;
}
/*
一个整数如果按从低位到高位的顺序,奇数位为奇数,偶数位为偶数,则该数为好数
24 -> 7, 2024 -> 150
*/

34
15lanqiao/test4.cpp Normal file
View File

@@ -0,0 +1,34 @@
#include<bits/stdc++.h>
using namespace std;
const int N = 2e3 + 10;
string s; int a[N];
int main() {
int n; cin >> n >> s;
reverse(s.begin(), s.end());
int pos = s.find("."); // 小数点位置
s.erase(pos, 1); // 删除小数点, 方便后续计算
int len = s.size();
for(int i = 0; i < len; i++) a[i+1] = s[i] - '0';
// a[1] = 4a[2] = 1a[3] = 3
//高精度*低精度模版
for(int i = 1; i<=n; i++){
// 顺序扫描每一位, 均乘2
for(int j = 1; j <= len; j++) a[j] *= 2;
for(int j = 1; j <= len; j++){
if(a[j] > 10){
a[j+1]++;
a[j] %= 10;
if(j == len) len++;
}
}
}
// a[1] = 6a[2] = 5a[3] = 2a[4] = 1 a[pos] = 5
if(a[pos] >= 5) a[pos+1]++;
for(int i = len; i >= pos + 1; i--) cout << a[i];
cout << endl;
return 0;
}
/*
2 3.14
*/

45
15lanqiao/test5.cpp Normal file
View File

@@ -0,0 +1,45 @@
#include<bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
int a[N];
vector<int> fac[N], s[N];
int main() {
for(int i = 1; i <= 1e5; i++){
for(int j = i; j <= 1e5; j += i){
fac[j].push_back(i);
}
}
int n; 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++){
for(int j = 0; j < fac[a[i]].size(); j++){
s[fac[a[i]][j]].push_back(a[i]);
}
}
for(int i = 1e5; i>=0; i--){
if(s[i].size() >= 3){
cout << s[i][0] << " " << s[i][1] << " " << s[i][2] << endl;
break;
}
}
return 0;
}
/* test samples
5
1 2 3 4 9
*/
/*
排序后的宝石为 [1, 2, 3, 4, 9]。
因数数组 fac 和宝石数组 s 的关系如下:
s[1] 包含所有宝石 [1, 2, 3, 4, 9]。
s[2] 包含 [2, 4]。
s[3] 包含 [3, 9]。
s[4] 包含 [4]。
s[9] 包含 [9]。
最终s[1] 中有5个宝石满足条件。
输出前三个宝石1 2 3。
*/

57
15lanqiao/test6.cpp Normal file
View File

@@ -0,0 +1,57 @@
#include<bits/stdc++.h>
using namespace std;
const int N = 20;
int a[N][N]; // 迷宫数组
int vis[N][N]; // 标记数组
int n, k;
// 方向数组 0 1 2 3 4 5 6 7
int dx[] = {-1, -1, 0, 1, 1, 1, 0, -1};
int dy[] = { 0, 1, 1, 1, 0, -1, -1, -1};
string res; // 结果字符串
void dfs(int x, int y, int pre, string s, int dep){
if(x == n && y == n && dep == n*n){
if(res.empty()) res = s; // 字典需最小的
return;
}
for(int i = 0; i < 8; i++){
int bx = x + dx[i], by = y + dy[i];
if(bx < 1 || bx > n || by < 1 || by > n) continue;
if(vis[bx][by]) continue;
// 防止交叉搜索
if(i == 1 && vis[x-1][y] && vis[x][y+1]) continue;
else if(i == 3 && vis[x+1][y] && vis[x][y+1]) continue;
else if(i == 5 && vis[x+1][y] && vis[x][y-1]) continue;
else if(i == 7 && vis[x-1][y] && vis[x][y-1]) continue;
// 保证路径数值为0 1 ... k-1
if((a[bx][by] < k && a[bx][by] == pre + 1) || (pre + 1 == k && a[bx][by] == 0)){
vis[bx][by] = 1;
dfs(bx, by, a[bx][by], s + to_string(i), dep + 1);
// 最优性剪枝
if(!res.empty()) return;
vis[bx][by] = 0; //回溯
}
}
}
int main() {
cin >> n >> k;
for(int i = 1; i <= n; i++){
for(int j = 1; j <= n; j++){
cin >> a[i][j];
}
}
string emp;
vis[1][1] = 1;
dfs(1, 1, 0, emp, 1);
if(res.empty()) cout << -1 << endl;
else cout << res << endl;
return 0;
}
/* test samples
3 3
0 2 0
1 1 1
2 0 2
*/

26
15lanqiao/test7.cpp Normal file
View File

@@ -0,0 +1,26 @@
#include<bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
int main(){
priority_queue<int> pq; // 大根堆
int n, p ,q; cin >> n >> p >> q;
for(int i = 1; i <= n; i++){
int x; cin >> x;
pq.push(x);
}
while(p || q){
int cur = pq.top(); // 最大值
pq.pop();
if(p){ cur = sqrt(cur); p--; }
else if(q) { cur = cur / 2; q--; }
pq.push(cur);
}
int s = 0; // 总的消耗
while(!pq.empty()) { s += pq.top(); pq.pop(); }
cout << s << endl;
return 0;
}
/* test samples
4 1 1
4 5 6 49
*/

44
15lanqiao/test8.cpp Normal file
View File

@@ -0,0 +1,44 @@
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N = 1e3+10;
int a[N], s[N]; // s为前缀和数组
multiset<int> ms;
signed main(){
int n; cin>>n;
for(int i = 1; i<=n; i++){
cin >> a[i];
s[i] = s[i-1] + a[i];
}
// 使用set数组去维护所有的区间和
for(int i = 1; i<=n; i++){
for(int j = 1; j<=n; j++){
ms.insert(s[j] - s[i-1]);
}
}
int ans = LLONG_MAX;
// 时间复杂度为 O(n^log2n)
for(int i = 1; i<=n; i++){
for(int j = 1; j<i; j++){
// 枚举以i结尾的区间
int sum = s[i] - s[j-1];
// 找到该区间和sum相似的区间和s1 and s2
auto it = ms.lower_bound(sum);
if(it!=ms.end()){ ans = min(ans, abs(*it - sum)); } // 找到了
if(it!=ms.begin()){
it--;
ans = min(ans, abs(*it - sum));
}
}
// 删除以i开头且以j结尾的区间防止后续查询区间的时候出现区间重叠/交叉重复问题
for(int j = i; j<=n; j++) ms.erase(ms.find(s[j] - s[i-1]));
}
cout << ans << endl;
return 0;
}
/* test samples
5
10 9 8 12 14
*/

9
test100.cpp Normal file
View File

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

134
test99-1.cpp Normal file
View File

@@ -0,0 +1,134 @@
#include<bits/stdc++.h>
using namespace std;
typedef struct LNode{
int data;
struct LNode *next;
}LNode, *LinkList;
bool InitList(LinkList &L){
L = (LNode *)malloc(sizeof(LNode)); // 带头节点
if(L == NULL) return false;
L->next = NULL;
return true;
}
void InputList(LinkList &L){
int x; cin>>x;
LinkList tail = L;
while(x != -99999){ // 输入 -99999 结束输入
LNode *newNode = (LNode*)malloc(sizeof(LNode));
newNode->data = x; newNode->next = NULL;
tail->next = newNode;
tail = newNode;
cin >> x;
}
}
void PrintList(LinkList &L){
LinkList p = L->next;
while(p != NULL){
cout << p->data << ",\n"[p->next == NULL];
p = p->next;
}
}
LinkList p1, p2, p3, tmp;
void MergeList1(LinkList &L1, LinkList &L2, LinkList &L3){
// 升序合并并去重
p1 = L1->next; p2 = L2->next;
L3 = p3 = L1;
while(p1 && p2){
if(p1->data < p2->data){
p3->next = p1; p3 = p1; p1 = p1->next;
}else if(p2->data < p1->data){
p3->next = p2; p3 = p2; p2 = p2->next;
}else{
p3->next = p1; p3 = p1; p1 = p1->next;
tmp = p2->next; delete p2; p2 = tmp;
}
}
p3->next = p1?p1:p2;
delete L2;
}
void MergeList2(LinkList &L1, LinkList &L2, LinkList &L3){
// 降序合并
p1 = L1->next; p2 = L2->next;
L3 = L1;
L3->next = NULL;
while(p1 || p2){
if(!p1) { tmp = p2; p2 = p2->next; }
else if(!p2) { tmp = p1; p1 = p1->next; }
else if(p1->data <= p2->data) { tmp = p1; p1 = p1->next; }
else { tmp = p2; p2 = p2->next; }
tmp->next = L3->next; L3->next = tmp;
}
delete L2;
}
void MixList(LinkList &L1, LinkList &L2, LinkList &L3){
// 两链表相交
p1 = L1->next; p2 = L2->next;
L3 = p3 = L1;
while(p1 && p2){
if(p1->data == p2->data){
p3->next = p1; p3 = p1; p1 = p1->next;
tmp = p2; p2 = p2->next; delete tmp;
}else if(p1->data < p2->data){
tmp = p1; p1 = p1->next; delete tmp;
}else{
tmp = p2; p2 = p2->next; delete tmp;
}
}
while(p1) { tmp = p1; p1 = p1->next; delete tmp; }
while(p2) { tmp = p2; p2 = p2->next; delete tmp; }
p3->next = NULL;
delete L2;
}
void DiffList(LinkList &L1, LinkList &L2, int &n){
// 两链表相差
p1 = L1->next; p2 = L2->next;
p3 = L1;
while(p1 && p2){
if(p1->data < p2->data){
p3 = p1; p1 = p1->next; n++;
}else if(p1->data > p2->data){
p2 = p2->next;
}else{
p3->next = p1->next;
tmp = p1; p1 = p1->next; delete tmp;
}
}
}
int main(){
LinkList L1, L2, L3; InitList(L1); InitList(L2); InitList(L3);
InputList(L1); InputList(L2); // PrintList(L1); PrintList(L2); // 初始化并输入输出两个链表
MergeList1(L1, L2, L3);
cout << "两链表升序合并并去重: "; PrintList(L3);
LinkList L4, L5, L6; InitList(L4); InitList(L5); InitList(L6);
InputList(L4); InputList(L5); // PrintList(L4); PrintList(L5); // 初始化并输入输出两个链表
MergeList2(L4, L5, L6);
cout << "两链表降序合并: "; PrintList(L6);
LinkList L7, L8, L9; InitList(L7); InitList(L8); InitList(L9);
InputList(L7); InputList(L8);
MixList(L7, L8, L9);
cout << "两链表相交: "; PrintList(L9);
LinkList L10, L11; InitList(L10); InitList(L11);
InputList(L10); InputList(L11); int n = 0;
DiffList(L10, L11, n);
cout << "两链表相差: "; PrintList(L10);
cout << "元素个数位: " << n << endl;
return 0;
}
/* test samples
1 3 5 7 9 13 15 -99999
2 4 6 8 10 11 13 14 15 -99999
*/

44
test99-2.cpp Normal file
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;
}

34
test99-3-1.cpp Normal file
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;
}

31
test99-3-2.cpp Normal file
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;
}

44
test99-3-3.cpp Normal file
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
*/

BIN
test99-3-4 Executable file

Binary file not shown.

33
test99-3-4.cpp Normal file
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
*/

BIN
test99-3-5 Executable file

Binary file not shown.

59
test99-3-5.cpp Normal file
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
.......
.##....
.##....
....##.
..####.
...###.
.......
*/

BIN
test99-4-1 Executable file

Binary file not shown.

44
test99-4-1-1.cpp Normal file
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
*/

45
test99-4-1-2.cpp Normal file
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
*/

BIN
test99-4-2 Executable file

Binary file not shown.

37
test99-4-2.cpp Normal file
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
*/

BIN
test99-4-3 Executable file

Binary file not shown.

39
test99-4-3.cpp Normal file
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
*/

BIN
test99-4-4 Executable file

Binary file not shown.

23
test99-4-4.cpp Normal file
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
*/

BIN
test99-4-5 Executable file

Binary file not shown.

66
test99-4-5.cpp Normal file
View File

@@ -0,0 +1,66 @@
#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
*/