F皮亚诺曲线距离
找规律的题目,应该有各种各样的方法来做
(代码写完才发现我的x,y和题目是反过来的qwq, 不过都一样)
(顺带吐槽一下,为啥不用写高精度就能过,题目里只是要求答案小于
1
0
18
10^{18}
1018,
x
1
,
x
2
,
y
1
,
y
2
x_1, x_2, y_1, y_2
x1,x2,y1,y2只要小于
3
100
3^{100}
3100就行了啊)不过不用写高精度我也就不写了
显然我们可以分别求两个点到原点的距离,然后相减即可。
我们发现,对于n阶皮亚诺曲线,它是由9个n-1阶皮亚诺曲线组合而成,我们可以把n-1阶皮亚诺曲线视为一个整体
我的做法是把矩阵分成4种模式, 起点的x坐标小于终点则第一位为0,起点的y坐标小于终点则第二位0
如图
红色箭头所在的矩阵为0型,绿色为1型,橙色为3型
通过判断n阶曲线的类型,就知道对应的格子之前有几个n-1阶曲线,然后再继续做相应的n-1阶曲线即可
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int model[4][3][3] = { //四种模式的遍历顺序
{
{1, 6, 7},
{2, 5, 8},
{3, 4, 9}
},
{
{7, 6, 1},
{8, 5, 2},
{9, 4, 3}
},
{
{3, 4, 9},
{2, 5, 8},
{1, 6, 7}
},
{
{9, 4, 3},
{8, 5, 2},
{7, 6, 1}
}
};
const int nxt[4][3][3] = { // 在该模式的该位置的下一阶曲线的类型
{
{0, 2, 0},
{1, 3, 1},
{0, 2, 0}
},
{
{1, 3, 1},
{0, 2, 0},
{1, 3, 1}
},
{
{2, 0, 2},
{3, 1, 3},
{2, 0, 2}
},
{
{3, 1, 3},
{2, 0, 2},
{3, 1, 3}
}
};
LL k, x_1, y_1, x_2, y_2;
LL calc(LL x, LL y) {
int K = 1, mod = 0;
LL ret = 0, w = 1;
while (w * 3 < max(x, y) + 1) ++K, w *= 3;
while (K--) {
int i = 0, j = 0;
while (x >= w * i) ++i;
while (y >= w * j) ++j;
--i, --j;
ret += (model[mod][i][j] - 1) * w * w;
mod = nxt[mod][i][j];
x -= w * i, y -= w * j, w /= 3;
}
return ret;
}
int main() {
cin >> k;
cin >> y_1 >> x_1;
cin >> y_2 >> x_2;
LL A = calc(x_1, y_1);
LL B = calc(x_2, y_2);
cout << (A > B ? A - B : B - A);
return 0;
}
G 出租车
我把每个路口拆成4个点,表示车开到如图数字所在位置所需的时间
然后跑最短路就可以了
我看到网上有一个更好的建图,直接用路的中点来建边比拆点来得容易多了
#include <bits/stdc++.h>
using namespace std;
inline int read() {
int ret = 0, f = 1; char ch = getchar();
for (; !isdigit(ch); ch = getchar()) if (ch == '-') f = -f;
for (; isdigit(ch); ch = getchar()) ret = ret * 10 + ch - 48;
return ret * f;
}
const int maxn = 105;
const int xx[4] = { -1, 0, 1, 0 };
const int yy[4] = { 0, 1, 0, -1 };
const double INF = 10000000000005ll;
bool vis[maxn][maxn][4];
double T = 0, dis[maxn][maxn][4];
struct node { int x, y, f; } a[6];
inline bool operator ==(node a, node b) { return a.x == b.x && a.y == b.y; }
struct Node { double d; node x; };
inline bool operator <(Node a, Node b) { return a.d < b.d; }
inline bool operator >(Node a, Node b) { return a.d > b.d; }
int N, M, Q, h[maxn], w[maxn], g[maxn][maxn], r[maxn][maxn];
inline double wait(node u, double tim) {
tim += T;
tim -= floor(tim / (g[u.x][u.y] + r[u.x][u.y])) * (g[u.x][u.y] + r[u.x][u.y]);
if (u.f & 1) { // 东西向 初始红
if (abs(tim - g[u.x][u.y]) < 1e-6) return 0;
if (tim > g[u.x][u.y]) return 0;
else return g[u.x][u.y] - tim;
}
else {
if (abs(tim - g[u.x][u.y]) < 1e-6) return r[u.x][u.y];
if (tim < g[u.x][u.y]) return 0;
else return r[u.x][u.y] + g[u.x][u.y] - tim;
}
}
inline double getDistance(node a, node b) {
return (a.x == b.x) ? abs(w[a.y] - w[b.y]) : abs(h[a.x] - h[b.x]);
}
double Calc() {
if (a[0] == a[2] && a[1] == a[3]) return 0;
for (int i = 1; i <= N; ++i)
for (int j = 1; j <= M; ++j)
for (int k = 0; k < 4; ++k) {
dis[i][j][k] = INF;
vis[i][j][k] = 0;
}
a[1].f = (a[0].x == a[1].x) ? ( (a[1].y > a[0].y) ? 3 : 1 ) : ( (a[1].x > a[0].x) ? 0 : 2 );
dis[a[1].x][a[1].y][a[1].f] = getDistance(a[0], a[1]) * 0.5;
priority_queue < Node, vector<Node>, greater<Node> > Q;
Node fuck; // 因为官网的c++不支持 Q.push({ dis[a[1].x][a[1].y][a[1].f], a[1] });
//所以临时改的,言辞比较激烈
fuck.d = dis[a[1].x][a[1].y][a[1].f];
fuck.x = a[1];
Q.push(fuck);
while (!Q.empty()) {
node u = Q.top().x, v;
Q.pop();
if (vis[u.x][u.y][u.f]) continue;
vis[u.x][u.y][u.f] = 1;
// 掉头
v.x = u.x + xx[u.f], v.y = u.y + yy[u.f], v.f = (u.f + 2) % 4;
if (v.x >= 1 && v.x <= N && v.y >= 1 && v.y <= M) {
if (dis[v.x][v.y][v.f] > dis[u.x][u.y][u.f] + getDistance(u, v)) {
dis[v.x][v.y][v.f] = dis[u.x][u.y][u.f] + getDistance(u, v);
fuck.d = dis[v.x][v.y][v.f];
fuck.x = v;
Q.push(fuck);
}
}
// 右转
v.x = u.x + xx[(u.f + 3) % 4], v.y = u.y + yy[(u.f + 3) % 4], v.f = (u.f + 1) % 4;
if (v.x >= 1 && v.x <= N && v.y >= 1 && v.y <= M) {
if (dis[v.x][v.y][v.f] > dis[u.x][u.y][u.f] + getDistance(u, v)) {
dis[v.x][v.y][v.f] = dis[u.x][u.y][u.f] + getDistance(u, v);
fuck.d = dis[v.x][v.y][v.f];
fuck.x = v;
Q.push(fuck);
}
}
// 左转
double t = wait(u, dis[u.x][u.y][u.f]);
v.x = u.x + xx[(u.f + 1) % 4], v.y = u.y + yy[(u.f + 1) % 4], v.f = (u.f + 3) % 4;
if (v.x >= 1 && v.x <= N && v.y >= 1 && v.y <= M) {
if (dis[v.x][v.y][v.f] > dis[u.x][u.y][u.f] + getDistance(u, v) + t) {
dis[v.x][v.y][v.f] = dis[u.x][u.y][u.f] + getDistance(u, v) + t;
fuck.d = dis[v.x][v.y][v.f];
fuck.x = v;
Q.push(fuck);
}
}
// 直行
t = wait(u, dis[u.x][u.y][u.f]);
v.x = u.x + xx[(u.f + 2) % 4], v.y = u.y + yy[(u.f + 2) % 4], v.f = u.f;
if (v.x >= 1 && v.x <= N && v.y >= 1 && v.y <= M) {
if (dis[v.x][v.y][v.f] > dis[u.x][u.y][u.f] + getDistance(u, v) + t) {
dis[v.x][v.y][v.f] = dis[u.x][u.y][u.f] + getDistance(u, v) + t;
fuck.d = dis[v.x][v.y][v.f];
fuck.x = v;
Q.push(fuck);
}
}
}
a[3].f = (a[2].x == a[3].x) ? ( (a[3].y > a[2].y) ? 3 : 1 ) : ( (a[3].x > a[2].x) ? 0 : 2 );
return dis[a[3].x][a[3].y][a[3].f] - getDistance(a[2], a[3]) * 0.5;
}
int main() {
N = read(), M = read();
for (int i = 2; i <= N; ++i) h[i] = read();
for (int i = 2; i <= M; ++i) w[i] = read();
for (int i = 1; i <= N; ++i)
for (int j = 1; j <= M; ++j) g[i][j] = read();
for (int i = 1; i <= N; ++i)
for (int j = 1; j <= M; ++j) r[i][j] = read();
a[0].x = read(), a[0].y = read(), a[1].x = read(), a[1].y = read();
a[4] = a[0], a[5] = a[1];
for (Q = read(); Q--; ) {
a[2].x = read(), a[2].y = read();
a[3].x = read(), a[3].y = read();
T += Calc();
a[0] = a[2], a[1] = a[3];
a[2].x = read(), a[2].y = read();
a[3].x = read(), a[3].y = read();
T += Calc();
a[0] = a[2], a[1] = a[3];
}
a[2] = a[4], a[3] = a[5];
T += Calc();
printf("%.1lf", T);
return 0;
}