首页 > 其他分享 >安徽农业大学寒假训练赛1题解

安徽农业大学寒假训练赛1题解

时间:2023-01-05 18:25:16浏览次数:35  
标签:农业大学 int 题解 nullptr cin long 训练赛 tie cout

D

#include <bits/stdc++.h>
using namespace std;

char g[10][10];

int main() {
    for (int i = 1; i <= 5; i ++) scanf("%s", g[i] + 1);//这样可以保证下标都从1开始

    for (int i = 1; i <= 4; i ++)
        for (int j = 1; j <= 4; j ++)
            if (g[i][j] == '*' && g[i][j + 1] == '*' && g[i + 1][j] == '*' && g[i + 1][j + 1] == '*') {
                cout << "Yes" << endl;
                return 0;
            }
    cout << "No" << endl;
    return 0;
}

E

题目保证有序情况下不重复,直接 j 从 i 开始, k 从 j 开始就行了

#include <bits/stdc++.h>
using namespace std;

int main() {
    ios::sync_with_stdio(false);    
    cin.tie(nullptr), cout.tie(nullptr);

    int ans = 0;

    int s, t; cin >> s >> t;
    for (int i = 0; i <= s; i ++)
        for (int j = i; j + i <= s; j ++)
            for (int k = j; i + j + k <= s; k ++)
                if (i * j * k <= t) 
                    ans ++;
    cout << ans << endl;
    return 0;
}

G

可以发现答案应该是1 * 1 + 2 * 2 + ... n * n,但是因为 n 最大值是 1e9,直接 for 循环会 TLE,所以需要推公式,公式就是

 

但是还有一些问题,int的最大值是2e9, n 若为 int 型, 1e9 * 1e9会爆 int,所以需要 long long存储,但是三个 1e9 级别的相乘还是会爆 long long,所以要提前取模一下

#include <bits/stdc++.h>
using namespace std;

const int mod = 1e9 + 7;

int main() {    
    ios::sync_with_stdio(false);    
    cin.tie(nullptr), cout.tie(nullptr);

    LL n; cin >> n;
    cout << n * (n + 1) % mod * (2 * n + 1) % mod;
    
    return 0;
}

H

大模拟题

STL版本

#include <bits/stdc++.h>
using namespace std;

const int N = 205;

int main() {
    ios::sync_with_stdio(false);    
    cin.tie(nullptr), cout.tie(nullptr);

    int t; cin >> t;
    while (t --) {
        int n, m;
        cin >> n >> m;
        set<int> col[N];

        for (int i = 1; i <= n; i ++)
            for (int j = 1; j <= m; j ++) {
                int x; cin >> x;
                col[i].insert(x);
            }
        
        vector<int> v;
        for (int i = 1; i <= n; i ++) v.push_back(i);

        while (m --) {
            int c; cin >> c;
            
            vector<int> temp;

            for (int x : v) {
                if (col[x].count(c)) temp.push_back(x);
            }

            v = temp;
            cout << v.size() << ' ';
        }
        cout << endl;
    }

    return 0;
}

数组版本

g 数组存第 i 件礼物有没有第 j 种颜色, st 数组为false的是存满足目前条件的礼物,为true的是不满足条件的礼物,每次从之前满足条件的礼物里面筛选

#include <bits/stdc++.h>
using namespace std;

const int N = 205;

bool g[N][N], st[N];

int main() {
    ios::sync_with_stdio(false);    
    cin.tie(nullptr), cout.tie(nullptr);

    int t; cin >> t;
    while (t --) {
        int n, m;
        cin >> n >> m;

        memset(g, 0, sizeof g);
        memset(st, 0, sizeof st);

        for (int i = 1; i <= n; i ++)
            for (int j = 1; j <= m; j ++) {
                int x; cin >> x;
                g[i][x] = true;
            }

        while (m --) {
            int c; cin >> c;

            int ans = 0;

            for (int i = 1; i <= n; i ++)
                if (!st[i]) {
                    if (g[i][c]) ans ++;
                    else st[i] = true;
                }

            cout << ans << ' ';
        }
        cout << endl;
    }

    return 0;
}

I

博弈题,首先思考若两者都没有技能卡,那么是不是最后一个回合谁操作谁输,那么如何求出有多少回合呢,由于 n 高达1e16,枚举肯定会TLE,那么直接用二分来求解有多少回合

