算法竞赛入门经典第五章

struct node {
    int x, y;
};
ostream& operator<<(ostream& out, node p) {
    return out << p.x << p.y;
}

模板类、模板函数

template<typename T>
T sum(T* begin, T* end) {
    T ans = 0;
    for (T *p = begin; p != end; ++p)
        ans += *p;
    return ans;
}
template<typename T>
struct Point {
    T x, y;
};

对于upper_bound来说,返回的是被查序列中第一个大于查找值的指针,也就是返回指向被查值>查找值的最小指针,lower_bound则是返回的是被查序列中第一个大于等于查找值的指针,也就是返回指向被查值>=查找值的最小指针。
unique函数删除有序数组中重复元素
优先队列:

struct cmp {//重载()符号
    bool operator()(const long long a, const long long b) {//a优先级小于b时返回true
        return a > b;
    }
};
    priority_queue<long long, vector<long long>, cmp > all;

生成随机数

#include<ctime>
#include<cstdlib>
void fill_random_int(vector<int>& v, int cnt) {
    v.clear();srand(time(NULL));//初始化随机数种子
    while (cnt--)
    v.push_back(rand());//rand()生成[0,32767]的随机数
}

assert(表达式) 当表达式真时无变化,假时强行终止程序,并给出错误提示。
大整数:

#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
using namespace std;
#pragma warning(disable:4996)
struct BigInteger {
        static const int BASE = 100000000;
        static const int WIDTH = 8;
        vector<int> s;
        BigInteger(long long num = 0) { *this = num; }
        BigInteger operator = (long long num) {
            s.clear();
            do {
                s.push_back(num % BASE);
                num /= BASE;
            } while (num > 0);
            return *this;
        }
        BigInteger operator = (const string& str) {
            s.clear();
            int x, len = (str.length() - 1) / WIDTH + 1;
            for (int i = 0; i < len-1; ++i) s.push_back(stoi(str.substr(str.length() - (i + 1)*WIDTH, WIDTH)));
            s.push_back(stoi(str.substr(0, (str.length() - 1) % 8 + 1)));
            return *this;
        }
    };
ostream& operator << (ostream &out, const BigInteger& x) {
    for (int i = x.s.size() - 1; i >= 0; --i)
        out << x.s[i];
    return out;
}
istream& operator << (istream &in, BigInteger& x) {
    string s;
    if(!(in >> s)) return in;
    x = s;
    return in;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值