代码

模板

缺省源

#include <bits/stdc++.h>

using namespace std;

void file() {
	freopen(".in", "r", stdin);
	freopen(".out", "w", stdout);
}

template <typename type>
void read(type &res) {
	int x = 0, f = 1;
	char c = getchar();
	for (; c < 48 || c > 57; c = getchar()) if (c == '-') f = ~f + 1;
	for (; c >= 48 && c <= 57; c = getchar()) x = (x << 3) + (x << 1) + (c ^ 48);
	res = f * x;
}

template <typename type>
void write(type x) {
	static short stack[50], top = 0;
	if (x < 0) putchar('-'), x = ~x + 1;
	if (x == 0) return void(putchar('0'));
	while (x) stack[++top] = x % 10, x /= 10;
	while (top) putchar(stack[top--] | 48);
}

template <typename type>
void write(type x, char c) {
	write(x);
	putchar(c);
}

int main() {
	return 0;
}

文件输入输出

void file() {
	freopen(".in", "r", stdin);
	freopen(".out", "w", stdout)
}

快读

template <typename type>
void read(type &res) {
	int x = 0, f = 1;
	char c = getchar();
	for (; c < 48 || c > 57; c = getchar()) if (c == '-') f = ~f + 1;
	for (; c >= 48 && c <= 57; c = getchar()) x = (x << 3) + (x << 1) + (c ^ 48);
	res = f * x;
}

快写

template <typename type>
void write(type x) {
	static short stack[50], top = 0;
	if (x < 0) putchar('-'), x = ~x + 1;
	if (x == 0) return void(putchar('0'));
	while (x) stack[++top] = x % 10, x /= 10;
	while (top) putchar(stack[top--] | 48);
}

template <typename type>
void write(type x, char c) {
	write(x);
	putchar(c);
}

快速幂

int power(int a, int b/*, int p*/) {
	int ans = 1 /*% p*/;
	while (b >>= 1) {
		if (b & 1) ans = ans * a/* % p*/;
		a = a * a/* % p*/;
	}
	return ans;
}

\(\text{KMP}\)

// s 表示模式串, t 表示模板串

const int N = /*模式串长度*/;

int f[N], next[N]

void init_next() {
	for (int i = 2, j = 0; i <= m; i++) {
		while (j && t[i] != t[j + 1])
			j = next[j];
		if (t[i] == t[j + 1]) j++;
		next[i] = j;
	}
}

void init_f() {
	for (int i = 1, j = 2; i <= n; i++) {
		while (j && (j == m || s[i] != t[j + 1])) 
			j = next[j];
		if (s[i] == t[j + 1]) j++;
		f[i] = j;
	}
}

\(\text{Trie}\) 树(字典树)

int get(char c) {
	//...;
}

void insert(char s[]) {
	int p = 1;
	int len = strlen(s + 1);
	for (int i = 1; i <= len; i++) {
		int c = get(s[i]);
		if (!trie[p][c])
			trie[p][c] = ++tot;
		p = trie[p][c];
	}
	end[p] = 1; // end[p] 表示当前节点是否是某个字符串的结尾
}

int find(char s[]) {
	int p = 1;
	int len = strlen(s + 1);
	for (int i = 1; i <= len; i++) {
		int c = get(s[i]);
		if (!trie[p][c])
			return 0;
		p = trie[p][c];
	}
	if (end[p]) return true;
	return false;
}

二叉堆

小根堆

class bh {
public:
	int tot;
	int top() {
		return heap[1];
	}
	int size() {
		return tot;
	}
	void clear() {
		tot = 0;
		memset(heap, 0, sizeof(heap));
	}
	void push(int x) {
		heap[++tot] = x;
		up(tot);
	}
	void pop() {
		std::swap(heap[1], heap[tot--]);
		down(1);
	}
private:
	int heap[N];
	void up(int p) {
		while (p > 1) {
			if (heap[p] < heap[p >> 1]) {
				std::swap(heap[p], heap[p >> 1]);
				p >>= 1;
			} else return;
		}
	}
	void down(int p) {
		int k = p << 1;
		while (k <= tot) {
			if (k < tot && heap[k] > heap[k + 1])
				k++;
			if (heap[k] < heap[p]) {
				std::swap(heap[p], heap[k]);
				p = k;
				k <<= 1;
			} else return;
		}
	}
};

大根堆

