歡迎點這裏閱讀QvQ
250
求
Solution
轉換成
Code
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define F first
#define S second
typedef long long LL;
typedef pair<int, int> pii;
LL gao(LL x) {
LL t = 0;
for (int i = 40; i >= 0; --i) {
LL p = 1ll << i;
if (x == 3 || !x) return t;
if (x == 2) return t + 3;
if (x == 1) return t + 1;
if (p <= x) {
if ((x - p + 1) & 1) t += p;
x ^= p;
}
}
return t;
}
struct EllysXors {
long long getXor(long long L, long long R) {
return gao(R) ^ gao(L - 1);
}
};
500
Description
在一個橫座標軸上有若干個垂直的線段.每個線段之間的距離爲
線段之間的運行速度爲speed[i],在線段上垂直運動的速度爲
請問最快多久可以到達目的地
Solution
很容易想到dp,dp[i][j]表示過了前i個線段,當前在j的答案,暴力轉移複雜度爆炸,容易發現,在每個線段的答案沿高度是單調的,轉移時維護上一次轉移的位置即可。
Code
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define F first
#define S second
typedef long long LL;
typedef pair<int, int> pii;
const double inf = 1e20;
double dp[2][100005];
struct EllysRivers {
double getMin(int length, int walk, vector <int> width, vector <int> speed) {
int n = width.size();
for (int i = 0; i <= length; ++i) dp[0][i] = (double)i / walk;
for (int i = 1; i <= n; ++i) {
for (int j = 0; j <= length; ++j) dp[i & 1][j] = inf;
int last = 0;
for (int j = 0; j <= length; ++j)
for (int k = last; k <= j; ++k) {
double t = dp[(i - 1) & 1][k] + hypot(width[i - 1], j - k) / speed[i - 1];
if (t > dp[i & 1][j]) break;
dp[i & 1][j] = t;
last = k;
}
}
return dp[n & 1][length];
}
};