zoukankan      html  css  js  c++  java
  • Codeforces 1283C Friends and Gifts

    题目链接:

    Codeforces 1283C Friends and Gifts

    思路:

    将还没有分配的人和还没有被分配的人分别存进两个数组,挨个遍历,如果相同位置是同一个人,就和下一个交换

    代码:

    #include<bits/stdc++.h>
    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<<a<<"---
    "
    #define rp(i,n) for(int i=0;i<n;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;
    }
    
  • 相关阅读:
    hdu 3367 Pseudoforest
    hdu 2489 Minimal Ratio Tree
    hdu 4009 Transfer water
    poj 3164 Command Network
    hdu 3926 Hand in Hand
    hdu 3938 Portal
    5-26日(面经总结)
    5-25日
    5-21日|5-22日
    5-13日记录|5-14日
  • 原文地址:https://www.cnblogs.com/yuhan-blog/p/12308712.html
Copyright © 2011-2022 走看看