wittyResry / myIssue

My issue mark down^_^ 欢迎吐槽,讨论~~
https://github.com/wittyResry/myIssue/issues
The Unlicense
5 stars 1 forks source link

C++函数式编程? #21

Open Resry opened 7 years ago

Resry commented 7 years ago
    vector<double> minimalExpectation(vector<int> x, vector<int> p) {
      int N = x.size();
      vector< pair<int, int> > A(N);
      for (int i = 0; i < N; ++i)
        A[i] = make_pair(x[i], p[i]);
      sort(A.begin(), A.end(), [&](pair<int, int> x, pair<int, int> y) {
        if (x.second != y.second)
          return x.second > y.second;
        return x.first < y.first;
      });
      //...
    }
Resry commented 7 years ago
   struct frac {
      int64_t top;
      int64_t bot;

      bool operator<(const frac& that) const {
        return top * that.bot < bot * that.top;
      }

      bool operator==(const frac& that) const {
        return top * that.bot == bot * that.top;
      }
    }; 
    struct event {
      int x, y;
      frac when;
      bool operator<(const event& that) const {
        if (when == that.when) {
          if (y != that.y)
            return y > that.y;
          return x > that.x;
        }
        return when < that.when;
      }
    };
Resry commented 7 years ago
      vector<event> E;
      E.reserve(N * N);
      for (int i = 0; i < N; ++i)
        for (int j = i + 1; j < N; ++j)
          if (A[i].second != A[j].second)
            E.emplace_back(event{i, j, frac{1LL * A[j].first * A[j].second - 1LL * A[i].first * A[i].second, A[i].second - A[j].second}});