This commit is contained in:
xianya 2025-03-17 00:29:43 +08:00
commit e576079c77
60 changed files with 3579 additions and 0 deletions

14
函数/素数定理.cpp Normal file
View File

@ -0,0 +1,14 @@
#include<cstdio>
#include<cmath>
memset(vis,0,sizeof(vis));
int get_prime_nums(int n){ //返回1 ~ 10^n素数个数的位数
double m=double(n-log10(n)-log10(log(10)));
return int(m)+1;
}
int main()
{
int n=12;
printf("%d",get_prime_nums(n));
}

View File

@ -0,0 +1 @@
{}

View File

@ -0,0 +1 @@
{}

View File

@ -0,0 +1,30 @@
{
"file-explorer": true,
"global-search": true,
"switcher": true,
"graph": true,
"backlink": true,
"canvas": true,
"outgoing-link": true,
"tag-pane": true,
"properties": false,
"page-preview": true,
"daily-notes": true,
"templates": true,
"note-composer": true,
"command-palette": true,
"slash-command": false,
"editor-status": true,
"bookmarks": true,
"markdown-importer": false,
"zk-prefixer": false,
"random-note": false,
"outline": true,
"word-count": true,
"slides": false,
"audio-recorder": false,
"workspaces": false,
"file-recovery": true,
"publish": false,
"sync": false
}

View File

@ -0,0 +1,20 @@
[
"file-explorer",
"global-search",
"switcher",
"graph",
"backlink",
"canvas",
"outgoing-link",
"tag-pane",
"page-preview",
"daily-notes",
"templates",
"note-composer",
"command-palette",
"editor-status",
"bookmarks",
"outline",
"word-count",
"file-recovery"
]

View File

@ -0,0 +1,202 @@
{
"main": {
"id": "b2b1866767927298",
"type": "split",
"children": [
{
"id": "f97597d49015723c",
"type": "tabs",
"children": [
{
"id": "527833ad58ba964c",
"type": "leaf",
"state": {
"type": "localgraph",
"state": {
"file": "数据结构.md",
"options": {
"collapse-filter": true,
"search": "",
"localJumps": 1,
"localBacklinks": true,
"localForelinks": true,
"localInterlinks": false,
"showTags": false,
"showAttachments": false,
"hideUnresolved": false,
"collapse-color-groups": true,
"colorGroups": [],
"collapse-display": true,
"showArrow": false,
"textFadeMultiplier": 0,
"nodeSizeMultiplier": 1,
"lineSizeMultiplier": 1,
"collapse-forces": true,
"centerStrength": 0.518713248970312,
"repelStrength": 10,
"linkStrength": 1,
"linkDistance": 250,
"scale": 1,
"close": true
}
}
}
},
{
"id": "3cc2e1952d04ba97",
"type": "leaf",
"state": {
"type": "markdown",
"state": {
"file": "数据结构.md",
"mode": "source",
"source": false
}
}
},
{
"id": "2b5effac673ea351",
"type": "leaf",
"state": {
"type": "markdown",
"state": {
"file": "数据结构.md",
"mode": "source",
"source": false
}
}
}
],
"currentTab": 2
}
],
"direction": "vertical"
},
"left": {
"id": "85889f78499fa715",
"type": "split",
"children": [
{
"id": "b147bc2e405e1f8a",
"type": "tabs",
"children": [
{
"id": "9f3cc4aacaf44a5a",
"type": "leaf",
"state": {
"type": "file-explorer",
"state": {
"sortOrder": "alphabetical"
}
}
},
{
"id": "0bf6afcfac58a7a2",
"type": "leaf",
"state": {
"type": "search",
"state": {
"query": "",
"matchingCase": false,
"explainSearch": false,
"collapseAll": false,
"extraContext": false,
"sortOrder": "alphabetical"
}
}
},
{
"id": "bdf493e1e11e49c2",
"type": "leaf",
"state": {
"type": "bookmarks",
"state": {}
}
}
]
}
],
"direction": "horizontal",
"width": 300
},
"right": {
"id": "da0e691d2228a719",
"type": "split",
"children": [
{
"id": "5bc1abf2c732887b",
"type": "tabs",
"children": [
{
"id": "2f622afb7172d75e",
"type": "leaf",
"state": {
"type": "backlink",
"state": {
"file": "数据结构.md",
"collapseAll": false,
"extraContext": false,
"sortOrder": "alphabetical",
"showSearch": false,
"searchQuery": "",
"backlinkCollapsed": false,
"unlinkedCollapsed": true
}
}
},
{
"id": "c71ab00dda97abc3",
"type": "leaf",
"state": {
"type": "outgoing-link",
"state": {
"file": "数据结构.md",
"linksCollapsed": false,
"unlinkedCollapsed": true
}
}
},
{
"id": "3e51961fafcf24d5",
"type": "leaf",
"state": {
"type": "tag",
"state": {
"sortOrder": "frequency",
"useHierarchy": true
}
}
},
{
"id": "ca2df154a024555c",
"type": "leaf",
"state": {
"type": "outline",
"state": {
"file": "数据结构.md"
}
}
}
]
}
],
"direction": "horizontal",
"width": 300
},
"left-ribbon": {
"hiddenItems": {
"switcher:打开快速切换": false,
"graph:查看关系图谱": false,
"canvas:新建白板": false,
"daily-notes:打开/创建今天的日记": false,
"templates:插入模板": false,
"command-palette:打开命令面板": false
}
},
"active": "9f3cc4aacaf44a5a",
"lastOpenFiles": [
"图/tips.md",
"数据结构.md",
"新建文件夹"
]
}

View File

