Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Algorithm 排列范围a(n)(a[i]<;=20)以使相等值形成连续段的最小成本是多少?_Algorithm_Sorting_Greedy - Fatal编程技术网

Algorithm 排列范围a(n)(a[i]<;=20)以使相等值形成连续段的最小成本是多少?

Algorithm 排列范围a(n)(a[i]<;=20)以使相等值形成连续段的最小成本是多少?,algorithm,sorting,greedy,Algorithm,Sorting,Greedy,您提供1个字符串:a1,a2..an(a[i]请注意,由于a[i]0(如果序列已经排序了…)?如果您正在寻找部分排序的想法,那么也许可以通过timsort获得一些想法(例如)是的,我认为这是一个贪婪的问题,或者找到了一个有助于解决这个问题的注释。因为在它的极限处,它有一个异常点a[I]@hellow交换任意两个元素,或者只是相邻的交换?噢,谢谢!我写的东西遗漏了问题的底线!每一步我们只交换两个相邻的位置a[I]&a[I+1]!我很抱歉写信不见了!但我希望你能帮忙me@Primusa我需要你的帮助

您提供1个字符串:
a1,a2..an(a[i]请注意,由于
a[i]
0(如果序列已经排序了…)?如果您正在寻找部分排序的想法,那么也许可以通过timsort获得一些想法(例如)是的,我认为这是一个贪婪的问题,或者找到了一个有助于解决这个问题的注释。因为在它的极限处,它有一个异常点a[I]@hellow交换任意两个元素,或者只是相邻的交换?噢,谢谢!我写的东西遗漏了问题的底线!每一步我们只交换两个相邻的位置a[I]&a[I+1]!我很抱歉写信不见了!但我希望你能帮忙me@Primusa我需要你的帮助!谢谢!希望我以后的问题能得到你的帮助…我爱你
1 1 3 1 3 2 3 2

Swap (a [3], a [4])

Swap (a [6], a [7])

-> 1 1 1 3 3 3 2 2

minimum = 2
dp = [float('inf')] * (1 << M) # every subset of A[i]
dp[0] = 0
for mask in range(len(dp)):
    for bit in range(M):
        # if this A[i] hasn't been moved to the front, we move it to the front
        if (mask >> bit) & 1 == 0: 
            dp[mask^(1 << bit)] = min(dp[mask^(1 << bit)], dp[mask] + cost(mask, bit))
for i in range(len(arr)):
    for j in range(i + 1, len(arr)):
        if arr[i] != arr[j]: inversions[arr[i]][arr[j]] += 1
cost = 0
for bit in range(M):
    # if bit isn't in the mask and thus needs to get swapped with A[i]
    if (subset >> bit) & 1 == 0:
        cost += inversions[bit][A[i]]