POJ

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

思路:dp[i]=dp[j]-a[j+1]*(i-j)+(s[i]-s[j]) 表示前i个数 i~j-1全部赋值a[j-1] 的最小花费

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<bitset>
#include<cstdlib>
#include<cmath>
#include<set>
#include<list>
#include<deque>
#include<map>
#include<queue>
#define ll long long int
using namespace std;
inline ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
inline ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
int moth[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int dir[4][2]={1,0 ,0,1 ,-1,0 ,0,-1};
int dirs[8][2]={1,0 ,0,1 ,-1,0 ,0,-1, -1,-1 ,-1,1 ,1,-1 ,1,1};
const int inf=0x3f3f3f3f;
const ll mod=1e9+7;
ll a[500007];
ll s[500007];
ll q[500007];
ll dp[500007];
ll dy(ll j,ll k){ //由于分子可能为0 所以要用乘积的形式 
    return (dp[j]-s[j]+a[j+1]*j-dp[k]+s[k]-a[k+1]*k);
}
ll dx(ll j,ll k){
    return (a[j+1]-a[k+1]);
}
int main(){
    int t;
    scanf("%d",&t);
    while(t--){
        int n,k;
        scanf("%d%d",&n,&k);
        for(int i=1;i<=n;i++){
            scanf("%lld",a+i);
            s[i]=s[i-1]+a[i];
        }
        int l,r; l=r=1;
        for(int i=k;i<=n;i++){
            while(l<r&&dy(q[l],q[l+1])>i*dx(q[l],q[l+1])) l++;
            dp[i]=dp[q[l]]-a[q[l]+1]*(i-q[l])+(s[i]-s[q[l]]);
            if(i-k+1>=k){ //转移的状态一定要满足 k<=j<=i-k
                while(l<r&&dy(q[r-1],q[r])*dx(q[r],i-k+1)>=dy(q[r],i-k+1)*dx(q[r-1],q[r])) r--;
                q[++r]=i-k+1;
            }
        }
        printf("%lld
",dp[n]);
    }
    return 0;
}
原文地址:https://www.cnblogs.com/wmj6/p/10800045.html