@ -0,0 +1,119 @@
#include<bits/stdc++.h>
using namespace std;
void solve() {
int64_t n, m;
cin >> n >> m;
vector<int64_t> a(n + 1);
for (int64_t i = 1; i <= n; i++)cin >> a[i];
vector<int64_t> tree(4 * n);
vector<int64_t> lazy(4 * n);
auto lz = [&](int64_t p, int64_t s, int64_t t, int64_t x) {
// cerr << "Lazy " << p << " " << s << " " << t << " " << x << "\n";
tree[p] += (t - s + 1) * x;
lazy[p] += x;
};
auto build = [&](auto &&self, int64_t s, int64_t t, int64_t p) {
if (s == t) {
tree[p] = a[s];
return;
}
int64_t m = (s+t)/2;
self(self, s, m, p * 2);
self(self, m + 1, t, p * 2 + 1);
tree[p] = tree[p * 2] + tree[p * 2 + 1];
};
build(build, 1, n, 1);
auto query = [&](auto &&self, int64_t l, int64_t r, int64_t s, int64_t t, int64_t p) -> int64_t {
// cerr << "query in " << l << " " << r << " " << s << " " << t << "\n";
int64_t sum = 0;
if (l <= s && t <= r) return tree[p];
int64_t m = (s+t)/2;
if (lazy[p]) {
// cerr << "Lazy p " << " " << s << " " << t << " " << p << " " << lazy[p] << "\n";
lz(p * 2, s, m, lazy[p]);
lz(p * 2 + 1, m + 1, t, lazy[p]);
lazy[p] = 0;
}
if (l <= m) {
sum += self(self, l, r, s, m, p * 2);
}
if (r >= m + 1) {
sum += self(self, l, r, m + 1, t, p * 2 + 1);
}
return sum;
};
auto modify = [&](auto &&self, int64_t l, int64_t r, int64_t s, int64_t t, int64_t p, int64_t x) -> void {
// cerr << "Modify in " << l << " " << r << " " << s << " " << t << " " << p << " " << x << "\n";
if (l <= s && t <= r) {
lz(p, s, t, x);
return;
}
int64_t m = s + ((t - s) >> 1);
if (lazy[p] && s != t) {
// cerr << "Lazy p " << " " << s << " " << t << " " << p << " " << lazy[p] << "\n";
lz(p * 2, s, m, lazy[p]);
lz(p * 2 + 1, m + 1, t, lazy[p]);
lazy[p] = 0;
}
if (l <= m) {
self(self, l, r, s, m, p * 2, x);
}
if (r >= m + 1) {
self(self, l, r, m + 1, t, p * 2 + 1, x);
}
tree[p]=tree[p*2]+tree[p*2+1];
};
for (int64_t i = 1; i <= m; i++) {
int64_t x;
cin >> x;
if (x == 1) {
int64_t l, r, x;
cin >> l >> r >> x;
modify(modify, l, r, 1, n, 1, x);
} else {
int64_t l, r;
cin >> l >> r;
cout << query(query, l, r, 1, n, 1) << "\n";
}
}
}
int fact(int x);
int fact(int x)
{
if(x==0)return 1;
return fact(x-1)*x;
}
int main() {
int64_t t;
// cin>>t;
t = 1;
while (t--) {
solve();
}
// struct F
// {
// int operator()(int x)const{
// if(x==0)return 1;
// return (*this)(x-1)*x;
// }
// };
// F f;
// auto f=[](auto &&self,int x){
// if(x==0)return 1;
// return self(self,x-1)*x;
// };
auto f=[](auto &&self,int x){
if(x==0)return 1;
return self(self,x-1)*x;
};
// f=[](int x){return x+1;};
f(f,1);
}

View File

@ -0,0 +1,39 @@
#include<bits/stdc++.h>
using namespace std;
int n;
int lowbit(int x) {
return x & -x;
}
int getsum(int x,vector<int>&c) { // a[1]..a[x]的和
int ans = 0;
while (x > 0) {
ans = ans + c[x];
x = x - lowbit(x);
}
return ans;
}
void add(int x, int k,vector<int>&c) {
while (x <= n) { // 不能越界
c[x] = c[x] + k;
x = x + lowbit(x);
}
}
int main(){
int m;cin>>n>>m;
vector<int>a(n+1);
vector<int>c(4*n);
for(int i=1;i<=n;i++){
cin>>a[i];
add(i,a[i],c);
}
for(int i=1;i<=m;i++){
int type,x,y;
cin>>type>>x>>y;
if(type==1){
add(x,y,c);
a[x]+=y;
}
else cout<<getsum(y,c)-getsum(x-1,c)<<"\n";
}
return 0;
}

View File

@ -0,0 +1,52 @@
#include<bits/stdc++.h>
struct edge
{
int u,v;
int w;
};
const int N=100;
std::priority_queue<std::pair<int,int>> pq;
std::vector <edge> e;
std::vector <std::vector<int>> h;
std::vector<int>dis;
std::vector<int>vis;
void rebuild(int &n)
{
e.clear();
h.resize(n+1);
h.assign(n+1,std::vector<int>());
dis.resize(n+1);
dis.assign(n+1,FLT_MAX);
vis.resize(n+1);
vis.assign(n+1,0);
}
void add(int u,int v,int w)
{
e.push_back({u,v,w});
h[u].push_back(e.size()-1);
}
void dij(int s)
{
int x;
dis[s]=0;
pq.push({0,s});
while(pq.size())
{
auto p=pq.top();pq.pop();
x=p.second;
//cout<<"x="<<x<<endl;
if(vis[x])continue;
vis[x]=true;
for(auto k : h[x])
{
int v=e[k].v,u=e[k].u;
float w=e[k].w;
if(vis[v]) return;
if(dis[u]+w<dis[v])dis[v]=dis[u]+w;
pq.push({-dis[v],v});
//cout<<"pq push"<<dis[v]<<" & "<<v<<endl;
}
}
}
int main()
{}

View File

@ -0,0 +1,4 @@
于是否成对:
是否异或1互等
1 ^ 1=0
0 ^ 1=1

View File

@ -0,0 +1,38 @@
#include<iostream>
#include<vector>
using namespace std;
const int N=1e5;
struct edge
{
int u,v,w;
};
vector <edge> e;
vector <int> h[N];
void add(int u,int v,int w)
{
e.push_back({u,v,w});
h[u].push_back(e.size()-1);
}
void dfs(int u,int fa)
{
for(int i=0;i<=h[u].size()-1;i++)
{
int j=h[u][i];
int v=e[j].v,w=e[j].w;
if(v==fa)continue;
cout<<u<<","<<v<<","<<w<<"\n";
dfs(v,u);
}
}
int main()
{
int n,m,u,v,w;
cin>>n>>m;
for(int i=1;i<=m;i++)
{
cin>>u>>v>>w;
add(u,v,w);
add(v,u,w);
}
dfs(1,0);
}

View File

@ -0,0 +1 @@
{}

View File

@ -0,0 +1 @@
{}

View File

@ -0,0 +1,30 @@
{
"file-explorer": true,
"global-search": true,
"switcher": true,
"graph": true,
"backlink": true,
"canvas": true,
"outgoing-link": true,
"tag-pane": true,
"properties": false,
"page-preview": true,
"daily-notes": true,
"templates": true,
"note-composer": true,
"command-palette": true,
"slash-command": false,
"editor-status": true,
"bookmarks": true,
"markdown-importer": false,
"zk-prefixer": false,
"random-note": false,
"outline": true,
"word-count": true,
"slides": false,
"audio-recorder": false,
"workspaces": false,
"file-recovery": true,
"publish": false,
"sync": false
}

View File

@ -0,0 +1,20 @@
[
"file-explorer",
"global-search",
"switcher",
"graph",
"backlink",
"canvas",
"outgoing-link",
"tag-pane",
"page-preview",
"daily-notes",
"templates",
"note-composer",
"command-palette",
"editor-status",
"bookmarks",
"outline",
"word-count",
"file-recovery"
]

View File

