跳到正文

个人记录

7.21 模拟总结

Max0823约 11 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 11 分钟

版本

  1. v1当前

day 1:

代码
模拟算法是OI中比较基础的内容,从这次做题,有几个点没有做好:
1.string 字符串 的输入输出处理(while(cin >> ) & getline)和字符串循环移位问题的思考,以后需要加强这方面的练习,考虑到学习C++的时间太短,一定要打好基础。
2.不开long long见祖宗!!!
3.学习使用map,set等数据结构。

以后要加强基础题目的练习,尽量能够在5分钟内写完一道题。

下面是详细内容:

A: WA×1+ AC(0:12:45) 签到题,给出立方体底面两顶点的坐标,则只要判断横坐标在两点围成的矩形内,竖坐标在范围内即可。 WA1:把立方体的高度h当作立方体的顶点竖坐标z0了。 AC:改了就过了。

B: WA×7+ TLE×1 + AC(6:57:00) 给一个字符串找另一个字符串数量,这题不用KMP,直接写滑动窗口就行,其实也很简单,但是写的有点迷糊,没用substr判字符串数量,结果自己写的字符串判数量拉了。 WA 1-5:失败的优化,没解决根本问题。 WA 6-7:优化了判字符串数量的部分,但是分类讨论不太对。 TLE:没有把k和6取最小值,变成O(n2)了。 AC:恶心我两小时…

C: AC(0:23:30) 先做的C题,一发AC,先读一个string,与str不同的标1,其他标0,如果只有一个0就输出坐标,如果只有一个1就输出第一个读入的坐标。 AC:一发。

D: WA×4 + AC(6:37:00) 石狮子转圈,把四个情况的次数算出取最小值就行。 WA×4:四个情况算错了(无语了) AC:竟然O(n)枚举都能过。

E: WA×1 + AC(3:30:52) 处理字符串,可以while(cin) 或者 getline(),没查过且没学过就放在查过的set(我用的map)里。 WA:思路不太好,是发现会一个词就ans—,但没考虑一个词多次出现的情况。 AC:思路见上,仿照即可。

H: WA×2 + RE×2 + AC 正常模拟,把三个类型转化下就行。但是积分可以超过1<<32,要开longlong。 RE: vector数组大小反了。 WA: 不开long long见祖宗!!! AC: 解脱了。

至于其他题我补完再汇总,就这样。

upd:2025.7.22

C++
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const double inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const int N = 1e5 + 20;
void work() {
    int z0, h, u0, v0, u1, v1;
    cin >> z0 >> h >> u0 >> v0 >> u1 >> v1;
    int q;
    cin >> q;
    while(q--) {
        int x, y, z, fl = 0;
        cin >> x >> y >> z;
        if(x >= min(u0, u1) && x <= max(u0, u1)) {
            if(y >= min(v0, v1) && y <= max(v0, v1)) {
                if(z <= h + z0 && z >= z0) fl = 1;
            }
        }
        if(fl == 1) cout << "Yes" << '\n';
        else cout << "No" << '\n';
    }

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


const string tar = "nanjing";
int count(const string &s) {
    int res = 0;
    for (int i = 0; i + 7 <= s.size(); i++) {
        if (s.substr(i, 7) == "nanjing") res++;
    }
    return res;
}
void work() {
    ll n, k;
    cin >> n >> k;
    string s;
    cin >> s;
    if(k == 0) {
    int ans = count(s);
        cout << ans << '\n';
        return;
    }
    if(n < 7) {
        cout << "0" << '\n';
        return;
    }
    int x = min(k, n);
    string S = s + s.substr(0, x);
    int l = S.size();
    vector<int> a(l, 0);
    for(int i = 0; i <= l - 7; ++i) a[i] = (S.substr(i, 7) == "nanjing");
    for(int i = 1; i < l; i++)  a[i] += a[i - 1];
    int ans = a[n-7];
    for(int i = 0, j = n - 6; j < l; i++, j++) ans = max(ans, a[j] - a[i]);
    cout << ans << '\n';
}


void work() {
    int n, m, k;
    cin >> n >> m >> k;
    string st;
    vector<vector<int>> v(n, vector<int>(m, 0));
    for(int i = 0; i < n; i++) {
        for(int j = 0; j < m; j++) {
            for(int o = 0; o < k; o++){
                char ch;
                cin >> ch;
                if(i == 0 && j == 0) st += ch;
                else if(ch != st[o]) v[i][j] = 1;
            }
        }
    }
    vector<pair<int, int>> ve;
    for(int i = 0; i < n; i++) {
        for(int j = 0; j < m; j++) {
            if(v[i][j] == 1) ve.push_back({i, j});
            if(ve.size() >= 2) break;
        }
    }
    if(ve.size() == 1) cout << ve[0].first + 1 << ' ' << ve[0].second + 1 << '\n';
    else cout << "1 1" << '\n';
}

ll tot[4] = {0};
void work() {
    ll n, ans = inf;
    cin >> n;
    vector<int> a(n);
    for(int i = 0; i < n; i++) {
        cin >> a[i];
    }
    for (int r = 0; r < 4; r++) {
        ll cnt = 0;
        for(auto &x : a) cnt += (x + 4 - r) % 4;
        cnt += (4 - (cnt + r) % 4) % 4;
        ans = min(ans, cnt);
    }
    cout << ans << '\n';
}


unordered_map<string, int> mp;
unordered_map<string, int> used;
void check(string &s) {
    for (char &c : s) {
        c = tolower(c);
    }
}
void solve(string &s) {
    string a;
    for (char c : s) {
        if (isalpha(c)) {
            a += c;
        } else {
            if (a.size()) {
                check(a);
                if (mp.count(a) == 0 && used.count(a) == 0) {
                    used[a] = 1;
                }
                a.clear();
            }
        }
    }
    if (a.size()) {
        if (mp.count(a) == 0 && used.count(a) == 0) {
            used[a] = 1;
        }
    }
}
void work() {
    int cnt = 0;
    string S;
    getline(cin, S);
    int n;
    cin >> n;
    for(int i = 0; i < n; i++) {
        string a;
        cin >> a;
        mp[a] = 1;
    }
    solve(S);
    cout << used.size() << '\n';
}


void work() {
    ll n, m, q;
    cin >> n >> m >> q;
    vector<ll> a(n+10, 0);
    vector< pair<ll, ll>> gra(m+1, {0, 0});
    for(int i = 1; i <= m; i++) gra[i].first = i;
    int now = 0, cnt = m;
    set<ll> st;
    for(int i = 0; i < q; i++) {
        int pos;
        cin >> pos;
        if(pos == 1){
            int x;
            cin >> x;
            if(x > n || x < 1 || a[x] == 1) continue;
            now = x; cnt = m; a[x] = 1;
            st.clear();
        }
        if(pos == 2) {
            int id, x;
            cin >> id >> x;
            if(st.find(id) != st.end() || x != now || id > m) continue;
            gra[id].second += cnt--;
            st.insert(id);
        }
        if(pos == 3) {
            int id, x;
            cin >> id >> x;
            if(st.find(id) != st.end() || x != now || id > m) continue;
            st.insert(id);
        }
    }
    sort(gra.begin() + 1, gra.end(), [](pair<int, int> a, pair<int, int> b) {
        if(a.second == b.second) return a.first < b.first;
        return a.second > b.second;
    });
    for(int i = 1; i <= m; i++) {
        cout << gra[i].first << ' ' << gra[i].second << '\n';
    }
}
原文