描述

给定一个整数 M,对于任意一个整数集合 S,定义“校验值”如下:

从集合 S 中取出 M 对数(即 2∗M 个数,不能重复使用集合中的数,如果 S 中的整 数不够 M 对,则取到不能取为止),使得“每对数的差的平方”之和最大,这个最大值 就称为集合 S 的“校验值”。

现在给定一个长度为 N 的数列 A 以及一个整数 T。我们要把 A 分成若干段,使得 每一段的“校验值”都不超过 T。求最少需要分成几段。

Advanced CPU Manufacturer (ACM) is one of the best CPU manufacturer in the world. Every day, they manufacture n CPU chips and sell them all over the world.

As you may know, each batch of CPU chips must pass a quality test by the QC department before they can be sold. The testing procedure is as follows:

1) Randomly pick m pairs of CPU chips from the batch of chips (If there are less than 2m CPU chips in the batch of chips, pick as many pairs as possible.)

2) For each pair, measure the Relative Performance Difference (RPD) between the two CPU chips. Let Di be the RPD of the i-th pair

3) Calculate the Sqared Performance Difference (SPD) of the batch according to the following formula:

SPD=∑Di2

If there are only 1 CPU in a batch, then the SPD of that batch is 0.

4) The batch of chips pass the test if and only if SPD≤k, where k is a preseted constant

Usually they send all the n CPU chips as a single batch to the QC department every day. As one of the best CPU manufacturer in the world, ACM never fail the test. However, with the continuous improvement of CPU performance, they find that they are at risk!

Of course they don't want to take any risks. So they make a decision to divide the n chips into several batches to ensure all of them pass the test. What’s more, each batch should be a continuous subsequence of their productions, otherwise the QC department will notice that they are cheating. Quality tests need time and money, so they want to minimize the number of batches.

Given the absolute performance of the n chips P1 ... Pn mesured by ACM in order of manufacture, your task is to determine the minimum number of batches to ensure that all chips pass the test. The RPD of two CPU chips equals to the difference of their absolute performance.

输入格式

The first line contains a single integer T, indicating the number of test cases.

In each test case, the first line contains three integers n, m, k. The second line contains n integers, P1 ... Pn.

输出格式

For each test case, print the answer in a single line.

样例输入

2
5 1 49
8 2 1 7 9
5 1 64
8 2 1 7 9

样例输出

2
1

数据范围与约定

  • T≤12
  • 1≤n,m≤5e5
  • 0≤k≤1e18
  • 0≤Pi≤2e20

思路:

我们知道max(sum) = (最大-最小)²+(次大-次小)²+....

①暴力枚举,枚举每个sum <= T的最大位置, Σ(ilogi) ≈  O(n²logn)

②二分      如果每次都只是向右扩展一位,那么二分的复杂度将会比枚举要高  O(n²log²n)

③倍增+二分   我们让其快速增长,过头后,快速下降。     (倍增+二分 logn、排序校验nlogn)    O(nlog²n)

我们让L = R = P = 1,先校验【L,R+P】,然后倍增R=R+P,P*=2;不符合要求就P/=2,直至P=0,L=R,就是sum <= T最大位置

那么每次符合倍增的时候【L,R】是上次校验过的,也就是说是有序的,我们只需要【R,R+P】排序,和前一段合并成【L,R+P】。

这样的复杂度是

 #include<bits/stdc++.h>
using namespace std; const int maxn = 5e5+;
int t;
int n,m;
int L,R,p; typedef long long ll;
ll k;
ll num[maxn];
ll tmpnum[maxn];
ll tmpnum2[maxn];
void Mergesort(int l,int mid,int r)
{
int i=l,j=mid+;
int k = l;
while(i <= mid && j <= r)
{
if(tmpnum2[i]<tmpnum2[j])
tmpnum[k++] = tmpnum2[i++];
else
tmpnum[k++] = tmpnum2[j++];
}
while(i <= mid)
tmpnum[k++] = tmpnum2[i++];
while(j <= r)
tmpnum[k++] = tmpnum2[j++];
} bool Merge(int l,int mid,int r,int p)
{
for(int i=l; i<=r; i++)
tmpnum2[i] = num[i];
sort(tmpnum2+mid+,tmpnum2+r+);
Mergesort(l,mid,r);
int len = (l+r)/;
ll tmp = ;
for(int i=l; i<=len&&i < l+m; i++)
{
tmp += (tmpnum[l+r-i] - tmpnum[i])*(tmpnum[l+r-i] - tmpnum[i]);
}
if(tmp > k)
return ;
else
{
for(int i=l; i<=r; i++)
{
num[i] = tmpnum[i];
}
return ;
}
} int main()
{
scanf("%d",&t);
while(t--)
{
scanf("%d%d%lld",&n,&m,&k);
for(int i=; i<=n; i++)
scanf("%lld",&num[i]);
p=,R=,L=;
int sum = ;
while(R <= n)
{
if(p)
{
if(R+p > n || Merge(L,R,R+p,p))
p/=;
else
R+=p,p *= ;
}
else
{
R++;
L=R;
p++;
sum++;
}
}
printf("%d\n",sum);
}
}