@ -0,0 +1,143 @@
{
"main": {
"id": "024a9f08a4a36715",
"type": "split",
"children": [
{
"id": "7ef1e397ed33f651",
"type": "tabs",
"children": [
{
"id": "bf850a0d6b69d292",
"type": "leaf",
"state": {
"type": "empty",
"state": {}
}
}
]
}
],
"direction": "vertical"
},
"left": {
"id": "3a85fc26c5dfdecd",
"type": "split",
"children": [
{
"id": "54350b95f3ed40f8",
"type": "tabs",
"children": [
{
"id": "90a534f2c0bc7d84",
"type": "leaf",
"state": {
"type": "file-explorer",
"state": {
"sortOrder": "alphabetical"
}
}
},
{
"id": "0592d798b63a27fa",
"type": "leaf",
"state": {
"type": "search",
"state": {
"query": "",
"matchingCase": false,
"explainSearch": false,
"collapseAll": false,
"extraContext": false,
"sortOrder": "alphabetical"
}
}
},
{
"id": "8e7c074cdf9bf099",
"type": "leaf",
"state": {
"type": "bookmarks",
"state": {}
}
}
]
}
],
"direction": "horizontal",
"width": 300
},
"right": {
"id": "5d948eb3b8d7acf5",
"type": "split",
"children": [
{
"id": "e4e30f69dd4cbea4",
"type": "tabs",
"children": [
{
"id": "95432adab680d8f1",
"type": "leaf",
"state": {
"type": "backlink",
"state": {
"collapseAll": false,
"extraContext": false,
"sortOrder": "alphabetical",
"showSearch": false,
"searchQuery": "",
"backlinkCollapsed": false,
"unlinkedCollapsed": true
}
}
},
{
"id": "64e7a6d309a56f98",
"type": "leaf",
"state": {
"type": "outgoing-link",
"state": {
"linksCollapsed": false,
"unlinkedCollapsed": true
}
}
},
{
"id": "71039966636d2285",
"type": "leaf",
"state": {
"type": "tag",
"state": {
"sortOrder": "frequency",
"useHierarchy": true
}
}
},
{
"id": "46a0dffb0a71fb95",
"type": "leaf",
"state": {
"type": "outline",
"state": {}
}
}
]
}
],
"direction": "horizontal",
"width": 300,
"collapsed": true
},
"left-ribbon": {
"hiddenItems": {
"switcher:打开快速切换": false,
"graph:查看关系图谱": false,
"canvas:新建白板": false,
"daily-notes:打开/创建今天的日记": false,
"templates:插入模板": false,
"command-palette:打开命令面板": false
}
},
"active": "bf850a0d6b69d292",
"lastOpenFiles": []
}

View File

@ -0,0 +1,23 @@
int kmp(string a1,string a2)
{
int n=a1.size(),m=a2.size();
//cout<<"n="<<n<<" m="<<m<<endl;
string s=a1 + '#' + a2;
//cout<<s<<endl;
vector<int> pi(s.size());
for (int i=n;i<s.size();i++)
{
int len=pi[i-1];
while(len != 0 && s[i] != s[len]){
len=pi[len-1];
}
//cout<<"len="<<len<<endl;
//cout<<"pi["<<i<<"]="<<pi[i]<<endl;
if(s[i] == s[len]){
pi[i] = ++len;
//cout<<"pi["<<i<<"]="<<pi[i]<<endl;
if(pi[i] == n) return i - n * 2;
}
}
return -1;
}

View File

@ -0,0 +1,46 @@
#include<bits/stdc++.h>
using namespace std;
int main() {
int n;
ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
cin>>n;
long long ans=0; //维护逆序对
vector<int>a(n);
for(int i=0;i<n;i++)
cin>>a[i];
auto merge=[&](vector<int>&a,vector<int>&b)->vector<int>
{
vector<int>res;
size_t ai=0,bi=0;
while(ai<a.size()&&bi<b.size()){
if(b[bi]<a[ai])
{
res.push_back(b[bi]);
bi++;
ans+=a.size()-ai; //更新逆序对
}
else {
res.push_back(a[ai]);
ai++;
}
}
if(ai==a.size())
res.insert(res.end(),b.begin()+bi,b.end());
else if(bi==b.size())
res.insert(res.end(),a.begin()+ai,a.end());
return res;
};
auto merge_sort=[&](auto &&self,vector<int>&arr)->vector<int>{
if(arr.size()<=1)return arr;
const size_t mid=arr.size()>>1;
vector<int> left(arr.begin(),arr.begin()+mid);
vector<int> right(arr.begin()+mid,arr.end());
left=self(self,left);
right=self(self,right);
return merge(left,right);
};
a=merge_sort(merge_sort,a);
cout<<ans; //输出逆序对
}

View File

@ -0,0 +1,14 @@
#include<cstring>
#include<cstdio>
#include<cmath>
const int mod=1;
int ksm(int a, int x) {
int ans = 1;
while (x) {
if (x & 1)
ans = ans * a % mod;
a = (a%mod) * (a%mod) % mod;
x >>= 1;
}
return ans;
}

View File

@ -0,0 +1,63 @@
#include<iostream>
#include<vector>
#include<algorithm>
#include<cstring>
int ch[25*600005][2],idx;
using namespace std;
void insert(int &x)
{
int p=0;
for(int i=8;i>=0;i--)
{
int j=x >> i & 1;
if(!ch[p][j])ch[p][j]=++idx;
p=ch[p][j];
}
}
int query(int x)
{
int p=0,res=0;
for(int i=8;i>=0;i--)
{
int j=x >> i & 1;
if(ch[p][!j]){
res+= 1 << i;
p=ch[p][!j];
}
else p=ch[p][j];
}
return res;
}
void solve(){
int n,res1;
cin>>n;
vector <int> preres(n,0);
for(int i=0;i<=n-1;i++)
{
if(i==0)cin>>res1;
else {
int x;
cin>>x;
res1 ^= x;
}
preres[i]=res1;
}
for_each(preres.begin(),preres.end(),insert);
int ans=0;
for(int i=0;i<=n-1;i++)
{
ans=max(ans,query(preres[i]));
ans=max(ans,preres[i]);
}
cout<<ans<<"\n";
}
int main()
{
int t;
cin>>t;
for(int i=1;i<=t;i++)
{
memset(ch,0,sizeof ch);
solve();
}
}

Binary file not shown.

View File

@ -0,0 +1,24 @@
#include<bits/stdc++.h>
using namespace std;
vector<int>prime(1000000);
vector<int>vis(10000001);
int k1=0;
int main() {
vector<int>prime();
vector<int>vis();
int k1=0;
auto primeadd = [&](int small, int big) {
int i, j;
if (small == 1)small = 2;
for (i = small; i <= big; i++) {
if (vis[i] == 0)prime[k1++] = i;
for (j = 0; j < k1; j++) {
if (i * prime[j] > big)break;
vis[i * prime[j]] = 1;
if (i % prime[j] == 0)break;
}
}
};
primeadd(1,2);
}

View File

@ -0,0 +1,32 @@
#include<cstdio>
#include<cstring>
int main(){
char a1[2005],b1[2005];
int a[2005],b[2005],c[10000],lena,lenb,lenc,i,j,x;
memset(a,0,sizeof a);
memset(b,0,sizeof b);
memset(c,0,sizeof c);
//scanf("%[^\n]%*c%[^\n]%*c",&a1,&b1);
scanf("%s%s",&a1,&b1);
lena=strlen(a1);
lenb=strlen(b1);
for(i=0;i<=lena-1;i++)a[lena-i]=a1[i]-48;//倒序写入a
for(i=0;i<=lenb-1;i++)b[lenb-i]=b1[i]-48;//倒序写入b
for(i=1;i<=lena;i++)
{
x=0; //用于存放进位
for(j=1;j<=lenb;j++) //对乘数的每一位进行处理
{
c[i+j-1]=c[i+j-1]+a[i]*b[j]+x; //原数加上当前乘积加上上次乘积进位
x=c[i+j-1]/10;
c[i+j-1]%=10;
}
c[i+lenb]=x; //进位
}
lenc=lena+lenb;
while(c[lenc]==0&&lenc>1) //删除前导0
lenc--;
for(i=lenc;i>=1;i--)
printf("%d",c[i]);
printf("\n");
}

View File

@ -0,0 +1,36 @@
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
int a1[500],b1[500],c[500];
int main()
{
memset(a1,0,sizeof a1);
memset(b1,0,sizeof b1);
memset(c,0,sizeof c);
int lena,lenb,lenc,i;
char a[500],b[500];
cin>>a>>b; //以字符串输入ab不用输入可以直接用int
lena=strlen(a);
lenb=strlen(b);
for(i=0;i<=lena-1;i++)a1[lena-i]=a[i]-48; //倒序写入a方便后面运算;
for(i=0;i<=lena-1;i++)b1[lenb-i]=b[i]-48; //倒序写入b;
//竖式加法
int x=0; //处理进位;
lenc=lena>lenb? lena:lenb; //用两个中最长的数决定运算次数;
for(i=1;i<=lenc;i++)
{
c[i]=a1[i]+b1[i]; //竖式加法;
x=c[i]/10;
c[i]%=10; //处理进位;
}
c[i+1]=x; //补上最高位;
lenc=lenc+1;
while(c[lenc]==0&&lenc>1)lenc--; //删除前导0;
for(i=lenc;i>=1;i--)
cout<<c[i];
}

10
思路/思路/.obsidian/app.json vendored Normal file
View File

@ -0,0 +1,10 @@
{
"promptDelete": false,
"pdfExportSettings": {
"includeName": false,
"pageSize": "A4",
"landscape": false,
"margin": "0",
"downscalePercent": 100
}
}

View File

@ -0,0 +1 @@
{}

View File

@ -0,0 +1,30 @@
{
"file-explorer": true,
"global-search": true,
"switcher": true,
"graph": true,
"backlink": true,
"canvas": true,
"outgoing-link": true,
"tag-pane": true,
"properties": false,
"page-preview": true,
"daily-notes": true,
"templates": true,
"note-composer": true,
"command-palette": true,
"slash-command": false,
"editor-status": true,
"bookmarks": true,
"markdown-importer": false,
"zk-prefixer": false,
"random-note": false,
"outline": true,
"word-count": true,
"slides": false,
"audio-recorder": false,
"workspaces": false,
"file-recovery": true,
"publish": false,
"sync": false
}

View File

@ -0,0 +1,30 @@
{
"file-explorer": true,
"global-search": true,
"switcher": true,
"graph": true,
"backlink": true,
"canvas": true,
"outgoing-link": true,
"tag-pane": true,
"properties": false,
"page-preview": true,
"daily-notes": true,
"templates": true,
"note-composer": true,
"command-palette": true,
"slash-command": false,
"editor-status": true,
"bookmarks": true,
"markdown-importer": false,
"zk-prefixer": false,
"random-note": false,
"outline": true,
"word-count": true,
"slides": false,
"audio-recorder": false,
"workspaces": false,
"file-recovery": true,
"publish": false,
"sync": false
}

22
思路/思路/.obsidian/graph.json vendored Normal file
View File

@ -0,0 +1,22 @@
{
"collapse-filter": true,
"search": "",
"showTags": false,
"showAttachments": false,
"hideUnresolved": false,
"showOrphans": true,
"collapse-color-groups": true,
"colorGroups": [],
"collapse-display": true,
"showArrow": false,
"textFadeMultiplier": 0,
"nodeSizeMultiplier": 1,
"lineSizeMultiplier": 1,
"collapse-forces": true,
"centerStrength": 0.518713248970312,
"repelStrength": 10,
"linkStrength": 1,
"linkDistance": 250,
"scale": 1,
"close": true
}

201
思路/思路/.obsidian/workspace.json vendored Normal file
View File

@ -0,0 +1,201 @@
{
"main": {
"id": "387a6da929beafab",
"type": "split",
"children": [
{
"id": "d1e2f5d3f7ec73c1",
"type": "tabs",
"children": [
{
"id": "d3ad06792a09c32c",
"type": "leaf",
"state": {
"type": "markdown",
"state": {
"file": "控制输出.md",
"mode": "source",
"source": false
},
"icon": "lucide-file",
"title": "控制输出"
}
}
]
}
],
"direction": "vertical"
},
"left": {
"id": "4d08106803562a63",
"type": "split",
"children": [
{
"id": "1134c9794d8fbb0b",
"type": "tabs",
"children": [
{
"id": "f7e361bda18778bd",
"type": "leaf",
"state": {
"type": "file-explorer",
"state": {
"sortOrder": "alphabetical"
},
"icon": "lucide-folder-closed",
"title": "文件列表"
}
},
{
"id": "a3f0906a120b02e0",
"type": "leaf",
"state": {
"type": "search",
"state": {
"query": "file:tips",
"matchingCase": false,
"explainSearch": false,
"collapseAll": false,
"extraContext": false,
"sortOrder": "alphabetical"
},
"icon": "lucide-search",
"title": "搜索"
}
},
{
"id": "73fbd18354c73029",
"type": "leaf",
"state": {
"type": "bookmarks",
"state": {},
"icon": "lucide-bookmark",
"title": "书签"
}
}
]
}
],
"direction": "horizontal",
"width": 300
},
"right": {
"id": "af024c54fa04716e",
"type": "split",
"children": [
{
"id": "40ac25103cd622a7",
"type": "tabs",
"children": [
{
"id": "4c3e3d1776217198",
"type": "leaf",
"state": {
"type": "backlink",
"state": {
"file": "控制输出.md",
"collapseAll": false,
"extraContext": false,
"sortOrder": "alphabetical",
"showSearch": false,
"searchQuery": "",
"backlinkCollapsed": false,
"unlinkedCollapsed": true
},
"icon": "links-coming-in",
"title": "控制输出 的反向链接列表"
}
},
{
"id": "c1c5bb4f2b06cb70",
"type": "leaf",
"state": {
"type": "outgoing-link",
"state": {
"file": "控制输出.md",
"linksCollapsed": false,
"unlinkedCollapsed": true
},
"icon": "links-going-out",
"title": "控制输出 的出链列表"
}
},
{
"id": "0d01c1382ce93bb9",
"type": "leaf",
"state": {
"type": "tag",
"state": {
"sortOrder": "frequency",
"useHierarchy": true
},
"icon": "lucide-tags",
"title": "标签"
}
},
{
"id": "d04db64e6816fd0b",
"type": "leaf",
"state": {
"type": "outline",
"state": {
"file": "控制输出.md"
},
"icon": "lucide-list",
"title": "控制输出 的大纲"
}
}
]
}
],
"direction": "horizontal",
"width": 300,
"collapsed": true
},
"left-ribbon": {
"hiddenItems": {
"switcher:打开快速切换": false,
"graph:查看关系图谱": false,
"canvas:新建白板": false,
"daily-notes:打开/创建今天的日记": false,
"templates:插入模板": false,
"command-palette:打开命令面板": false
}
},
"active": "d3ad06792a09c32c",
"lastOpenFiles": [
"algorithm/for_each.md",
"需要学习的东西.md",
"未命名/未命名.md",
"Pasted image 20240722135718.png",
"板子.md",
"未命名.md",
"未命名",
"控制输出.md",
"algorithm/next_permutation.md",
"下一个排列.md",
"algorithm/replace.md",
"algorithm/equal.md",
"algorithm/replace_if.md",
"algorithm/find.md",
"algorithm/find_if.md",
"tips.md",
"Pasted image 20240723133402.png",
"Pasted image 20240723133310.png",
"P1045.md",
"algorithm/sort.md",
"algorithm/swap.md",
"algorithm/count.md",
"algorithm/algorithm库.md",
"D1 B.md",
"algorithm/迭代器.md",
"algorithm",
"类扫雷算法(有边界的多维数组).md",
"Pasted image 20240723193025.png",
"Pasted image 20240723133440.png",
"Pasted image 20240723133551.png",
"Pasted image 20240723133840.png",
"Pasted image 20240722135849.png",
"欢迎.md"
]
}

71
思路/思路/D1 B.md Normal file
View File

@ -0,0 +1,71 @@
要求给定数列 $a_1, a_2, \ldots, a_n$ 的区间最大异或和问题我们可以使用一种称为“前缀异或和”与“字典树Trie树”结合的方法来解决。
### 步骤概述
1. **计算前缀异或和**:首先,我们计算数列 $a$ 的前缀异或和数组 $prefixi]$,其中 $prefix[i] = a_1 \oplus a_2 \oplus \cdots \oplus a_i$$\oplus$ 表示异或操作)。注意 $prefix[0]$ 可以定义为 $0$(方便计算)。
2. **构建字典树**:然后,我们使用 $prefix$ 数组中的值来构建一个字典树Trie树。字典树的每个节点代表一个二进制位从最高位到最低位。
3. **查询最大异或和**:对于 $prefix$ 数组中的每个值 $prefix[i]$$i$ 从 $1$ 到 $n$),我们在字典树中查询与 $prefix[i]$ 异或结果最大的值。这可以通过从 $prefix[i]$ 的最高位开始,在字典树中尽可能选择与 $prefix[i]$ 当前位不同的路径来实现。
4. **记录并更新最大异或和**:在查询过程中,我们记录并更新遇到的最大异或和。
### 详细算法
#### 1. 计算前缀异或和
```python
prefix = [0] * (n + 1)
for i in range(1, n + 1):
prefix[i] = prefix[i - 1] ^ a[i - 1]
```
#### 2. 构建字典树
```python
class TrieNode:
def __init__(self):
self.children = [None, None] # 0 for left, 1 for right
class Trie:
def __init__(self):
self.root = TrieNode()
def insert(self, num):
node = self.root
for i in range(31, -1, -1): # Assuming 32-bit integers
bit = (num >> i) & 1
if not node.children[bit]:
node.children[bit] = TrieNode()
node = node.children[bit]
def find_max_xor(self, num):
node = self.root
max_xor = 0
for i in range(31, -1, -1):
bit = (num >> i) & 1
opposite_bit = 1 - bit
if node.children[opposite_bit]:
max_xor |= (1 << i) # Set the bit in the result
node = node.children[opposite_bit]
else:
node = node.children[bit]
return max_xor
```
#### 3. 查询并更新最大异或和
```python
trie = Trie()
max_xor_sum = 0
for i in range(1, n + 1):
trie.insert(prefix[i - 1]) # Insert prefix[i-1] into the trie
max_xor_sum = max(max_xor_sum, trie.find_max_xor(prefix[i]))
print(max_xor_sum)
```
注意:上述代码中的 `find_max_xor` 函数实际上是在查找与 `prefix[i]` 之前的某个前缀异或和的最大异或值,而不是直接求区间 $[l, r]$ 的最大异或和。但是,由于我们使用了前缀异或和,所以可以通过 $prefix[r] \oplus prefix[l-1]$ 来计算区间 $[l, r]$ 的异或和。因此,上述算法实际上是在遍历所有可能的区间,并找到能产生最大异或和的区间。
另外,上述代码中的 `31` 是基于32位整数的假设。如果你的整数位数不同需要相应地调整这个值。
内容由AI大模型生成仅供参考

5
思路/思路/P1045.md Normal file
View File

@ -0,0 +1,5 @@
快速幂
2^P
P对2取模P&1
整除为0
下一个

Binary file not shown.

After

Width:  |  Height:  |  Size: 143 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 26 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 134 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 153 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 191 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 29 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 289 KiB

View File

@ -0,0 +1,101 @@
**<1>序列容器遍历操作**
常用参数对照:
frist : 起点,为一个[[迭代器]]
last : 终点,为一个[[迭代器]]
[[for_each]]
功能: 遍历序列容器将区间【fist,last之内每一个元素传入函数fn在函数fn内对元素进行操作
[[find]]
功能: 返回一个迭代器该迭代器返回等于val的范围内的第一个元素的迭代器。如果找不到这样的元素则函数返回last迭代器。
[[find_if]]
功能: 将区间【fist,last)之内每一个元素传入一元判断式当pred(elem) 返回true时返回第一个达成条件的元素迭代器
[[equal]]
功能: 比较两个序列的逐个元素是否相等;
[[count]]
功能: 返回序列中等于给定值元素的个数;
**<2> 序列修改操作**
[[swap]]
函数功能交换a和b
[[replace]]
功能: 把序列中等于old_value元素替换为 new_value
[[replace_if]]
功能: 将区间[fist,last)之内每一个元素传入一元判断式当pred(elem) 返回true时的元素替换成new_value
**序列排序**
[[sort]]
函数功能:
两个重载: 第一个对first到last 升序排序
第二个在第一个基础上传入一个传入一元判断式comp为a < b则升序否则降序
**排列函数**
[[next_permutation]]
函数功能:
求的是序列当前排列的下一个排列当当前序列不存在下一个排列时函数返回false否则返回true。
注意: next_permutation() 在使用前需要对欲排列数组按升序排序,否则只能找出该序列之后的全排列数。
代码:
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
int num[3]={1,2,3};
do
{
cout<<num[0]<<" "<<num[1]<<" "<<num[2]<<endl;
}while(next_permutation(num,num+3));
return 0;
}
输出:
1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1
需要强调的是next_permutation() 在使用前需要对欲排列数组按升序排序,否则只能找出该序列之后的全排列数。
下面的代码将展示如何使用next_permutation在接收一个数组p之后对数组p的内容进行全排列,其中n代表数组长度:
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int main()
{
int n,p[10];
scanf("%d",&n);
for(int i = 0;i < n;i ++)
{
scanf("%d",&p[i]);
}
sort(p,p+n); //事先排序以保证全排列
do
{
for(int i = 0;i < n;i ++)
{
printf("%d ",p[i]);//对于排好序的数组,直接输出即为第一个排列
}
printf("\n");
}while(next_permutation(p,p+n));
return 0;
}

View File

