#include <bits/stdc++.h>
using namespace std;
#define st first
#define nd second
const int INF = 3e3 + 7;
int n, maxsize, t[INF], zlicz[INF];
pair<int, int> p[INF];
vector<int> v[INF];
stack<int> st;
int main(){
cin.tie(0);
cout.tie(0);
ios_base::sync_with_stdio(0);
cin >> n;
for(int i = 1; i <= n; i++){
cin >> t[i];
p[i].st = t[i];
p[i].nd = i;
}
sort(p + 1, p + n + 1);
for(int i = 1; i <= n; i++){
if(p[i].nd != i && zlicz[i] == 0){
v[p[i].nd].push_back(i);
v[i].push_back(p[i].nd);
zlicz[p[i].nd] = 1;
zlicz[i] = 1;
}
if(maxsize < v[i].size()){
maxsize = v[i].size();
}
}
cout << maxsize << endl;
if(maxsize == 0){
cout << 0;
return 0;
}
if(maxsize == 1){
for(int i = 1; i <= n; i++){
if(v[i].size() > 0){
cout << v[v[i][0]][0] << " ";
st.push(v[i][0]);
// v[v[i]].pop();
v[v[i][0]].pop_back();
v[i].pop_back();
// cout << st.top() << " ";
}
}
while(!st.empty()){
cout << st.top() << " ";
st.pop();
}
}
/*for(int i = 1; i <= n; i++){
for(int j = 0; j < v[i].size(); j++){
cout << v[i][j] << " ";
}
cout << '\n';
}*/
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | #include <bits/stdc++.h> using namespace std; #define st first #define nd second const int INF = 3e3 + 7; int n, maxsize, t[INF], zlicz[INF]; pair<int, int> p[INF]; vector<int> v[INF]; stack<int> st; int main(){ cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(0); cin >> n; for(int i = 1; i <= n; i++){ cin >> t[i]; p[i].st = t[i]; p[i].nd = i; } sort(p + 1, p + n + 1); for(int i = 1; i <= n; i++){ if(p[i].nd != i && zlicz[i] == 0){ v[p[i].nd].push_back(i); v[i].push_back(p[i].nd); zlicz[p[i].nd] = 1; zlicz[i] = 1; } if(maxsize < v[i].size()){ maxsize = v[i].size(); } } cout << maxsize << endl; if(maxsize == 0){ cout << 0; return 0; } if(maxsize == 1){ for(int i = 1; i <= n; i++){ if(v[i].size() > 0){ cout << v[v[i][0]][0] << " "; st.push(v[i][0]); // v[v[i]].pop(); v[v[i][0]].pop_back(); v[i].pop_back(); // cout << st.top() << " "; } } while(!st.empty()){ cout << st.top() << " "; st.pop(); } } /*for(int i = 1; i <= n; i++){ for(int j = 0; j < v[i].size(); j++){ cout << v[i][j] << " "; } cout << '\n'; }*/ } |
English