package com.kingdz.algorithm.time201705;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
/**
* 排座位
*
* <pre>
* 为调皮的学生调座位,教室有M行N列桌椅,需要设置K条横向过道和L条纵向过道。一共有D对同学私下说话
* 设计出在什么位置设置过道会使得私下说话的同学的对数最少
* 第一行输入M N K L D
* 接下来的D行每行用4个空格的整数,表示说话的同学坐标,该坐标前后相邻或左右相邻
* 样例输入:
* 5 6 1 2 3
* 4 2 4 3
* 2 2 1 2
* 3 4 3 5
* 第一行输出包含K个整数,a1,a2。。。说明a1和a1+1行之间开辟过道,最后一位不是空格
* 第二行输出包含L个整数,b1,b2。。。说明b1和b1+1列之间开辟过道,最后一位不是空格
* 样例输出:
* 1
* 2 4
* </pre>
*
* @author kingdz
*
*/
public class Algo12 {
public static void main(String[] args) {
List<String> inputList = new ArrayList<String>();
inputList.add("5 6 1 2 3");
inputList.add("4 2 4 3");
inputList.add("2 2 1 2");
inputList.add("3 4 3 5");
List<String> outputList = genSeat(inputList);
for (String str : outputList) {
System.out.println(str);
}
}
private static List<String> genSeat(List<String> inputList) {
List<String> ret = new ArrayList<String>();
String now = inputList.get(0);
String[] array = now.split(" ");
int m = Integer.parseInt(array[0]);
int n = Integer.parseInt(array[1]);
int k = Integer.parseInt(array[2]);
int l = Integer.parseInt(array[3]);
int d = Integer.parseInt(array[4]);
int[] row = new int[m];
int[] col = new int[n];
for (int i = 1; i < d + 1; i++) {
now = inputList.get(i);
array = now.split(" ");
int a1 = Integer.parseInt(array[0]);
int a2 = Integer.parseInt(array[1]);
int b1 = Integer.parseInt(array[2]);
int b2 = Integer.parseInt(array[3]);
if (a1 == b1) {
int min = Math.min(a2, b2);
col[min]++;
} else if (a2 == b2) {
int min = Math.min(a1, b1);
row[min]++;
} else {
System.out.println("ERROR[" + now + "]");
}
}
List<String> result1 = sortArray(row);
List<String> result2 = sortArray(col);
{
StringBuilder strb = new StringBuilder();
if (result1.size() > 1) {
strb.append(result1.get(0).split(",")[0]);
}
for (int i = 1; i < k; i++) {
strb.append(" ");
strb.append(result1.get(i).split(",")[0]);
}
ret.add(strb.toString());
strb = new StringBuilder();
if (result2.size() > 1) {
strb.append(result2.get(0).split(",")[0]);
}
for (int i = 1; i < l; i++) {
strb.append(" ");
strb.append(result2.get(i).split(",")[0]);
}
ret.add(strb.toString());
}
return ret;
}
/**
* 排序数组带下标
*
* @param array
* @return
*/
private static List<String> sortArray(int[] array) {
List<String> ret = new ArrayList<String>();
for (int i = 0; i < array.length; i++) {
ret.add(i + "," + array[i]);
}
Collections.sort(ret, new Comparator<String>() {
public int compare(String o1, String o2) {
int i1 = Integer.parseInt(o1.split(",")[1]);
int i2 = Integer.parseInt(o2.split(",")[1]);
return i2 - i1;
}
});
return ret;
}
}