2020 年 “联想杯”第三届上海理工大学程序设计竞赛 H. Hay Mower 矩阵操作

文章目录

    • 1. 题目描述
      • 1.1. Limit
      • 1.2. Problem Description
      • 1.3. Input
      • 1.4. Output
      • 1.5. Sample Input 1
      • 1.6. Sample Output 1
      • 1.7. Sample Input 2
      • 1.8. Sample Output 2
      • 1.10. Source
    • 2. 解读
    • 3. 代码

1. 题目描述

1.1. Limit

Time Limit: 2.0 sec

Memory Limit: 256 MB

1.2. Problem Description

Are you tired of city life? Have you ever had illusions of pastoral peace? The clean atmosphere, the closeness to nature and the gentle pace of living, all made Setsuna yearn for the pastoral life more.

In order to experience the simple pastoral life, Setsuna has moved to Star Valley and started her farming journey.

Soon, she discovers the problem: overgrown weeds are harming her farm. In Chinese, we call it “Sheng Cao”. She realized that weeding should be put at the top priority.

The farm can be described as an n × m n \times m n×m matrix. The growth rate of weed in row i i i and column j j j is denoted as a i , j a_{i,j} ai,j, indicating that the weed will grow a i , j a_{i,j} ai,j units every beginning of the moment. At the end of moment 0 0 0, there is no weed on the farm.

Setsuna will use mower k k k times, where the i i i-th use occurs at the end of moment t i t_i ti. Each use of the mower completely removes weeds in a row or column.

Setsuna wonders how many units of weed she will remove.

The answer might be very large, so please output the desired answer modulo 998244353 998244353 998244353.


1.3. Input

The first line contains three integers n , m , k ( 1 ≤ n , m ≤ 500 , 1 ≤ k ≤ 3 × 1 0 5 ) n,m,k(1\le n,m \le 500,1 \le k \le 3 \times 10^5) n,m,k(1n,m500,1k3×105).

The next n n n lines contains m m m integers each, where the j j j-th integer of the i i i-th line is a i , j a_{i,j} ai,j ( 0 ≤ a i , j ≤ 1 0 18 ) (0 \le a_{i,j} \le 10^{18}) (0ai,j1018).

The i i i-th of the next k k k lines contains one character and twointegers.

  • r r r x i x_i xi t i t_i ti - Setsuna clears the weeds in row x i x_i xi at the end of moment t i t_i ti.
  • c c c y i y_i yi t i t_i ti - Setsuna clears the weeds in column y i yi yi at the end of moment t i t_i ti.

For each test case output a single line containing “YES” if it is possible to solve the jigsaw puzzle, or “NO” otherwise. You can print each letter in any case (upper or lower).

It is guaranteed that 1 ≤ x i ≤ n , 1 ≤ y i ≤ m , 1 ≤ t i ≤ 1 0 18 1 \le x_i \le n,1 \le y_i \le m,1 \le t_i \le 10^{18} 1xin,1yim,1ti1018 hold for 1 ≤ i ≤ k 1 \le i \le k 1ik and t i t_i ti is strictly increasing.


1.4. Output

Output one integer indicating the answer modulo 998244353 998244353 998244353.


1.5. Sample Input 1

2 2 3
1 2
3 4
r 1 5
c 2 6
r 1 7

1.6. Sample Output 1

45

1.7. Sample Input 2

3 4 1
1 2 3 4
5 6 7 8
9 10 11 12
r 1 1000000000000000000

1.8. Sample Output 2

172998509

1.10. Source

2020 年 “联想杯”全国高校程序设计在线邀请赛暨第三届上海理工大学程序设计竞赛 H. Hay Mower


2. 解读

直接用二维数组存矩阵,然后在每个 t i t_i ti 对整个矩阵进行更新的话会超时。

通过对题目再进行分析,我们可以发现每次对矩阵进行操作时只影响到某一行或某一列,所以我们用 t i m e M a t r i x timeMatrix timeMatrix 存储上一次更新时间,在每个 t i t_i ti 对其操作的行或列进行运算,将该行/列在 t i − t i − 1 t_i - t_{i-1} titi1时间内新增加的数值累加就得到了答案,然后将 t i m e M a t r i x timeMatrix timeMatrix 存储的上一次更新时间赋值为 t i t_i ti 即可。

还有一个需要注意的地方就是每次相乘运算都要先对两个数取模,不然特别容易溢出。

3. 代码

#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;

const int NUM = 500 + 1;
const long long mod = 998244353;

long long timeMatrix[NUM][NUM] = { { 0 } };
long long growMatrix[NUM][NUM] = { { 0 } };

map<long long, pair<char, long long>> mp;

int main()
{
    long long n, m, k, ans = 0;
    scanf("%lld %lld %lld", &n, &m, &k);
    long long ai, aj, mark, t, lastMoment, moment = 0;
    char ch;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            scanf("%lld", &growMatrix[i][j]);
        }
    }
    // 读换行符
    getchar();
    while (k--) {
        ch = getchar();
        scanf("%lld %lld\n", &ai, &aj);
        mp[aj] = make_pair(ch, ai);
    }
    // 计算
    for (auto it = mp.begin(); it != mp.end(); it++) {
        t = it->first;
        ch = it->second.first;
        mark = it->second.second;

        // 行
        if (ch == 'r') {
            for (int i = 1; i <= m; i++) {
                ans = (ans + ((t - timeMatrix[mark][i]) % mod) * (growMatrix[mark][i] % mod)) % mod;
                timeMatrix[mark][i] = t;
            }
        } else {
            // 列
            for (int j = 1; j <= n; j++) {
                ans = (ans + ((t - timeMatrix[j][mark]) % mod) * (growMatrix[j][mark] % mod)) % mod;
                timeMatrix[j][mark] = t;
            }
        }
    }
    printf("%lld\n", ans);

    return 0;
}

联系邮箱[email protected]

CSDN:https://me.csdn.net/qq_41729780

知乎:https://zhuanlan.zhihu.com/c_1225417532351741952

公众号复杂网络与机器学习

欢迎关注/转载,有问题欢迎通过邮箱交流。

2020 年 “联想杯”第三届上海理工大学程序设计竞赛 H. Hay Mower 矩阵操作_第1张图片

你可能感兴趣的:(算法刷题笔记,#,矩阵操作,算法,数据结构,c++,矩阵运算)