ACM-ICPC Beijing 2016 Genius ACM(倍增+二分)
描述
给定一个整数 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(倍增+二分)的更多相关文章
- 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 ...
- 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 ...
- CH0601 Genius ACM【倍增】【归并排序】
0601 Genius ACM 0x00「基本算法」例题 描述 给定一个整数 M,对于任意一个整数集合 S,定义“校验值”如下: 从集合 S 中取出 M 对数(即 2∗M 个数,不能重复使用集合中的数 ...
- 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 ...
- 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 ...
- 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) ...
- 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 ...
- 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 ( ...
- 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 ...
随机推荐
- Net 4.5 WebSocket 在 Windows 7, Windows 8 and Server 2012上的比较以及问题
Net 4.5 WebSocket在Windows 8, Windows 10, Windows Server 2012可以,但是在Windows 7, 就会报错. 错误1.“一个文件正在被访问,当前 ...
- Confluence 6 PostgreSQL 创建数据库和数据库用户
一旦你成功的安装了 PostgreSQL 数据库: 创建一个数据库用户,例如 confluenceuser. 你的新用户必须能够 创建数据库对象(create database objects) 和 ...
- UserNotifications ios10 通知使用
通知在ios10 中推荐使用 导入 import UserNotifications 头文件 if #available(iOS 10.0, *) { UNUserNotificationCent ...
- pytorch中的 requires_grad和volatile
https://blog.csdn.net/u012436149/article/details/66971822 简单总结其用途 (1)requires_grad=Fasle时不需要更新梯度, 适用 ...
- npm无反应的问题&npm常用命令
RT: windows安装完nodejs后做了相关环境变量配置后,cmd输入npm命令无反应,就光标一直闪,百度了半天终于找到解决办法 解决方法:C:\Users\Administrator(或你的账 ...
- mysql通配符使用
mysql通配符使用: w3cchool 在mysql查询中,经常会用到通配符,而且mysql的通配符和pgsql是有所不同的,甚至mysql中还可以使用正则表达式.本文就为大家带来mysql查询中通 ...
- 怎么编辑PDF,如何给PDF加水印
在使用PDF文件的时候,往往会用到PDF编辑器来修改,那么,在使用PDF编辑器修改文件的时候,想要在文件中添加水印,这该怎么操作呢,不会的小伙伴可以看看下面的文章了哦,说不定就会了. 1.打开运行PD ...
- OrCAD Capture CIS 16.6 快速地编辑Part的引脚名称
操作系统:Windows 10 x64 工具1:OrCAD Capture CIS 16.6-S062 (v16-6-112FF) 工具2:Excel 参考1:http://www.360doc.co ...
- linux 压缩和解压缩
压缩 tar -cvf jpg.tar *.jpg //将目录里所有jpg文件打包成tar.jpg tar -czf jpg.tar.gz *.jpg //将目录里所有jpg文件打包成jpg.ta ...
- C++ Primer 笔记——重载运算
1.对于二元运算符来说,左侧运算对象传递给第一个参数,而右侧运算对象传递给第二个参数.除了重载的函数调用运算符operator()之外,其他重载元素运算符不能含有默认实参. class test { ...