Skip to content

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

目录#

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 matrix. The growth rate of weed in row and column is denoted as , indicating that the weed will grow units every beginning of the moment. At the end of moment , there is no weed on the farm.

Setsuna will use mower times, where the -th use occurs at the end of moment . 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 .


1.3. Input#

The first line contains three integers .

The next lines contains integers each, where the -th integer of the -th line is .

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

  • - Setsuna clears the weeds in row at the end of moment .
  • - Setsuna clears the weeds in column at the end of moment .

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 hold for and is strictly increasing.


1.4. Output#

Output one integer indicating the answer modulo .


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. 解读#

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

通过对题目再进行分析,我们可以发现每次对矩阵进行操作时只影响到某一行或某一列,所以我们用 存储上一次更新时间,在每个 对其操作的行或列进行运算,将该行/列在 时间内新增加的数值累加就得到了答案,然后将 存储的上一次更新时间赋值为 即可。

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

3. 代码#

#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <string.h>
#include <vector>
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;
}

联系邮箱:curren_wong@163.com

CSDNhttps://me.csdn.net/qq_41729780

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

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

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

二维码

Back to top