安徽农业大学蓝桥杯模拟赛1

看不懂题解的再问我

 

A

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

#define endl '\n'
#define LL long long
#define ph push_back    
#define INF 0x3f3f3f3f
#define PII pair<int,int>

int n, k;

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

    if (n % k == 0) cout << n - k << endl;
    else cout << n - n % k << endl;
    return 0;
}
View Code

B

int 最大值2147483647 (2.1e9) 开 longlong

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

#define endl '\n'
#define LL long long
#define ph push_back    
#define INF 0x3f3f3f3f
#define PII pair<int,int>

int main() {    
    ios::sync_with_stdio(false);    
    cin.tie(nullptr), cout.tie(nullptr);
    
    int T; cin >> T;
    while (T --) {
        LL x, y, a, b, c; cin >> x >> y >> a >> b >> c;
        cout << max(x * y, a + b) + c << endl;
    }
    return 0;
}
View Code

C

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

#define endl '\n'
#define LL long long
#define ph push_back    
#define INF 0x3f3f3f3f
#define PII pair<int,int>

string str;
vector<int> ans;

int main() {    
    ios::sync_with_stdio(false);    
    cin.tie(nullptr), cout.tie(nullptr);
    
    cin >> str;
    int n = str.size();

    for (int i = 0; i < n; i ++) 
        if (str[i] != '0') {
            int t = (str[i] - '0') * pow(10, n - 1 - i);
            ans.ph(t);
        }

    cout << ans.size() << endl;
    for (int x : ans) cout << x << ' ';
    return 0;
}
View Code

D

先手的话必胜

E

暴力会T,需要二分

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

#define endl '\n'
#define fx first
#define fy second
#define LL long long
#define pb push_back    
#define INF 0x3f3f3f3f
#define PII pair<int,int>

const int N = 1e5 + 5;

int n, m;
PII a[N];

bool check(int mid) {
    LL tot = 0;
    for (int i = 1; i <= n; i ++) {
        tot += (LL)(a[i].first / mid) * (a[i].second / mid);
        if (tot >= m) return true;
    }
    return false;
}

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

    cin >> n >> m;
    for (int i = 1; i <= n; i ++) {
        int x, y; cin >> x >> y;
        a[i] = {x, y};
    }

    int l = 1, r = 1e5;
    while (l < r) {
        int mid = l + r + 1 >> 1;
        if (check(mid)) l = mid;
        else r = mid - 1;
    }
    cout << l << endl;
    return 0;
}
View Code

F

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

#define endl '\n'
#define LL long long
#define ph push_back    
#define INF 0x3f3f3f3f
#define PII pair<int,int>

int n, k;

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

    vector<int> num;

    while (n) {
        num.ph(n % k);
        n /= k;
    }

    for (int i = num.size() - 1; ~i; i --) cout << num[i];
    return 0;
}
View Code

G

BFS

两种写法

第一种:直接BFS,但是要记录每个点到达时候的状态(携带了钥匙、未携带钥匙)

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

#define endl '\n'
#define LL long long
#define ph push_back    
#define INF 0x3f3f3f3f
#define PII pair<int,int>

const int N = 505;

struct node {
    int x, y;
    bool flag;
};

int n, m;
char g[N][N];
int dist[N][N][2];

int dx[] = {-1, 0, 1, 0};
int dy[] = {0, 1, 0, -1};

void solve() {
    scanf("%d %d", &n, &m);

    for (int i = 1; i <= n; i ++) scanf("%s", g[i] + 1);

    int sx, sy, fx, fy;
    for (int i = 1; i <= n; i ++)
        for (int j = 1; j <= m; j ++)
            if (g[i][j] == 'P') sx = i, sy = j;
            else if (g[i][j] == 'E') fx = i, fy = j;

    queue<node> q;
    q.push({sx, sy, false});

    memset(dist, 0x3f, sizeof dist);
    dist[sx][sy][0] = 0;

    while (q.size()) {
        auto t = q.front(); q.pop();

        for (int i = 0; i < 4; i ++) {
            int a = t.x + dx[i], b = t.y + dy[i];

            if (a < 1 || a > n || b < 1 || b > m) continue;
            if (g[a][b] == '#' || g[a][b] == 'E' && t.flag == false) continue;

            bool f = t.flag;
            if (g[a][b] == 'K') f = true;

            if (dist[a][b][f] <= dist[t.x][t.y][t.flag] + 1) continue;
            dist[a][b][f] = dist[t.x][t.y][t.flag] + 1;

            q.push({a, b, f});
        }
    }

    if (dist[fx][fy][true] == INF) cout << "No solution" << endl;
    else cout << dist[fx][fy][true] << endl;
}

