蔚来杯2022牛客暑期多校训练营2 GJK
G
题解
知识点:思维。
表示最小上升子序列分划数 (Dilworth 定理)
因此将排列构造多个长度为 递增串,并且递增串的最大值一定递减,形如 789456123
即可。
时间复杂度
空间复杂度
代码
#include <bits/stdc++.h> #define ll long long using namespace std; bool solve() { int n; cin >> n; int sn = ceil(sqrt(n)); for (int i = 1;i <= n;i += sn) { for (int j = min(i + sn - 1, n);j >= i;j--) cout << j << ' '; } cout << '\n'; return true; } int main() { std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); int t = 1; cin >> t; while (t--) { if (!solve()) cout << -1 << '\n'; } return 0; }
J
题解
方法一
知识点:线性回归。
显然运用线性回归公式:
注意精度。
时间复杂度
空间复杂度
方法二
知识点:三分。
如果不知道线性回归,可以运用三分斜率 ,已知过 求 ,同样注意精度。
时间复杂度
空间复杂度
代码
方法一
#include <bits/stdc++.h> #include <cstdio> #include <cctype> #define ll long long using namespace std; namespace GTI { char gc(void) { const int S = 1 << 16; static char buf[S], *s = buf, *t = buf; if (s == t) t = buf + fread(s = buf, 1, S, stdin); if (s == t) return EOF; return *s++; } int gti(void) { int a = 0, b = 1, c = gc(); for (; !isdigit(c); c = gc()) b ^= (c == '-'); for (; isdigit(c); c = gc()) a = a * 10 + c - '0'; return b ? a : -a; } } using GTI::gti; int a[100007]; bool solve() { int n = gti(); double xavg = (1 + n) / 2.0, yavg = 0; ll Au = 0, Av = 0; for (int i = 1;i <= n;i++) { a[i] = gti(); yavg += a[i]; Au += 1LL * i * a[i]; Av += 1LL * i * i; } yavg /= n; double A = (Au - n * xavg * yavg) / (Av - n * xavg * xavg); double B = yavg - A * xavg; double ans = 0; for (int i = 1;i <= n;i++) ans += (A * i + B - a[i]) * (A * i + B - a[i]); cout << fixed << setprecision(9) << ans << '\n'; return true; } int main() { std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); int t = 1; t = gti(); while (t--) { if (!solve()) cout << -1 << '\n'; } return 0; }
方法二
没写
K
题解
知识点:计数dp。
表示为 放了 个括号, 与 的lcs为 ,还有 个未匹配的左括号。
转移方程为:
int t = j + (s[j + 1] == '('); dp[i + 1][t][k + 1] = (dp[i][j][k] + dp[i + 1][t][k + 1]) % mod; t = j + (s[j + 1] == ')'); if (k) dp[i + 1][t][k - 1] = (dp[i][j][k] + dp[i + 1][t][k - 1]) % mod;
上面是下一个位置放左括号的情况,下面是下一个位置放右括号的情况。
但是,通常转移方程都是当前状态是目标状态,这里是当前状态 是上一次状态,而目标状态并非当前状态。原因是如果知道当前状态,是找不到上一次状态的,因为不知道当前括号是不是与lcs对应的括号,因此转换递推方式,遍历已知推未知,而非遍历未知找已知推。
时间复杂度
空间复杂度
代码
#include <bits/stdc++.h> #define ll long long using namespace std; const int mod = 1e9 + 7; int dp[207][207][207]; bool solve() { memset(dp, 0, sizeof(dp)); int n, m; cin >> n >> m; string s; cin >> s; s = '?' + s + '?'; dp[0][0][0] = 1; for (int i = 0;i <= m - 1;i++) { for (int j = 0;j <= min(i, n);j++) { for (int k = 0;k <= i;k++) { int t = j + (s[j + 1] == '('); dp[i + 1][t][k + 1] = (dp[i][j][k] + dp[i + 1][t][k + 1]) % mod; t = j + (s[j + 1] == ')'); if (k) dp[i + 1][t][k - 1] = (dp[i][j][k] + dp[i + 1][t][k - 1]) % mod; } } } cout << dp[m][n][0] << '\n'; return true; } int main() { std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); int t = 1; cin >> t; while (t--) { if (!solve()) cout << -1 << '\n'; } return 0; }
本文来自博客园,作者:空白菌,转载请注明原文链接:https://www.cnblogs.com/BlankYang/p/16522803.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