class edge
{
public:
int positon;
int height;
bool ifup;
edge() = default;
edge(int p, int h, bool dir)
{
positon = p;
height = h;
ifup = dir;
}
};
//给定一堆楼的位置参数与其高度即[startpositoon,endposition,height],然后求出这堆楼的整体轮廓就像photoshop里的一些抠图工具一样,只获取楼的边缘。
vector<vector<int>> getprofile(vector<vector<int>> build)
{
vector<vector<int>> res;
if (build.empty())
return res;
vector<edge*> arr(build.size() * 2);
int ind = 0;
for (auto var : build)
{
arr[ind++] = new edge(var[0], var[2], true);
arr[ind++] = new edge(var[1], var[2], false);
}
sort(arr.begin(), arr.end(), [](edge* l,edge* r){return l->positon < r->positon});
map<int, int> heimap;
map<int, int> promap;
for (int i = 0; i < arr.size(); i++)
{
if (arr[i]->ifup)
heimap[arr[i]->height]++;
else if (heimap[arr[i]->height] == 1)
heimap.erase(arr[i]->height);
else
heimap[arr[i]->height]--;
if (heimap.empty())
promap[arr[i]->positon] = 0;
else
promap[arr[i]->positon] = (heimap.rbegin())->first;
}
int start = 0;
int height = 0;
for (auto var : promap)
{
if (var.second != height)
{
if (height != 0)
{
res.push_back({ start, var.first, height });
}
start = var.first;
height = var.second;
}
}
return res;
}
求数组中子数组中和为aim的最长子数组的长度
int maxlen(vector<int> arr,int aim)
{
int sum = 0;
int res = 0;
unordered_map<int, int> presum;
presum[0] = -1;
for (int i = 0; i < arr.size(); i++)
{
sum += arr[i];
int p=0;
if (presum.find(sum - aim) != presum.end())
{
p = presum[sum - aim];
res = max(res, i - p);
}
if (presum.find(sum)==presum.end())
{
presum[sum]=i;
}
}
return res;
}
给定一个数组并对该数组进行划分,求一种最优的划分方法,使得划分得到的子数组中异或和为0的子数组最多
int maxdif(vector<int> arr)
{
vector<int> dp(arr.size(), 0);//动态规划方法求解该问题,对于数组中的每一个位置,该位置都会根据之前有没有当前异或和相同的异或和来决定,该位置是否有可能是最优划分中异或和为0的子数组。然后根据它是异或和为0的子数组和不是异或和为0的子数组的情况下子数组的数量的最大值来决定当前位置情况下的最优划分子数组的异或和为0的子数组的数量
unordered_map<int, int> search;
search[0] = -1;
int xor = 0;
for (int i = 0; i < arr.size(); i++)
{
xor ^= arr[i];
if (search.find(xor) != search.end())
{
dp[i] = search[xor] == -1 ? 1 : dp[search[xor]]+1;
}
if (i>0)
dp[i] = max(dp[i - 1], dp[i]);
search[xor] = i;
}
return dp[arr.size() - 1];
}