cf1214F
Description
Two large companies “Cecsi” and “Poca Pola” are fighting against each other for a long time. In order to overcome their competitor, “Poca Pola” started a super secret project, for which it has total n vacancies in all of their offices. After many tests and interviews n candidates were selected and the only thing left was their employment.
Because all candidates have the same skills, it doesn’t matter where each of them will work. That is why the company decided to distribute candidates between workplaces so that the total distance between home and workplace over all candidates is minimal.
It is well known that Earth is round, so it can be described as a circle, and all m cities on Earth can be described as points on this circle. All cities are enumerated from 1 to m so that for each i (1≤i≤m−1) cities with indexes i and i+1 are neighbors and cities with indexes 1 and m are neighbors as well. People can move only along the circle. The distance between any two cities equals to minimal number of transitions between neighboring cities you have to perform to get from one city to another. In particular, the distance between the city and itself equals 0.
The “Poca Pola” vacancies are located at offices in cities a1,a2,…,an. The candidates live in cities b1,b2,…,bn. It is possible that some vacancies are located in the same cities and some candidates live in the same cities.
The “Poca Pola” managers are too busy with super secret project, so you were asked to help “Poca Pola” to distribute candidates between workplaces, so that the sum of the distance between home and workplace over all candidates is minimum possible.
Input
The first line contains two integers m and n (1≤m≤109, 1≤n≤200000) — the number of cities on Earth and the number of vacancies.
The second line contains n integers a1,a2,a3,…,an (1≤ai≤m) — the cities where vacancies are located.
The third line contains n integers b1,b2,b3,…,bn (1≤bi≤m) — the cities where the candidates live.
Output
The first line should contain the minimum total distance between home and workplace over all candidates.
The second line should contain n different integers from 1 to n. The ith of them should be the index of candidate that should work at ith workplace.
Sample Input
1
2
3 10 3
1 5 5
10 4 6Sample Output
1
2 3
1 2 3
Solution
先给出不会证明正确性的 $O(n^2)$ 代码
1  for(int i=1;i<=n;i++){ 
然后考虑一个分段函数
枚举 $i$ ，把 $a_1$ 对应 $b_i$ 的情况的答案设为 $ans_i$
同理
注意两个不等式不等价，比如 $a_i=b_j$ 的时候一个符号为正一个符号为负，使用尺取即可， 细节见代码把
Code
1 
