
可以化简为求n条线段的最大覆盖问题,需要注意的是对于实数而言。
#include <iostream>
#include <vector>
#include <string>
#include <queue>
#include <map>
#include <string.h>
using namespace std; class PiecewiseLinearFunction {
private:
map<int, int> Y2i;
int value[];
public:
int maximumSolutions(vector <int> Y) {
for (int i = ; i < Y.size(); i++) {
Y2i[Y[i]] = ;
}
int no = ;
for (map<int, int>::iterator it = Y2i.begin(); it != Y2i.end(); it++) {
//cout << it->first << endl;
it->second = * no++;
} memset(value, , sizeof(value)); for (int i = ; i < Y.size() - ; i++) {
int d = Y[i + ] - Y[i];
if (d == ) {
return -;
} else {
d = d / abs(d);
int begin = Y2i[Y[i]];
int end = Y2i[Y[i + ]];
while (begin != end) {
value[begin]++;
begin += d;
}
}
}
value[Y2i[Y.back()]]++; int ans = ;
for (int i = ; i < ; i++) {
ans = max(ans, value[i]);
}
return ans;
}
};