smu2024蓝桥杯训练1

A

查看代码

void solve() {
    int n;
    cin >> n;
    if (n <= 100) cout << 100;
    else if(n <= 150) cout << 150;
    else if(n <= 300) cout << 300;
    else if(n <= 400) cout << 400;
    else if(n <= 1000) cout << 1000;
}

 

B

注意:数据末尾为/r/t,需要getchar两次,或者cin.ignore(100,'\n')
查看代码
 #include<bits/stdc++.h>
using namespace std;
#define int long long
#define PII pair<int, int>
#define PDI pair<double, int>
const double eps = 1e-12;
const int N = 1e4 + 5;
const int dx[4] = {0,-1}, dy[4] = {-1, 0};
const int INF = 0x3f3f3f3f;



void solve() {
    string a, b, ss;
    getline(cin, ss);
//    assert( ss.back() != '\r');
    a = ss.substr(0, 8), b = ss.substr(9, 8);
    auto P = [](string s) {
        int ans = 0;
        string x = s.substr(0,2);
        int y = stoi(x);
        ans += y * 3600;
        x = s.substr(3, 2);
        y = stoi(x);
        ans += y * 60;
        x = s.substr(6, 2);
        y = stoi(x);
        ans += y;
        return ans;
    };
    int x1 = P(a), x2 = P(b);
    if (ss.size() > 18) {
          x2 += (ss[20] - '0') * 24 * 60 * 60;
    }
    getline(cin, ss);
    a = ss.substr(0, 8), b = ss.substr(9, 8);
    int y1 = P(a), y2 = P(b);
    if (ss.size() > 18) {
        y2 += (ss[20] - '0') * 24 * 60 * 60;
    }
//    cout << x1 << ' ' << x2 << '|' << y1 << ' ' << y2 << '\n';
//    cout << x2 - x1 << ' ' << y2 - y1 << '\n';
    int c = (x2 - x1 + (y2 - y1)) / 2;
    auto Print = [](int x) {
        int a, b, c;
        c = x % 60; x /= 60;
        b = x % 60; x /= 60;
        a = x;
        printf("%02lld:%02lld:%02lld\n", a, b, c);
    };
    Print(c);
}

signed main() {
//    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    int T = 1;
    cin >> T;
    cin.ignore(100, '\n');
//    getchar();
//    getchar();
    while (T--) {
        solve();
    }
}

 

C

哈希
 
查看代码
 #include<bits/stdc++.h>
using namespace std;
#define int long long
#define PII pair<int, int>
#define PDI pair<double, int>
const double eps = 1e-12;
const int N = 1e4 + 5;
const int dx[4] = {0,-1}, dy[4] = {-1, 0};
const int INF = 0x3f3f3f3f;



void solve() {
    int n, k;
    cin >> n >> k;
    map<string, int> mp;
    for (int i = 0, x; i < n; ++i) {
        string s;
        cin >> s >> x;
        mp[s] = x;
    }
    getchar();
    for (int i = 0; i < k; ++i) {
        string s;
        getline(cin, s);
//        cout << s << '\n';
        for (int j = 0; j < s.size(); ++j) {
            if (s[j] == '{') {
                int p = j + 1;
                string x;
                while (p < s.size() && s[p] != '}') {
                    x.push_back(s[p++]);
                }
                j = p;
                if (mp.count(x)) cout << mp[x];
                else cout << '{' << x << '}';
            }
            else cout << s[j];
        }
        cout << '\n';
    }
}

signed main() {
//    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    int T = 1;
//    cin >> T;
//    getchar();
    while (T--) {
        solve();
    }
}

 

D

dfs搜索每种情况
查看代码
 #include<bits/stdc++.h>
using namespace std;
#define int long long
#define PII pair<int, int>
#define PDI pair<double, int>
const double eps = 1e-12;
const int N = 1e4 + 5;
const int dx[4] = {0,-1}, dy[4] = {-1, 0};
const int INF = 0x3f3f3f3f;


