Skip to content

Commit 2f59004

Browse files
committed
update codeforces/constructive/1650D
1 parent 0d22d95 commit 2f59004

File tree

1 file changed

+29
-0
lines changed
  • codeforces/constructive_algorithms

1 file changed

+29
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# codeforces 1650D
2+
3+
> 这是一类题目,当我们想得到一个序列的时候,就通过最后一个位置的信息进行回溯。相同的做法还有迪杰斯特拉算法最短路径输出
4+
5+
```cpp
6+
#include <bit/stdc++.h>
7+
using namespace std;
8+
int a[10010], b[10010];
9+
int main(){
10+
ios_base::sync_with_stdio(0);
11+
cin.tie(0);
12+
int t, n;
13+
cin >> t;
14+
while(t--){
15+
cin >> n;
16+
for(int i = 0; i < n; i++) cin >> a[i];
17+
for(int i = n - 1; i >= 0; i--) { //从后往前回溯
18+
int j = find(a, a + n, i + 1) - a;
19+
b[i] = (j + 1) % (i + 1);
20+
rotate(a, a[j], a + i + 1);
21+
}
22+
for(int i = 0; i < n; i++) cout << b[i] << ' ';
23+
cout << endl;
24+
}
25+
return 0;
26+
}
27+
```
28+
29+

0 commit comments

Comments
 (0)