#include<bits/stdc++.h>usingnamespace std;#defineintlonglongtypedef pair<int,int> PII;#definexfirst#defineysecondconstint N =2e5+10;int a[N], n , dp[N], b[N], ans =-0x3f3f3f3f;signedmain(){scanf("%lld",&n);for(int i =1; i <= n ; i ++)scanf("%lld",&a[i]);
stack<PII> s;// 单调栈维护的是 pair , 不会的话百度查一下
s.push({0,0});// 栈初始化 :如果没有查找到左边第一个比它小的数 , 等价于 dp[i] = a[i] * (i - 0) for(int i =1; i <= n ; i ++){while(s.size()&& a[i]<= s.top().x) s.pop();
PII last = s.top();
s.push({a[i], i});
dp[i]= dp[last.y]+ a[i]*(i - last.y);
ans =max(ans , dp[i]);}printf("%lld\n", ans);}
滑动窗口 /【模板】单调队列
#include<iostream>usingnamespace std;constint N =1000010;int a[N], q[N];intmain(){int n, k;scanf("%d%d",&n,&k);for(int i =0; i < n; i ++)scanf("%d",&a[i]);int hh =0, tt =-1;for(int i =0; i < n; i ++){if(hh <= tt && i - k +1> q[hh]) hh ++;while(hh <= tt && a[q[tt]]>= a[i]) tt --;
q[++ tt]= i;if(i >= k -1)printf("%d ", a[q[hh]]);}puts("");
hh =0, tt =-1;for(int i =0; i < n; i ++){if(hh <= tt && i - k +1> q[hh]) hh ++;while(hh <= tt && a[q[tt]]<= a[i]) tt --;
q[++ tt]= i;if(i >= k -1)printf("%d ", a[q[hh]]);}puts("");return0;}
【模板】并查集
#include<bits/stdc++.h>#defineintlonglongusingnamespace std;constint N =2e5+10;int p[N], a[N], n , m;intfind(int x){if(x == p[x])return x;elsereturn p[x]=find(p[x]);}signedmain(){
cin >> n >> m;for(int i =1; i <= n ; i ++){
p[i]= i;}while(m --){int a , b , c;
cin >> c >> a >> b;if(c ==1){
a =find(a), b =find(b);if(a != b){
p[b]= a;}}else{if(find(a)!=find(b)) cout <<"N"<< endl;else cout <<"Y"<< endl;}}}
P1892 [BOI2003]团伙
#include<bits/stdc++.h>#defineintlonglongusingnamespace std;constint N =2e5+10;int p[N], a[N], n , m;intfind(int x){if(x == p[x])return x;elsereturn p[x]=find(p[x]);}signedmain(){
cin >> n >> m;for(int i =1; i <=2* n ; i ++){
p[i]= i;}int ct =0;while(m --){int a , b;char c;
cin >> c >> a >> b;if(c =='F'){
a =find(a), b =find(b);if(a != b){
p[b]= a;}}else{int aa =find(a), bb =find(b);int c =find(a + n), d =find(b + n);if(bb != c) p[c]= bb;if(d != aa) p[d]= aa;}}for(int i =1; i <= n ; i ++){if(p[i]== i) ct ++;}
cout << ct << endl;}
字典树模板
#include<iostream>usingnamespace std;constint N =100010;int son[N][26], cnt[N], idx;char str[N];voidinsert(char*str){int p =0;for(int i =0; str[i]; i ++){int u = str[i]-'a';if(!son[p][u]) son[p][u]=++ idx;
p = son[p][u];}
cnt[p]++;}intquery(char*str){int p =0;for(int i =0; str[i]; i ++){int u = str[i]-'a';if(!son[p][u])return0;
p = son[p][u];}return cnt[p];}intmain(){int n;scanf("%d",&n);while(n --){char op[2];scanf("%s%s", op, str);if(*op =='I')insert(str);elseprintf("%d\n",query(str));}return0;}
最大异或对【字典树】
#include<iostream>#include<algorithm>usingnamespace std;constint N =100010, M =3100010;int n;int a[N], son[M][2], idx;voidinsert(int x){int p =0;for(int i =30; i >=0; i --){int&s = son[p][x >> i &1];if(!s) s =++ idx;
p = s;}}intsearch(int x){int p =0, res =0;for(int i =30; i >=0; i --){int s = x >> i &1;if(son[p][!s]){
res +=1<< i;
p = son[p][!s];}else p = son[p][s];}return res;}intmain(){scanf("%d",&n);for(int i =0; i < n; i ++){scanf("%d",&a[i]);insert(a[i]);}int res =0;for(int i =0; i < n; i ++) res =max(res,search(a[i]));printf("%d\n", res);return0;}
P3375 【模板】KMP字符串匹配
#include<iostream>#include<cstring>usingnamespace std;constint N =100010, M =1000010;int n, m;int ne[N];char s[M], p[N];intmain(){
cin >> s +1>> p +1;
m =strlen(s +1);
n =strlen(p +1);for(int i =2, j =0; i <= n; i ++){while(j && p[i]!= p[j +1]) j = ne[j];if(p[i]== p[j +1]) j ++;
ne[i]= j;}for(int i =1, j =0; i <= m; i ++){while(j && s[i]!= p[j +1]) j = ne[j];if(s[i]== p[j +1]) j ++;if(j == n){printf("%d\n", i - n +1);
j = ne[j];}}for(int i =1; i <= n ; i ++)printf("%lld ", ne[i]);return0;}