找回密码
 立即注册
首页 业界区 业界 模拟赛SXJ202507270900比赛记录&题解

模拟赛SXJ202507270900比赛记录&题解

甄婉丽 2025-8-5 12:04:17
题目请看

T1

贪心:主要考察\(50\%\)时\(差值\ mod \ 3 \neq 0\)的情况
\(\begin{cases}\text{计算 } cha = 50 - n \\\text{如果 } cha \bmod 2 \neq 0 \text{ 则} \\\quad \text{输出 } \left\lceil \dfrac{cha}{2} \right\rceil + 1 \\\text{否则} \\\quad \text{输出 } \dfrac{cha}{2} \\\end{cases}\)
\(\begin{cases}\quad \text{计算 } cha = n - 50 \\\quad \begin{cases}\text{如果 } cha \bmod 3 = 1 \text{ 则} & \text{输出 } \dfrac{cha - 1}{3} + 2 \\\text{如果 } cha \bmod 3 = 2 \text{ 则} & \text{输出 } \dfrac{cha - 2}{3} + 4 \\\text{如果 } cha \bmod 3 = 0 \text{ 则} & \text{输出 } \dfrac{cha}{3} \\\end{cases} \\\end{cases}\)
贴一下代码
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define fre(c) freopen(c".in","r",stdin);freopen(c".out","w",stdout);
  4. #define ll long long
  5. #define endl "\n"
  6. #define ios ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);
  7. #define cst const
  8. int t, jia = 2, jian = 3;
  9. ll read() {
  10.         char c;
  11.         ll sum = 0;
  12.         int f = 1;
  13.         c = getchar();
  14.         while (!isdigit(c)) {
  15.                 if (c == '-')
  16.                         f *= -1;
  17.                 c = getchar();
  18.         }
  19.         while (isdigit(c)) {
  20.                 sum = sum * 10 + (c - '0');
  21.                 c = getchar();
  22.         }
  23.         return sum * f;
  24. }
  25. void fun() {
  26.         int n;
  27.         cin >> n;
  28.         if (n == 50) {
  29.                 cout << "0" << endl;
  30.         }
  31.         if (n < 50) {
  32.                 int cha = 50 - n;
  33.                 if (cha % jia != 0) {
  34.                         cha += jian;
  35.                         cout << cha / 2 + 1 << endl;
  36.                 } else {
  37.                         cout << cha / 2 << endl;
  38.                 }
  39.         }
  40.         if (n > 50) {
  41.                 int cha = n - 50;
  42.                 if (cha % jian == 1) {
  43.                         cout << (cha - 1) / jian + 2 << endl;
  44.                 }
  45.                 if (cha % jian == 2) {
  46.                         cout << (cha - 2) / jian + 4 << endl;
  47.                 }
  48.                 if (cha % jian == 0) {
  49.                         cout << cha / jian << endl;
  50.                 }
  51.         }
  52. }
  53. int main() {
  54.         ios
  55. //        fre("3")
  56.         fre("light")
  57.         cin >> t;
  58.         while (t --) {
  59.                 fun();
  60.         }
  61.         return 0;
  62. }
复制代码
可惜:TLE 40point
正确做法就是每次把充能值在根节点相加,那么根节点的叶子节点都会有\(父亲节点+自己单独的充能\)
贴一下代码[code]#include using namespace std;#define fre(c) freopen(c".in","r",stdin);freopen(c".out","w",stdout);#define ll long long#define endl "\n"#define ios ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);#define cst constcst int N = 2 * 1e5 + 5;int n, q, head[N], tot = 0, fa[N];ll val[N];struct Edge {        int to, nxt;} tree[N * 2];void link(int x, int y) {        tot ++;        tree[tot].to = y;        tree[tot].nxt = head[x];        head[x] = tot;}void dfs(int u, int root) {        for (int i = head; i; i = tree.nxt) {                int v = tree.to;                if (v == root) {                        continue ;                }                val[v] += val;                dfs(v, u);        }}int main() {        ios        fre("tree")        cin >> n >> q;        for (int i = 1; i > u >> v;                link(u, v);                link(v, u);                fa[v] = u;        }        while (q --) {                int p;                ll x;                cin >> p >> x;                val[p] += x;//                dfs(p, fa[p], x);        }        dfs(1, -1);        for (int i = 1; i
您需要登录后才可以回帖 登录 | 立即注册