int n;
struct E {
    int l, r, c;
};
vector<E> ve;
vector<int> st, f;
bool dfs(int u) {
    if (u == n) {
        return true;
    }
    for (int i = 0; i < n; ++i) {
        if (st[i]) continue;
        if (f.empty()) f.push_back(ve[i].l + ve[i].c);
        else {
            if (f.back() > ve[i].r) continue;
            f.push_back(max(f.back(), ve[i].l) + ve[i].c);
        }
        st[i] = 1;
        if (dfs(u + 1)) return true;
        f.pop_back();
        st[i] = 0;
    }
    return false;
}
void solve() {
    cin >> n;
    ve = vector <E> (n);
    st = vector<int> (n);
    f.clear();
    for (int i = 0; i < n; ++i) cin >> ve[i].l >> ve[i].r >> ve[i].c, ve[i].r += ve[i].l;
    bool ok = dfs(0);
    if (ok) cout << "YES\n";
    else cout << "NO\n";
}

signed main() {
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    int T = 1;
    cin >> T;
    while (T--) {
        solve();
    }
}

 

E

dp,f[i][j]表示前i个数中末尾的数最后一位为j的最少删除次数

查看代码
 #include<bits/stdc++.h>
using namespace std;
#define int long long
#define PII pair<int, int>
#define PDI pair<double, int>
const double eps = 1e-12;
const int N = 1e4 + 5;
const int dx[4] = {0,-1}, dy[4] = {-1, 0};
const int INF = 0x3f3f3f3f;


void solve() {
    int n;
    cin >> n;
    vector f(n + 1, vector <int> (10, INF));
    vector<int> a(n + 1), pre(n + 1), las(n + 1);
    auto P = [] (int x) {
        int y;
        while (x > 0) {
            y = x % 10;
            x /= 10;
        }
        return y;
    };
    for (int i = 1; i <= n; ++i) cin >> a[i], pre[i] = P(a[i]), las[i] = a[i] % 10;
    for (int i = 0; i <= n; ++i) {
        for (int j = 0; j <= 9; ++j) {
            if (i == 0) f[i][j] = 0;
            else {
                f[i][j] = f[i - 1][j] + 1;
            }
        }
        if (i) f[i][las[i]] = min(f[i][las[i]], f[i - 1][pre[i]]);
    }
    int ans = INF;
    for (int i = 0; i <= 9; ++i) ans = min (ans, f[n][i]);
    cout << ans;
}

signed main() {
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    int T = 1;
//    cin >> T;
    while (T--) {
        solve();
    }
}

 

F

一次一步或者k步,跑最短路即可
查看代码
 #include<bits/stdc++.h>
using namespace std;
#define int long long
#define PII pair<int, int>
#define PDI pair<double, int>
const double eps = 1e-12;
const int N = 1e4 + 5;
const int dx[4] = {0,-1}, dy[4] = {-1, 0};
const int INF = 0x3f3f3f3f;



void solve() {
    int n, k;
    cin >> n >> k;
    vector<int> d(n, INF), st(n);
    queue<int> q;
    d[0] = 0;
    q.push(0);
    while (q.size()) {
        auto t = q.front();
        q.pop();
        if (st[t]) continue;
        st[t] = 1;
        if (!st[(t + 1) % n] && d[(t + 1) % n] > d[t] + 1) {
            d[(t + 1) % n] = d[t] + 1;
            q.push((t + 1) % n);
        }
        if (!st[(t + k) % n] && d[(t + k) % n] > d[t] + 1) {
            d[(t + k) % n] = d[t] + 1;
            q.push((t + k) % n);
        }
    }
    int ans = 0;
    for (int i = 1; i < n; ++i) ans = max(ans, d[i]);
    cout << ans;
}

signed main() {
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    int T = 1;
//    cin >> T;
    while (T--) {
        solve();
    }
}

 

G

 

[蓝桥杯 2023 省 A] 更小的数

n的范围不大,n2枚举区间,根据区间大小从小到大枚举,若s[l] > s[r]答案加一,若s[l] = s[r],答案取决于 [l + 1,r - 1]
查看代码
 #include<bits/stdc++.h>
using namespace std;
#define int long long
#define PII pair<int, int>
#define PDI pair<double, int>
const double eps = 1e-12;
const int N = 1e4 + 5;
const int dx[4] = {0,-1}, dy[4] = {-1, 0};
const int INF = 0x3f3f3f3f;


