離散化思想
將已知數值進行排序,從而獲取每個數值在已知數值中的地位。
數值離散化
struct Discretization
{
vector<int> mp;
int len;
void clear(){mp.clear();len=0;}
void add(int val){mp.push_back(val);}
void discrete()
{
sort(mp.begin(),mp.end());
mp.resize(unique(mp.begin(),mp.end())-mp.begin());
len=mp.size();
}
int getPos(int val)
{
int pos=lower_bound(mp.begin(),mp.end(),val)-mp.begin();
if(pos==len)return -1;
else return pos;
}
int operator[](int pos){return mp[pos];}
}disc;