Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstdio>
- #include <iostream>
- #include <vector>
- #include <algorithm>
- using namespace std;
- vector<int> a;
- bool next_permutation(vector<int>::iterator begin, vector<int>::iterator end) {
- vector<int>::iterator it = end - 1;
- while (it !=begin && *(it - 1) >= *it) {
- --it;
- }
- if (it == begin) {
- return false;
- }
- int element = *it;
- vector<int>::iterator pos;
- for (vector<int>::iterator i = end - 1; i != it + 1; --i) {
- if (*i > element) {
- pos = i;
- break;
- }
- }
- swap(it, pos);
- sort(it + 1, end);
- return true;
- }
- int main() {
- int n;
- int c;
- scanf("%d", &n);
- for (int i = 0; i < n; ++i) {
- scanf("%d", &c);
- a.push_back(c);
- }
- printf("%d\n", next_permutation(a.begin(), a.end()));
- for (int i = 0; i < a.size(); ++i) {
- printf("%d ", a[i]);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement