E. Sending a Sequence Over the Network
E. Sending a Sequence Over the Network
The sequence is sent over the network as follows:
- sequence is split into segments (each element of the sequence belongs to exactly one segment, each segment is group of consecutive elements of sequence);
- for each segment, its length is written next to it, either to the left of it or to the right of it;
- the resulting sequence is sent over the network.
For example, we needed to send the sequence . Suppose it was split into segments as follows: . Then we could have the following sequences:
- ,
- ,
- ,
- .
If a different segmentation had been used, the sent sequence might have been different.
The sequence is given. Could the sequence be sent over the network? In other words, is there such a sequence that converting to send it over the network could result in sequence ?
Input
The first line of input data contains a single integer — the number of test cases.
Each test case consists of two lines.
The first line of the test case contains an integer — the size of the sequence .
The second line of test case contains integers — the sequence itself.
It is guaranteed that the sum of over all test cases does not exceed .
Output
For each test case print on a separate line:
- if sequence could be sent over the network, that is, if sequence could be obtained from some sequence to send over the network.
- otherwise.
You can output and in any case (for example, strings , , and will be recognized as positive response).
Example
input
7 9 1 1 2 3 1 3 2 2 3 5 12 1 2 7 5 6 5 7 8 9 10 3 4 4 8 6 2 2 3 1 10 4 6 2 1 9 4 9 3 4 2 1 1
output
YES
YES
YES
NO
YES
YES
NO
Note
In the first case, the sequence could be obtained from the sequence with the following partition: . The sequence : .
In the second case, the sequence could be obtained from the sequence with the following partition: . The sequence : .
In the third case, the sequence could be obtained from the sequence with the following partition: . The sequence : .
In the fourth case, there is no sequence such that changing a for transmission over the network could produce a sequence .
解题思路
写这题的时候完全没想到是动态规划。
定义状态表示所有以为结尾的前缀合法方案的集合,如果存在合法方案的话那么,否则。
考虑每一个位置作为信息码的情况,如果下标作为信息段的右端点,且,意味着区间可以作为一段合法的信息,此时如果要保证以为结尾的前缀存在合法方案,就要看看以为结尾的前缀是否存在合法方案,这个值就是,因此。同理,再考虑作为信息段树的左端点,如果,意味着区间可以作为一段合法的信息,此时如果要保证以为结尾的前缀存在合法方案,就要看看以为结尾的前缀是否存在合法方案,这个值就是,因此。
AC代码如下:
1 #include <bits/stdc++.h> 2 using namespace std; 3 4 const int N = 2e5 + 10; 5 6 int a[N]; 7 bool f[N]; 8 9 void solve() { 10 int n; 11 scanf("%d", &n); 12 for (int i = 1; i <= n; i++) { 13 scanf("%d", a + i); 14 } 15 memset(f, 0, sizeof(f)); 16 f[0] = true; 17 for (int i = 1; i <= n; i++) { 18 if (i - a[i] - 1 >= 0) f[i] |= f[i - a[i] - 1]; 19 if (i + a[i] <= n) f[i + a[i]] |= f[i - 1]; 20 } 21 printf("%s\n", f[n] ? "YES" : "NO"); 22 } 23 24 int main() { 25 int t; 26 scanf("%d", &t); 27 while (t--) { 28 solve(); 29 } 30 31 return 0; 32 }
参考资料
Codeforces Round #826 (Div. 3) Editorial:https://codeforces.com/blog/entry/107908
本文来自博客园,作者:onlyblues,转载请注明原文链接:https://www.cnblogs.com/onlyblues/p/16786198.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
· SQL Server 2025 AI相关能力初探
· 为什么 退出登录 或 修改密码 无法使 token 失效