void solve() {
    string s;
    cin >> s;
    int n = s.size(), ans = 0;
    s.insert(s.begin(), ' ');
    vector f(n + 1, vector<int> (n + 1));
    for (int i = 2; i <= n; ++i) {
        for (int j = 1; j + i - 1 <= n; ++j) {
            int l = j, r = j + i - 1;
            if (s[l] > s[r]) f[l][r] = 1;
            else if (s[l] == s[r] && i > 2) f[l][r] = f[l + 1][r - 1];
            ans += f[l][r];
        }
    }
    cout << ans;
}

signed main() {
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    int T = 1;
//    cin >> T;
    while (T--) {
        solve();
    }
}

 

H

首先可以二分y,考虑如何check。可以将来回看作一共跳2x次,对于长度为y的区间内每只青蛙必须在此区间跳至少一次(跳跃极限是y,不可能一步跳过长度为y的区间),那么长度为y的区间内至少要有2x个石子(加上高度),用区间和判断即可。即check所有长度为y的区间和是否不小于2x
查看代码
 #include<bits/stdc++.h>
using namespace std;
#define int long long
#define PII pair<int, int>
#define PDI pair<double, int>
const double eps = 1e-12;
const int N = 1e4 + 5;
const int dx[4] = {0,-1}, dy[4] = {-1, 0};
const int INF = 0x3f3f3f3f;



void solve() {
    int n, x;
    cin >> n >> x;
    vector<int> a(n + 1);
    for (int i = 1; i < n; ++i) {
        cin >> a[i];
        a[i] += a[i - 1];
    }
    a[n] = a[n - 1];
    int l = 1, r = n, res;
    auto check = [n, a, x](int y) {
        for (int i = y; i < n; ++i) {
            if (a[i] - a[i - y] < 2 * x) return false;
        }
        return true;
    };
    while (l <= r) {
        int mid = l + r >> 1;
        if (check(mid)) res = mid, r = mid - 1;
        else l = mid + 1;
    }
    cout << res;
}

signed main() {
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    int T = 1;
//    cin >> T;
    while (T--) {
        solve();
    }
}

I

[蓝桥杯 2021 省 AB2] 国际象棋

状压dp,一列的状态只与前一列和前前一列的状态和个数有关,n也不大,直接枚举个数、当前列、前列、前前列的状态即可

查看代码
 #include<bits/stdc++.h>
using namespace std;
#define int long long
#define PII pair<int, int>
#define PDI pair<double, int>
const double eps = 1e-12;
const int N = 1e4 + 5;
const int dx[4] = {0,-1}, dy[4] = {-1, 0};
const int INF = 0x3f3f3f3f, mod = 1e9 + 7;

int f[105][65][65][25];

void solve() {
     int n, m, k;
     cin >> n >> m >> k;
     vector<int> sum(100);
     auto C = [](int x) {
         int ans = 0;
         while (x > 0) {
             if (x % 2 == 1)ans ++;
             x /= 2;
         }
         return ans;
     };
     for (int i = 0; i < 1 << n; ++i) sum[i] = C(i);
     for (int i = 0; i < 1 << n; ++i)
         for (int j = 0; j < 1 << n; ++j) f[2][i][j][sum[i] + sum[j]] = 1;
     for (int i = 2; i <= m; ++i) {
         for (int c = 0; c < 1 << n; ++c) {
             for (int d = sum[c]; d <= k; ++d) {
                 for (int b = 0; b < 1 << n; ++b) {
                     for (int a = 0; a < 1 << n; ++a) {
                         if (sum[c] + sum[b] + sum[a] > k) continue;
                         if (a & (c >> 1) || a & (c << 1) || a & (b >> 2) || a & (b << 2) || b & (c >> 2) || b & ( c << 2))continue;
                         f[i][b][c][d] = (f[i][b][c][d] + f[i - 1][a][b][d - sum[c]]) % mod;
                     }
                 }
             }
         }
     }
     int ans = 0;
     for (int i = 0; i < 1 << n; ++i) {
         for (int j = 0; j < 1 << n; ++j) ans = (ans + f[m][i][j][k]) % mod;
     }
     cout << ans;
}

signed main() {
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    int T = 1;
//    cin >> T;
    while (T--) {
        solve();
    }
}

J

 
posted @ 2024-04-02 19:00  bible_w  阅读(12)  评论(0编辑  收藏  举报