int main() {
    int T; cin >> T;
    while (T --) solve();
    return 0;
}
View Code

第二种:从起点BFS一次再从终点BFS一次,然后枚举所有钥匙,若起点能到达某个钥匙且终点也能到达某个钥匙,就行,然后取 min

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

#define endl '\n'
#define LL long long
#define ph push_back    
#define INF 0x3f3f3f3f
#define PII pair<int,int>

const int N = 505;

int n, m;
char g[N][N];
int dist1[N][N];
int dist2[N][N];

int dx[] = {-1, 0, 1, 0};
int dy[] = {0, 1, 0, -1};

void bfs(int x, int y, int op) {
    queue<PII> q;
    q.push({x, y});

    while (q.size()) {
        auto t = q.front(); q.pop();

        for (int i = 0; i < 4; i ++) {
            int a = t.first + dx[i], b = t.second + dy[i];
            if (a < 1 || a > n || b < 1 || b > m) continue;
            if (g[a][b] == '#') continue;
            if (op == 1 && (dist1[a][b] != INF || g[a][b] == 'E')) continue;
            if (op == 2 && dist2[a][b] != INF) continue;

            if (op == 1) dist1[a][b] = dist1[t.first][t.second] + 1;
            else dist2[a][b] = dist2[t.first][t.second] + 1;

            q.push({a, b});
        }
    }
}

void solve() {
    scanf("%d %d", &n, &m);

    for (int i = 1; i <= n; i ++) scanf("%s", g[i] + 1);

    int sx, sy, fx, fy;
    for (int i = 1; i <= n; i ++)
        for (int j = 1; j <= m; j ++)
            if (g[i][j] == 'P') sx = i, sy = j;
            else if (g[i][j] == 'E') fx = i, fy = j;

    memset(dist1, 0x3f, sizeof dist1);
    memset(dist2, 0x3f, sizeof dist2);

    dist1[sx][sy] = 0, dist2[fx][fy] = 0;

    bfs(sx, sy, 1), bfs(fx, fy, 2);

    int ans = INF;
    for (int i = 1; i <= n; i ++)
        for (int j = 1; j <= m; j ++)
            if (g[i][j] == 'K') 
                ans = min(ans, dist1[i][j] + dist2[i][j]);
        
    if (ans == INF) cout << "No solution" << endl;
    else cout << ans << endl;
}

int main() {
    int T; cin >> T;
    while (T --) solve();
    return 0;
}
View Code

H

有很多解法,这里给一种通用解法

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

#define endl '\n'
#define LL long long
#define ph push_back    
#define INF 0x3f3f3f3f
#define PII pair<int,int>

