// File: /workspace/github_cpp_makefile_star2_O1/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), %rcx
movq 0x8(%rdi), %rax
movq %rax, %rdi
subq %rcx, %rdi
movq (%rsi), %rsi
movq 0x8(%r14), %rdx
movq %rdx, %r8
subq %rsi, %r8
cmpq %r8, %rdi
jbe 0x3723
movq 0x10(%r15), %rdi
movq %rsi, (%r15)
movq %rdx, 0x8(%r15)
movq 0x10(%r14), %rdx
movq %rdx, 0x10(%r15)
movq %rcx, (%r14)
movq %rax, 0x8(%r14)
movq %rdi, 0x10(%r14)
movq 0x8(%r14), %rax
subq (%r14), %rax
sarq $0x3, %rax
movabsq $-0xf0f0f0f0f0f0f0f, %rcx # imm = 0xF0F0F0F0F0F0F0F1
imulq %rcx, %rax
movq 0x8(%r15), %rcx
subq (%r15), %rcx
sarq $0x3, %rcx
movabsq $0xf0f0f0f0f0f0f0f, %r13 # imm = 0xF0F0F0F0F0F0F0F
imulq %rcx, %r13
addq %rax, %r13
shrq %r13
testl %r13d, %r13d
jle 0x38d0
movl %r13d, %edx
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
leaq 0xd0(%rsp), %r12
movl $0xf, %ecx
movq %r12, %rdi
movq %r14, %rsi
callq 0x45ec
movq %r12, %rdi
callq 0x473e
leaq 0xd0(%rsp), %rdi
callq 0x48e0
leaq 0x20(%rsp), %rdi
leaq 0xd0(%rsp), %rsi
callq 0x496e
movq 0x28(%rsp), %rax
subq 0x20(%rsp), %rax
shrq $0x3, %rax
decl %eax
js 0x3811
movq 0x8(%r14), %rcx
subq (%r14), %rcx
shrq $0x3, %rcx
imull $0xf0f0f0f1, %ecx, %r12d # imm = 0xF0F0F0F1
movl %eax, %ebp
incq %rbp
decl %r12d
movq 0x20(%rsp), %rax
movq -0x8(%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 0x41d3
decl %r12d
decq %rbp
jg 0x37d8
cmpl $0x2, %r13d
movl $0x1, %ebp
cmovgel %r13d, %ebp
movq $-0x88, %r12
movq 0x8(%r14), %rsi
addq %r12, %rsi
movq %r15, %rdi
callq 0x40ea
movq 0x8(%r14), %rsi
addq %r12, %rsi
movq %rsi, 0x8(%r14)
movq %r14, %rdi
callq 0x515e
decl %ebp
jne 0x3825
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x3862
movq 0x30(%rsp), %rsi
subq %rdi, %rsi
callq 0x2230
leaq 0x178(%rsp), %rdi
callq 0x4d4e
leaq 0x148(%rsp), %r12
movq %r12, %rdi
callq 0x4df6
movq -0x18(%r12), %rdi
testq %rdi, %rdi
je 0x3899
movq 0x140(%rsp), %rsi
subq %rdi, %rsi
callq 0x2230
movq 0x118(%rsp), %rdi
testq %rdi, %rdi
je 0x38b6
movq 0x128(%rsp), %rsi
subq %rdi, %rsi
callq 0x2230
leaq 0x100(%rsp), %rdi
callq 0x4a9a
leaq 0xe8(%rsp), %rdi
callq 0x4a9a
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 0x3e3c
movq %rsp, %rsi
leaq 0x40(%rsp), %rdx
leaq 0xd0(%rsp), %rcx
movq %r14, %rdi
callq 0x3e3c
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 0x3b24
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 0x3d88
movq %rax, %r13
movq 0xb0(%rsp), %rsi
addq %r15, %rsi
movq %r14, %rdi
callq 0x40ea
movq 0xb0(%rsp), %rax
leaq (%rax,%r15), %rsi
addq $0x88, %rsi
movq %r14, %rdi
callq 0x40ea
imulq $0x110, %r13, %rbp      # imm = 0x110
movq 0x40(%rsp), %rsi
addq %rbp, %rsi
movq %r14, %rdi
callq 0x40ea
movq 0x40(%rsp), %rax
leaq (%rax,%rbp), %rsi
addq $0x88, %rsi
movq %r14, %rdi
callq 0x40ea
leaq 0x60(%rsp), %rdi
movq %r14, %rsi
callq 0x4a6e
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
je 0x3ac6
movq %r13, %rax
shlq $0x7, %rax
leaq (%rax,%r13,8), %rax
addq %rax, %rdi
movq $-0x88, %rax
addq %rax, %rsi
callq 0x41d3
movq %r12, 0xa0(%rsp)
movq 0x40(%rsp), %r12
movq 0x48(%rsp), %r13
leaq (%r12,%rbp), %rdi
leaq -0x110(%r13), %rsi
callq 0x41d3
leaq (%r12,%rbp), %rdi
addq $0x88, %rdi
movq 0xa0(%rsp), %r12
movq $-0x88, %rax
addq %rax, %r13
movq %r13, %rsi
callq 0x41d3
movq 0x8(%rsp), %rsi
movq $-0x88, %rax
addq %rax, %rsi
movq %rsi, 0x8(%rsp)
movq %rsp, %r13
movq %r13, %rdi
callq 0x515e
movq 0x48(%rsp), %rdi
movq $-0x110, %rbp           # imm = 0xFEF0
addq %rbp, %rdi
movq %rdi, 0x48(%rsp)
callq 0x4406
movq %r14, %rdi
callq 0x4a9a
movq 0xa8(%rsp), %rcx
movq $-0x88, %rax
addq %rax, %rcx
addq %rbp, %r15
decq %r12
jg 0x3982
movq 0x8(%rsp), %rax
subq (%rsp), %rax
cmpq $0x88, %rax
jne 0x3b5e
movq 0x40(%rsp), %rsi
leaq 0xd0(%rsp), %rdi
callq 0x40ea
movl $0x88, %esi
addq 0x40(%rsp), %rsi
leaq 0xd0(%rsp), %rdi
callq 0x40ea
movq 0xd0(%rsp), %rax
cmpq 0xd8(%rsp), %rax
je 0x3b82
leaq 0x60(%rsp), %rdi
leaq 0xd0(%rsp), %rsi
callq 0x4a6e
leaq 0x655b(%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 0x3ca0
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 0x3c71
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 0x7164
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 0x3c16
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 0x3bc8
leaq 0x60(%rsp), %rdi
callq 0x4192
leaq 0x40(%rsp), %rdi
callq 0x4aca
leaq 0xb0(%rsp), %rdi
callq 0x4aca
movq %rsp, %rdi
callq 0x4a9a
leaq 0x20(%rsp), %rdi
callq 0x4a9a
leaq 0xd0(%rsp), %rdi
callq 0x4a9a
addq $0x1a8, %rsp            # imm = 0x1A8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x3cf4
movq %rax, %rbx
jmp 0x3d1a
movq %rax, %rbx
jmp 0x3d4a
jmp 0x3d3d
movq %rax, %rbx
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x3d1a
movq 0x30(%rsp), %rsi
subq %rdi, %rsi
callq 0x2230
leaq 0xd0(%rsp), %rdi
callq 0x4a14
jmp 0x3d80
movq %rax, %rbx
leaq 0x80(%rsp), %rdi
callq 0x4a9a
jmp 0x3d40
jmp 0x3d3d
movq %rax, %rbx
leaq 0x60(%rsp), %rdi
callq 0x4192
leaq 0x40(%rsp), %rdi
callq 0x4aca
leaq 0xb0(%rsp), %rdi
callq 0x4aca
movq %rsp, %rdi
callq 0x4a9a
leaq 0x20(%rsp), %rdi
callq 0x4a9a
leaq 0xd0(%rsp), %rdi
callq 0x4a9a
movq %rbx, %rdi
callq 0x2370