Codeforces 1283C Friends and Gifts

1983 단어 Codeforces
제목 링크:Codeforces 1283C Friends and Gifts 사고방식: 분배되지 않은 사람과 분배되지 않은 사람을 각각 두 개의 그룹에 저장하여 하나씩 옮겨다니며 같은 위치가 같은 사람이면 다음 교환 코드와 교환한다.
#include
using namespace std;
typedef pair<int,int> P;
typedef long long ll;
#define fi first
#define sc second
#define pb(a) push_back(a)
#define mp(a,b) make_pair(a,b)
#define pt(a) cerr<#define rp(i,n) for(int i=0;i
#define rpn(i,n) for(int i=1;i<=n;i++)
const int maxn=2e5+5;
int f[maxn];
bool flag[maxn];
int main(){
ios::sync_with_stdio(false); cin.tie(nullptr);
int n; cin>>n;
rpn(i,n){
cin>>f[i]; flag[f[i]]=true;
}
vector<int> a,b;
rpn(i,n){
if(f[i]==0) a.pb(i);
if(flag[i]==false) b.pb(i);
}
int sz=a.size();
for(int i=0;i<sz;i++){
if(a[i]==b[i]){
int x=i,y=(i+1)%sz;
swap(b[x],b[y]);
}
}
rp(i,sz) f[a[i]]=b[i];
rpn(i,n) cout<<f[i]<<' ';
return 0;
}

좋은 웹페이지 즐겨찾기