怎样减少时间复杂度?
2023-11-07 18:55:12
发布于:浙江
173阅读
0回复
0点赞
以下是结果全部正确的代码(就是有点超时pwp)
#include<bits/stdc++.h>
using namespace std;
const int MAXN = 3e4 + 5;
const int MAXS = 2e3 + 5;
const int MAXM = 7;
const int P = 1e9 + 7;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
template <typename T> void chkmax(T &x, T y) {x = max(x, y); }
template <typename T> void chkmin(T &x, T y) {x = min(x, y); }
template <typename T> void read(T &x) {
x = 0; int f = 1;
char c = getchar();
for (; !isdigit(c); c = getchar()) if (c == '-') f = -f;
for (; isdigit(c); c = getchar()) x = x * 10 + c - '0';
x *= f;
}
template <typename T> void write(T x) {
if (x < 0) x = -x, putchar('-');
if (x > 9) write(x / 10);
putchar(x % 10 + '0');
}
template <typename T> void writeln(T x) {
write(x);
puts("");
}
deque <vector <int>> que;
map <vector <int>, int> mp;
ll optl[2][MAXS]; int ways[2][MAXS];
vector <pair <int, int>> trans[MAXS][MAXM];
int n, m, tot, ans;
int lcost[MAXN][MAXM];
int ucost[MAXN][MAXM];
void update(int &x, int y) {
x += y;
if (x >= P) x -= P;
}
void update(int dest, int state, ll len, int cnt) {
if (optl[dest][state] == -1) {
optl[dest][state] = len;
ways[dest][state] = cnt;
} else {
if (optl[dest][state] < len) return;
if (optl[dest][state] > len) {
optl[dest][state] = len;
ways[dest][state] = cnt;
} else update(ways[dest][state], cnt);
}
}
void extend(int dest, int x, int y, int state, ll len, int cnt) {
for (auto t : trans[state][y]) {
int delta = (t.second >= 2) * ucost[x][y] + (t.second % 2) * lcost[x][y];
update(dest, t.first, len + delta, cnt);
}
}
void work(int x, int y, int now, int dest) {
if (x == n + 1) {
ans = ways[now][1];
return;
}
if (y == m + 1) {
work(x + 1, 1, now, dest);
return;
}
memset(optl[dest], -1, sizeof(optl[dest]));
for (int i = 1; i <= tot; i++)
if (optl[now][i] != -1) extend(dest, x, y, i, optl[now][i], ways[now][i]);
work(x, y + 1, dest, now);
}
void update(vector <int> a) {
if (mp.count(a)) return;
que.push_back(a), mp[a] = ++tot;
}
vector <int> normal(vector <int> a) {
static int col[MAXM]; int tot = 0;
memset(col, 0, sizeof(col));
for (auto &x : a)
if (x) {
if (!col[x]) col[x] = ++tot;
x = col[x];
}
return a;
}
vector <int> cipher() {
vector <int> ans;
for (int i = 0; i <= m; i++)
ans.push_back(i);
return ans;
}
int bitcount(vector <int> &a, int type) {
int ans = 0;
for (auto x : a)
if (x == type) ans++;
return ans;
}
vector <int> nbit(vector <int> a, int pos) {
a[pos] = m;
return normal(a);
}
vector <int> cbit(vector <int> a, int pos) {
a[pos] = a[pos - 1];
return normal(a);
}
vector <int> merge(vector <int> a, int pos) {
if (a[pos] == a[pos - 1]) return a;
int tmp = a[pos];
for (auto &x : a) if (x == tmp) x = a[pos - 1];
return normal(a);
}
void init() {
for (int i = 1; i <= (1 << m) - 1; i += 2) {
vector <int> now; now.push_back(0);
for (int j = 0; j <= m - 1; j++)
if (i & (1 << j)) now.push_back(now.back() + 1);
else now.push_back(now.back());
update(now);
}
while (!que.empty()) {
vector <int> s = que.front();
que.pop_front(); int now = mp[s];
for (int i = 1; i <= m; i++) {
if (i == 1) {
trans[now][i].emplace_back(now, 2);
if (bitcount(s, s[i]) != 1) {
update(nbit(s, i));
trans[now][i].emplace_back(mp[nbit(s, i)], 0);
}
} else {
trans[now][i].emplace_back(now, 2);
update(merge(s, i));
trans[now][i].emplace_back(mp[merge(s, i)], 3);
if (bitcount(s, s[i]) != 1) {
update(nbit(s, i));
trans[now][i].emplace_back(mp[nbit(s, i)], 0);
update(cbit(s, i));
trans[now][i].emplace_back(mp[cbit(s, i)], 1);
}
}
}
}
//cerr << mp.size() << endl;
}
int main() {
freopen("escape.in", "r", stdin);
freopen("escape.out", "w", stdout);
read(n), read(m), init();
for (int i = 1; i <= n; i++)
for (int j = 2; j <= m; j++)
read(lcost[i][j]);
for (int j = 1; j <= m; j++)
for (int i = 2; i <= n; i++)
read(ucost[i][j]);
memset(optl, -1, sizeof(optl));
for (int i = 1; i <= (1 << m) - 1; i += 2) {
vector <int> now;
now.push_back(0);
ll length = 0;
for (int j = 0; j <= m - 1; j++)
if (i & (1 << j)) now.push_back(now.back() + 1);
else {
now.push_back(now.back());
length += lcost[1][j + 1];
}
update(0, mp[now], length, 1);
}
work(2, 1, 0, 1);
writeln(ans);
return 0;
}
全部评论 4
666骗我提交
26分钟前 来自 广东
0666
2024-03-17 来自 广东
06
2024-02-03 来自 广东
06
2024-02-03 来自 浙江
0
有帮助,赞一个