• 2022牛客多校第二场解题报告


    D.Link with Game Glitch

    思路:对所有物品建图,二分w,边权是log(w*c/a).然后判负环,一直到没有负环为止

    技巧:1.乘积->加减法(log) 2.图上二分答案 3.spfa在整个图不连通情况下判负环

    代码:

    1. #include
    2. #define int long long
    3. #define IOS ios::sync_with_stdio(false), cin.tie(0)
    4. #define ll long long
    5. #define ull unsigned long long
    6. #define PII pair
    7. #define PDI pair
    8. #define PDD pair
    9. #define debug(a) cout << #a << " = " << a << endl
    10. #define all(x) (x).begin(), (x).end()
    11. #define mem(x, y) memset((x), (y), sizeof(x))
    12. #define lbt(x) (x & (-x))
    13. #define SZ(x) ((x).size())
    14. #define inf 0x3f3f3f3f
    15. #define INF 0x3f3f3f3f3f3f3f3f
    16. // namespace nqio{const unsigned R = 4e5, W = 4e5; char *a, *b, i[R], o[W], *c = o, *d = o + W, h[40], *p = h, y; bool s; struct q{void r(char &x){x = a == b && (b = (a = i) + fread(i, 1, R, stdin), a == b) ? -1 : *a++;} void f(){fwrite(o, 1, c - o, stdout); c = o;} ~q(){f();}void w(char x){*c = x;if (++c == d) f();} q &operator >>(char &x){do r(x);while (x <= 32); return *this;} q &operator >>(char *x){do r(*x); while (*x <= 32); while (*x > 32) r(*++x); *x = 0; return *this;} template q&operator>>(t &x){for (r(y),s = 0; !isdigit(y); r(y)) s |= y == 45;if (s) for (x = 0; isdigit(y); r(y)) x = x * 10 - (y ^ 48); else for (x = 0; isdigit(y); r(y)) x = x * 10 + (y ^ 48); return *this;} q &operator <<(char x){w(x);return *this;}q &operator<< (char *x){while (*x) w(*x++); return *this;}q &operator <<(const char *x){while (*x) w(*x++); return *this;}template q &operator<< (t x) {if (!x) w(48); else if (x < 0) for (w(45); x; x /= 10) *p++ = 48 | -(x % 10); else for (; x; x /= 10) *p++ = 48 | x % 10; while (p != h) w(*--p);return *this;}}qio; }using nqio::qio;
    17. using namespace std;
    18. const int N = 4010, M = 1e6 + 10;
    19. const long double eps = 1e-10;
    20. int n, m;
    21. int h[N], v[M], to[M], tot;
    22. int a[N], b[N], c[N], d[N];
    23. bool vis[N];
    24. long double w[M], dis[N];
    25. int cnt[N]; //cnt[x]表示从1到x的最短路径包含的边数,若cnt[x]>=n,则由抽屉原理判定为有负环
    26. void add(int a, int b, long double c) {
    27. w[++tot] = c, v[tot] = b, to[tot] = h[a], h[a] = tot;
    28. }
    29. bool check(long double x) {
    30. for (int i = 0; i <= 1500; ++i) dis[i] = 0, vis[i] = 0, h[i] = 0, cnt[i] = 0;
    31. tot = 0;
    32. for (int i = 1; i <= m; ++i) {
    33. long double ww = log(a[i]) - log(c[i]) - log(x);
    34. add(b[i], d[i], ww);
    35. }
    36. queue<int> q;
    37. for (int i = 1; i <= n; ++i) q.emplace(i);
    38. while (q.size()) {
    39. int x = q.front(); q.pop();
    40. vis[x] = 0;
    41. for (int i = h[x]; i; i = to[i]) {
    42. int y = v[i];
    43. long double z = w[i];
    44. if (dis[y] > dis[x] + z) {
    45. dis[y] = dis[x] + z;
    46. cnt[y] = cnt[x] + 1;
    47. if (cnt[y] >= n) return false;
    48. if (!vis[y]) q.emplace(y), vis[y] = 1;
    49. }
    50. }
    51. }
    52. return true;
    53. }
    54. signed main() {
    55. IOS;
    56. cin >> n >> m;
    57. for (int i = 1; i <= m; ++i) cin >> a[i] >> b[i] >> c[i] >> d[i];
    58. long double l = 0, r = 1;
    59. for (int i = 1; i <= 100; ++i) {
    60. long double mid = (l + r) / 2;
    61. if (check(mid)) l = mid;
    62. else r = mid;
    63. }
    64. cout << fixed << setprecision(20);
    65. cout << l << '\n';
    66. }

    E.Falfa with Substring

    思路:先考虑至少有k个bit的情况,假设为f(k),有f(k)=C_{n-2k}^{k}*26^{n-3k},然后设g(k)为恰好有k个bit的情况,那么有f(k)=\sum_{k\leq j\leq n}C_{j}^{k}*g(j),由二项式反演可得g(k)=\sum_{k\leq j\leq n}(-1)^{j-k}C_{j}^{k}f(j),然后变化一些卷积即可.(不会卷qwq)

    这里我详细讲一下,f(k)的求法.

     

    技巧:"恰好"->容斥, 求固定有k个的方法.

    G.Link with Monotonic Subsequence

    思路:构造即可

    代码:

    1. #include
    2. #include
    3. #include
    4. #include
    5. #include
    6. #include
    7. #include
    8. #include
    9. #include
    10. #include
    11. #include
    12. #include
    13. #include
    14. #include
    15. #include
    16. #include
    17. #include
    18. #define IOS ios::sync_with_stdio(false), cin.tie(0)
    19. #define int long long
    20. #define ll long long
    21. #define ull unsigned long long
    22. #define PII pair
    23. #define PDI pair
    24. #define PDD pair
    25. #define debug(a) cout << #a << " = " << a << endl
    26. #define all(x) (x).begin(), (x).end()
    27. #define mem(x, y) memset((x), (y), sizeof(x))
    28. #define lbt(x) (x & (-x))
    29. #define SZ(x) ((x).size())
    30. #define wtn(x) wt(x), printf("\n")
    31. #define wtt(x) wt(x), printf(" ")
    32. #define inf 0x3f3f3f3f
    33. #define INF 0x3f3f3f3f3f3f3f3f
    34. #define MOD 1000000007
    35. #define eps 1e-8
    36. int T = 1;
    37. using namespace std;
    38. inline int rd() {
    39. int x = 0, y = 1; char c = getchar();
    40. while (!isdigit(c)) { if (c == '-') y = -1; c = getchar(); }
    41. while (isdigit(c)) { x = (x << 3) + (x << 1) + (c ^ 48); c = getchar(); }
    42. return x *= y;
    43. }
    44. inline void wt(int x) {
    45. if (x < 0)x = -x, putchar('-'); ll sta[100], top = 0;
    46. do sta[top++] = x % 10, x /= 10; while (x);
    47. while (top) putchar(sta[--top] + '0');
    48. }
    49. void solve() {
    50. int n = rd(), block = ceil(sqrt(n));
    51. for (int i = 1; i * block <= n; ++i)
    52. for (int j = i * block; j >= (i - 1) * block + 1; --j) wtt(j);
    53. int k = n / block;
    54. for (int i = n; i >= k * block + 1; --i) wtt(i);
    55. puts("");
    56. }
    57. signed main() {
    58. // IOS;
    59. // cin >> T;
    60. T = rd();
    61. for (int i = 1; i <= T; ++i) { solve();}
    62. }

    J.Link with Arithmetic Progression

    思路:线性回归,注意精度

    代码:

    1. #include
    2. #define IOS ios::sync_with_stdio(false), cin.tie(0)
    3. #define int long long
    4. #define ll long long
    5. #define ull unsigned long long
    6. #define PII pair
    7. #define PDI pair
    8. #define PDD pair
    9. #define debug(a) cout << #a << " = " << a << endl
    10. #define all(x) (x).begin(), (x).end()
    11. #define mem(x, y) memset((x), (y), sizeof(x))
    12. #define lbt(x) (x & (-x))
    13. #define SZ(x) ((x).size())
    14. #define wtn(x) wt(x), printf("\n")
    15. #define wtt(x) wt(x), printf(" ")
    16. #define inf 0x3f3f3f3f
    17. #define INF 0x3f3f3f3f3f3f3f3f
    18. #define MOD 1000000007
    19. #define eps 1e-8
    20. int T = 1;
    21. using namespace std;
    22. inline int rd() {
    23. int x = 0, y = 1; char c = getchar();
    24. while (!isdigit(c)) { if (c == '-') y = -1; c = getchar(); }
    25. while (isdigit(c)) { x = (x << 3) + (x << 1) + (c ^ 48); c = getchar(); }
    26. return x *= y;
    27. }
    28. inline void wt(int x) {
    29. if (x < 0)x = -x, putchar('-'); ll sta[100], top = 0;
    30. do sta[top++] = x % 10, x /= 10; while (x);
    31. while (top) putchar(sta[--top] + '0');
    32. }
    33. const int N = 1e6 + 10;
    34. int n;
    35. long double x[N], y[N], xy[N], d[N], dd[N], s[N];
    36. void solve() {
    37. long double b, a, avrx = 0, avry = 0, sumx = 0, sumy = 0;
    38. // n = rd();
    39. cin >> n;
    40. for (int i = 1; i <= n; ++i) cin >> y[i], sumx += i, sumy += y[i];
    41. avrx = sumx / n, avry = sumy / n;
    42. long double sumxy = 0, sumxx = 0;
    43. for (int i = 1; i <= n; ++i) sumxy += i * y[i], sumxx += i * i;
    44. b = (sumxy - n * avrx * avry) / (sumxx - n * avrx * avrx);
    45. a = avry - b * avrx;
    46. long double ans = 0;
    47. for (int i = 1; i <= n; ++i) {
    48. long double x = b * i + a - y[i];
    49. ans += x * x;
    50. }
    51. cout << fixed << setprecision(20);
    52. cout << ans << endl;
    53. }
    54. signed main() {
    55. IOS;
    56. cin >> T;
    57. for (int i = 1; i <= T; ++i) { solve();}
    58. }

    K.Link with Bracket Sequence I

    思路:f[i][j][k]表示前i个b字符串匹配了前j个a字符串,还剩下k个左括号没匹配的情况
    f[i][j][k] -> f[i+1][j+(s[j+1]=='(')][k+1]
    f[i][j][k] -> f[i+1][j+(s[j+1]==')')][k-1]
    技巧:两个序列->lcs

    代码:

    1. #include
    2. #define int long long
    3. #define IOS ios::sync_with_stdio(false), cin.tie(0)
    4. #define ll long long
    5. #define ull unsigned long long
    6. #define PII pair
    7. #define PDI pair
    8. #define PDD pair
    9. #define debug(a) cout << #a << " = " << a << endl
    10. #define all(x) (x).begin(), (x).end()
    11. #define mem(x, y) memset((x), (y), sizeof(x))
    12. #define lbt(x) (x & (-x))
    13. #define SZ(x) ((x).size())
    14. #define inf 0x3f3f3f3f
    15. #define INF 0x3f3f3f3f3f3f3f3f
    16. namespace nqio{const unsigned R = 4e5, W = 4e5; char *a, *b, i[R], o[W], *c = o, *d = o + W, h[40], *p = h, y; bool s; struct q{void r(char &x){x = a == b && (b = (a = i) + fread(i, 1, R, stdin), a == b) ? -1 : *a++;} void f(){fwrite(o, 1, c - o, stdout); c = o;} ~q(){f();}void w(char x){*c = x;if (++c == d) f();} q &operator >>(char &x){do r(x);while (x <= 32); return *this;} q &operator >>(char *x){do r(*x); while (*x <= 32); while (*x > 32) r(*++x); *x = 0; return *this;} template<typename t> q&operator>>(t &x){for (r(y),s = 0; !isdigit(y); r(y)) s |= y == 45;if (s) for (x = 0; isdigit(y); r(y)) x = x * 10 - (y ^ 48); else for (x = 0; isdigit(y); r(y)) x = x * 10 + (y ^ 48); return *this;} q &operator <<(char x){w(x);return *this;}q &operator<< (char *x){while (*x) w(*x++); return *this;}q &operator <<(const char *x){while (*x) w(*x++); return *this;}template<typename t> q &operator<< (t x) {if (!x) w(48); else if (x < 0) for (w(45); x; x /= 10) *p++ = 48 | -(x % 10); else for (; x; x /= 10) *p++ = 48 | x % 10; while (p != h) w(*--p);return *this;}}qio; }using nqio::qio;
    17. using namespace std;
    18. const int N = 1e6 + 10, MOD = 1e9 + 7;
    19. char s[N];
    20. int n, m, f[210][210][210];
    21. //f[i][j][k]表示前i个b字符串匹配了前j个a字符串,还剩下k个左括号没匹配的情况
    22. //f[i][j][k] -> f[i+1][j+(s[j+1]=='(')][k+1]
    23. //f[i][j][k] -> f[i+1][j+(s[j+1]==')')][k-1]
    24. //
    25. void solve() {
    26. qio >> n >> m >> s + 1;
    27. for (int i = 0; i <= m; ++i)
    28. for (int j = 0; j <= n; ++j)
    29. for (int k = 0; k <= m; ++k) f[i][j][k] = 0;
    30. f[0][0][0] = 1;
    31. for (int i = 0; i <= m; ++i)
    32. for (int j = 0; j <= min(i, n); ++j)
    33. for (int k = 0; k <= i; ++k) {
    34. (f[i + 1][j + (s[j + 1] == '(')][k + 1] += f[i][j][k]) %= MOD;
    35. if (k) (f[i + 1][j + (s[j + 1] == ')')][k - 1] += f[i][j][k]) %= MOD;
    36. }
    37. qio << f[m][n][0] << "\n";
    38. }
    39. signed main() {
    40. int T;
    41. qio >> T;
    42. while (T--) solve();
    43. }

    L.Link with Level Editor I

    思路:建图(i, u) -> (i + 1, v), 然后dp, f[i][j]表示到达第i个世界第j个点最大的世界是多少

    代码:

    1. #include
    2. #define int long long
    3. #define IOS ios::sync_with_stdio(false), cin.tie(0)
    4. #define ll long long
    5. #define ull unsigned long long
    6. #define PII pair
    7. #define PDI pair
    8. #define PDD pair
    9. #define debug(a) cout << #a << " = " << a << endl
    10. #define all(x) (x).begin(), (x).end()
    11. #define mem(x, y) memset((x), (y), sizeof(x))
    12. #define lbt(x) (x & (-x))
    13. #define SZ(x) ((x).size())
    14. #define inf 0x3f3f3f3f
    15. #define INF 0x3f3f3f3f3f3f3f3f
    16. namespace nqio{const unsigned R = 4e5, W = 4e5; char *a, *b, i[R], o[W], *c = o, *d = o + W, h[40], *p = h, y; bool s; struct q{void r(char &x){x = a == b && (b = (a = i) + fread(i, 1, R, stdin), a == b) ? -1 : *a++;} void f(){fwrite(o, 1, c - o, stdout); c = o;} ~q(){f();}void w(char x){*c = x;if (++c == d) f();} q &operator >>(char &x){do r(x);while (x <= 32); return *this;} q &operator >>(char *x){do r(*x); while (*x <= 32); while (*x > 32) r(*++x); *x = 0; return *this;} template<typename t> q&operator>>(t &x){for (r(y),s = 0; !isdigit(y); r(y)) s |= y == 45;if (s) for (x = 0; isdigit(y); r(y)) x = x * 10 - (y ^ 48); else for (x = 0; isdigit(y); r(y)) x = x * 10 + (y ^ 48); return *this;} q &operator <<(char x){w(x);return *this;}q &operator<< (char *x){while (*x) w(*x++); return *this;}q &operator <<(const char *x){while (*x) w(*x++); return *this;}template<typename t> q &operator<< (t x) {if (!x) w(48); else if (x < 0) for (w(45); x; x /= 10) *p++ = 48 | -(x % 10); else for (; x; x /= 10) *p++ = 48 | x % 10; while (p != h) w(*--p);return *this;}}qio; }using nqio::qio;
    17. using namespace std;
    18. const int N = 1e4 + 10, M = 2e3 + 10;
    19. //f[i][j]表示到达第i个世界的第j个点编号最大的世界
    20. int n, m;
    21. signed main() {
    22. qio >> n >> m;
    23. vector<int> f(m + 1, -1);
    24. int ans = INF;
    25. for (int i = 1; i <= n; ++i) {
    26. int l;
    27. qio >> l;
    28. auto nf = f;
    29. f[1] = i;
    30. while (l--) {
    31. int u, v;
    32. qio >> u >> v;
    33. nf[v] = max(nf[v], f[u]);
    34. if (v == m && nf[v] != -1) ans = min(ans, i - nf[v] + 1);
    35. }
    36. f.swap(nf);
    37. }
    38. qio << (ans >= INF / 2 ? -1 : ans) << '\n';
    39. }

  • 相关阅读:
    医疗器械标准目录汇编2022版共178页(文中附下载链接!)
    聊聊springboot的liveness及readiness
    十三、Qt多线程与线程安全
    Nodejs沙箱逃逸
    CSS总结---持续更新中 2022.8.4
    27岁,准备转行做网络安全渗透,完全零基础,有前途吗?
    Alibaba商品详情API接口
    mysql、clickhouse时间日期加法
    js排序的基础原理理解
    读取链式计数器
  • 原文地址:https://blog.csdn.net/CK1513710764/article/details/126013254