POJ3709 K-Anonymous Sequence

题意

Language:
K-Anonymous Sequence
Time Limit: 4000MSMemory Limit: 65536K
Total Submissions: 6618Accepted: 2210

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 ≤ kn). 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

分析

由于只能减小,所以问题变得简单,不是中位数相关了。先列出DP方程:

\[F[i]=\min_{0\le j\le i-k} \{F[j]+sum[i]-sum[j]-(i-j)*a[j+1]\} \]

整理成斜率式:

\[F[j]-sum[j]+j*a[j+1]=i*a[j+1]+F[i]-sum[i] \]

维护下凸包即可。

时间复杂度\(O(n)\)

代码

学了一招,计算存储分母。

#include<iostream>
#define rg register
#define il inline
#define co const
template<class T>il T read(){
    rg T data=0,w=1;rg char ch=getchar();
    while(!isdigit(ch)) {if(ch=='-') w=-1;ch=getchar();}
    while(isdigit(ch)) data=data*10+ch-'0',ch=getchar();
    return data*w;
}
template<class T>il T read(rg T&x) {return x=read<T>();}
typedef long long ll;

co int N=5e5+1;
ll a[N],s[N],f[N],h[N];
int n,m,q[N];
void K_Anonymous_Sequence(){
	read(n),read(m);
	for(int i=1;i<=n;++i) s[i]=s[i-1]+read(a[i]);
	int l=1,r=0;
	for(int i=1;i<=n;++i){
		h[i-1]=f[i-1]-s[i-1]+(i-1)*a[i];
		if(i>=m<<1){
			int j=i-m;
			while(l<r&&(h[j]-h[q[r]])*(a[q[r]+1]-a[q[r-1]+1])<=(h[q[r]]-h[q[r-1]])*(a[j+1]-a[q[r]+1])) --r;
			q[++r]=j;
			while(l<r&&h[q[l+1]]-h[q[l]]<=i*(a[q[l+1]+1]-a[q[l]+1])) ++l;
			f[i]=f[q[l]]+s[i]-s[q[l]]-a[q[l]+1]*(i-q[l]);
		}
		else f[i]=f[i-1]+a[i]-a[1];
	}
	printf("%lld\n",f[n]);
}
int main(){
	for(int t=read<int>();t--;)K_Anonymous_Sequence();
	return 0;
}

posted on 2019-05-02 17:33  autoint  阅读(150)  评论(0编辑  收藏  举报

导航