int m[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

int year = 1937, month = 1, day = 1, now = 5;

bool run() {
    return year % 4 == 0 && year % 100 != 0 || year % 400 == 0;
}

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

    while (year != 2021 || month != 3 || day != 20) {
        day ++;
        now ++;
        if (now == 8) now = 1;
        if (now == 1) ans ++;

        if (month == 2) {
            if (run()) {
                if (day > 29) day = 1, month = 3;
            }
            else if (day > 28) day = 1, month = 3;
            
        }
        else {
            if (day > m[month]) month ++, day = 1;
            if (month == 13) year ++, month = 1;
        }
    }

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

I

ai 和 bi 奇偶性必须相同,总和必须相同

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

#define endl '\n'
#define LL long long
#define ph push_back    
#define INF 0x3f3f3f3f
#define PII pair<int,int>

const int N = 2e5 + 5;

int n;
LL a[N], b[N];

void solve() {
    cin >> n;

    for (int i = 1; i <= n; i ++) cin >> a[i];
    for (int i = 1; i <= n; i ++) cin >> b[i];

    LL ans = 0;
    for (int i = 1; i < n; i ++) {
        if ((a[i] & 1) != (b[i] & 1)) {
            cout << -1 << endl;
            return;
        }
        ans += abs(a[i] - b[i]) / 2;
        a[i + 1] += a[i] - b[i];
    }

    if (a[n] != b[n]) ans = -1;
    cout << ans << endl;
}

int main() {    
    ios::sync_with_stdio(false);    
    cin.tie(nullptr), cout.tie(nullptr);
    
    int T; cin >> T;
    while (T --) solve();
    return 0;
}
View Code

J

数据范围很小,可以直接暴力 dijkstra,也可以求 lca

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

#define endl '\n'
#define LL long long
#define ph push_back    
#define INF 0x3f3f3f3f
#define PII pair<int,int>

const int N = 1e3 + 5;

int n, m;
int l[N], r[N], fa[N];
int dist[N];

void dfs(int u, int depth) {
    dist[u] = depth;

    if (l[u] != -1) fa[l[u]] = u, dfs(l[u], depth + 1);
    if (r[u] != -1) fa[r[u]] = u, dfs(r[u], depth + 1);
}

int lca(int u, int v) {
    if (dist[u] < dist[v]) return lca(v, u);

    while (dist[u] > dist[v]) u = fa[u];
    while (u != v) u = fa[u], v = fa[v];
    return u;
}

int main() {    
    ios::sync_with_stdio(false);    
    cin.tie(nullptr), cout.tie(nullptr);
    
    cin >> n >> m;
    for (int i = 1; i <= n; i ++) {
        int a, b; cin >> a >> b;
        l[i] = a, r[i] = b;
    }

    dfs(1, 0);

    while (m --) {
        int u, v; cin >> u >> v;
        cout << dist[u] + dist[v] - 2 * dist[lca(u, v)] << endl;
    }
    return 0;
}
View Code

K

题目的每次操作是任选一个区间 -1,可以想到差分,不然会 T

最后求的是让所有数都变成 1,可以抽象成数组 a 的差分数组 b,b第一个元素为 1, 其余元素为 0,那么抽象成差分数组的话,对原数组的每次操作相当于对差分数组选两个坐标 i, j, i < j, bi = bi - 1, bj = bj + 1

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

#define endl '\n'
#define LL long long
#define ph push_back    
#define INF 0x3f3f3f3f
#define PII pair<int,int>

const int N = 1e5 + 5;

int n;
LL a[N], s[N];

int main() {    
    ios::sync_with_stdio(false);    
    cin.tie(nullptr), cout.tie(nullptr);
    
    int T; cin >> T;
    while (T --) {
        cin >> n;
        for (int i = 1; i <= n; i ++) {
            cin >> a[i];
            s[i] = a[i] - a[i - 1];
        }

        LL ans = 0;
        for (int i = 1; i <= n; i ++)
            if (s[i] > 0)
                ans += s[i];

        cout << ans - 1 << endl;
    }
    return 0;
}
View Code

L

使坏区间最少的形式一定是 100 100 100 100,坏区间有 0 个,每次消耗一个 1 和两个 0

所以要尽量摆成这种形式

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

#define endl '\n'
#define fx first
#define fy second
#define LL long long
#define ph push_back    
#define INF 0x3f3f3f3f
#define PII pair<int,int>

int n, m;

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

    cin >> n >> m;
    int x = m, y = n - m;

    if (!y) {
        cout << n - 2 << endl;
        return 0;
    }

    while (x && y >= 2) {
        x --;
        y -= 2;
    }

    if (!x) cout << 0 << endl;
    else {
        cout << x - 1 << endl;
    }
    return 0;
}
View Code

M

思考对于答案的二进制的每一位

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

#define endl '\n'
#define LL long long
#define ph push_back    
#define INF 0x3f3f3f3f
#define PII pair<int,int>

const int N = 55;

int n;
int a[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];

    int ans = 0;
    for (int i = 1; i <= n; i ++)
        for (int j = 30; ~j; j --)
            if (!(ans >> j & 1) && a[i] >= (1 << j)) {
                ans += 1 << j;
                a[i] -= 1 << j;
            }
    cout << ans << endl;
    return 0;
}
View Code

 

posted @ 2023-03-18 16:35  Leocsse  阅读(127)  评论(0)    收藏  举报