• CSP-J 2023 第二轮认证入门级(含答案)


    一。题目 

     

    二。答案

    T1 ⼩苹果(apple)

    每⼀轮拿掉的苹果数量为 。模拟拿苹果的过程,每⼀轮中令 ,当 时最后⼀个苹果会被拿掉。 时间复杂度为对数。 

    1. #include
    2. using namespace std;
    3. int n;
    4. int ans1, ans2;
    5. bool flag;
    6. int main() {
    7. cin >> n;
    8. while (n) {
    9. ans1++;
    10. if (!flag && n % 3 == 1) {
    11. ans2 = ans1;
    12. flag = 1;
    13. }
    14. n -= n % 3 ? n / 3 + 1 : n / 3;
    15. }
    16. cout << ans1 << ' ' << ans2 << '\n';
    17. return 0;
    18. }

    T2 公路(road) 

    简单的贪⼼题。 如果下⼀个站点的油费⽐当前站点贵,显然应该在当前站点把油加⾜。 ⽤ 表⽰当前所在的站点, 表⽰当前油箱中的剩余油量,每次找到 之后第⼀个油价低 于 的站点 ,加上⾜够的油开过去即可。注意使⽤ long long 。 双指针遍历即可得出答案,时间复杂度为线性。

    1. #include
    2. #define N 100010
    3. #define ll long long
    4. using namespace std;
    5. int n;
    6. ll d, v[N], p[N], ans;
    7. int main() {
    8. cin >> n >> d;
    9. for (int i = 1; i < n; i++) cin >> v[i];
    10. for (int i = 1; i <= n; i++) cin >> p[i];
    11. int now = 1, nxt = 2;
    12. ll tank = 0;
    13. while (now < n) {
    14. ll dis = v[now];
    15. while (nxt < n && p[nxt] >= p[now])
    16. dis += v[nxt++];
    17. int cnt = 0;
    18. while (tank + cnt * d < dis) cnt++;
    19. ans += p[now] * cnt;
    20. tank += cnt * d - dis;
    21. now = nxt++;
    22. }
    23. cout << ans << endl;
    24. return 0;
    25. }

    T3 ⼀元⼆次⽅程(uqe) 

    ⼤⽔题。 给出⼀元⼆次⽅程 的三个系数 ,求该⽅程⽐较⼤的根,且化简为最简形 式。 ⾸先根据判别式 判定是否⽆解,在有解的情况下,要将 中的完全平⽅因⼦提到根 号外⾯。 设 ,根据算术基本定理 : 如果 为偶数,则将 累乘到 ; 如果 为奇数,则将 累成到 ; 当 时结果为⽆理数, 或 时结果都是有理数,分情况讨论输出即可。 有理数化为最简分式只需分⼦分⺟同除以它们的最⼤公约数。 特别需要注意 的符号,由于题⽬要求输出⽐较⼤的根,所以当 时, 的符号也应该为负。 ⽐较⽅便的处理⽅法是:如果 ,则将 全部反号,避免⽆谓的分类讨论。

    1. #include
    2. #define N 1010
    3. int T, M, a, b, c;
    4. int P[N], C[N], tot;
    5. void divide(int n) {
    6. tot = 0;
    7. for (int i = 2; i * i <= n; i++) {
    8. if (n % i == 0) {
    9. P[++tot] = i;
    10. C[tot] = 0;
    11. while (n % i == 0) {
    12. n /= i;
    13. C[tot]++;
    14. }
    15. }
    16. }
    17. if (n > 1) {
    18. P[++tot] = n;
    19. C[tot] = 1;
    20. }
    21. }
    22. int qpow(int x, int y) {
    23. int res = 1;
    24. while (y) {
    25. if (y & 1) res *= x;
    26. x *= x;
    27. y >>= 1;
    28. }
    29. return res;
    30. }
    31. int gcd(int x, int y) {
    32. return y ? gcd(y, x % y) : x;
    33. }
    34. void print_rational(int x, int y) {
    35. if (x * y < 0) printf("-");
    36. if (x < 0) x *= -1;
    37. if (y < 0) y *= -1;
    38. if (x % y == 0)
    39. printf("%d", x / y);
    40. else
    41. printf("%d/%d", x / gcd(x, y), y / gcd(x, y));
    42. }
    43. int main() {
    44. scanf("%d %d", &T, &M);
    45. while (T--) {
    46. scanf("%d %d %d", &a, &b, &c);
    47. if (a < 0) {
    48. a *= -1;
    49. b *= -1;
    50. c *= -1;
    51. }
    52. int delta = b * b - 4 * a * c;
    53. if (delta < 0) {
    54. puts("NO");
    55. continue;
    56. }
    57. // 除去 delta 中的完全平⽅因⼦
    58. divide(delta);
    59. int d = 1;
    60. for (int i = 1; i <= tot; i++) {
    61. if (C[i] & 1)
    62. d *= qpow(P[i], (C[i] - 1) >> 1);
    63. else
    64. d *= qpow(P[i], C[i] >> 1);
    65. }
    66. delta /= d * d;
    67. if (delta == 0)
    68. print_rational(-b, a << 1);
    69. else if (delta == 1) {
    70. print_rational(d - b, a << 1);
    71. else {
    72. if (b) {
    73. print_rational(-b, a << 1);
    74. printf("+");
    75. }
    76. int x = d;
    77. int y = a << 1;
    78. if (x % y == 0) {
    79. x /= y;
    80. if (x > 1) printf("%d*", x);
    81. printf("sqrt(%d)", delta);
    82. } else {
    83. if (x / gcd(x, y) > 1) printf("%d*", x / gcd(x, y));
    84. printf("sqrt(%d)/%d", delta, y / gcd(x, y));
    85. }
    86. }
    87. puts("");
    88. }
    89. return 0;
    90. }

    T4 旅游巴⼠

    1. #include
    2. #include
    3. #include
    4. #define N 10010
    5. #define M 20010
    6. #define K 110
    7. #define INF 0x3f3f3f3f
    8. int n, m, k;
    9. int dis[N][K];
    10. struct T {
    11. int head, to, nxt, w;
    12. } a[M];
    13. int tot;
    14. void add(int u, int v, int w) {
    15. a[++tot].to = v;
    16. a[tot].w = w;
    17. a[tot].nxt = a[u].head;
    18. a[u].head = tot;
    19. }
    20. inline int ceil(int x, int y) {
    21. return x % y == 0 ? x / y : x / y + 1;
    22. }
    23. void dfs(int u, int now) {
    24. if (now >= dis[n][0]) return; // 最优性剪枝
    25. for (int i = a[u].head; i; i = a[i].nxt) {
    26. int v = a[i].to, w = a[i].w;
    27. int nxt = now >= w ? now + 1 : now + 1 + ceil(w - now, k) * k;
    28. if (dis[v][nxt % k] > nxt) {
    29. dis[v][nxt % k] = nxt;
    30. dfs(v, nxt);
    31. }
    32. }
    33. }
    34. int main() {
    35. scanf("%d %d %d", &n, &m, &k);
    36. for (int i = 1; i <= m; i++) {
    37. int u, v, w;
    38. scanf("%d %d %d", &u, &v, &w);
    39. add(u, v, w);
    40. }
    41. memset(dis, 0x3f, sizeof(dis));
    42. dfs(1, 0);
    43. printf("%d\n", dis[n][0] == INF ? -1 : dis[n][0]);
    44. return 0;
    45. }

     如果 ⼀开始进⼊的是错误的分⽀,会导致 数组收敛的很慢。将 的过程改为 AC即可 。

    1. #include
    2. #include
    3. #include
    4. #include
    5. #define N 10010
    6. #define M 20010
    7. #define K 110
    8. #define INF 0x3f3f3f3f
    9. int n, m, k;
    10. int dis[N][K];
    11. struct T {
    12. int head, to, nxt, w;
    13. } a[M];
    14. int tot;
    15. void add(int u, int v, int w) {
    16. a[++tot].to = v;
    17. a[tot].w = w;
    18. a[tot].nxt = a[u].head;
    19. a[u].head = tot;
    20. }
    21. inline int ceil(int x, int y) {
    22. return x % y == 0 ? x / y : x / y + 1;
    23. }
    24. struct V {
    25. int id, dis;
    26. } s, now, nxt;
    27. void bfs() {
    28. s.id = 1;
    29. s.dis = dis[1][0] = 0;
    30. std::queue q;
    31. q.push(s);
    32. while (q.size()) {
    33. now = q.front();
    34. q.pop();
    35. int u = now.id;
    36. for (int i = a[u].head; i; i = a[i].nxt) {
    37. nxt.id = a[i].to;
    38. nxt.dis = now.dis >= a[i].w ? now.dis + 1 : now.dis + 1 + ceil(a[i].w - now.dis, k) * k;
    39. if (dis[nxt.id][nxt.dis % k] > nxt.dis) {
    40. dis[nxt.id][nxt.dis % k] = nxt.dis;
    41. q.push(nxt);
    42. }
    43. }
    44. }
    45. }
    46. int main() {
    47. scanf("%d %d %d", &n, &m, &k);
    48. for (int i = 1; i <= m; i++) {
    49. int u, v, w;
    50. scanf("%d %d %d", &u, &v, &w);
    51. add(u, v, w);
    52. }
    53. memset(dis, 0x3f, sizeof(dis));
    54. bfs();
    55. printf("%d\n", dis[n][0] == INF ? -1 : dis[n][0]);
    56. return 0;
    57. }

  • 相关阅读:
    数据隐私新篇章:Facebook如何保护用户信息
    【Linux】:Linux环境与版本
    玩转Jetson Nano(三):安装Pytorch GPU版
    Vue3 - reactive 复杂类型(通俗易懂,详细教程)
    spring boot 一个极简单的 demo 示例
    SHAP解释模型(二)
    Docker容器之Consul部署
    设计循环队列,解决假溢出问题
    MBIST BAP(Bist Access Port)直接访问接口(1)
    什么是 Potatoz NFT 系列?
  • 原文地址:https://blog.csdn.net/Qpeterqiufengyi/article/details/134096398