这里(#define LL long long了,写起来更方便)

        LL l = 1, r = 1e8;
        while (l < r) {
            LL mid = l + r >> 1;
            LL x = mid * (mid + 1) / 2;
            if (x <= n) l = mid + 1;
            else r = mid;
        }

那么因为第一回合牛妹操作,那么如果有奇数个回合是不是牛妹就输了,有偶数个回合是不是牛牛就输了

现在考虑双方都有技能卡的情况,那么一个人的技能卡是可以抵消另一个人的技能卡的,所以可以类比为只有一个人可以发动技能或者都发动不了(双方技能卡数量相等)

那么就比如最后一回合是我输了,那么我如果技能卡比另一个多,那么我就可以使用技能卡,让另一个人输

那么就比如最后一回合是另一个输,那么我要保证我技能卡大于等于另一个人的技能卡,使他发不出技能,保证我赢

#include <bits/stdc++.h>
using namespace std;

#define LL long long

int main() {    
    ios::sync_with_stdio(false);    
    cin.tie(nullptr), cout.tie(nullptr);

    int T; cin >> T;
    while (T --) {
        LL n, a, b; cin >> n >> a >> b;

        LL len;
        LL l = 1, r = 1e8;
        while (l < r) {
            LL mid = l + r >> 1;
            LL x = mid * (mid + 1) / 2;
            if (x <= n) l = mid + 1;
            else r = mid;
        }
        len = r;

        if (len & 1) {
            if (a >= b) cout << "niuniu" << endl;
            else cout << "niumei" << endl;
        } else {
            if (a > b) cout << "niuniu" << endl;
            else cout << "niumei" << endl;
        }
    }
    return 0;
}

J

很简单,可以发现若横纵坐标之和为奇数那么就找不到

#include <bits/stdc++.h>
using namespace std;

int main() {    
    ios::sync_with_stdio(false);    
    cin.tie(nullptr), cout.tie(nullptr);

    int T; cin >> T;
    while (T --) {
        int x, y; cin >> x >> y;

        if (x + y & 1) cout << -1 << ' ' << -1 << endl;
        else if (x & 1) {
            if (x > y) cout << (x + y) / 2 << ' ' << 0 << endl;
            else cout << 0 << ' ' << (x + y) / 2 << endl;
        }
        else cout << x / 2 << ' ' << y / 2 << endl;
    }
    return 0;
}

K

模拟题

#include <bits/stdc++.h>
using namespace std;

#define LL long long

const int N = 1e5 + 5;

int n, k;
string strs[N];
unordered_map<string, int> mp;

int main() {    
    ios::sync_with_stdio(false);    
    cin.tie(nullptr), cout.tie(nullptr);

    cin >> n >> k;
    for (int i = 1; i <= n; i ++) cin >> strs[i];

    LL ans = 0;
    for (int i = 1; i <= n; i ++) {
        if (i - k - 2 >= 1) mp[strs[i - k - 2]] --;
        ans += mp[strs[i]];
        mp[strs[i]] ++;
    }
    cout << ans << endl;
    return 0;
}

M

可以发现答案就是周长,图形可能是个非常规图形,但是因为边一定是要么平行于 x 轴要么平行于 y 轴, 那么看本题的说明

 

其实把边平移一下,求周长可以平移为求外面最大的矩形的周长,那么只需要找到最上面的羊的坐标,最下面的羊的坐标,以及最左边和最右边的羊的坐标

由于最终结果可能是2 * 1e9 * 1e9是会爆 int 的,所以需要在计算过程种插入long long型, 所以用 2ll 去乘

#include <bits/stdc++.h>
using namespace std;

int n, m, k;
int a, b, c, d;
int l, r, up, down;

int main() {
    ios::sync_with_stdio(false);    
    cin.tie(nullptr);

    cin >> n >> m >> k;
    cin >> a >> b >> c >> d;
    l = a, r = c, up = d, down = b;

    while (k --) {
        int x, y; cin >> x >> y;
        l = min(l, x), r = max(r, x + 1);
        down = min(down, y), up = max(up, y + 1);
    } 

    long long ans = 2ll * (up - down + r - l);

    cout << ans << endl;
    return 0;
}

 

 N

思维题

最不好想的一点就是最小的数为 1 的情况,想到了就对了

#include <bits/stdc++.h>
using namespace std;int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    
    int t; cin >> t;
    while (t --) {
        int a[3];
        cin >> a[0] >> a[1] >> a[2];
sort(a, a + 3); if (a[0] == a[1] || a[1] == a[2]) cout << 1 << endl; else if (a[1] + a[0] == a[2] || a[1] * a[0] == a[2] || a[1] == a[0] * 2 || a[2] == a[1] * 2 || a[2] == a[0] * 2 || a[0] == 1) cout << 2 << endl; else cout << 3 << endl; } return 0; }

