描述

给定一个整数 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. ASP.NET的路由系统:路由映射

    总的来说,我们可以通过RouteTable的静态属性Routes得到一个基于应用的全局路由表,通过上面的介绍我们知道这是一个类型的RouteCollection的集合对象,我们可以通过调用它的MapP ...

  2. mysql 命令总结 每天5个

    mysql -u root   mysql> use mysql;   mysql> UPDATE user SET Password = PASSWORD('newpass') WHER ...

  3. swift 实践- 13 -- UIStepper

    import UIKit class ViewController: UIViewController { var stepper: UIStepper! var label: UILabel! ov ...

  4. easyui生成合并行,合计计算价格

    easyui生成合并行,合计计算价格 注:本文来源: 原创 一:图样你效果图 二:代码实现 1:datagrid 列展示: window.dataGrid = $("#dataGrid&qu ...

  5. opencv 图像矫正

    四个坐标系的转换:https://blog.csdn.net/humanking7/article/details/44756073 标定和矫正:https://blog.csdn.net/u0134 ...

  6. vue 中样式的绑定

    1.class的对象绑定 //对应的css <style> .active { color: red; } </style> <!--html 对应的代码--> & ...

  7. Nginx详解八:Nginx基础篇之Nginx请求限制的配置语法与原理

    Nginx的请求限制: 连接频率的限制:limit_conn_module 配置语法:limit_conn_zone key zone=name:size;默认状态:-配置方法:http 配置语法:l ...

  8. tomcat启动报错 关键字:java.lang.NoClassDefFoundError和 java.lang.ClassNotFoundExceeption

    启动tomcat时报错情况如下图所示:实际上就是依赖的bean出错,百度上很多方法都说是tomcat没有部署正确,项目-->右键----->proterties----->Targe ...

  9. jenkins上节点显示swap空间不足解决方案

    查看内存占用情况:free   -m   1.swap分区原理: swap分区在系统的物理内存不够用的时候,把物理内存中的一部分空间释放出来,以供当前运行的程序使用.那些被释放的空间可能来自一些很长时 ...

  10. 蓝桥杯  历届试题 幸运数  dfs

    历届试题 幸运数 时间限制:1.0s   内存限制:256.0MB 问题描述 幸运数是波兰数学家乌拉姆命名的.它采用与生成素数类似的"筛法"生成 . 首先从1开始写出自然数1,2, ...