首页 > 其他分享 >ZZJC新生训练赛第二场题解

ZZJC新生训练赛第二场题解

时间:2024-10-02 22:49:19浏览次数:6  
标签:ZZJC Point int 题解 新生训练 nullptr cin tie cout

先给出比赛链接:
https://ac.nowcoder.com/acm/contest/92036

A 小红打怪

Show Code A

class Point { // 点类
public:
    int x, y;
    Point () {}
    Point (int x, int y) : x(x), y(y) {}
    Point operator+(const Point &P) const { return Point(x + P.x, y + P.y); }
};
int main() {
    cin.tie(nullptr)->sync_with_stdio(false);
    map<char, point=""> mp{{'W', {-1,0}}, {'S', {1,0}}, {'A', {0,-1}}, {'D', {0,1}}};
    int n, m;
    cin >> n >> m;
    Point be;
    vector<vector> g(n + 1, vector(m + 1));
    for (int i = 1; i <= n; ++ i) {
        for (int j = 1; j <= m; ++ j) {
            cin >> g[i][j];
            if (g[i][j] != '.' && g[i][j] != '*') be = {i, j};
        }
    }
    auto check = [&](Point p) {
        if (1 <= p.x && p.x <= n && 1 <= p.y && p.y <= m) {
            return 1;
        } else {
            return 0;
        }
    }; // 用于判断点是否在范围内
    int ans = 0;
    Point d = mp[g[be.x][be.y]];
    Point cur = be + d;
    while (check(cur)) {
        if (g[cur.x][cur.y] == '*') {
            ans ++;
        }
        cur = cur + d;
    }
    cout << ans << "\n";
}




B 马走日

Show Code B

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    int tt;
    cin >> tt;
    while (tt--) {
        ll n, m;
        cin >> n >> m;
        if (n == 1 || m == 1) {
            cout << 1 << "\n";
        } else if (n == 1 && m == 2) {
            cout << 1 << "\n";
        } else if (n == 2 && m == 1) {
            cout << 1 << "\n";
        } else if (n == 2 && m == 2) {
            cout << 1 << "\n";
        } else if (n == 2 && m == 3) {
            cout << 2 << "\n";
        } else if (n == 3 && m == 2) {
            cout << 2 << "\n";
        } else if (n == 3 && m == 3) {
            cout << 8 << "\n";
        } else {
            ll ans;
            if (n == 2) {
                ans = (m + 1) / 2;
            } else if (m == 2) {
                ans = (n + 1) / 2;
            } else {
                ans = n * m;
            }
            cout << ans << "\n";
        }
    }
}




C 捡石头

Show Code C

int main() {
    cin.tie(nullptr)->sync_with_stdio(false);
    ll n, m;
    cin >> n >> m; 
    if (n % (m + 1) == 0) {
        cout << "second\n";
    } else {
        cout << "first\n";
    }
}




D 小红进地下城

Show Code D

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    string s1 , s2 ;
    cin >> s1 >> s2 ;
    if (s1 == s2) {
        cout << "Yes\n";
    } else {
        cout << "No\n";
    }
}




F 小红的字符串生成

Show Code F

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    string s1, s2;
    cin >> s1 >> s2;
    if (s1 == s2) {
        cout << 2 << "\n";
        cout << s1 << "\n";
        cout << s1 + s2 << "\n";
    } else {
        cout << 4 << "\n";
        cout << s1 << "\n";
        cout << s2 + s1 << "\n";
        cout << s1 + s2 << "\n";
        cout << s2 << "\n";
    }
}




G 小红的字符串中值

Show Code G

int main() {
    cin.tie(nullptr)->sync_with_stdio(false);
    int n;
    char chr;
    cin >> n >> chr;
    string s;
    cin >> s;
    ll ans = 0;
    for (int i = 0; i < n; ++ i) {
        if (s[i] == chr) {
            ll left = i + 1;
            ll right = n - i;
            ans += min(left, right);
        }
    }
    cout << ans << "\n";
}




H [NOIP2015]扫雷游戏

Show Code H

class Point { // 点类
public:
    int x, y;
    Point () {}
    Point (int x, int y) : x(x), y(y) {}
    Point operator+(const Point &P) const { return Point(x + P.x, y + P.y); }
};
int main() {
    cin.tie(nullptr)->sync_with_stdio(false);
    int n, m;
    cin >> n >> m;
    vector<vector> g(n + 1, vector(m + 1));
    vector<vector> dp(n + 1, vector(m + 1)); // dp[i][j] 表示(i, j)周围有几个雷
    for (int i = 1; i <= n; ++ i) {
        for (int j = 1; j <= m; ++ j) {
            cin >> g[i][j];
        }
    }
    auto check = [&](Point p) {
        if (1 <= p.x && p.x <= n && 1 <= p.y && p.y <= m) {
            return 1;
        } else {
            return 0;
        }
    }; // 用于判断点是否在范围内
    for (int i = 1; i <= n; ++ i) {
        for (int j = 1; j <= m; ++ j) {
            if (g[i][j] == '*') {
                dp[i][j] = -1;
                for (int k1 = i - 1; k1 <= i + 1; ++ k1) {
                    for (int k2 = j - 1; k2 <= j + 1; ++ k2) {
                        if (check({k1, k2}) && (k1 != i || k2 != j) && g[k1][k2] != '*') {
                            dp[k1][k2]++;
                        }
                    }
                }
            } 
        }
    }
    for (int i = 1; i <= n; ++ i) {
        for (int j = 1; j <= m; ++ j) {
            if (dp[i][j] == -1) {
                cout << '*';
            } else {
                cout << dp[i][j];
            }
        }
        cout << "\n";
    }
}




I 可编程拖拉机比赛

