POJ3709 K-Anonymous Sequence

时间:2023-01-01 21:42:28

题意

Language:Default
K-Anonymous Sequence
Time Limit: 4000MS Memory Limit: 65536K
Total Submissions: 6618 Accepted: 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;
}

POJ3709 K-Anonymous Sequence的更多相关文章

  1. 【poj3709】 K-Anonymous Sequence

    http://poj.org/problem?id=3709 (题目链接) 题意 给出一个n个数的序列,要求将其中一些数改为另一个比它小的数,改动的花费为两数的绝对值,完成改动后使得整个序列中出现过的 ...

  2. 【dfs】Sequence Decoding

    Sequence Decoding 题目描述 The amino acids in proteins are classified into two types of elements, hydrop ...

  3. Gym 100703G---Game of numbers(DP)

    题目链接 http://vjudge.net/contest/132391#problem/G Description standard input/outputStatements — It' s ...

  4. 转:Python获取随机数(中文)

    下面介绍下random中常见的函数. 前提:需要导入random模块 >>>import random 1.random.random random.random() 用于生成一个0 ...

  5. Qt4--加密日记本&lpar;子例化QMainWindow文本加密解密&rpar;

    近来刚学习Qt4编程,想找个实例练习练习,于是产生了一个想法,就是怎么样做一个文本加密,这样,自己保存的一些文档可以通过软件 生成加密文本,到时候要看的时候,通过自己的软件读取就可以.既然有想法了,那 ...

  6. python随机数

    前提:需要导入random模块 >>>import random 1.random.random random.random()用于生成一个0到1的随机符小数: 0 <= n ...

  7. 关于python 模块导入

    如何将自己写的库加入到python的库路径中: 首先查看python包含的库路径,步骤如下: a.打开python命令界面 b.import  sys    c.sys.path 1.在python安 ...

  8. 剑指Offer 23&period; 二叉搜索树的后序遍历序列 (二叉搜索树)

    题目描述 输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果.如果是则输出Yes,否则输出No.假设输入的数组的任意两个数字都互不相同. 题目地址 https://www.nowcoder ...

  9. 开发笔记:python与随机数&lpar;转&rpar;

    这些天需要用到从一堆数中随机提取几个数,于是重新研究了下random模块. 下面介绍下random中常见的函数. 前提:需要导入random模块 >>>import random 1 ...

  10. Leetcode 413&period; Arithmetic Slice 算术序列切片&lpar;动态规划,暴力&rpar;

    Leetcode 413. Arithmetic Slice 算术序列切片(动态规划,暴力) 题目描述 如果一个数组1.至少三个元素2.两两之间差值相同,那么这个数组就是算术序列 比如下面的数组都是算 ...

随机推荐

  1. 最全的Windows Azure学习教程汇总

    Windows Azure 是微软基于云计算的操作系统,能够为开发者提供一个平台,帮助开发可运行在云服务器.数据中心.Web 和 PC 上的应用程序. Azure 是一种灵活和支持互操作的平台,能够将 ...

  2. SPFA

    SPFA算法用来求单源最短路.可以处理任何有解的情况. 先建一个数组\(dist_x = 起点到x的最短路长度\),当\(x=起点\)时为0,当x和起点不通时为INF(本题中为\(2^31-1\)). ...

  3. 【转】段错误调试神器 - Core Dump详解

    from:http://www.embeddedlinux.org.cn/html/jishuzixun/201307/08-2594.html 段错误调试神器 - Core Dump详解 来源:互联 ...

  4. Bulk&lowbar;Collect&lowbar;Performance 比较

    上一篇讲到了调用集锦,这篇关注一下性能问题吧. DECLARE CURSOR c_tool_list IS SELECT descr d1 FROM hardware; l_descr hardwar ...

  5. Powershell---1 介绍和安装

    Powershell 介绍和安装   Powershell 是运行在windows机器上实现系统和应用程序管理自动化的命令行脚本环境.你可以把它看成是命令行提示符cmd.exe的扩充,不对,应当是颠覆 ...

  6. AET 本征半导体

    本征半导体就是纯净的半导体,不掺杂质的半导体 note:(1)本征半导体中载流子数目极少,其导电性能很差:(2)温度愈高,载流子数目越多,半导体的性能也就越好. 杂质半导体 对于4价半导体,可惨杂3价 ...

  7. win10怎么查看激活到期时间如何看是否永久激活

    win10怎么查看激活到期时间如何看是否永久激活     我们知道Windows系统需要激活后才可以使用全部功能,那么你的Windows10激活了吗?如何查看激活时间呢?是不是永久激活的?带着这些问题 ...

  8. 蜻蜓fm面试

    一面: 面试官首先看简历上写了在腾讯的实习,然后就探讨了半天,各种虚拟化的技术.... 说完之后,估计都半小时过去了,然后就又说了一下你用什么语言,你做的东西都比较偏底层呢,然后你对工作有什么要求吗? ...

  9. Execution Plan 执行计划介绍

    后面的练习中需要下载 Demo 数据库, 有很多不同的版本, 可以根据个人需要下载.  下载地址 -http://msftdbprodsamples.codeplex.com/ 1. 什么是执行计划 ...

  10. LinkedBlockingQueue源码解析(3)

    此文已由作者赵计刚授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 4.3.public E take() throws InterruptedException 原理: 将队 ...