| 14755 | 2025-02-01 18:28:09 | arnoldbeiland | Következő permutáció (50 pont) | cpp17 | Forditási hiba |
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;
//ifstream fin("input.txt");
//ofstream fout("output.txt");
int globalis;
bool mycmp(int x, int y)
{
if (x > globalis && y > globalis)
return x < y;
if (x < globalis && y < globalis)
return x > y;
return x < y;
}
bool csokkeno(const vector<int> &v)
{
int n = v.size();
for (int i = 0; i <= n-2; i++)
if (v[i] < v[i+1])
return false;
return true;
}
void megold(vector<int> &v)
{
int n = v.size();
int i = n-2;
while (v[i] > v[i+1])
i--;
if (i < 0)
return;
int hol = i;
int a = v[hol];
int minimum = v[hol+1];
int minimum_hely = hol+1;
for (i = hol+2; i < n; i++)
if (v[i] < minimum && v[i] > a) {
minimum = v[i];
minimum_hely = i;
}
v[minimum_hely] = a;
v[hol] = minimum;
globalis = minimum;
sort(v.begin()+hol+1, v.end(), mycmp);
}
int main()
{
int n;
cin >> n;
vector<int> v(n);
for (int i = 0; i < n; i++)
cin >> v[i];
megold(v);
for (int i = 0; i < n; i++)
cout << v[i] << " ";
cout << endl;
return 0;
}
int main_trukkos()
{
int n;
cin >> n;
vector<int> v(n);
for (int i = 0; i < n; i++)
v[i] = i+1;
while (true) {
for (int i = 0; i < n; i++)
fout << v[i] << " ";
fout << endl;
if (csokkeno(v))
break;
megold(v);
}
return 0;
}
bool mentes(const vector<int> &v)
{
int n = v.size();
for (int i = 0; i < n; i++)
for (int j = i+1; j < n; j++)
for (int k = j+1; k < n; k++)
if (v[i] > v[k] && v[k] > v[j])
return false;
return true;
}
int main_brute()
{
int n;
cin >> n;
vector<int> v(n);
for (int i = 0; i < n; i++)
v[i] = i+1;
do {
/*
for (int i = 0; i < n; i++)
fout << v[i] << " ";
if (mentes(v))
fout << " ok";
fout << endl;
*/
if (mentes(v)) {
for (int i = 0; i < n; i++)
fout << v[i] << " ";
fout << endl;
}
} while (next_permutation(v.begin(), v.end()));
return 0;
}
open /var/local/lib/isolate/426/box/a.out: no such file or directory
main.cpp: In function 'int main_trukkos()':
main.cpp:91:13: error: 'fout' was not declared in this scope
91 | fout << v[i] << " ";
| ^~~~
main.cpp:92:9: error: 'fout' was not declared in this scope
92 | fout << endl;
| ^~~~
main.cpp: In function 'int main_brute()':
main.cpp:139:17: error: 'fout' was not declared in this scope
139 | fout << v[i] << " ";
| ^~~~
main.cpp:140:13: error: 'fout' was not declared in this scope
140 | fout << endl;
| ^~~~