Show Code I

int main() {
    cin.tie(nullptr)->sync_with_stdio(false);
    int n;
    cin >> n;
    int ans = 0;
    for (int i = 1; i <= 3; i ++) {
        ans += (n * i + 9) / 10 - n * i / 10;
        cout << ans << " ";
    }
}




J 数圈圈

Show Code A

int main() {
    cin.tie(nullptr)->sync_with_stdio(false);
    map<char, ll=""> mp{{'0', 1}, {'4', 1}, {'6', 1}, {'8', 2}, {'9', 1}};
    int N = 1e6;
    vector a(N + 1); // a[i] i这个数有几个圈
    vector sa(N + 1); // sa[i] 1~i这些数有几个圈
    for (int i = 1; i <= N; ++ i) {
        string s = to_string(i);
        for (auto si : s) {
            sa[i] += mp[si];
        }
        sa[i] += sa[i - 1];
    }
    int tt = 1;
    cin >> tt;
    while (tt--) {
        int a, b;
        cin >> a >> b;
        cout << sa[b] - sa[a - 1] << "\n";
    }
}




K

Show Code A

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    int n;
    cin >> n;
    cout << (10 - n % 10) % 10 << "\n";
}




标签:ZZJC,Point,int,题解,新生训练,nullptr,cin,tie,cout
From: https://www.cnblogs.com/Proaes/p/18444685

相关文章

  • 题解:TopCoder12316 ThreeColorability
    Vjudge可以出成《三色绘恋》背景。题意给一个格点数为\((n+1)\times(m+1)\)的网格,给格点染色,相邻的格点不能染成同样的颜色。每个格子有一条对角线的边,可选N形和Z形。现在有一个残缺的网格,存在一些格子的对角线连法不确定,构造一种字典序最小的方案使得至少存在一种染色......
  • 树上最值路径 题解
    题意简述给你一棵\(n\)个结点的树,编号为\(1\simn\),求有多少路径\(\operatorname{Path}(u\rightarrowv)\),满足\(u=\max\{x\midx\in\operatorname{Path}(u\rightarrowv)\}\),\(v=\min\{x\midx\in\operatorname{Path}(u\rightarrowv)\}\)。......
  • 题解:CF2014D Robert Hood and Mrs Hood
    差分,每次差分将\(\max(1,l-d+1)\)加\(1\),将\(r+1\)位置减\(1\)。然后前缀和求原数组,再遍历一下求最小值和最大值即可。代码:#include<bits/stdc++.h>usingnamespacestd;intmain(){ intt; cin>>t; while(t--){ intn,d,k; cin>>n>>d>>k;......
  • 题解:CF2009E Klee's SUPER DUPER LARGE Array!!!
    设\(m\)为\(a_1+\dots+a_i\),\(n\)为\(a_{i+1}+\dots+a_n\)。我们可以使用二分查找来搜索\(i\),使得\(m-n\)为最小的负数。如果我们移动到\(i+1\),则此时\(m-n\)为最小的整数。答案是两种情况下的最小绝对值。代码:#include<bits/stdc++.h>usingnamespacestd;pair<......
  • 题解:CF2009D Satyam and Counting
    比较容易观察的一道题,但是场上不开longlong见祖宗了。由于这题的\(x\)最大值比较小,所以我们可以直接存每个坐标是否有点。有两种三角形符号条件:存在两个点\((x,0),(x,1)\),可以观察到任意的其它点都可以成为第三点。有三个点为\((x,0),(x+1,1),(x+2,0)\)或\((x,1),(x+1......
  • 题解:CF2009C The Legend of Freya the Frog
    比较一眼的题目,场切了。分别考虑\(x\)和\(y\)。在\(x\)方向上我们需要的跳跃次数是\(\lceil\frac{x}{k}\rceil\),在\(y\)方向上我们需要的跳跃次数是\(\lceil\frac{y}{k}\rceil\)。考虑下面的两种情况:\(\lceil\frac{y}{k}\rceil\geq\lceil\frac{x}{k}\rceil......
  • 题解:P11137 [APC001] B - Checker
    注意到每个字符串长度相同,所以我们可以按照题意逐个遍历小K的题目和所有题库里的题目,统计相同位置字符相同的个数,如果大于\(\left\lceil\frac{k}{2}\right\rceil\),这两个题目就是重题。代码:#include<bits/stdc++.h>usingnamespacestd;boolc(stringa,stringb){in......
  • 题解:CF2014C Robin Hood in Town
    分享一种二分答案做法。先特判\(n=1\),\(n=2\),开始就有超过一半的人不高兴的情况。然后二分\(x\),计算新的和,新的平均值,如果有超过一半的人不高兴,就更新答案。代码:#include<bits/stdc++.h>usingnamespacestd;intmain(){ intt; cin>>t; while(t--){ intn; cin>>n......
  • 题解:SP7973 ACPC10E - Sometimes, a penalty is good!
    比较简单的一道数学题。思路:计算小组赛的比赛总数。longlongstage1=G*T*(T-1)/2;每组有\(T\)个队伍,每个队伍都需要与其他\(T-1\)个队伍比赛,共有\(T\cdot(T-1)\)场比赛。共有\(G\)组,因此小组赛总比赛数为\(\frac{G\cdotT\cdot(T-1)}{2}\)。计算进入......
  • 题解:SP10242 ACPC11D - Dice on a Board
    思路递归生成所有的可能的筛子朝向,用DFS标记所有可达的位置,用dijkstra计算从起始位置到目标位置的最优路径,并确定在移动过程中能够获得的最大分数。generate函数generate用于生成所有可能的骰子朝向排列,\(mask\)作为参数,用于表示哪些数字已经被使用。使用二进制压缩。......