ACM-ICPC Beijing 2016 Genius ACM(倍增+二分)的更多相关文章

  1. 2016 ACM/ICPC Asia Regional Qingdao Online 1001/HDU5878 打表二分

    I Count Two Three Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  2. HDU 5875 Function 【倍增】 (2016 ACM/ICPC Asia Regional Dalian Online)

    Function Time Limit: 7000/3500 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total ...

  3. CH0601 Genius ACM【倍增】【归并排序】

    0601 Genius ACM 0x00「基本算法」例题 描述 给定一个整数 M,对于任意一个整数集合 S,定义“校验值”如下: 从集合 S 中取出 M 对数(即 2∗M 个数,不能重复使用集合中的数 ...

  4. 2016 ACM/ICPC Asia Regional Qingdao Online(2016ACM青岛网络赛部分题解)

    2016 ACM/ICPC Asia Regional Qingdao Online(部分题解) 5878---I Count Two Three http://acm.hdu.edu.cn/show ...

  5. 2016 ACM/ICPC Asia Regional Shenyang Online 1003/HDU 5894 数学/组合数/逆元

    hannnnah_j’s Biological Test Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K ...

  6. 2016 ACM/ICPC Asia Regional Shenyang Online 1009/HDU 5900 区间dp

    QSC and Master Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) ...

  7. 2016 ACM/ICPC Asia Regional Shenyang Online 1007/HDU 5898 数位dp

    odd-even number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)T ...

  8. 2016 ACM/ICPC Asia Regional Dalian Online 1002/HDU 5869

    Different GCD Subarray Query Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K ( ...

  9. 2016 ACM/ICPC Asia Regional Dalian Online 1006 /HDU 5873

    Football Games Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)To ...

随机推荐

  1. Swift 学习- 09 -- 枚举

    // 递归枚举 // 美家居为一组相关的值定义了一个共同的类型, 使你可以在代码中以类型安全的的方式使用这些值. // 如果你熟悉C语言, 你会知道在C语言中, 枚举会为一组整型值分配相关联的名称, ...

  2. Confluence 6 服务器的许可证信息

    Confluence 6 服务器的许可证信息. https://www.cwiki.us/display/CONFLUENCEWIKI/Managing+your+Confluence+License

  3. Confluence 6 布局高级自定义

    重载 Velocity 模板 velocity 目录是 Confluence Velocity 模板文件进行搜索时候需要的文件夹.例如,你可以通过将你的 Velocity 文件使用正确的文件名放置到正 ...

  4. vuejs中使用echarts

    <style scoped> .content { /*自行添加样式即可*/ } #main { /*需要制定具体高度,以px为单位*/ height: 400px; } </sty ...

  5. mvc 模式和mtc 模式的区别

    首先说说Web服务器开发领域里著名的MVC模式,所谓MVC就是把Web应用分为模型(M),控制器(C)和视图(V)三层,他们之间以一种插件式的.松耦合的方式连接在一起,模型负责业务对象与数据库的映射( ...

  6. vue.js 监听属性的学习/ 千米、米的转换 /时、分、秒 的转换

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...

  7. kafka消息存储与partition副本原理

    消息的存储原理: 消息的文件存储机制: 前面我们知道了一个 topic 的多个 partition 在物理磁盘上的保存路径,那么我们再来分析日志的存储方式.通过 ll /tmp/kafka-logs/ ...

  8. ERROR 1045 (28000): Access denied for user 'mysql'@'localhost' (using password: YES

    一.有时可以直接输入命令: mysql进入数据库 启动数据库:# mysqld_safe & 二.查看用户命令: mysql> use mysql; Reading table info ...

  9. laravel 注入那点事

    public function delete(Group $groupId, Post $postId) { $postId->delete(); return response()->j ...

  10. bzoj2973转移矩阵构造法!

    /* 构造单位矩阵(转移矩阵) 给定n*m网格,每个格子独立按照长度不超过6的操作串循环操作 对应的操作有 0-9:拿x个石头到这个格子 nwse:把这个格子的石头推移到相邻格子 d:清空该格石子 开 ...