编程语言
首页 > 编程语言> > 离散化算法

离散化算法

作者:互联网

离散化

什么是离散化?

离散化的步骤

离散化的核心板子(源自https://www.acwing.com/blog/content/277/)

vector<int> alls; // 存储所有待离散化的值
sort(alls.begin(), alls.end()); // 将所有值排序
alls.erase(unique(alls.begin(), alls.end()), alls.end());   // 去掉重复元素

// 二分求出x对应的离散化的值
int find(int x) // 找到第一个大于等于x的位置
{
    int l = 0, r = alls.size() - 1;
    while (l < r)
    {
        int mid = l + r >> 1;
        if (alls[mid] >= x) r = mid;
        else l = mid + 1;
    }
    return r + 1; // 映射到1, 2, ...n
}

离散化的板子题(题目源自https://www.acwing.com/problem/content/804/)

#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 3e5 + 10;
typedef pair<int, int> PII;
int sum[N];
int n, m;
PII add[N], ask[N];
unordered_map<int, int> mmap;
vector<int>vec;
vector<int>::iterator unique(vector<int>& t) {
    int j = 0;
    for (int i = 0; i < t.size(); ++i) {
        if (!i || t[i] != t[i - 1]) t[j++] = t[i];
    }
    return t.begin() + j;
}
inline int read() {
    char ch = getchar();
    int s = 0, f = 1;
    for (;ch < '0' || ch > '9';ch = getchar()) if (ch = '-')f = -1;
    for (;ch >= '0' && ch <= '9';ch = getchar()) s = (s << 3) + (s << 1) + (ch ^ 48);
    return f == -1 ? -s : s;
}
signed main(void) {
    n = read(), m = read();
    for (int i = 1; i <= n;++i) {
        int x, c;
        x = read(), c = read();
        add[i] = make_pair(x, c);
        vec.push_back(x);
    }
    for (int i = 1; i <= m; ++i) {
        int l, r;
        l = read(), r = read();
        ask[i] = make_pair(l, r);
        vec.push_back(l), vec.push_back(r);
    }
    sort(vec.begin(), vec.end());
    //vec.erase(unique(vec.begin(), vec.end()), vec.end());  STL中的unique
    vec.erase(unique(vec), vec.end());
    for (int i = 0; i < vec.size(); ++i) mmap[vec[i]] = i + 1;
    for (int i = 1; i <= n; ++i) {
        int idx = mmap[add[i].first];
        sum[idx] += add[i].second;
    }

    for (int i = 1; i <= vec.size(); i++) {
        sum[i] += sum[i - 1];
    }
    for (int i = 1; i <= m; ++i) {
        int ll = mmap[ask[i].first], rr = mmap[ask[i].second];
        cout << sum[rr] - sum[ll - 1] << endl;
    }
}

标签:ch,int,mid,离散,算法,alls,哈希
来源: https://www.cnblogs.com/slbaba/p/16526185.html