我的OI解题模板

前言

        这个模板的代码来源于Codeforces上的知名算法专家Dreamoon,我对它进行了一些修改,我的大部分算法题都是用这个模板来完成的。

        这个模板一是用来加快写代码的速度,二是用来使关注点聚焦到算法本身上,而不是输入输出等细枝末节上。

        这个模板定义了三个宏,all用来取一个vector的begin和end;rep和repd用来替代for循环,其中repd是反向for循环;sort_unique用来对vector排序并去重。例如

for(int i=0; i<100; i++)
    printf("%d\n", i);

可以写为

rep(i,0,100)
    W(i);

        这个模板还对pair和vector进行了typedef,因为vector实在是太常用了。我以前学OI的时候,有很多人为了运行速度,用一个静态数组加一个索引来表示一个动态数组,比如
int v[10000]; int tot = 0;

于是向数组中添加一个元素的操作就是

v[tot++] = n;

        用这种写法的问题是,当时你可能还记得这段代码是做什么用的,但是过一个月以后再读这段代码你恐怕会感到非常难受。用vector的好处就是使代码易读,我跟许多人不同,我从学OI的开始就非常注重代码的易读性,而且使用vector也并没有很多速度上的损失,所以我建议大家使用vector。

        接下来模板里定义了许多方便读写的函数,只需要关注R函数和W函数,其中R表示Read,W表示Write,以后需要读写的时候,比如

printf("%d", n);可以替代为W(n);

scanf("%d", &n);可以替代为R(n);

printf("%d", n, m);可以替代为W(n,m);

scanf("%d%d", &n, &m);可以替代为R(n,m);

        如果你打算在本地进行一些测试,可以在代码的开头加上

#define DEBUG

这样R函数就会从1.in这个文件中读取数据。

——2023年12月19日 傍晚

模板

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<string>
#include<iostream>
#include<sstream>
#include<vector>
#include<list>
#include<stack>
#include<queue>
#include<set>
#include<map>
#include<bitset>
#include<limits.h>
#include<assert.h>
using namespace std;
#define all(X) (X).begin(), (X).end()
#define rep(I,A,B) for(int I=(A);I<(B);I++)
#define repd(I,A,B) for(int I=(A);I>(B);I--)
#define sort_unique(c) (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
typedef long long LL;
typedef pair<int,int> PII;
typedef pair<LL,LL> PLL;
typedef vector<int> VI;
typedef vector<PII> VPII;
typedef vector<LL> VLL;
typedef vector<PLL> VPLL;
template<class T> void _R(T &x) { cin >> x; }
void _R(int &x) { scanf("%d", &x); }
void _R(int64_t &x) { scanf("%lld", &x); }
void _R(double &x) { scanf("%lf", &x); }
void _R(char &x) { scanf(" %c", &x); }
void _R(char *x) { scanf("%s", x); }
void R() {}
template<class T, class... U> void R(T &&head, U &&... tail) { _R(head); R(tail...); }
template<class T> void _W(const T &x) { cout << x; }
void _W(const int &x) { printf("%d", x); }
void _W(const int64_t &x) { printf("%lld", x); }
void _W(const double &x) { printf("%.16f", x); }
void _W(const char &x) { putchar(x); }
void _W(const char *x) { printf("%s", x); }
template<class T,class U> void _W(const pair<T,U> &x) {_W("{"); _W(x.first); putchar(','); _W(x.second); _W("}");}
template<class T> void _W(const vector<T> &x) { for (auto i = x.begin(); i != x.end(); _W(*i++)) if (i != x.cbegin()) putchar(' '); }
void W() { }
template<class T, class... U> void W(const T &head, const U &... tail) { _W(head); putchar(sizeof...(tail) ? ' ' : '\n'); W(tail...); }
VI range(int A, int B) { VI v; rep(i,A,B) v.push_back(i); return v;}

int main()
{
    #ifdef DEBUG
    freopen("1.in","r",stdin);
    //freopen("tmp.out","w",stdout);
    #endif

    return 0;
}


 

技巧

1. 
开启O2优化(要加在开头)

#ifndef DEBUG
#pragma GCC optimize("O2")
#endif

2. assert的意思是确保,可以用assert(...)确保某个条件成立

3. W("")用于打印空行,W(vector(a,a+n))用于打印数组

4. lower_bound(a,x):可以理解为找出x在数组a中的下界,即找到第一个大于等于x的元素的位置

5. upper_bound(a,y):可以理解为找出x在数组a中的上界,即找到最后一个小于等于x的元素的位置

6. 

char S[10];
R(S+1); // 会报错,因为S+1不是左值

char *P=S+1;
R(P); // OK


 

  • 10
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值