文章目錄
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;
}
聯繫郵箱:[email protected]
CSDN:https://me.csdn.net/qq_41729780
知乎:https://zhuanlan.zhihu.com/c_1225417532351741952
公衆號:複雜網絡與機器學習
歡迎關注/轉載,有問題歡迎通過郵箱交流。