题目相关
题目链接
AtCoder Beginner Contest 179 D 题,https://atcoder.jp/contests/abc179/tasks/abc179_d。
Problem Statement
There are N cells arranged in a row, numbered 1,2,…,N from left to right.
Tak lives in these cells and is currently on Cell 1. He is trying to reach Cell N by using the procedure described below.
You are given an integer K that is less than or equal to 10, and K non-intersecting segments [L1,R1],[L2,R2],…,[LK,RK]. Let S be the union of these K segments. Here, the segment [l,r] denotes the set consisting of all integers ii that satisfy l≤i≤r.
- When you are on Cell i, pick an integer dd from S and move to Cell i+d. You cannot move out of the cells.
To help Tak, find the number of ways to go to Cell N, modulo 998244353.
Input
Input is given from Standard Input in the following format:
N K
L1 R1
L2 R2
:
LK RK
Output
Print the number of ways for Tak to go from Cell 1 to Cell N, modulo 998244353.
Samples1
Sample Input 1
5 2
1 1
3 4
Sample Output 1
4
Explaination
The set S is the union of the segment [1,1] and the segment [3,4], therefore S={1,3,4} holds.
There are 4 possible ways to get to Cell 5:
- 1→2→3→4→5,
- 1→2→5,
- 1→4→5 and
- 1→5.
Samples2
Sample Input 2
5 2
3 3
5 5
Sample Output 2
0
Explaination
Because S={3,5} holds, you cannot reach to Cell 5. Print 0.
Samples3
Sample Input 3
5 1
1 2
Sample Output 3
5
Samples4
Sample Input 4
60 3
5 8
1 3
10 15
Sample Output 4
221823067
Constraints
- 2 ≤ N ≤ 2×10^5
- 1 ≤ K ≤ min(N,10)
- 1 ≤ Li ≤ Ri ≤ N
- [Li, Ri] and [Lj, Rj] do not intersect (i ≠ j)
- All values in input are integers.
题解报告
题目翻译
给 N 个房间排成一行,房间编号为 1, 2, ..., N。高桥目前住的房间号为 1,他想移动到编号为 N 的房间。
我们提供一个整数 K(K≤10)和 K 个不相交的区间 ,集合 S 是这 K 个区间的并集。移动的规则如下:
- 高桥在房间 i,可以从集合 S 中选择一个整数 d,这样可以移动到房间 i+d。
- 高桥不可以移动到房间以外。
题目分析
看上去又是一个 DFS 题目。由于 N 的最大值为 2e5,可能会出现 TLE。
本题可以用递推来完成。
样例数据分析
我们使用 DFS 的思路来分析。
样例 1
根据样例,N=5 表示一个 5 个房间 1, 2, 3, 4, 5。K=2 表示两个不相交区间,分别是 [1, 1] 和 [3, 4]。这样构成一个集合 S [1, 3, 4]。高桥将从 1 号房间出发,要到达 5 号房间。我们采用 DFS 方式,辅助数据为一个堆栈 S 和房间访问标志,我们设计的访问距离根据集合数据次序,也就是先 1,再 3,再 4。
1、初始状态。堆栈 S 为空,所有房间都没有访问过。如下图。
2、开始的时候将起点 1 放到堆栈 S 中,这样 S 内的元素为 {1}。如下图所示:
3、堆栈 S 不是空,弹出 S 的对顶元素。得到 1,1 不是目标。根据访问次序,2(1+1)将 2 压入堆栈;4(1+3)将 4 压入堆栈;5(1+4),由于 5 是目标,所以等到一个答案,也就是 1→5。目前堆栈数据下图。
4、堆栈 S 不是空,弹出 S 的对顶元素。得到 3,3 不是目标。根据访问次序,4(3+1)将 4 压入堆栈;6(3+3)超过房间号码,丢弃。目前堆栈数据下图。
5、堆栈 S 不是空,弹出 S 的对顶元素。得到 4,4 不是目标。根据访问次序,5(5+1),得到一个答案,也就是1→4→5;7(4+3)超过房间号码,丢弃。目前堆栈数据下图。
6、堆栈 S 不是空,弹出 S 的对顶元素。得到 2,2 不是目标。根据访问次序,3(2+1)将 3 压入堆栈;5(2+3),得到一个答案,也就是1→2→5;6(2+4),超过房间号码,丢弃。目前堆栈数据下图。
7、堆栈 S 不是空,弹出 S 的对顶元素。得到 3,3 不是目标。根据访问次序,4(3+1)将 4 压入堆栈;6(3+3),超过房间号码,丢弃。目前堆栈数据下图。
8、堆栈 S 不是空,弹出 S 的对顶元素。得到 4,4 不是目标。根据访问次序,5(4+1)到一个答案,也就是1→2→3→4→5;7(4+3),超过房间号码,丢弃。目前堆栈数据下图。
9、堆栈 S 为空,搜索结束。
这样我们得到了 4 个答案,分别是:
1、1→5
2、1→4→5;
3、1→2→5;
4、1→2→3→4→5。
样例 2
根据样例,N=5 表示一个 5 个房间 1, 2, 3, 4, 5。K=1 表示一个不相交区间,[1, 2]。这样构成一个集合 S [1, 2]。高桥将从 1 号房间出发,要到达 5 号房间。
我就不画了,挺累的。有兴趣,自己画画吧。增加对 DFS 的理解。
DFS参考代码
//https://atcoder.jp/contests/abc179/tasks/abc179_d
#include <iostream>
#include <vector>
#include <algorithm>
#include <stack>
using namespace std;
const int MAXK=10+2;
const int MO=998244353;
int l[MAXK];
int len=0;
stack<int> myStack;
unsigned long long dfs(int start, int target) {
unsigned long long ans = 0;
myStack.push(start);
while (false == myStack.empty()) {
int x = myStack.top();
myStack.pop();
for (int i=0; i<len; i++) {
int y=x+l[i];
if (target==y) {
ans=(ans+1)%MO;
} else if (target>y) {
myStack.push(y);
} else {
break;
}
}
}
return ans;
}
int main() {
int n,k;
cin>>n>>k;
for (int i=0; i<k; i++) {
int x;
for (int j=0; j<2; j++) {
cin>>x;
int *p=find(l, l+len, x);
if (p==l+len) {
l[len]=x;
len++;
}
}
}
sort(l, l+len);
unsigned long long ans=dfs(1, n);
cout<<ans<<"\n";
return 0;
}
我去,竟然有 WA。
具体看了一下,一个WA + 六个 TLE。为什么会 WA,我再考虑一下细节。失败。
递推
哎,换一个思路吧。设 ans[i] 表示走到 i 的方案数。
根据题目描述,对于一个区间 [l, r],显然我们可以推导出 ans[i]=ans[i-l]+ans[i-l+1]+...+ans[i-r]。
这又是一个前缀和。
递推参考代码
//https://atcoder.jp/contests/abc179/tasks/abc179_d
#include <iostream>
#include <vector>
#include <algorithm>
#include <utility>
using namespace std;
const int MAXN=2e5+4;
long long ans[MAXN];//递推答案
long long sum[MAXN];//前缀和
const int MAXK=10+4;
pair<int, int> seg[MAXK];//区间
const int MO=998244353;
int main() {
int n,k;
cin>>n>>k;
for (int i=1; i<=k; i++) {
cin>>seg[i].first>>seg[i].second;
}
//初始化
ans[1]=1;
sum[1]=1;
for (int i=2; i<=n; i++) {
for (int j=1; j<=k; j++) {
int l=seg[j].first;
int r=seg[j].second;
ans[i]=(ans[i]+sum[max(0, i-l)]-sum[max(0, i-r-1)]+MO)%MO;
}
sum[i]=(sum[i-1]+ans[i])%MO;
}
cout<<ans[n]<<"\n";
return 0;
}
时间复杂度
很明显是 O(N*K)。由于本题的 K 非常小,因此可以认为是 O(N)。
补充
水平真菜,这个思路在竞赛的时候竟然没有想到,脑子一片浆糊。