【POJ 3709】K-Anonymous Sequence

K-Anonymous Sequence
Time Limit: 4000MS   Memory Limit: 65536K
Total Submissions: 5214   Accepted: 1659

Description

The explosively increasing network data in various application domains has raised privacy concerns for the individuals involved. Recent studies show that simply removing the identities of nodes before publishing the graph/social network data does not guarantee privacy. The structure of the graph itself, along with its basic form the degree of nodes, can reveal the identities of individuals.

To address this issue, we study a specific graph-anonymization problem. We call a graph k-anonymous if for every node v, there exist at least k-1 other nodes in the graph with the same degree as v. And we are interested in achieving k-anonymous on a graph with the minimum number of graph-modification operations.

We simplify the problem. Pick n nodes out of the entire graph G and list their degrees in ascending order. We define a sequence k-anonymous if for every element s, there exist at least k-1 other elements in the sequence equal to s. To let the given sequence k-anonymous, you could do one operation only—decrease some of the numbers in the sequence. And we define the cost of the modification the sum of the difference of all numbers you modified. e.g. sequence 2, 2, 3, 4, 4, 5, 5, with k=3, can be modified to 2, 2, 2, 4, 4, 4, 4, which satisfy 3-anonymous property and the cost of the modification will be |3-2| + |5-4| + |5-4| = 3.

Give a sequence with n numbers in ascending order and k, we want to know the modification with minimal cost among all modifications which adjust the sequence k-anonymous.

Input

The first line of the input file contains a single integer T (1 ≤ T ≤ 20) – the number of tests in the input file. Each test starts with a line containing two numbers n (2 ≤ n ≤ 500000) – the amount of numbers in the sequence and k (2 ≤ k ≤ n). It is followed by a line with n integer numbers—the degree sequence in ascending order. And every number s in the sequence is in the range [0, 500000].

Output

For each test, output one line containing a single integer—the minimal cost.

Sample Input

2
7 3
2 2 3 4 4 5 5
6 2
0 3 3 4 8 9

Sample Output

3
5

Source

 

ANALISIS
动态规划+斜率优化
很容易想到DP方程
  f[i] = min{f[j] + s[i] - s[j] - (i - j) * a[j] | j < i}
斜率优化:
  假设决策k比决策j更优,且j < k,显然有不等式成立
    f[j] + s[i] - s[j] - (i - j) * a[j + 1] > f[k] + s[i] - s[k] - (i - k) * a[k + 1]
  整理得到
    ((f[j] - s[j] + j * a[j + 1]) - (f[j] - s[k] + k * a[k + 1])) > i * (a[j + 1] - a[k + 1])
  设
    G(k, j) = (f[j] - s[j] + j * a[j + 1]) - (f[j] - s[k] + k * a[k + 1])
    S(k, j) = a[j + 1] - a[k + 1]
  因为 S(k, j) <= 0
  设 slope(k, j) = G(k, j) / S(k, j)
  得到 slope(k, j) < i
  很明显,当没有k使得j满足上式时的j是最优决策。
我们用单调队列来维护上述下凹性(画个图出来更好理解一点,点Pi的坐标是(a[i + 1], f[i] - s[i] + i * a[i + 1]),画出来的图会凹下去)。
设队列元素是qh,qh+1,qh+2...qt-3,qt-2,qt-1,且满足slope(qh+1, qh) < slope(qh+2, qh+1)... slope(qt-2, qt-3) < slope(qt-1, qt-2)
插入元素后也必须满足上述条件,且去队首元素时,队首元素要是最优决策。
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;

const int MAXN=500005;
long long f[MAXN],s[MAXN], a[MAXN];
int q[MAXN];
int h, t;

long long G(long long k, long long j)
{
    return f[j] - f[k] - s[j] + s[k] + j * a[j + 1] - k * a[k + 1];
}

long long S(long long k, long long j)
{
    return a[j + 1] - a[k + 1];
}

int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        int n,k;
        scanf("%d %d",&n,&k);
        s[0]=0;
        for(int i=1;i<=n;++i)
        {
            scanf("%I64d",&a[i]);
            s[i]=s[i-1]+a[i];
        }
        memset(f, 0, sizeof(f));
        h = t = 0;
        q[t++] = 0;
        for(int i=k;i<=n;++i)
        {
            if (i - k >= k)
            {
                while (t - h >= 2 && G(q[t - 1], q[t - 2]) * S(i - k, q[t - 1]) >= G(i - k, q[t - 1]) * S(q[t - 1], q[t - 2])) t--;
                q[t++] = i - k;
            }
            while (t - h >= 2 && G(q[h + 1], q[h]) >= i * S(q[h + 1], q[h])) h++;
            f[i] = f[q[h]] + s[i] - s[q[h]] - (i - q[h]) * a[q[h] + 1];
        }
        if (n > k) cout << f[n] << endl;
        else cout << s[n] - n * a[1] << endl;
    }
    return 0;
}
原文地址:https://www.cnblogs.com/albert7xie/p/5058687.html