class bh {
public:
	int tot;
	int top() {
		return heap[1];
	}
	int size() {
		return tot;
	}
	void clear() {
		tot = 0;
		memset(heap, 0, sizeof(heap));
	}
	void push(int x) {
		heap[++tot] = x;
		up(tot);
	}
	void pop() {
		std::swap(heap[1], heap[tot--]);
		down(1);
	}
private:
	int heap[N];
	void up(int p) {
		while (p > 1) {
			if (heap[p] > heap[p >> 1]) {
				std::swap(heap[p], heap[p >> 1]);
				p >>= 1;
			} else return;
		}
	}
	void down(int p) {
		int k = p << 1;
		while (k <= tot) {
			if (k < tot && heap[k] < heap[k + 1])
				k++;
			if (heap[k] > heap[p]) {
				std::swap(heap[p], heap[k]);
				p = k;
				k <<= 1;
			} else return;
		}
	}
};

树状数组

class Binary_Indexed_Trees {
public:
	int ask(int x) {
		int sum = 0;
		for (; x; x -= lowbit(x)) 
			sum += x;
		return sum;
	}
	void add(int x, int k) {
		for (; x <= n; x += lowbit(x)) 
			bit[x] += k;
	}
private:
	int bit[N];
	int lowbit(int x) {
		return x & -x;
	}
};

最长上升子序列

void LIS() {
	int f[N];
	f[0] = -INT_MAX;
	for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
	for (int i = 1; i <= n + 1; i++) f[i] = INT_MAX;
	for (int i = 1; i <= n; i++) *lower_bound(f + 1, f + 1 + n, a[i]) = a[i];
	printf("%d\n", lower_bound(f + 1,f + 2 + n, INT_MAX) - f - 1);
}

对拍

// 随机数据生成模板
#include<cstdlib>
#include<ctime>

int random(int n) {
    return (long long)rand() * rand() % n;
}

int main() {
    srand((unsigned)time(0));
    // ...具体内容...
}

// 实例:随机生成整数序列
// 不超过100000个绝对值在1000000000内的整数
int n = random(100000) + 1;
int m = 1000000000;
for (int i = 1; i <= n; i++) {
    a[i] = random(2 * m + 1) - m;
}

// 实例:随机生成区间列
for (int i = 1; i <= m; i++) {
    int l = random(n) + 1;
    int r = random(n) + 1;
    if (l > r) swap(l, r);
    printf("%d %d\n", l, r);
}

// 实例:随机生成树
for (int i = 2; i <= n; i++) {
    // 从 2~n 之间的每个点 i 向 1~i-1 之间的点随机连一条边
    int fa = random(i - 1) + 1;
    int val = random(1000000000) + 1;
    printf("%d %d %d\n", fa, i, val);
}

// 实例:随机生成图
// 无向图,连通,不含重边、自环
pair<int, int> e[1000005]; // 保存数据
map< pair<int, int>, bool > h; // 防止重边
// 先生成一棵树,保证连通
for (int i = 1; i < n; i++) {
    int fa = random(i) + 1;
    e[i] = make_pair(fa, i + 1);
    h[e[i]] = h[make_pair(i + 1, fa)] = 1;
}
// 再生成剩余的 m-n+1 条边
for (int i = n; i <= m; i++) {
    int x, y;
    do {
        x = random(n) + 1, y = random(n) + 1;
    } while (x == y || h[make_pair(x, y)]);
    e[i] = make_pair(x, y);
    h[e[i]] = h[make_pair(y, x)] = 1;
}
// 随机打乱,输出
random_shuffle(e + 1, e + m + 1);
for (int i = 1; i <= m; i++)
    printf("%d %d\n", e[i].first, e[i].second);


// Windows系统对拍程序
#include<cstdlib>
#include<cstdio>
#include<ctime>
int main() {
    for (int T = 1; T <= 10000; T++) {
        // 自行设定适当的路径
        system("C:\\random.exe");
        // 返回当前程序已经运行的CPU时间,windows下单位ms,类unix下单位s
        double st = clock();
        system("C:\\sol.exe");
        double ed = clock();
        system("C:\\bf.exe");
        if (system("fc C:\\data.out C:\\data.ans")) {
            puts("Wrong Answer");
            // 程序立即退出,此时data.in即为发生错误的数据,可人工演算、调试
            return 0;
        }
        else {
            printf("Accepted, 测试点 #%d, 用时 %.0lfms\n", T, ed - st);
        }
    }
}

头文件

万能头文件

#ifndef _GLIBCXX_NO_ASSERT
#include <cassert>
#endif
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#if __cplusplus >= 201103L
#include <ccomplex>
#include <cfenv>
#include <cinttypes>
#include <cstdalign>
#include <cstdbool>
#include <cstdint>
#include <ctgmath>
#include <cwchar>
#include <cwctype>
#endif
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#if __cplusplus >= 201103L
#include <array>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <forward_list>
#include <future>
#include <initializer_list>
#include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <system_error>
#include <thread>
#include <tuple>
#include <typeindex>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#endif
posted @ 2023-10-14 13:20  cz2010124  阅读(8)  评论(0编辑  收藏  举报