// File: /workspace/github_cpp_makefile_star2_O3/cooljacket[P]Dormitory-distribution/main.cpp
// Function: divide(std::vector<Student, std::allocator<Student>>&, std::vector<Student, std::allocator<Student>>&, std::ostream&)
void divide(vector<Student>& GroupA, vector<Student>& GroupB, ostream& out) {
	if (GroupA.size() > GroupB.size())
		swap(GroupA, GroupB);

	// Step 1: 使用kmeans抽取GroupB中尽量不同的人到GroupA中
	int num = (GroupB.size() - GroupA.size()) / 2;
	if (num > 0) {
		KMeans<Student> km(GroupB, num, 15);
		km.train_inc();
		km.explain();
		vector<size_t> reps = km.getSortedRepresentative();
		for (int i = reps.size()-1, j=GroupB.size()-1; i >= 0; --i, --j) {
			swap(GroupB[reps[i]], GroupB[j]);
		}
		for (int i = 0; i < num; ++i) {
			GroupA.push_back(*(GroupB.rbegin()));
			GroupB.pop_back();
		}
	}



	// Step 2: 每个组内按相似度两两结合，没法结合的元素单独放到“special宿舍”中
	vector<Student> special;
	vector<Student> CombA, CombB;
	vector<pair<Student, Student> > CombAPairs, CombBPairs;
	CombGroupIntoPairs(GroupA, CombA, CombAPairs, special);
	CombGroupIntoPairs(GroupB, CombB, CombBPairs, special);


	// Step 3: 对于每个两两组合后的“平均者”，CombA在CombB中寻找最相似的，作为同个宿舍的划分
	// 从CombA的最后面开始配对，将CombB中与其最相似的那个交换到尾部，方便降低删除的复杂度
	vector<vector<Student> > result;
	for (int i = CombA.size()-1; i >= 0; --i) {
		vector<Student> unit;
		size_t goal = findNearestExclude(CombA[i], CombB, CombB.size());
		unit.push_back(CombAPairs[i].first);
		unit.push_back(CombAPairs[i].second);
		unit.push_back(CombBPairs[goal].first);
		unit.push_back(CombBPairs[goal].second);
		result.push_back(unit);
		
		if (goal != CombB.size()-1) {
			swap(CombB[goal], *(CombB.rbegin()));
			swap(CombBPairs[goal], *(CombBPairs.rbegin()));
		}
		CombB.pop_back();
		CombBPairs.pop_back();
	}

	// CombB最后的size只可能是0或1
	if (CombB.size() == 1) {
		special.push_back(CombBPairs[0].first);
		special.push_back(CombBPairs[0].second);
	}

	if (!special.empty())
		result.push_back(special);

	out << "宿舍号,学号,姓名,性别,生源地,起床时间,就寝时间,兴趣爱好,性格,易受他人影响,集体住宿经历,家庭人均收入,喜欢开空调" << endl;
	for (int i = 0; i < result.size(); ++i) {
		out << i+1;
		for (int j = 0; j < result[i].size(); ++j) {
			out << ',';
			result[i][j].output(out);
		}
		out << endl;
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1a8, %rsp            # imm = 0x1A8
movq %rdx, %rbx
movq %rsi, %r14
movq %rdi, %r15
movq (%rdi), %rdx
movq 0x8(%rdi), %rcx
movq %rcx, %rax
subq %rdx, %rax
movq (%rsi), %rdi
movq 0x8(%rsi), %rsi
movq %rsi, %r8
subq %rdi, %r8
cmpq %r8, %rax
jbe 0x369c
movq 0x10(%r15), %r8
movq %rdi, (%r15)
movq %rsi, 0x8(%r15)
movq 0x10(%r14), %rsi
movq %rsi, 0x10(%r15)
movq %rdx, (%r14)
movq %rcx, 0x8(%r14)
movq %r8, 0x10(%r14)
movq 0x8(%r15), %rcx
subq (%r15), %rcx
jmp 0x36a2
movq %rax, %rcx
movq %r8, %rax
sarq $0x3, %rax
movabsq $-0xf0f0f0f0f0f0f0f, %rdx # imm = 0xF0F0F0F0F0F0F0F1
imulq %rdx, %rax
sarq $0x3, %rcx
movabsq $0xf0f0f0f0f0f0f0f, %r13 # imm = 0xF0F0F0F0F0F0F0F
imulq %rcx, %r13
addq %rax, %r13
shrq %r13
testl %r13d, %r13d
jle 0x3832
movl %r13d, %edx
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
leaq 0xd0(%rsp), %r12
movl $0xf, %ecx
movq %r12, %rdi
movq %r14, %rsi
callq 0x45d6
movq %r12, %rdi
callq 0x4728
leaq 0xd0(%rsp), %rdi
callq 0x48cc
leaq 0x20(%rsp), %rdi
leaq 0xd0(%rsp), %rsi
callq 0x4960
movq 0x28(%rsp), %rax
subq 0x20(%rsp), %rax
shrq $0x3, %rax
decl %eax
js 0x377f
movq 0x8(%r14), %rcx
subq (%r14), %rcx
shrq $0x3, %rcx
imull $0xf0f0f0f1, %ecx, %r12d # imm = 0xF0F0F0F1
movl %eax, %ebp
decl %r12d
movq 0x20(%rsp), %rax
movq (%rax,%rbp,8), %rax
movq (%r14), %rcx
movq %rax, %rdx
shlq $0x7, %rdx
leaq (%rdx,%rax,8), %rdi
addq %rcx, %rdi
movslq %r12d, %r12
movq %r12, %rax
shlq $0x7, %rax
leaq (%rax,%r12,8), %rsi
addq %rcx, %rsi
callq 0x414b
decl %r12d
addq $-0x1, %rbp
jb 0x3746
movq $-0x88, %r12
movq 0x8(%r14), %rsi
addq %r12, %rsi
movq %r15, %rdi
callq 0x405c
addq 0x8(%r14), %r12
movq %r12, 0x8(%r14)
movq %r14, %rdi
movq %r12, %rsi
callq 0x514c
decl %r13d
jne 0x377f
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x37c4
movq 0x30(%rsp), %rsi
subq %rdi, %rsi
callq 0x2230
leaq 0x178(%rsp), %rdi
callq 0x4d54
leaq 0x148(%rsp), %r12
movq %r12, %rdi
callq 0x4dfc
movq -0x18(%r12), %rdi
testq %rdi, %rdi
je 0x37fb
movq 0x140(%rsp), %rsi
subq %rdi, %rsi
callq 0x2230
movq 0x118(%rsp), %rdi
testq %rdi, %rdi
je 0x3818
movq 0x128(%rsp), %rsi
subq %rdi, %rsi
callq 0x2230
leaq 0x100(%rsp), %rdi
callq 0x4a8c
leaq 0xe8(%rsp), %rdi
callq 0x4a8c
xorps %xmm0, %xmm0
leaq 0xd0(%rsp), %rcx
movaps %xmm0, (%rcx)
xorl %eax, %eax
movq %rax, 0x10(%rcx)
leaq 0x20(%rsp), %rsi
movq %rax, 0x10(%rsi)
movaps %xmm0, (%rsi)
movq %rax, 0x10(%rsp)
movaps %xmm0, (%rsp)
leaq 0xb0(%rsp), %rdx
movq %rax, 0x10(%rdx)
movaps %xmm0, (%rdx)
movq %rax, 0x50(%rsp)
movaps %xmm0, 0x40(%rsp)
movq %r15, %rdi
callq 0x3da2
movq %rsp, %rsi
leaq 0x40(%rsp), %rdx
leaq 0xd0(%rsp), %rcx
movq %r14, %rdi
callq 0x3da2
xorps %xmm0, %xmm0
movaps %xmm0, 0x60(%rsp)
movq 0x28(%rsp), %rax
subq 0x20(%rsp), %rax
movq $0x0, 0x70(%rsp)
shrq $0x3, %rax
imull $0xf0f0f0f1, %eax, %eax # imm = 0xF0F0F0F1
decl %eax
js 0x3a8a
movl %eax, %eax
leaq 0x1(%rax), %r12
movq %rax, %rcx
shlq $0x7, %rcx
leaq (%rcx,%rax,8), %rcx
imulq $0x110, %rax, %r15      # imm = 0x110
movq %rsp, %r13
leaq 0x80(%rsp), %r14
xorps %xmm0, %xmm0
movaps %xmm0, 0x80(%rsp)
movq $0x0, 0x90(%rsp)
movq 0x20(%rsp), %rdi
movq %rcx, 0xa8(%rsp)
addq %rcx, %rdi
movq 0x8(%rsp), %rdx
subq (%rsp), %rdx
sarq $0x3, %rdx
movabsq $-0xf0f0f0f0f0f0f0f, %rax # imm = 0xF0F0F0F0F0F0F0F1
imulq %rax, %rdx
movq %r13, %rsi
callq 0x3cee
movq %rax, %r13
movq 0xb0(%rsp), %rsi
addq %r15, %rsi
movq %r14, %rdi
callq 0x405c
movq 0xb0(%rsp), %rax
leaq (%rax,%r15), %rsi
addq $0x88, %rsi
movq %r14, %rdi
callq 0x405c
imulq $0x110, %r13, %rbp      # imm = 0x110
movq 0x40(%rsp), %rsi
addq %rbp, %rsi
movq %r14, %rdi
callq 0x405c
movq 0x40(%rsp), %rax
leaq (%rax,%rbp), %rsi
addq $0x88, %rsi
movq %r14, %rdi
callq 0x405c
leaq 0x60(%rsp), %rdi
movq %r14, %rsi
callq 0x4a60
movq %r15, 0xa0(%rsp)
movq (%rsp), %rdi
movq 0x8(%rsp), %rsi
movq %rsi, %rax
subq %rdi, %rax
sarq $0x3, %rax
movabsq $-0xf0f0f0f0f0f0f0f, %rcx # imm = 0xF0F0F0F0F0F0F0F1
imulq %rcx, %rax
decq %rax
cmpq %rax, %r13
jne 0x39d5
movq $-0x88, %r15
jmp 0x3a37
movq %r13, %rax
shlq $0x7, %rax
leaq (%rax,%r13,8), %rax
addq %rax, %rdi
movq $-0x88, %r15
addq %r15, %rsi
callq 0x414b
movq %r12, 0x98(%rsp)
movq 0x40(%rsp), %r12
movq 0x48(%rsp), %r13
leaq (%r12,%rbp), %rdi
leaq -0x110(%r13), %rsi
callq 0x414b
leaq (%r12,%rbp), %rdi
addq $0x88, %rdi
movq 0x98(%rsp), %r12
addq %r15, %r13
movq %r13, %rsi
callq 0x414b
movq 0x8(%rsp), %rsi
addq %r15, %rsi
movq %rsi, 0x8(%rsp)
movq %rsp, %r13
movq %r13, %rdi
callq 0x514c
movq 0x48(%rsp), %rdi
movq $-0x110, %rbp           # imm = 0xFEF0
addq %rbp, %rdi
movq %rdi, 0x48(%rsp)
callq 0x43f0
movq %r14, %rdi
callq 0x4a8c
movq 0xa8(%rsp), %rcx
addq %r15, %rcx
movq 0xa0(%rsp), %r15
addq %rbp, %r15
decq %r12
jg 0x38e4
movq 0x8(%rsp), %rax
subq (%rsp), %rax
cmpq $0x88, %rax
jne 0x3ac4
movq 0x40(%rsp), %rsi
leaq 0xd0(%rsp), %rdi
callq 0x405c
movl $0x88, %esi
addq 0x40(%rsp), %rsi
leaq 0xd0(%rsp), %rdi
callq 0x405c
movq 0xd0(%rsp), %rax
cmpq 0xd8(%rsp), %rax
je 0x3ae8
leaq 0x60(%rsp), %rdi
leaq 0xd0(%rsp), %rsi
callq 0x4a60
leaq 0x65f5(%rip), %rsi      # 0xa0e4
movl $0x9f, %edx
movq %rbx, %rdi
callq 0x2250
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x2210
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2160
xorl %r12d, %r12d
leaq 0x80(%rsp), %r14
movq 0x68(%rsp), %rax
subq 0x60(%rsp), %rax
sarq $0x3, %rax
movabsq $-0x5555555555555555, %rcx # imm = 0xAAAAAAAAAAAAAAAB
imulq %rcx, %rax
cmpq %r12, %rax
jbe 0x3c06
leaq 0x1(%r12), %r15
movq %rbx, %rdi
movl %r15d, %esi
callq 0x2350
movq 0x60(%rsp), %rax
leaq (%r12,%r12,2), %r13
movq 0x8(%rax,%r13,8), %rcx
cmpq (%rax,%r13,8), %rcx
je 0x3bd7
xorl %ebp, %ebp
xorl %r12d, %r12d
movb $0x2c, 0x80(%rsp)
movl $0x1, %edx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x2250
movq 0x60(%rsp), %rax
movq (%rax,%r13,8), %rdi
addq %rbp, %rdi
movq %rbx, %rsi
callq 0x7170
incq %r12
movq 0x60(%rsp), %rax
movq 0x8(%rax,%r13,8), %rcx
subq (%rax,%r13,8), %rcx
sarq $0x3, %rcx
movabsq $-0xf0f0f0f0f0f0f0f, %rax # imm = 0xF0F0F0F0F0F0F0F1
imulq %rax, %rcx
addq $0x88, %rbp
cmpq %r12, %rcx
ja 0x3b7c
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x2210
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2160
movq %r15, %r12
jmp 0x3b2e
leaq 0x60(%rsp), %rdi
callq 0x4104
leaq 0x40(%rsp), %rdi
callq 0x4abc
leaq 0xb0(%rsp), %rdi
callq 0x4abc
movq %rsp, %rdi
callq 0x4a8c
leaq 0x20(%rsp), %rdi
callq 0x4a8c
leaq 0xd0(%rsp), %rdi
callq 0x4a8c
addq $0x1a8, %rsp            # imm = 0x1A8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x3c5a
movq %rax, %rbx
jmp 0x3c80
movq %rax, %rbx
jmp 0x3cb0
jmp 0x3ca3
movq %rax, %rbx
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x3c80
movq 0x30(%rsp), %rsi
subq %rdi, %rsi
callq 0x2230
leaq 0xd0(%rsp), %rdi
callq 0x4a06
jmp 0x3ce6
movq %rax, %rbx
leaq 0x80(%rsp), %rdi
callq 0x4a8c
jmp 0x3ca6
jmp 0x3ca3
movq %rax, %rbx
leaq 0x60(%rsp), %rdi
callq 0x4104
leaq 0x40(%rsp), %rdi
callq 0x4abc
leaq 0xb0(%rsp), %rdi
callq 0x4abc
movq %rsp, %rdi
callq 0x4a8c
leaq 0x20(%rsp), %rdi
callq 0x4a8c
leaq 0xd0(%rsp), %rdi
callq 0x4a8c
movq %rbx, %rdi
callq 0x2370