解題思路
兩個平移後重合的凸包,其每個頂點都必須出現在兩點集中;因此枚舉平移向量v,考慮S1與S2−v的交集,就涵蓋所有答案。以所有點對的差向量分組,每組正是該平移下的共同點。
固定多邊形字典序最小頂點p,只考慮字典序在p之後的點,它們都在同一半平面,依對p的極角排序。dp[i][j]用位元集合保存由p出發、最後兩頂點i,j的可行兩倍面積。三角形以叉積初始化。
若i,j,k形成嚴格左轉,且p,j,k亦正向,便可加入k,面積增加cross(p,j,k),用位元左移後OR更新。角度序與所有左轉保證形成凸多邊形,閉合邊至p也合法。匯總正面積位元,依序輸出;坐標上限20使兩倍面積最多800。
令各平移交集大小為qv、s=min(n,m),時間O(Σqv⁴·ceil(801/word)),Σqv=nm,故上界O(nm·s³·ceil(801/word));工作DP O(s²·801/word),分組O(nm)。
Step-by-step animation
平移交集與凸多邊形面積位元DP|兩組點都為(0,0),(2,0),(0,2),平移0的共同點就是三點。
步驟 1 / 3
共同三角形
兩組點都為(0,0),(2,0),(0,2),平移0的共同點就是三點。
(0,0)
(2,0)
(0,2)
- 共同點數
- 3
#include <bits/stdc++.h>
using namespace std;
struct P {
int x, y;
bool operator<(const P &o) const {
return tie(x, y) < tie(o.x, o.y);
}
};
int cross(P a, P b, P c) {
return (b.x - a.x) * (c.y - a.y) - (b.y - a.y) * (c.x - a.x);
}
int main() {
int n, m;
cin >> n >> m;
vector<P> a(n), b(m);
for (auto &p : a)
cin >> p.x >> p.y;
for (auto &p : b)
cin >> p.x >> p.y;
map<pair<int, int>, vector<P>> groups;
for (auto p : a)
for (auto q : b)
groups[{q.x - p.x, q.y - p.y}].push_back(p);
bitset<801> answer;
for (auto &entry : groups) {
auto points = entry.second;
sort(points.begin(), points.end());
for (int base = 0; base + 2 < (int)points.size(); base++) {
P p = points[base];
vector<P> v(points.begin() + base + 1, points.end());
sort(v.begin(), v.end(), [&](P x, P y) {
int z = cross(p, x, y);
if (z)
return z > 0;
return (x.x - p.x) * (x.x - p.x) + (x.y - p.y) * (x.y - p.y) <
(y.x - p.x) * (y.x - p.x) + (y.y - p.y) * (y.y - p.y);
});
int s = v.size();
vector<vector<bitset<801>>> d(s, vector<bitset<801>>(s));
for (int i = 0; i < s; i++)
for (int j = i + 1; j < s; j++) {
int ar = cross(p, v[i], v[j]);
if (ar > 0)
d[i][j].set(ar);
}
for (int j = 1; j < s; j++)
for (int i = 0; i < j; i++)
if (d[i][j].any()) {
answer |= d[i][j];
for (int k = j + 1; k < s; k++) {
int ar = cross(p, v[j], v[k]);
if (ar > 0 && cross(v[i], v[j], v[k]) > 0)
d[j][k] |= d[i][j] << ar;
}
}
}
}
answer.reset(0);
cout << answer.count() << "\n";
for (int i = 1; i <= 800; i++)
if (answer[i])
cout << i << "\n";
}
先試著自己完成,再切換語言比對解法。參考程式可複製到題目編輯器。