@ -0,0 +1,32 @@
函数原型:
```
template <class InputIterator, class T>
typename iterator_traits<InputIterator>::difference_type
count (InputIterator first, InputIterator last, const T& val)
{
typename iterator_traits<InputIterator>::difference_type ret = 0;
while (first!=last) {
if (*first == val) ++ret;
++first;
}
return ret;
}
```
功能: 返回序列中等于给定值元素的个数;
示例:
```
#include <iostream>
#include <vector>
#include <algorithm>
int main() {
std::vector<int> numbers = {1, 2, 3, 2, 4, 2, 5};
int countOfTwos = std::count(numbers.begin(), numbers.end(), 2);
std::cout << "数字 2 出现的次数: " << countOfTwos << std::endl;
return 0;
}
```

View File

@ -0,0 +1,29 @@
函数原型:
```
template <class InputIterator1, class InputIterator2>
bool equal ( InputIterator1 first1, InputIterator1 last1, InputIterator2 first2 )
{
while (first1!=last1) {
if (!(*first1 == *first2)) // or: if (!pred(*first1,*first2)), for version 2
return false;
++first1; ++first2;
}
return true;
}
```
功能: 比较两个序列的逐个元素是否相等。返回的是一个bool值
示例:
```
// 比较 vec1 和 vec2
if (std::equal(vec1.begin(), vec1.end(), vec2.begin()))
{
std::cout << "vec1 和 vec2 是相等的。" << std::endl;
}
else
{
std::cout << "vec1 和 vec2 是不相等的。" << std::endl;
}
```

View File

@ -0,0 +1,24 @@
函数原型:
```
template<class InputIterator, class T>
InputIterator find (InputIterator first, InputIterator last, const T& val)
{
while (first!=last) {
if (first*val) return first;*
*++first;*
*}*
*return last;*
}
```
功能: 返回一个迭代器该迭代器返回等于val的范围内的第一个元素的迭代器。如果找不到这样的元素则函数返回last迭代器。
示例:
```
int a[] = {1,2,3,4,5,6,7};
vector<int> v(a,a+7);
vector<int>::iterator it = find(v.begin(),v.end(),5);//*it = 5
it = find(v.begin(),v.end(),9);//*it = 0*
```

View File

@ -0,0 +1,25 @@
函数原型:
```
template<class InputIterator, class UnaryPredicate>
InputIterator find_if (InputIterator first, InputIterator last, UnaryPredicate pred)
{
while (first!=last) {
if (pred(*first)) return first;
++first;
}
return last;
}
```
功能: 将区间【fist,last)之内每一个元素传入一元判断式当pred(elem) 返回true时返回第一个达成条件的元素迭代器
示例:
```
std::vector<int> numbers = {1, 2, 3, 4, 5, 6};
// 查找第一个大于4的元素
auto it = std::find_if(numbers.begin(), numbers.end(), [](int n) {
return n > 4;
});
```

View File

@ -0,0 +1,27 @@
函数原型:
template<class InputIterator, class Function> Function for_each(InputIterator first, InputIterator last, Function fn)
功能: 遍历序列容器将区间【fist,last之内每一个元素传入函数fn在函数fn内对元素进行操作
示例:
```
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
//遍历数组vector修改vector里的所有元素值为5,输出元素
void fn(int &item)
{
item = 5;
cout << item << endl;
}
int main()
{
int a[] = {1,2,3,4,5,6,7};
vector<int> v(a,a+7);
for_each(v.begin(),v.end(),fn);
}
```

View File

@ -0,0 +1,8 @@
函数原型:
```
bool next_permutation(iterator start,iterator end)
```
函数功能:
求的是序列当前排列的[[下一个排列]]当当前序列不存在下一个排列时函数返回false否则返回true。
注意: next_permutation() 在使用前需要对欲排列数组按升序排序,否则只能找出该序列之后的全排列数。

View File

@ -0,0 +1,23 @@
函数原型:
```
template <class ForwardIterator, class T>
void replace (ForwardIterator first, ForwardIterator last,
const T& old_value, const T& new_value)
{
while (first!=last) {
if (*first == old_value) *first=new_value;
++first;
}
}
```
功能: 把序列中等于old_value元素替换为 new_value
示例:
```
int a[] = {1,4,6,5,5,3,7};
vector<int> v(a,a+7);
replace(v.begin(),v.end(),5,55);//把5替换成55
```

View File

@ -0,0 +1,35 @@
函数原型:
```
template < class ForwardIterator, class UnaryPredicate, class T >
void replace_if (ForwardIterator first, ForwardIterator last,
UnaryPredicate pred, const T& new_value)
{
while (first!=last) {
if (pred(*first)) *first=new_value;
++first;
}
}
```
功能: 将区间[fist,last)之内每一个元素传入一元判断式当pred(elem) 返回true时的元素替换成new_value
示例代码:
```
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
//把小于5的替换为8
bool fun(int i)
{
return i < 5;
}
int main()
{
int a[] = {1,2,3,4,5,6,7};
vector<int> vec(a,a+7);
replace_if(vec.begin(),vec.end(),fun,8); //vec = [8 8 8 8 5 6 7]
return 0;
}
```

View File

@ -0,0 +1,29 @@
函数原型:
```
template <class RandomAccessIterator>
void sort (RandomAccessIterator first, RandomAccessIterator last);
template <class RandomAccessIterator, class Compare>
void sort (RandomAccessIterator first, RandomAccessIterator last, Compare comp);
```
函数功能:
两个重载: 第一个对first到last 升序排序
第二个在第一个基础上传入一个传入一元判断式comp为a < b则升序否则降序
示例:
```
bool fun(int a,int b)
{
return a < b; // a < b : vec = 1 2 3 4 5 6 7
// a > b : vec = 7 6 5 4 3 2 1
}
int main()
{
int a[] = {1,3,2,4,6,7,5};
vector<int> vec(a,a+7);
sort(vec.begin(),vec.end(),fun); //vec = [1 2 3 4 5 6 7]
return 0;
}
```

View File

@ -0,0 +1,3 @@
```
swap(a,b);
```

View File

@ -0,0 +1 @@
C++中的迭代器是提供对容器(如数组、向量、列表等)中元素进行访问的方法,它类似于指针,但比指针更通用、更安全,可以遍历容器中的元素而不需要了解容器的内部结构。

3
思路/思路/tips.md Normal file
View File

@ -0,0 +1,3 @@
scanf“%c”会识别换行符。
![[Pasted image 20240723133840.png]]
不如cin

View File

@ -0,0 +1,11 @@
要手动计算下一个排列,可以遵循以下步骤。我们将使用字典序来说明如何找到下一个排列。
### 方法步骤:
1. **找到最大的 i**:从后向前找到第一个地方 `i`,使得 `nums[i] < nums[i + 1]`。如果没有找到这样的 i表示当前排列是最后一个排列返回第一个排列即将数组逆序
2. **找到最大的 j**:从后向前找到一个 `j`,使得 `nums[j] > nums[i]`。这一步的目的是找到一个比 `nums[i]` 大的最小元素。
3. **交换**:交换 `nums[i]` 和 `nums[j]`
4. **反转**:然后,将 `nums[i + 1]` 到 `nums[n - 1]` 的部分进行反转(使其按照升序排列)。

View File