O

先看题目下面的说明了解大概

 

 

如果原本 a 数组就等于 b 数组,那么他肯定一个药丸都不吃

那么只要有一个不相等,他就需要吃药丸,就考虑最坏情况呗,他把 n 种使得 a[i] 远离 b[i] 都吃了一次,所以答案应该是 ∑ abs(a[i] + b[i]) + 1 + 1,因为他还需要多吃一次抵消掉上次的远离

#include <bits/stdc++.h>
using namespace std;

#define LL long long const int N = 1e5 + 5; int n; int a[N], b[N]; int main() { ios::sync_with_stdio(false); cin.tie(nullptr), cout.tie(nullptr); cin >> n; for (int i = 1; i <= n; i ++) cin >> a[i]; for (int i = 1; i <= n; i ++) cin >> b[i]; bool flag = true; for (int i = 1; i <= n; i ++) if (a[i] != b[i]) { flag = false; break; } if (flag) { cout << 0 << endl; return 0; } LL ans = 0; for (int i = 1; i <= n; i ++) ans += abs(a[i] - b[i]) + 2; cout << ans << endl; return 0; }

 

标签:农业大学,int,题解,nullptr,cin,long,训练赛,tie,cout
From: https://www.cnblogs.com/Leocsse/p/17028541.html

相关文章

  • 题解 : Luogu P2197 【模板】nim 游戏
    题目链接:link结论如果$a_1\oplusa_2\oplus...\oplusa_n\not=0$,则先手必胜证明操作到最后时,每堆石子数都是\(0\),\(0\oplus0\oplus...\oplus0=0......
  • Starling常见问题解决办法
    ​​Starling常见问题解决办法​​来自:​​智慧+毅力=无所不能​​ 1、Android设备上阻止用户按下后退后的行为侦听按键事件//阻止后退行为view.stage.addEventL......
  • JSOI2009 题解
    Count二维树状数组板子题,开\(c\)个二维树状数组即可过。可以通过离线对每个权值单独操作做到只开一个二维树状数组。如果空间要求更紧的话可以cdq分治做到只开一个......
  • unity和VS2019联调问题解决
    以前使用VS2015和17的时候联调的时候是可以附加到unity进行联调的,今天用的2019发现不可以了。研究了一下是少装了一个插件。装上插件就解决了。过程如下:当前使用VS版本2019......
  • 洛谷 p5536 题解
    题目链接:https://www.luogu.com.cn/problem/P5536此题为树的dfs的一个应用。思路树dfs时,可以计算每个点的深度。如图所示可以多次dfs,从而找到不同的信息。代码......
  • CF513F2 题解
    题意传送门有\(a+b+1\)个会动的棋子,在一个大小为\(n\timesm\)的棋盘上,棋盘上有一些点有障碍。棋子中,有\(a\)个红色棋子,\(b\)个蓝色棋子,和\(1\)个既能当作红棋......
  • Codeforces Round #839 (Div. 3)题解
    C.DifferentDifferences(贪心)题意​ 给定\(k\),\(n\)\((2\lek\len\le40)\)。从\([1-n]\)中不重复地任选\(k\)个数组成一个数组,使这个数组的差分数组中不同的......
  • 【题解】CF700E Cool Slogans
    原题面很简洁,懒得概括了。思路后缀自动机+结论。这种题出题人没有十年脑血栓都没法出。结论1:\(s_{i-1}\)必定是\(s_i\)的后缀。考虑\(s_i\)中\(s_{i-1......
  • 【题解】CF1073G Yet Another LCP Problem
    题面很清楚,不概括了。思路后缀树+树剖。套路题。看到lcp考虑转化成后缀树上求lca,这里可以用SAM构造反串的parenttree解撅(f**kuukk)于是问题转化成:每次给......
  • 【题解】CF808E Selling Souvenirs
    题意多重背包,但数据范围很大并且体积小于等于三。思路乱搞。很自然地考虑将物品按照体积分成三类。显然对于同一类的物品从最大开始取最优,那么有一个贪心的想法。直......