2026团体程序设计天梯赛256分参考代码(L3-2 16分 L3-3 10分)

L1-1

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define int ll
using vi = vector<int>;
#define all(a) a.begin(), a.end()
signed main() {
    cout << "Building the Future, One Line of Code at a Time." << endl;
    return 0;
}

L1-2

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define int ll
using vi = vector<int>;
#define all(a) a.begin(), a.end()
signed main() {
    int n;
    cin >> n;
    cout << n * 15 << endl;
    return 0;
}

L1-3

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define int ll
using vi = vector<int>;
#define all(a) a.begin(), a.end()
signed main() {
    int a, b;
    cin >> a >> b;
    int c = b - a;
    cout << c << endl;
    if (c < 0) {
        cout << "hai sheng ma?" << endl;
    } else if (c > 250) {
        cout << "jiu ting tu ran de..." << endl;
    } else {
        cout << "nin tai cong ming le!" << endl;
    }
    return 0;
}

L1-4

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define int ll
using vi = vector<int>;
#define all(a) a.begin(), a.end()
signed main() {
    int n;
    cin >> n;
    int ans = 0, tmp;
    for (int i = 0; i < n; i++) {
        cin >> tmp;
        if (tmp < 1700) {
            ans++;
        }
    }
    cout << ans << endl;
    return 0;
}

L1-5

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define int ll
using vi = vector<int>;
#define all(a) a.begin(), a.end()
signed main() {
    int n;
    cin >> n;
    map<int, int> mp;
    int id, op;
    for (int i = 0; i < n; i++) {
        cin >> id >> op;
        if (op == 0) {
            if (mp[id] == 0) {
                mp[id] = 1;
            }
        } else {
            mp[id] = 2;
        }
    }
    vi ans;
    for (auto [a, b] : mp) {
        if (b == 1) {
            ans.emplace_back(a);
        }
    }
    sort(all(ans));
    int sz = ans.size();
    if (sz == 0) {
        cout << "NONE" << endl;
    } else {
        for (int i = 0; i < sz; i++) {
            cout << ans[i] << (i == sz - 1 ? '\n' : ' ');
        }
    }
    return 0;
}

L1-6

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define int ll
using vi = vector<int>;
#define all(a) a.begin(), a.end()
signed main() {
    string ans = "";
    string s;
    for (int i = 0; i < 11; i++) {
        getline(cin, s);
        ans += s.size() + '0';
    }
    cout << ans << endl;
    return 0;
}

L1-7

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define int ll
using vi = vector<int>;
#define all(a) a.begin(), a.end()
ll INF = 0x3f3f3f3f3f3f3f3f;
signed main() {
    int n;
    cin >> n;
    int maxx = 0, minn = INF, tot = 0;
    vi a(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i];
        maxx = max(maxx, a[i]);
        minn = min(minn, a[i]);
        tot += a[i];
    }
    int avg = tot / n;
    vi ans;
    for (int i = 0; i < n; i++) {
        if (a[i] > 2 * avg) {
            ans.emplace_back(i + 1);
        }
    }
    int sz = ans.size();
    cout << maxx << ' ' << minn << ' ' << avg << endl;
    if (sz == 0) {
        cout << "Normal" << endl;
    } else {
        for (int i = 0; i < sz; i++) {
            cout << ans[i] << (i == sz - 1 ? '\n' : ' ');
        }
    }
    return 0;
}

L1-8

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define int ll
using vi = vector<int>;
#define all(a) a.begin(), a.end()
signed main() {
    int n;
    string s;
    cin >> n >> s;
    int op;
    for (int i = 0; i < n; i++) {
        cin >> op;
        int sz = s.size();
        if (op == 1) {
            string s1;
            cin >> s1;
            int sz1 = s1.size();
            vi ans;
            for (int i = 0; i < sz - sz1 + 1; i++) {
                for (int j = 0; j < sz1; j++) {
                    if (s[i + j] != s1[j])
                        break;
                    if (j == sz1 - 1) {
                        ans.emplace_back(i);
                    }
                }
            }
            int sza = ans.size();
            if (sza == 0) {
                cout << -1 << endl;
            } else {
                sza = min(sza, 3ll);
                for (int i = 0; i < sza; i++) {
                    cout << ans[i] << (i == sza - 1 ? '\n' : ' ');
                }
            }
        } else if (op == 2) {
            int p;
            string s2;
            cin >> p >> s2;
            string ss;
            if (p == sz) {
                ss = s + s2;
            } else {
                ss += s.substr(0, p);
                ss += s2;
                ss += s.substr(p, sz);
            }
            cout << ss << endl;
            s = ss;
        } else {
            int l, r;
            cin >> l >> r;
            string ss;
            ss += s.substr(0, l);
            for (int i = r; i >= l; i--) {
                ss += s[i];
            }
            ss += s.substr(r + 1, sz);
            cout << ss << endl;
            s = ss;
        }
    }
    return 0;
}

L2-1

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define int ll
using vi = vector<int>;
#define all(a) a.begin(), a.end()
signed main() {
    int n, t;
    cin >> n >> t;
    vi a(n), ans;
    int tot = 0;
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }
    stack<int> st;
    for (int i = 0; i < n; i++) {
        if (a[i] <= t) {
            ans.emplace_back(i + 1);
        } else {
            st.push(i);
            tot += a[i];
        }
    }
    while (!st.empty()) {
        stack<int> st2;
        t = tot / st.size();
        tot = 0;
        while (!st.empty()) {
            int aa = st.top();
            st.pop();
            if (a[aa] <= t) {
                ans.emplace_back(aa + 1);
            } else {
                st2.push(aa);
                tot += a[aa];
            }
        }
        st = st2;
    }
    for (int i = 0; i < n; i++) {
        cout << ans[i] << (i == n - 1 ? '\n' : ' ');
    }
    return 0;
}

L2-2

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define int ll
using vi = vector<int>;
#define all(a) a.begin(), a.end()
signed main() {
    int n;
    cin >> n;
    vi a(n);
    int maxx = 0;
    for (int i = 0; i < n; i++) {
        cin >> a[i];
        maxx = max(maxx, a[i]);
    }
    vi ansmax;
    for (int i = 0; i < n; i++) {
        if (a[i] == maxx) {
            ansmax.emplace_back(i + 1);
        }
    }
    int sz = ansmax.size();
    for (int i = 0; i < sz; i++) {
        cout << ansmax[i] << (i == sz - 1 ? '\n' : ' ');
    }
    int m, x;
    cin >> m;
    vi aa = a;
    sort(all(aa));
    map<int, int> mp;
    for (int i = 0; i < n; i++) {
        if (mp[a[i]] == 0) {
            mp[a[i]] = i + 1;
        }
    }
    for (int i = 0; i < m; i++) {
        cin >> x;
        int tar = *lower_bound(all(aa), x + 1);
        if (x >= maxx) {
            cout << 0 << endl;
        } else {
            cout << mp[tar] << endl;
        }
    }
    return 0;
}

L2-3

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define int ll
using vi = vector<int>;
using vvi = vector<vi>;
#define all(a) a.begin(), a.end()
vector<vector<pair<int, int>>> adj;
int maxx;
vi ans;
map<int, vi> mp;
void dfs(int a, int curr) {
    ans[a] = curr;
    for (auto [j, s] : adj[a]) {
        if (ans[j] == -1) {
            int tmp = min(curr, s);
            dfs(j, tmp);
        }
    }
    if (adj[a].size() < 2 && a != 0) {
        mp[curr].push_back(a);
        maxx = max(maxx, curr);
    }
}
signed main() {
    int n;
    cin >> n;
    adj.assign(n, vector<pair<int, int>>());
    ans.assign(n, -1);
    int j, s;
    for (int i = 1; i < n; i++) {
        cin >> j >> s;
        adj[i].push_back({j, s});
        adj[j].push_back({i, s});
    }
    dfs(0, 100);
    cout << maxx << endl;
    int sz = mp[maxx].size();
    sort(all(mp[maxx]));
    for (int i = 0; i < sz; i++) {
        cout << mp[maxx][i] << (i == sz - 1 ? '\n' : ' ');
    }
    return 0;
}

L2-4

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define int ll
using vi = vector<int>;
#define all(a) a.begin(), a.end()
vector<vector<pair<int, int>>> adj;
vector<bool> flag;
vi ans;
void dfs(int a) {
    ans.emplace_back(a);
    flag[a] = true;
    int next = -1, maxx = -1;
    for (auto [b, p] : adj[a]) {
        if (!flag[b] && (p > maxx || (p == maxx && b < next))) {
            maxx = p;
            next = b;
        }
    }
    if (next != -1)
        dfs(next);
}
signed main() {
    int n, m;
    cin >> n >> m;
    adj.assign(n + 1, vector<pair<int, int>>());
    int a, b, p;
    for (int i = 0; i < m; i++) {
        cin >> a >> b >> p;
        adj[a].push_back({b, p});
    }
    int k;
    cin >> k;
    int root;
    for (int i = 0; i < k; i++) {
        flag.assign(n + 1, false);
        ans.clear();
        cin >> root;
        dfs(root);
        int sz = ans.size();
        for (int i = 0; i < sz; i++) {
            cout << ans[i] << (i == sz - 1 ? "\n" : "->");
        }
    }
    return 0;
}

