1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

next_permutation

Last updated at Posted at 2022-02-22

入力した配列より後のパターンを辞書順にすべて試す

next_permutation(a, a + n)
(aは配列、nは要素数)

計算量
O(N) * N!
(1順列あたり) * (全てのパターン)

do {
  
} while(next_permutation(a, a + n));

n = 3, a = {2, 3, 1} の実行結果

2,3,1
3,1,2
3,2,1
do {
        for(int i = 0; i < N - 1; i++) sum += dist(v[i], v[i+1]); //v[i]は配列の要素
    } while(next_permutation(v.begin(), v.end())); //vは配列
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?