基础算法(递归、分治、枚举、贪心、二分、二分答案)
随便玩玩
开学了8
go on
展开
-
C - Chat Ban
C - Chat Ban注意二分边界条件吧// Problem: C. Chat Ban// Contest: Codeforces - Educational Codeforces Round 117 (Rated for Div. 2)// URL: https://codeforces.com/contest/1612/problem/C// Memory Limit: 512 MB// Time Limit: 2000 ms// // Powered by CP Editor (ht原创 2022-03-03 23:26:57 · 266 阅读 · 0 评论 -
rmq(区间最值问题)
rmqRMQ(Range Minimum/Maximum Query),即区间最值查询,是指这样一个问题:对于长度为nnn的数列AAA,设A[i]A[i]A[i]是要求区间最值的数列,F[i,j]F[i, j]F[i,j]表示从第iii个数起连续2j2^j2j个数中的最大值。(DP的状态)例如:A数列为:3 2 4 5 6 8 1 2 9 7F[1,0]F[1,0]F[1,0]表示第1个数起,长度为20=12^0=120=1的最大值,其实就是333这个数。同理F[1,1]=max(3,2)=3F原创 2021-11-24 11:00:54 · 571 阅读 · 0 评论 -
Third-Party Software - 2(贪心+最小覆盖问题)
#include<iostream>#include<cstdio>#include<string>#include<ctime>#include<cmath>#include<cstring>#include<algorithm>#include<stack>#include<climits>#include<queue>#include<map>#in原创 2021-10-26 20:33:35 · 127 阅读 · 0 评论 -
三分模板题
#include <iostream>using namespace std;#define eps 1e-10int n;double l, r, a[14];inline double cal(double x){ double res = 0.0; for(int i = 1; i <= n + 1; i++) res = res * x + a[i]; return res;} void sanf(){ while(r - l > eps)原创 2021-08-29 19:07:16 · 45 阅读 · 0 评论 -
二分查找数组中目标元素的最后一个元素和第一个元素
int search(int x) // 最后一个{ int l = 1, r = n; while(l < r) { int mid = (l + r + 1) >> 1; if(a[mid] > x) r = mid - 1; else l = mid; } if(a[r] == x) return r; else return -1; }int search(int x) // 第一个{ int l = 1, r = n; while(l原创 2021-08-29 16:52:40 · 94 阅读 · 0 评论 -
编辑距离模板题
// Problem: P2758 编辑距离// Contest: Luogu// URL: https://www.luogu.com.cn/problem/P2758// Memory Limit: 125 MB// Time Limit: 1000 ms// // Powered by CP Editor (https://cpeditor.org)#include<iostream>using namespace std;#define rep(i,l,r) for.原创 2021-08-19 15:29:21 · 48 阅读 · 0 评论 -
c++高精度模板
两个大整数相乘#include <iostream>#include <cstring>using namespace std;const int N = 50000;char a[N], b[N];int A[N], B[N], C[2*N];int main(){ cin >> a >> b; A[0] = strlen(a); B[0] = strlen(b); for(int i=原创 2021-07-30 22:42:10 · 350 阅读 · 0 评论 -
两个数学公式1^2 + 2^2 + ... + n^2 = n(n+1)(2n+1)/6 && 1^3 + 2^3 + ... + n^3 = [n(n+1)/2]^2
这应该不是数论,而是计算几何吧12+22+...+n2=n(n+1)(2n+1)/61^2 + 2^2 + ... + n^2 = n(n+1)(2n+1)/612+22+...+n2=n(n+1)(2n+1)/613+23+...+n3=[n(n+1)/2]21^3 + 2^3 + ... + n^3 = [n(n+1)/2]^213+23+...+n3=[n(n+1)/2]2对于第一个公式而言,求解一个正方形内有多少个小的正方形,包含本身对于第二个公式,一个正方体内有多少个小的正方体,包含本身原创 2021-07-22 16:42:30 · 8198 阅读 · 0 评论 -
100. 增减序列 (差分)
要求所有数都一样,那么只需要从2开始,差分数组为0就可以啦贪心选择的过程就是,min(q, p) + abs(p-q) = max(p, q)把正数和负数按最小的值互相兑掉,剩下abs(p-q) 拿头或者尾来兑换最少也存在一中情况所以最后+1#include <iostream>using namespace std;const int maxn = 1e5+10;int a[maxn];long long p, q;int main(){ int n; scanf原创 2021-07-22 16:34:51 · 99 阅读 · 0 评论 -
差分差分差分
class Solution {public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * 返回m天后高度为奇数的树的数量 * @param n int整型 * @param m int整型 * @param l int整型vector * @param r int整型vector * @return int整型 */ int oddnumber(int n原创 2021-07-22 14:08:37 · 90 阅读 · 0 评论 -
数组差分&&二维矩阵的前缀和与差分,在O(n)时间内完成查询
a[i]=b[1]+b[2]+...b[i−1]+b[i]a[i] = b[1] + b[2] + ...b[i-1]+b[i]a[i]=b[1]+b[2]+...b[i−1]+b[i]b[l]=b[l]+cb[l] = b[l] + cb[l]=b[l]+cb[r+1]=b[r+1]−cb[r+1] = b[r+1] - cb[r+1]=b[r+1]−c使得a数组两头的数不变,中间求前缀和时,就会自动加上c 值。#include <iostream>using namespac.原创 2021-07-22 11:31:02 · 92 阅读 · 0 评论 -
Penguins(bfs 较难的题目)
#include <bits/stdc++.h>using namespace std;#define pii pair<int, int>#define ff first#define ss secondconst int N = 22;struct node{ int x1, y1, x2, y2; // 四个坐标 vector<pii > A1; vector<pii > A2; string ans; //原创 2021-07-21 22:23:53 · 74 阅读 · 0 评论 -
奇怪的电梯
#include <bits/stdc++.h>using namespace std;int a[205],vis[205],res=205;/*(1) 为什么要设置vis 数组,之前搜过的东西,早就有答案了,如果再去搜,要么没有答案,要么答案小于之前搜过的答案(2)注意第44行代码,调试了一阵时间才出结果(3)套路都是差不多的,要注意细节。*/struct point{ int now; // 现在的位置 int step; // 步数};queue &l原创 2021-07-21 18:58:55 · 60 阅读 · 0 评论 -
P1443 马的遍历 (bfs)
注意左对齐,宽五格的输出,为此wa了两次#include <bits/stdc++.h>using namespace std;#define inf 0x3f3f3f3fconst int N = 405;int vis[N][N], res[N][N];struct point{ int x; int y; int step;};int dx[] = {1, 2, 2, 1, -1, -2, -2, -1};int dy[] = {2, 1, -1, -2,原创 2021-07-21 17:13:40 · 95 阅读 · 0 评论 -
深度优先搜索DFS(洛谷) P1162 填涂颜色
#include <bits/stdc++.h>using namespace std;#define inf 0x3f3f3f3fint n, a[35][35], vis[35][35];struct point{ int x; int y;};int dx[] = {0, 1, 0 ,-1};int dy[] = {1, 0, -1, 0}; int main(){ int n; cin >> n; for (int i = 1; i &原创 2021-07-21 16:30:52 · 113 阅读 · 0 评论 -
单调队列模板
#include <bits/stdc++.h>using namespace std;#define inf 0x3f3f3f3fint main(){ int n; cin >> n; vector <int> v(n+1),res(n+1); for (int i = 1; i <= n; i++) scanf("%d",&v[i]); stack<int> st; for (int i = 1; i原创 2021-07-21 15:16:25 · 45 阅读 · 0 评论 -
归并排序(没试过)
#include <iostream>using namespace std;const int maxn = 1e6+7;int arr[maxn], tmp[maxn];void m_sort(int arr[], int l ,int r){ if (l >= r) return ; int mid = l + r >> 1; m_sort(arr, l, mid); m_sort(arr, mid+1, r); int k = 0, i = l原创 2021-07-20 15:43:02 · 44 阅读 · 0 评论 -
快速排序模板
其实不太想写, 因为意义不大, 真正比赛的时候,没有人会去用,直接sort就行快速排序,平均 O(nlogn)O(nlogn)O(nlogn),最坏 O(n2)O(n^2)O(n2)#include <iostream>using namespace std;const int max = 1e6 + 7;void quick_sort(int arr[], int l, int r) { if (l >= r) return; int x = arr[l + r >原创 2021-07-20 15:04:42 · 48 阅读 · 0 评论 -
区间映射,区间离散化,大范围映射到小范围
区间离散化当然int x = find (targe);等价于int x = low_bound(0,alls.size()-1, targe) - alls + 1;#include <bits/stdc++.h>using namespace std;#define ios ios::sync_with_stdio(false);cin.tie(0)#define rep(i,a,b) for(int i=a;i<=b;++i)#define fep(i,a,b) fo原创 2021-07-17 10:45:08 · 529 阅读 · 0 评论 -
石子合并问题(区间dp)
#include <iostream>using namespace std;#define ios ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)const int N = 310; // O(n^3)int n;int s[N], a[N], f[N][N];void solve(){ memset(f, 0x3f, sizeof(f)); cin >> n; for原创 2021-06-29 16:28:22 · 68 阅读 · 0 评论 -
分数背包问题(贪心问题)
与01背包不同的是,背包容量C、物品体积V、物品价值S是正实数,不是正整数;且允许每种物品的一部分装入背包求总价值最大:物品 u1,u2,u3u_1,u_2,u_3u1,u2,u3体积 18,15,1018,15,1018,15,10价值 25,24,1525,24,1525,24,15将物品的性价比进行排序,性价比 = 价值 / 体积性价比:u2=1.6,u3=1.5,u1=1.38u_2 = 1.6,u_3 = 1.5,u_1 = 1.38u2=1.6,u3=1.5,u1=原创 2021-05-31 12:18:01 · 971 阅读 · 0 评论 -
2020-5-20-POJ 1182-关系型并查集
并查集:POJ 1182#include<bits/stdc++.h>#define rep(i,a,b) for(int i=a;i<=b;++i)const int N = 5e4;using namespace std;int n;int pre[N+10], rela[N+10];int find(int x){ int y=x,cnt=0; while(y!=pre[y]){ // 加上逻辑距离 cnt+=rela[y]; y=pre[原创 2021-05-21 19:19:55 · 58 阅读 · 0 评论 -
并查集--简单记录
并查集也是一种树状结构,用于处理一些不相交集合的合并以及查询问题查询:int find(int x){ while(x!=parent[x]) x = parent[x]; return x;}这种方法递归耗时多,且有大量的重复计算。稍微改变一下:int find(int x){ while(x!=parent[x]) return x = parent[x];}根据递归的性质,把节点直接全部连接到根节点上如果数据很大,递归可能会溢出栈int find(int x){原创 2021-05-19 19:42:35 · 33 阅读 · 0 评论 -
M. Game Theory(简单博弈论)
https://codeforc.es/group/vFwRVj9WjO/contest/325796/problem/M老师随机选择一个数,对于其他学生,抽一个来分析即可,随机抽一个来计算,总的概率为0代码及其简单,主要是思维#include <bits/stdc++.h>using namespace std;int main(){ int t; cin >> t; puts("0.0000"); return 0;}.原创 2021-04-30 16:30:16 · 455 阅读 · 1 评论 -
64位整数乘法(大整数乘法)
给定大整数a, b求a * b % pimport java.io.*;import java.math.*;import java.util.*; public class Main{ public static void main(String args[]) { Scanner cin = new Scanner(System.in); BigInteger a=cin.nextBigInteger(); BigInteger b=cin.nextBigI原创 2021-04-23 18:45:55 · 120 阅读 · 0 评论 -
java大整数类
传送门向比较于c++,java的大整数类,便捷!原创 2021-04-23 18:43:23 · 50 阅读 · 0 评论 -
快速计算a的n次方(快速幂&&运算取模规则)
https://www.luogu.com.cn/problem/P2415假设集合有n个元素每个元素出现2n2^n2n次#include <bits/stdc++.h>using namespace std;long long a[31];int main(){ int i = 0; long long ans = 1, sum = 0; while(cin >> a[i]) sum += a[i++]; ans = sum * p原创 2021-03-27 12:49:27 · 2212 阅读 · 0 评论