[Codeforces Round #611(Div. 3)] C. Friends and Gifts(랜덤 대법이 좋다)

5631 단어
[Codeforces Round #611(Div. 3)] C. Friends and Gifts(랜덤 대법이 좋다)
C. Friends and Gifts
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
There are nn friends who want to give gifts for the New Year to each other. Each friend should give exactly one gift and receive exactly one gift. The friend cannot give the gift to himself.
For each friend the value fifi is known: it is either fi=0fi=0 if the ii-th friend doesn't know whom he wants to give the gift to or 1≤fi≤n1≤fi≤n if the ii-th friend wants to give the gift to the friend fifi.
You want to fill in the unknown values (fi=0fi=0) in such a way that each friend gives exactly one gift and receives exactly one gift and there is no friend who gives the gift to himself. It is guaranteed that the initial information isn't contradictory.
If there are several answers, you can print any.
Input
The first line of the input contains one integer nn (2≤n≤2⋅1052≤n≤2⋅105) — the number of friends.
The second line of the input contains nn integers f1,f2,…,fnf1,f2,…,fn (0≤fi≤n0≤fi≤n, fi≠ifi≠i, all fi≠0fi≠0 are distinct), where fifi is the either fi=0fi=0 if the ii-th friend doesn't know whom he wants to give the gift to or 1≤fi≤n1≤fi≤n if the ii-th friend wants to give the gift to the friend fifi. It is also guaranteed that there is at least two values fi=0fi=0.
Output
Print nn integers nf1,nf2,…,nfnnf1,nf2,…,nfn, where nfinfi should be equal to fifi if fi≠0fi≠0 or the number of friend whom the ii-th friend wants to give the gift to. All values nfinfi should be distinct, nfinfi cannot be equal to ii. Each friend gives exactly one gift and receives exactly one gift and there is no friend who gives the gift to himself.
If there are several answers, you can print any.
Examples
input
Copy
5
5 0 0 2 4

output
Copy
5 3 1 2 4 

input
Copy
7
7 0 0 1 4 0 6

output
Copy
7 3 2 1 4 5 6 

input
Copy
7
7 4 0 3 0 5 1

output
Copy
7 4 2 3 6 5 1 

input
Copy
5
2 1 0 0 0

output
Copy
2 1 4 5 3 

제목:
정수 n과 1~n의 전체 배열 p를 정하고,
p중 적어도 2개의 숫자가 지워졌다(지워진 후 p[i]=0). 지워진 부분을 보충해 보자.
만족:
1~n의 전체 배열이며 p[i]는 i와 같지 않다
아이디어:
이 문제는 기민하게 무작위로 풀 수 있다.
우리는 지워진 숫자를vector에 추가하고,
mt 랜덤수\(mt19937 generator(time(0)\)를 엔진으로
shuffle 함수를 호출하여vector의 순서를 무작위로 어지럽히고 p에 p[i]=i가 존재하지 않을 때까지 만족시키면 됩니다
코드:
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#define ALL(x) (x).begin(), (x).end()
#define sz(a) int(a.size())
#define rep(i,x,n) for(int i=x;i
#define pll pair
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define chu(x) cout<>= 1;} return ans;}
void Pv(const vector &V) {int Len = sz(V); for (int i = 0; i < Len; ++i) {printf("%d", V[i] ); if (i != Len - 1) {printf(" ");} else {printf("
");}}} void Pvl(const vector &V) {int Len = sz(V); for (int i = 0; i < Len; ++i) {printf("%lld", V[i] ); if (i != Len - 1) {printf(" ");} else {printf("
");}}} inline long long readll() {long long tmp = 0, fh = 1; char c = getchar(); while (c < '0' || c > '9') {if (c == '-') fh = -1; c = getchar();} while (c >= '0' && c <= '9') tmp = tmp * 10 + c - 48, c = getchar(); return tmp * fh;} inline int readint() {int tmp = 0, fh = 1; char c = getchar(); while (c < '0' || c > '9') {if (c == '-') fh = -1; c = getchar();} while (c >= '0' && c <= '9') tmp = tmp * 10 + c - 48, c = getchar(); return tmp * fh;} const int maxn = 1000010; const int inf = 0x3f3f3f3f; /*** TEMPLATE CODE * * STARTS HERE ***/ int n; int a[maxn]; int b[maxn]; std::vector v, v2; int main() { //freopen("D:\\code\\text\\input.txt","r",stdin); //freopen("D:\\code\\text\\output.txt","w",stdout); n = readint(); srand ( (unsigned int) time (NULL) ); repd(i, 1, n) { a[i] = readint(); b[a[i]] = 1; if (a[i] == 0) { v2.push_back(i); } } repd(i, 1, n) { if (!b[i]) { v.push_back(i); } } mt19937 generator(time(0)); repd(rp, 1, 20) { shuffle(ALL(v), generator); int len = sz(v2); int isok = 1; for (int i = 0; i < len; ++i) { if (v[i] == v2[i]) { isok = 0; break; } } if (isok) { break; } } int id = 0; repd(i, 1, n) { printf("%d%c", a[i] == 0 ? v[id++] : a[i], i == n ? '
' : ' '); } return 0; }

좋은 웹페이지 즐겨찾기