Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
Reputeless committed Dec 20, 2021
1 parent 31da8c0 commit f9a4709
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 1 deletion.
55 changes: 55 additions & 0 deletions 044.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# 044 - Shift and Swapping (★3)

## 解答
`T == 2` のとき、実際に要素を動かしてしまうと時間がかかります。オフセット値(移動量)だけを記録し、`T == 1`, `T == 3` での操作に使うインデックス値を、オフセット値に応じて計算します。

```cpp
#include <iostream>
#include <vector>

// オフセット値をもとに、対応するインデックス値に変換
int OffsetIndex(int i, int offset, int N)
{
// 例: i = 1, offset = 2, N = 4 のとき
// (1 - 2 + 4) % N = 3
return (i - offset + N) % N;
}

int main()
{
// 長さ N の整数列, Q 個のクエリ
int N, Q;
std::cin >> N >> Q;

std::vector<int> A(N);
for (auto& a : A)
{
std::cin >> a;
}

// オフセット値
int offset = 0;

for (int i = 0; i < Q; ++i)
{
int T, x, y;
std::cin >> T >> x >> y;

if (T == 1)
{
// 要素を交換
std::swap(A[OffsetIndex(x - 1, offset, N)], A[OffsetIndex(y - 1, offset, N)]);
}
else if (T == 2)
{
// オフセット値をインクリメント, オフセット値が N 以上にならないよう %= N
++offset %= N;
}
else
{
// 要素を出力
std::cout << A[OffsetIndex(x - 1, offset, N)] << '\n';
}
}
}
```
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ C++17 標準ライブラリの機能を優先して使い、競技プログラ
|[020](https://atcoder.jp/contests/typical90/tasks/typical90_t)|[Log Inequality](./020.md)|★3|[👨‍🏫](https://raw.githubusercontent.com/E869120/kyopro_educational_90/main/editorial/020.jpg) / [📝](https://github.com/E869120/kyopro_educational_90/blob/main/sol/020.cpp)|整数で処理して誤差をなくそう|
|[032](https://atcoder.jp/contests/typical90/tasks/typical90_af)|[AtCoder Ekiden](./032.md)|★3|[👨‍🏫](https://raw.githubusercontent.com/E869120/kyopro_educational_90/main/editorial/032.jpg) / [📝](https://github.com/E869120/kyopro_educational_90/blob/main/sol/032.cpp)|小さい制約は順列全探索|
|[038](https://atcoder.jp/contests/typical90/tasks/typical90_af)|[Large LCM](./038.md)|★3|[👨‍🏫](https://raw.githubusercontent.com/E869120/kyopro_educational_90/main/editorial/038.jpg) / [📝](https://github.com/E869120/kyopro_educational_90/blob/main/sol/038.cpp)|オーバーフローに注意|
|044| | | | |
|[044](https://atcoder.jp/contests/typical90/tasks/typical90_ar)|[Shift and Swapping](./044.md)|★3|[👨‍🏫](https://raw.githubusercontent.com/E869120/kyopro_educational_90/main/editorial/044.jpg) / [📝](https://github.com/E869120/kyopro_educational_90/blob/main/sol/044.cpp)|見かけ上の変化をメモ|
|046| | | | |
|048| | | | |
|050| | | | |
Expand Down

0 comments on commit f9a4709

Please sign in to comment.