T1:数字字符串

本题难度中等,使用队列存储 \(s\) 的字符。用一个变量 \(r\) 维护 \(s\)\(\bmod\) 的余数

  • 对于操作 \(1\),直接 \(r = r \cdot 10 +x\) 后模 \(\bmod\)
  • 对于操作 \(2\),设 \(s\) 此时有 \(k\) 位、首位是 \(a\),那么删除首位后, \(r\) 应该减少 \(a \cdot 10^{k-1}\)
  • 对于操作 \(3\),直接输出 \(r\)
代码实现
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)

using namespace std;
using ll = long long;

const int mod = 998244353;
//const int mod = 1000000007;
struct mint {
    ll x;
    mint(ll x=0):x((x%mod+mod)%mod) {}
    mint operator-() const {
        return mint(-x);
    }
    mint& operator+=(const mint a) {
        if ((x += a.x) >= mod) x -= mod;
        return *this;
    }
    mint& operator-=(const mint a) {
        if ((x += mod-a.x) >= mod) x -= mod;
        return *this;
    }
    mint& operator*=(const mint a) {
        (x *= a.x) %= mod;
        return *this;
    }
    mint operator+(const mint a) const {
        return mint(*this) += a;
    }
    mint operator-(const mint a) const {
        return mint(*this) -= a;
    }
    mint operator*(const mint a) const {
        return mint(*this) *= a;
    }
    mint pow(ll t) const {
        if (!t) return 1;
        mint a = pow(t>>1);
        a *= a;
        if (t&1) a *= *this;
        return a;
    }

    // for prime mod
    mint inv() const {
        return pow(mod-2);
    }
    mint& operator/=(const mint a) {
        return *this *= a.inv();
    }
    mint operator/(const mint a) const {
        return mint(*this) /= a;
    }
};
istream& operator>>(istream& is, mint& a) {
    return is >> a.x;
}
ostream& operator<<(ostream& os, const mint& a) {
    return os << a.x;
}

int main() {
    int Q;
    cin >> Q;
    
    queue<int> q;
    q.push(1);
    mint ans = 1;
    rep(qi, Q) {
        int type;
        cin >> type;
        if (type == 1) {
            int x;
            cin >> x;
            q.push(x);
            ans = ans*10+x;
        }
        if (type == 2) {
            int x = q.front(); q.pop();
            ans -= mint(x)*mint(10).pow(q.size());
        }
        if (type == 3) {
            cout << ans << '\n';
        }
    }
    
    return 0;
}

T2:战车的得分

本题难度较大,一个简单的想法是选择和最大的行以及和最大的列,但是如果行列的交叉点处有棋子,就需要减去交叉点的棋子,这样得到的和就不一定是最大了。所以我们需要枚举行。

首先需要离散化。用 map 记录每行每列的和。每个棋子的位置和分数也用 map 记录,可以用 pair 作为 key,也可以做二级 map。下面是使用二级 map 的代码:

map<int, ll> rsum, csum;
map<int, map<int, int>> s;
for 所有点(r, c, x):
    rsum[r] += x;
    csum[c] += x;
    s[r][c] = x

接下来将行与列的和分别按照从大到小排序。(map中是按照坐标排序的,需要用结构体重新排序)
按照和从大到小排序后,用二重循环枚举行 \(r\)、列 \(c\),用 rsum[r]+csum[c]-s[r][c] 去更新答案。直接二重循环的复杂度是 \(O(n^2\log n)\)
如果遇到某列的交叉点处没有棋子(即 s[r][c] = 0),那么此时的得分是 rsum[c]+csum[c]。对于 csum 更小的列 c',得分 \(rsum[r]+csum[c']-s[r][c'] \leqslant rsum[r] + csum[c'] \leqslant rsum[r]+csum[c]\)。也就是说继续枚举 csum 更小的列也不会得到更好的答案,所以更新答案后直接 break。因为 \(s[r][c] \neq 0\) 的点最多 \(n\) 个,所以总时间复杂度为 \(O(n\log n)\)

代码实现
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)

using namespace std;
using ll = long long;
using P = pair<ll, int>;

inline void chmax(ll& x, ll y) { if (x < y) x = y; }

int main() {
    int n;
    cin >> n;
    
    map<int, ll> rsum, csum;
    map<int, map<int, int>> s;
    rep(i, n) {
        int r, c, x;
        cin >> r >> c >> x;
        rsum[r] += x;
        csum[c] += x;
        s[r][c] = x;
    }
    
    vector<P> a;
    for (auto [r, rs] : rsum) {
        a.emplace_back(rs, r);
    }
    vector<P> b;
    for (auto [c, cs] : csum) {
        b.emplace_back(cs, c);
    }
    sort(a.rbegin(), a.rend());
    sort(b.rbegin(), b.rend());
    
    ll ans = 0;
    rep(i, a.size()) {
        rep(j, b.size()) {
            auto [rs, r] = a[i];
            auto [cs, c] = b[j];
            ll now = rs+cs;
            if (s[r][c]) chmax(ans, now-s[r][c]);
            else {
                chmax(ans, now);
                break;
            }
            
        }
    }
    
    cout << ans << '\n';
    
    return 0;
}