离散化

    科技2024-10-29  13

    离散化

    1.

    #include <iostream> #include <algorithm> using namespace std; const int MAXN = 1005; int n; int rev[MAXN], a[MAXN]; int main() { cin >> n; for(int i = 1; i <= n; i++) cin >> a[i], rev[i] = a[i]; sort(rev + 1, rev + 1 + n); int len = unique(rev + 1, rev + 1 + n) - rev - 1; for(int i = 1; i <= n; i++) { a[i] = lower_bound(rev + 1, rev + 1 + len, a[i]) - rev; } return 0; }

    2.

    #include <iostream> #include <algorithm> using namespace std; const int MAXN = 1e5; int n, b[MAXN]; struct node { int val, index; }a[MAXN]; int main() { cin >> n; for(int i = 1; i <= n; i++) cin >> a[i].val, a[i].index = i; sort(a + 1, a + 1 + n, cmp); int sum = 0; for(int i = 1; i <= n; i++) { if(a[i].val != a[i - 1].val || i == 1) { b[a[i].index] = ++sum; } else { b[a[i].index] = sum; } } return 0; }

    3.

    int Find (int x) { return lower_bound (v.begin (), v.end (), x) - v.begin (); } sort (v.begin (), v.end ()); v.erase (unique (v.begin (), v.end ()), v.end ());
    Processed: 0.024, SQL: 8