// File: /workspace/github_cpp_makefile_star2_O2/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 $0x198, %rsp            # imm = 0x198
movq %rdx, %rbx
movq %rsi, %r14
movq %rdi, %r15
movq 0x8(%rdi), %rcx
subq (%rdi), %rcx
movq 0x8(%rsi), %rax
subq (%rsi), %rax
cmpq %rax, %rcx
jbe 0x3073
movq %r15, %rdi
movq %r14, %rsi
callq 0x4872
movq 0x8(%r14), %rax
movq 0x8(%r15), %rcx
subq (%r14), %rax
subq (%r15), %rcx
movl $0x88, %esi
cqto
idivq %rsi
movq %rax, %rsi
movq $-0x88, %rdi
movq %rcx, %rax
cqto
idivq %rdi
movq %rax, %r12
addq %rsi, %r12
shrq %r12
testl %r12d, %r12d
jle 0x3180
movl %r12d, %edx
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
leaq 0xc0(%rsp), %r13
pushq $0xf
popq %rcx
movq %r13, %rdi
movq %r14, %rsi
callq 0x39ac
movq %r13, %rdi
callq 0x3ae4
leaq 0xc0(%rsp), %rdi
callq 0x3c52
leaq 0x40(%rsp), %rdi
leaq 0xc0(%rsp), %rsi
callq 0x3cd4
movq 0x48(%rsp), %rcx
subq 0x40(%rsp), %rcx
shrq $0x3, %rcx
movq 0x8(%r14), %rax
subq (%r14), %rax
movl $0x88, %esi
cqto
idivq %rsi
movq %rax, %r13
movl %ecx, %ebp
decq %rbp
decl %r13d
testl %ebp, %ebp
js 0x3143
movl %ebp, %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movq 0x40(%rsp), %rcx
movq (%r14), %rdx
imulq $0x88, (%rcx,%rax,8), %rdi
addq %rdx, %rdi
movslq %r13d, %r13
imulq $0x88, %r13, %rsi
addq %rdx, %rsi
callq 0x38b1
jmp 0x310b
movq $-0x88, %r13
subl $0x1, %r12d
jb 0x3169
movq 0x8(%r14), %rsi
addq %r13, %rsi
movq %r15, %rdi
callq 0x379e
movq %r14, %rdi
callq 0x3994
jmp 0x314a
leaq 0x40(%rsp), %rdi
callq 0x4d6e
leaq 0xc0(%rsp), %rdi
callq 0x3d3e
leaq 0xc0(%rsp), %rcx
andq $0x0, 0x10(%rcx)
xorps %xmm0, %xmm0
movaps %xmm0, (%rcx)
leaq 0x40(%rsp), %rsi
movaps %xmm0, (%rsi)
andq $0x0, 0x10(%rsi)
movaps %xmm0, (%rsp)
andq $0x0, 0x10(%rsp)
leaq 0x80(%rsp), %rdx
movaps %xmm0, (%rdx)
andq $0x0, 0x10(%rdx)
movaps %xmm0, 0x20(%rsp)
andq $0x0, 0x30(%rsp)
movq %r15, %rdi
callq 0x35e9
movq %rsp, %rsi
leaq 0x20(%rsp), %rdx
leaq 0xc0(%rsp), %rcx
movq %r14, %rdi
callq 0x35e9
xorps %xmm0, %xmm0
leaq 0x60(%rsp), %rax
movaps %xmm0, (%rax)
andq $0x0, 0x10(%rax)
movq 0x48(%rsp), %rax
subq 0x40(%rsp), %rax
movl $0x88, %r14d
cqto
idivq %r14
movl %eax, %r15d
decq %r15
leaq 0xa0(%rsp), %r12
testl %r15d, %r15d
js 0x3369
andq $0x0, 0xb0(%rsp)
movl %r15d, %ebp
andl $0x7fffffff, %ebp       # imm = 0x7FFFFFFF
imulq $0x88, %rbp, %rdi
addq 0x40(%rsp), %rdi
xorps %xmm0, %xmm0
movaps %xmm0, 0xa0(%rsp)
movq 0x8(%rsp), %rax
subq (%rsp), %rax
cqto
idivq %r14
movq %rsp, %rsi
movq %rax, %rdx
callq 0x354b
movq %rax, %r13
imulq $0x110, %rbp, %rbp      # imm = 0x110
movq 0x80(%rsp), %rsi
addq %rbp, %rsi
movq %r12, %rdi
callq 0x379e
movq 0x80(%rsp), %rax
leaq (%rax,%rbp), %rsi
addq $0x88, %rsi
movq %r12, %rdi
callq 0x379e
imulq $0x110, %r13, %rbp      # imm = 0x110
movq 0x20(%rsp), %rsi
addq %rbp, %rsi
movq %r12, %rdi
callq 0x379e
movq 0x20(%rsp), %rax
leaq (%rax,%rbp), %rsi
addq $0x88, %rsi
movq %r12, %rdi
callq 0x379e
leaq 0x60(%rsp), %rdi
movq %r12, %rsi
callq 0x3d80
movq (%rsp), %rdi
movq 0x8(%rsp), %rsi
movq %rsi, %rax
subq %rdi, %rax
cqto
idivq %r14
decq %rax
cmpq %rax, %r13
jne 0x32fc
movq $-0x110, %r13           # imm = 0xFEF0
jmp 0x3333
imulq $0x88, %r13, %rax
addq %rax, %rdi
addq $-0x88, %rsi
callq 0x38b1
addq 0x20(%rsp), %rbp
movq 0x28(%rsp), %rsi
movq $-0x110, %r13           # imm = 0xFEF0
addq %r13, %rsi
movq %rbp, %rdi
callq 0x61ce
movq 0x8(%rsp), %rsi
addq $-0x88, %rsi
movq %rsi, 0x8(%rsp)
movq %rsi, %rdi
callq 0x37ce
movq 0x28(%rsp), %rdi
addq %r13, %rdi
movq %rdi, 0x28(%rsp)
callq 0x38fe
movq %r12, %rdi
callq 0x3dac
decq %r15
jmp 0x3218
movq 0x8(%rsp), %rax
subq (%rsp), %rax
cmpq $0x88, %rax
jne 0x33a3
movq 0x20(%rsp), %rsi
leaq 0xc0(%rsp), %rdi
callq 0x379e
movl $0x88, %esi
addq 0x20(%rsp), %rsi
leaq 0xc0(%rsp), %rdi
callq 0x379e
movq 0xc0(%rsp), %rax
cmpq 0xc8(%rsp), %rax
je 0x33c7
leaq 0x60(%rsp), %rdi
leaq 0xc0(%rsp), %rsi
callq 0x3d80
leaq 0x5d16(%rip), %rsi      # 0x90e4
movq %rbx, %rdi
callq 0x2200
movq %rax, %rdi
callq 0x20f0
xorl %r14d, %r14d
movl $0x88, %r12d
pushq $0x2c
popq %rbp
movq 0x68(%rsp), %rax
subq 0x60(%rsp), %rax
cqto
pushq $0x18
popq %rcx
idivq %rcx
cmpq %r14, %rax
jbe 0x3470
leaq 0x1(%r14), %rsi
movq %rbx, %rdi
movq %rsi, 0x78(%rsp)
callq 0x2340
imulq $0x18, %r14, %r13
xorl %r15d, %r15d
xorl %r14d, %r14d
movq 0x60(%rsp), %rcx
movq 0x8(%rcx,%r13), %rax
subq (%rcx,%r13), %rax
cqto
idivq %r12
cmpq %r14, %rax
jbe 0x345e
movq %rbx, %rdi
movl %ebp, %esi
callq 0x2250
movq 0x60(%rsp), %rax
movq (%rax,%r13), %rdi
addq %r15, %rdi
movq %rbx, %rsi
callq 0x6498
incq %r14
addq $0x88, %r15
jmp 0x341c
movq %rbx, %rdi
callq 0x20f0
movq 0x78(%rsp), %r14
jmp 0x33ea
leaq 0x60(%rsp), %rdi
callq 0x37f6
leaq 0x20(%rsp), %rdi
callq 0x3dce
leaq 0x80(%rsp), %rdi
callq 0x3dce
movq %rsp, %rdi
callq 0x3dac
leaq 0x40(%rsp), %rdi
callq 0x3dac
leaq 0xc0(%rsp), %rdi
callq 0x3dac
addq $0x198, %rsp            # imm = 0x198
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x34c4
movq %rax, %rbx
jmp 0x34dd
movq %rax, %rbx
jmp 0x350d
jmp 0x3500
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
callq 0x4d6e
leaq 0xc0(%rsp), %rdi
callq 0x3d3e
jmp 0x3543
jmp 0x3500
movq %rax, %rbx
leaq 0xa0(%rsp), %rdi
callq 0x3dac
jmp 0x3503
movq %rax, %rbx
leaq 0x60(%rsp), %rdi
callq 0x37f6
leaq 0x20(%rsp), %rdi
callq 0x3dce
leaq 0x80(%rsp), %rdi
callq 0x3dce
movq %rsp, %rdi
callq 0x3dac
leaq 0x40(%rsp), %rdi
callq 0x3dac
leaq 0xc0(%rsp), %rdi
callq 0x3dac
movq %rbx, %rdi
callq 0x2350