T1:字符串加密(二)

本题难度简单,是一个模拟题,注意 \(k\) 可能非常大,需要先模 \(26\)

代码实现
#include <bits/stdc++.h>

using namespace std;
using ll = long long;

int main() {
    string m;
    cin >> m;
    ll k;
    cin >> k;
    k %= 26;
    
    string ans;
    for (char c : m) {
        if (isupper(c)) ans += (c-'A'-k+26)%26 + 'A';
        else ans += (c-'a'-k+26)%26 + 'a';
    }
    
    cout << ans << '\n';
    
    return 0;
}

T2:石头剪刀布

本题难度较大,是一个模拟+排序的问题,用结构体绑定每个人的得分和编号,然后按题意模拟即可。

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

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

bool win(char a, char b) {
	if (a == 'C' and b == 'G') return true;
	if (a == 'P' and b == 'C') return true;
	if (a == 'G' and b == 'P') return true;
	return false;
}

int main() {
	int n, m;
	cin >> n >> m;
	
	int n2 = n*2;
	vector<string> a(n2);
	rep(i, n2) cin >> a[i];
	
	vector<P> d(n2);
	rep(i, n2) d[i] = P(0, i);
	rep(mi, m) {
		rep(ni, n) {
			int i = ni*2, j = ni*2+1;
			int ai = d[i].second, aj = d[j].second;
			if (win(a[ai][mi], a[aj][mi])) d[i].first--;
			if (win(a[aj][mi], a[ai][mi])) d[j].first--;
		}
		sort(d.begin(), d.end());
	}
	
	rep(i, n2) {
	    cout << d[i].second + 1 << '\n';
	}
	
	return 0;
} 

T3:Excel表格函数

本题难度较大,通过普通的模拟就可以拿到 \(50\) 分。注意到保证操作 \(1\)、操作 \(4\) 和操作 \(5\) 出现的总次数不超过 \(10000\) 次,因此可以使用前缀和技巧处理每次询问的区间和。

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

using namespace std;

int a[2005][2005];
int row_s[2005][2005];
int col_s[2005][2005];

int main() {
    cin.tie(nullptr) -> sync_with_stdio(false);
    
    int n, m, q;
    cin >> n >> m >> q;
    
    rep(i, n)rep(j, m) cin >> a[i][j];
    
    rep(i, n)rep(j, m) row_s[i][j] += row_s[i][j-1]+a[i][j];
    rep(i, n)rep(j, m) col_s[i][j] += col_s[i-1][j]+a[i][j];
    
    rep(qi, q) {
        int op;
        cin >> op;
        if (op == 1) {
            int x, y, z;
            cin >> x >> y >> z;
            a[x][y] += z;
            for (int j = y; j <= m; ++j) row_s[x][j] += z;
            for (int i = x; i <= n; ++i) col_s[i][y] += z;
        }
        else if (op == 2) {
            int x, y1, y2, s = 0;
            cin >> x >> y1 >> y2;
            s += row_s[x][y2];
            s -= row_s[x][y1-1];
            cout << s << '\n';
        }
        else if (op == 3) {
            int y, x1, x2, s = 0;
            cin >> y >> x1 >> x2;
            s += col_s[x2][y];
            s -= col_s[x1-1][y];
            cout << s << '\n';
        }
        else if (op == 4) {
            int x, y1, y2, s = -1001001001;
            cin >> x >> y1 >> y2;
            for (int j = y1; j <= y2; ++j) s = max(s, a[x][j]);
            cout << s << '\n';
        }
        else {
            int y, x1, x2, s = 1001001001;
            cin >> y >> x1 >> x2;
            for (int i = x1; i <= x2; ++i) s = min(s, a[i][y]);
            cout << s << '\n';
        }
    }
    
    return 0;
}