@ -0,0 +1,66 @@
[C++ 格式化输出前置补0有效位数保留小数上下取整四舍五入_c++输出前面补0-CSDN博客](https://blog.csdn.net/HYY_2000/article/details/124026458)
![[Pasted image 20240723133310.png]]
头文件iomanip
控制符setw
输出控制符setfill
![[Pasted image 20240723133402.png]]
setprecision控制有效数字、
![[Pasted image 20240723133440.png]]
fixed控制小数点后几位
![[Pasted image 20240723133551.png]]
cmath
ceil向上取整
floor向下取整
round四舍五入
string str生成空字符串
string s(str)生成字符串为str的复制品
string s(str, strbegin,strlen)将字符串str中从下标strbegin开始、长度为strlen的部分作为字符串初值
string s(cstr, char_len)以C_string类型cstr的前char_len个字符串作为字符串s的初值
string s(num ,c)生成num个c字符的字符串
string s(str, stridx)将字符串str中从下标stridx开始到字符串结束的位置作为字符串初值
eg:
string str1; //生成空字符串
string str2("123456789"); //生成"1234456789"的复制品
string str3("12345", 0, 3);//结果为"123"
string str4("012345", 5); //结果为"01234"
string str5(5, '1'); //结果为"11111"
string str6(str2, 2); //结果为"3456789"
1. size()和length()返回string对象的字符个数他们执行效果相同。
2. max_size()返回string对象最多包含的字符数超出会抛出length_error异常
3. capacity()重新分配内存之前string对象能包含的最大字符数
1. C ++字符串支持常见的比较操作符(>,>=,<,<=,==,!=甚至支持string与C-string的比较(如 str<”hello”)。
在使用>,>=,<,<=这些操作符的时候是根据“当前字符特性”将字符按字典顺序进行逐一得 比较。字典排序靠前的字符小,
比较的顺序是从前向后比较,遇到不相等的字符就按这个位置上的两个字符的比较结果确定两个字符串的大小(前面减后面)
同时string (“aaaa”) <string(aaaaa)
2. 另一个功能强大的比较函数是成员函数compare()。他支持多参数处理,支持用索引值和长度定位子串来进行比较。
他返回一个整数来表示比较结果返回值意义如下0相等 1大于 -1小于 (A的ASCII码是65a的ASCII码是97)
void test4()
{
string s1;
// 尾插一个字符
s1.push_back('a');
s1.push_back('b');
s1.push_back('c');
cout<<"s1:"<<s1<<endl; // s1:abc
// insert(pos,char):在制定的位置pos前插入字符char
s1.insert(s1.begin(),'1');
cout<<"s1:"<<s1<<endl; // s1:1abc
}

399
思路/思路/未命名.md Normal file
View File

@ -0,0 +1,399 @@
<1>序列容器遍历操作
常用参数对照:
frist : 起点,为一个迭代器
last : 终点,为一个迭代器
for_each
函数原型:
template<class InputIterator, class Function>
Function for_each(InputIterator first, InputIterator last, Function fn)
1
2
功能: 遍历序列容器,将区间[fist,last)之内每一个元素传入函数fn在函数fn内对元素进行操作
示例:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
//遍历数组vector修改vector里的所有元素值为5,输出元素
void fn(int &item)
{
item = 5;
cout << item << endl;
}
int main()
{
int a[] = {1,2,3,4,5,6,7};
vector<int> v(a,a+7);
for_each(v.begin(),v.end(),fn);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
find
函数原型:
template<class InputIterator, class T>
InputIterator find (InputIterator first, InputIterator last, const T& val)
{
while (first!=last) {
if (*first==val) return first;
++first;
}
return last;
}
1
2
3
4
5
6
7
8
9
功能: 返回一个迭代器该迭代器返回等于val的范围内的第一个元素的迭代器。如果找不到这样的元素则函数返回last迭代器。
示例:
int a[] = {1,2,3,4,5,6,7};
vector<int> v(a,a+7);
vector<int>::iterator it = find(v.begin(),v.end(),5);//*it = 5
it = find(v.begin(),v.end(),9);//*it = 0
1
2
3
4
find_if
函数原型:
template<class InputIterator, class UnaryPredicate>
InputIterator find_if (InputIterator first, InputIterator last, UnaryPredicate pred)
{
while (first!=last) {
if (pred(*first)) return first;
++first;
}
return last;
}
1
2
3
4
5
6
7
8
9
功能: 将区间[fist,last)之内每一个元素传入一元判断式当pred(elem) 返回true时返回第一个达成条件的元素迭代器
示例:
int a[] = {1,4,6,2,5,3,7};
vector<int> v(a,a+7);
vector<int>::iterator it = find_if(v.begin(),v.end(),fun);//*it = 6
1
2
3
equal
函数原型:
template <class InputIterator1, class InputIterator2>
bool equal ( InputIterator1 first1, InputIterator1 last1, InputIterator2 first2 )
{
while (first1!=last1) {
if (!(*first1 == *first2)) // or: if (!pred(*first1,*first2)), for version 2
return false;
++first1; ++first2;
}
return true;
}
1
2
3
4
5
6
7
8
9
10
功能: 比较两个序列的逐个元素是否相等;
count
函数原型:
template <class InputIterator, class T>
typename iterator_traits<InputIterator>::difference_type
count (InputIterator first, InputIterator last, const T& val)
{
typename iterator_traits<InputIterator>::difference_type ret = 0;
while (first!=last) {
if (*first == val) ++ret;
++first;
}
return ret;
}
1
2
3
4
5
6
7
8
9
10
11
功能: 返回序列中等于给定值元素的个数;
示例:
int a[] = {1,4,6,4,5,3,7};
vector<int> v(a,a+7);
//把所有的三的倍数替换为 8
replace_if(v.begin(),v.end(),fun,8); //v = [1 4 8 4 5 8 7]
1
2
3
4
<2> 序列修改操作
swap
函数原型:
template <class T> void swap ( T& a, T& b )
{
T c(a); a=b; b=c;
}
1
2
3
4
函数功能交换a和b
replace
函数原型:
template <class ForwardIterator, class T>
void replace (ForwardIterator first, ForwardIterator last,
const T& old_value, const T& new_value)
{
while (first!=last) {
if (*first == old_value) *first=new_value;
++first;
}
}
1
2
3
4
5
6
7
8
9
功能: 把序列中等于old_value元素替换为 new_value
示例:
int a[] = {1,4,6,5,5,3,7};
vector<int> v(a,a+7);
replace(v.begin(),v.end(),5,55);//把5替换成55
1
2
3
replace_if
函数原型:
template < class ForwardIterator, class UnaryPredicate, class T >
void replace_if (ForwardIterator first, ForwardIterator last,
UnaryPredicate pred, const T& new_value)
{
while (first!=last) {
if (pred(*first)) *first=new_value;
++first;
}
}
1
2
3
4
5
6
7
8
9
功能: 将区间[fist,last)之内每一个元素传入一元判断式当pred(elem) 返回true时的元素替换成new_value
示例代码:
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
//把小于5的替换为8
bool fun(int i)
{
return i < 5;
}
int main()
{
int a[] = {1,2,3,4,5,6,7};
vector<int> vec(a,a+7);
replace_if(vec.begin(),vec.end(),fun,8); //vec = [8 8 8 8 5 6 7]
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
序列排序
sort
函数原型:
template <class RandomAccessIterator>
void sort (RandomAccessIterator first, RandomAccessIterator last);
template <class RandomAccessIterator, class Compare>
void sort (RandomAccessIterator first, RandomAccessIterator last, Compare comp);
1
2
3
4
函数功能:
两个重载: 第一个对first到last 升序排序
第二个在第一个基础上传入一个传入一元判断式comp为a < b则升序否则降序
示例:
bool fun(int a,int b)
{
return a < b; // a < b : vec = 1 2 3 4 5 6 7
// a > b : vec = 7 6 5 4 3 2 1
}
int main()
{
int a[] = {1,3,2,4,6,7,5};
vector<int> vec(a,a+7);
sort(vec.begin(),vec.end(),fun); //vec = [1 2 3 4 5 6 7]
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
排列函数
next_permutation
函数原型:
bool next_permutation(iterator start,iterator end)
1
函数功能:
求的是序列当前排列的下一个排列当当前序列不存在下一个排列时函数返回false否则返回true。
注意: next_permutation() 在使用前需要对欲排列数组按升序排序,否则只能找出该序列之后的全排列数。
代码:
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
int num[3]={1,2,3};
do
{
cout<<num[0]<<" "<<num[1]<<" "<<num[2]<<endl;
}while(next_permutation(num,num+3));
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
输出:
1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1
需要强调的是next_permutation() 在使用前需要对欲排列数组按升序排序,否则只能找出该序列之后的全排列数。
下面的代码将展示如何使用next_permutation在接收一个数组p之后对数组p的内容进行全排列,其中n代表数组长度:
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int main()
{
int n,p[10];
scanf("%d",&n);
for(int i = 0;i < n;i ++)
{
scanf("%d",&p[i]);
}
sort(p,p+n); //事先排序以保证全排列
do
{
for(int i = 0;i < n;i ++)
{
printf("%d ",p[i]);//对于排好序的数组,直接输出即为第一个排列
}
printf("\n");
}while(next_permutation(p,p+n));
return 0;
}
————————————————
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
原文链接https://blog.csdn.net/qq_44829295/article/details/107632453

View File

@ -0,0 +1,126 @@
```cpp
#include<bits/stdc++.h>
using namespace std;
int main() {
int n;
ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
cin>>n;
// long long ans=0; //维护逆序对
vector<int>a(n);
for(int i=0;i<n;i++)
cin>>a[i];
auto merge=[&](vector<int>&a,vector<int>&b)->vector<int>
{
vector<int>res;
size_t ai=0,bi=0;
while(ai<a.size()&&bi<b.size()){
if(b[bi]<a[ai])
{
res.push_back(b[bi]);
bi++;
// ans+=a.size()-ai; //更新逆序对
}
else {
res.push_back(a[ai]);
ai++;
}
}
if(ai==a.size())
res.insert(res.end(),b.begin()+bi,b.end());
else if(bi==b.size())
res.insert(res.end(),a.begin()+ai,a.end());
return res;
};
auto merge_sort=[&](auto &&self,vector<int>&arr)->vector<int>{
if(arr.size()<=1)return arr;
const size_t mid=arr.size()>>1;
vector<int> left(arr.begin(),arr.begin()+mid);
vector<int> right(arr.begin()+mid,arr.end());
left=self(self,left);
right=self(self,right);
return merge(left,right);
};
a=merge_sort(merge_sort,a);
for(auto i:a)
cout<<i<<" ";
// cout<<ans; //输出逆序对
}
```
```cpp
struct sgt
{
int w,l,r;
};
void dfssgt(int p,vector<sgt>&a)
{
printf("[%d,%d]=(%d,%d,%d)\n",a[p].l,a[p].r,a[p].w,0,0);
dfssgt(p*2,a);
dfssgt(p*2+1,a);
}
void sgtdbg(vector<sgt>&a)
{
printf("Tree{\n");
dfssgt(1,a);
printf("}\n");
}
void build(int p,vector<sgt>&tr)
{
if(tr[p].l==tr[p].r)return;
int m=tr[p].l+tr[p].r>>1;
tr[2*p].l=tr[p].l,tr[2*p].r=m,tr[p*2+1].l=m+1,tr[p*2+1].r=tr[p].r;
build(2*p,tr);build(2*p+1,tr);
}
void modify(int n,int x,int p,vector<sgt>&tr)
{
if(tr[p].l==tr[p].r){
tr[p].w=x;
return;
}
if(n<=tr[2*p].r)modify(n,x,p*2,tr);
else modify(n,x,p*2+1,tr);
tr[p].w=max(tr[2*p].w,tr[2*p+1].w);
}
int querys(int l,int r,int p,vector<sgt>&tr)
{
int s=tr[p].l,t=tr[p].r;
if(l<=s&&t<=r)return tr[p].w;
int maxx=0;
if(l<=tr[2*p].r)maxx=max(maxx,querys(l,r,2*p,tr));
if(r>=tr[2*p+1].l)maxx=max(maxx,querys(l,r,2*p+1,tr));
return maxx;
}
```
线段交
```cpp
struct pt {
long long x, y;
pt() {}
pt(long long _x, long long _y) : x(_x), y(_y) {}
pt operator-(const pt& p) const { return pt(x - p.x, y - p.y); }
long long cross(const pt& p) const { return x * p.y - y * p.x; }
long long cross(const pt& a, const pt& b) const { return (a - *this).cross(b - *this); }
};
int sgn(const long long& x) { return x >= 0 ? x ? 1 : 0 : -1; }
bool inter1(long long a, long long b, long long c, long long d) {
if (a > b)
swap(a, b);
if (c > d)
swap(c, d);
return max(a, c) <= min(b, d);
}
bool check_inter(const pt& a, const pt& b, const pt& c, const pt& d) {
if (c.cross(a, d) == 0 && c.cross(b, d) == 0)
return inter1(a.x, b.x, c.x, d.x) && inter1(a.y, b.y, c.y, d.y);
return sgn(a.cross(b, c)) != sgn(a.cross(b, d)) &&
sgn(c.cross(d, a)) != sgn(c.cross(d, b));
}
```

1266
思路/思路/板子.md Normal file

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,5 @@
当模型存在边界时可以将边界扩大使所有数据执行统一操作如
![[Pasted image 20240722135718.png]]
可以在创建时扩大数组边界并使数据从11开始输入
![[Pasted image 20240722135849.png]]
进而化简问题。

View File

@ -0,0 +1,43 @@
1.01tire
2.实现遍历区间的运算。
代码:
```
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
auto f()
{
using vec_t=vector<string>;
vec_t vec;
std::move(vec);
//
return vec;
}
int main()
{
// 右值引用
vector<string>&& v=f();
// auto f=f_impl;
int x;
int& y=x ;
static_cast<int&>(x);
//const auto& xx=1;
const auto xx_impl=1;
auto&& xx=xx_impl; "aaaaa";
int n;
cin>>n;
vector<string> a(n);
for(int i=0;i<n;i++)
cin>>a[i];
sort(a.begin(),a.end());
for(auto&& x:a){
cout<<x;
}
}
```