题目请看
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}\)
贴一下代码- #include <bits/stdc++.h>
- 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 const
- int t, jia = 2, jian = 3;
- ll read() {
- char c;
- ll sum = 0;
- int f = 1;
- c = getchar();
- while (!isdigit(c)) {
- if (c == '-')
- f *= -1;
- c = getchar();
- }
- while (isdigit(c)) {
- sum = sum * 10 + (c - '0');
- c = getchar();
- }
- return sum * f;
- }
- void fun() {
- int n;
- cin >> n;
- if (n == 50) {
- cout << "0" << endl;
- }
- if (n < 50) {
- int cha = 50 - n;
- if (cha % jia != 0) {
- cha += jian;
- cout << cha / 2 + 1 << endl;
- } else {
- cout << cha / 2 << endl;
- }
- }
- if (n > 50) {
- int cha = n - 50;
- if (cha % jian == 1) {
- cout << (cha - 1) / jian + 2 << endl;
- }
- if (cha % jian == 2) {
- cout << (cha - 2) / jian + 4 << endl;
- }
- if (cha % jian == 0) {
- cout << cha / jian << endl;
- }
- }
- }
- int main() {
- ios
- // fre("3")
- fre("light")
- cin >> t;
- while (t --) {
- fun();
- }
- return 0;
- }
复制代码 可惜: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 |