L3-1

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define int ll
using vi = vector<int>;
#define all(a) a.begin(), a.end()
struct peo {
    int t1, t2, age;
    string id;
    bool flag;
};
signed main() {
    int n;
    cin >> n;
    vector<peo> P(n);
    vi arr(n + 1, 0);
    map<int, int> mp;
    vector<pair<int, string>> ans;
    for (int i = 0; i < n; i++) {
        cin >> P[i].t1 >> P[i].t2 >> P[i].id >> P[i].age;
        P[i].flag = false;
        arr[P[i].t1] = i + 1;
        mp[P[i].t2] = i;
    }
    for (int i = 1; i <= n; i++) {
        if (arr[i] == 0) {
            arr[i] = arr[i - 1];
        }
    }
    int i = 0, j = 0;
    set<int> s, old;
    while (ans.size() < n) {
        i++;
        for (; i <= n && j < arr[i]; j++) {
            if (P[j].age >= 80)
                old.insert(P[j].t2);
            else
                s.insert(P[j].t2);
        }
        if (i <= n && P[mp[i]].t1 <= i && !P[mp[i]].flag) {
            if (P[mp[i]].age >= 80)
                old.erase(old.find(i));
            else
                s.erase(s.find(i));
            P[mp[i]].flag = true;
            ans.push_back({i, P[mp[i]].id});
        } else {
            if (old.size() > 0) {
                P[mp[*old.begin()]].flag = true;
                ans.push_back({i, P[mp[*old.begin()]].id});
                old.erase(old.begin());
            } else if (s.size() > 0) {
                P[mp[*s.begin()]].flag = true;
                ans.push_back({i, P[mp[*s.begin()]].id});
                s.erase(s.begin());
            }
        }
    }
    for (int i = 0; i < n; i++) {
        cout << ans[i].first << ' ' << ans[i].second << endl;
    }
    return 0;
}

L3-2

惊天不对称小巧思骗到16分

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define int ll
using vi = vector<int>;
#define all(a) a.begin(), a.end()
void work() {
    int n;
    cin >> n;
    map<int, int> R, B, RR, BB;
    vector<pair<int, int>> a;
    int ans = n;
    for (int i = 0; i < n; i++) {
        int r, b;
        cin >> r >> b;
        a.push_back({r, b});
        R[r]++, B[b]++;
    }
    for (auto [r, b] : a) {
        if (R[r] > 1 && B[b] > 1) {
            RR[r] += 2; // 神之一手
            BB[b]++;
        }
    }
    for (auto [r, b] : a) {
        if (R[r] == 1) {
            R[r]--;
        } else if (B[b] == 1) {
            B[b]--;
        } else if (RR[r] > BB[b]) {
            R[r]--;
        } else if (BB[b] > RR[r]) {
            B[b]--;
        } else if (R[r] >= B[b]) {
            B[b]--;
        } else {
            R[r]--;
        }
    }
    for (auto [_, __] : R) {
        if (__ > 0) {
            ans++;
        }
    }
    for (auto [_, __] : B) {
        if (__ > 0) {
            ans++;
        }
    }
    cout << ans << endl;
}
signed main() {
    int T;
    cin >> T;
    while (T--) {
        work();
    }
    return 0;
}

L3-3

暴力10分

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define int ll
using vi = vector<int>;
#define all(a) a.begin(), a.end()
void work() {
    int n;
    cin >> n;
    vi a(n);
    int ans = 0;
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }
    for (int i = 0; i < pow(n, n); i++) {
        vi b(n, 1);
        int ii = i;
        for (int j = n - 1; j >= 0; j--) {
            b[j] += ii % n;
            ii /= n;
        }
        for (int j = 0; j < n; j++) {
            if (b[a[j] - 1] != a[b[j] - 1]) {
                break;
            }
            if (j == n - 1) {
                ans++;
            }
        }
    }
    cout << ans << endl;
}
signed main() {
    int T;
    cin >> T;
    while (T--) {
        work();
    }
    return 0;
}
暂无评论

发送评论 编辑评论


				
😋
😘
😍
😂
😅
😡
😫
😱
😭
✌️
👍
🙏
🍭
😀
🙂
😊
🙃
😌
😜
😝
😏
😒
🙄
😳
🌝
😣
😶
😔
🌚
🙌
🖕
💩
👻
🙈
💊
🍦
🍉
Emoji
本人惯用
猫猫虫
上一篇