### A. Number Assignment

Author: Suhendry Effendy

(link to problem A)

This problem supposed to be the easiest problem in this contest.

First, notice that we can ignore all duplicate numbers as putting two same numbers in one group costs nothing. Next, observe that each group in the optimal partition will contain adjacent numbers. By adjacent I mean the nearest next or previous number in the given list. Therefore, to solve this problem, first we need to sort the given numbers.

There are two main approaches to form the partition: greedy approach and dynamic programming.

**Greedy Approach**

This is the simplest approach. Starts with A[N] – A[1] as the cost (note that A has been sorted, thus A[N] is the largest element while A[1] is the smallest). Next, find the largest M – 1 gap between numbers (gap = difference between two adjacent numbers in the sorted list) and substracts the initial cost with this number; this is the answer to the problem.

To understand why this approach works, consider these three figures. Let the sorted given numbers are 2, 3, 6, 7, 8 and 10.

Figure 1

Figure 2

Figure 3

The gaps between adjacent numbers in the sorted list are:

: 2 -(1)- 3 -(3)- 6 -(1)- 7 -(1)- 8 -(2)- 10

: 1, 3, 1, 1, 2

Note that for M groups, there will be M-1 gaps. In order to minimize the total cost of the partition (number assignment), we need to maximize the total gaps, thus removing the M-1 highest gap will produce the partition with the smallest cost.

This approach has O(N lg N) time complexity.

**Dynamic Programming**

Instead of analysing which gaps we should remove, we can “try all combination” using dynamic programming approach. The simplest idea (which I can think) for this approach needs O(N^{2}) space and O(N^{3}) time complexity. We need to keep the smallest element which has not been grouped and the number of group left to be form as the DP state/dimension; for each state, we need to iterate through ungrouped elements and “try” to create a group up to that element.

As the constraint for this problem is quite small (N <= 100), this approach will get accepted.

The reason I set the constraint to be small is because I don’t want to bother myself with big input file for such easy problem. However, to my surprise, most of the teams (if not all) used dynamic programming approach.

are you sure there isn’t mod operation ?

and this : j = j + 1, why it isn’t j = j-1 ?

Ah, you’re right, it should be j =j – 1 (updated).

MOD operation is not needed, What we really need is whether a two substrings have a same hash value or not, not the hash value itself. There are only addition and multiplication operators, so overflowing the result doesn’t matter.

^that comment above is for pasti pas

Halo Mr. Suhendry.

Saya ingin sekali bisa berkompetisi di ACM ICPC. Saya Mahasiswa tingkat 2 di salah satu perguruan tinggi di bandung. Tapi skill koding saya tidak terlalu bagus. algoritma sorting saja masih bingung. Gimana ya kiat nya untuk belajar algoritma yg baik dan benar agar bisa nanti suatu saat ke ACM ICPC?

How to determine which prime number should be used? I tried some prime numbers and the result is not as expected.

(Problem F). Just use large prime number (larger is better, its chance to collide is smaller). I used 1000003 and it’s fine.

I have implemented it and submitted on Live Archive, but got WA 😀

Is there something wrong with my implementation? Could you please check it? http://pastebin.com/ECJX9yEa

I didn’t read your code, but it failed the first sample input: PASTIPAS.

Yes and it works if I do MOD operation, but still WA on Live Archive. Does your solution with above algorithm still work on LA? Maybe you/others have added more test cases to break that hash function? Or my implementation wrong?

I’m the one who send the data to LA and I’m pretty sure LA’s admin was quite busy to do any changes on the dataset.

BTW, I’ve just noticed an error in F’s pseudocode. There is one line which is wrong. Try to figure that out! 🙂

hint: pay attention to the rolling hash (when you do the multiplication with prime number); you might want to check other resources on rolling hash.

^ problem F Pasti Pas!

Problem H can be solved by Dynamic Programming,here I find two states,one is index of the current question,another is wrong answers used/left so far, and then minimize the answer. But this solution does not work,though sample test case gives correct output. I saw another boolean state in some accepted codes in HUST, which tries both minimizing and maximizing. Can you please explain why another state is needed here?