CH0601 Genius ACM【倍增】【归并排序】
0601 Genius ACM 0x00「基本算法」例题
描述
给定一个整数 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≤5×105
0≤k≤1018
0≤Pi≤220
来源
沈洋,ACM-ICPC Beijing 2016
Contest Hunter - 信息学自助比赛平台
思路:
不论Ai,Aj,Ak,Al的相对大小如何,
的大小不变。因此要使得该式最大,应使4个数中最大的那一个数之前的系数最小。也就是说,最大的和最小的相配,次大的和次小的相配。
为了让分段的段数最少,我们应该要尽量让每一段都在不超过k的前提下尽量多的包含数。
那么我们从头开始对A进行分段,让每一段尽量长,到结尾时就可以得到答案。
所以我们枚举每一段的开头,去找右端点。求校验值时需要对一段进行排序。如果二分右端点R,R可能只会扩展一点点,浪费很多时间。
用倍增求右端点会比较好:
1.初始化p=1,R = L
2.求出[L, R + p]区间的校验值,若校验值<= k, 则R+=p,p*=2,否则p/=2
3.重复,直到p=0,R即为右端点
如果单纯只使用sort,还是会T,因为我们每次倍增时,有一部分的数已经有序了。所以可以运用归并排序的思想。将已经有序的序列和新扩展的序列合并,会大大缩短时间。
虐狗宝典学习笔记:
我们在进行递推时,如果状态空间很大,通常的线性递推无法满足时间与空间复杂度的要求,那么我们可以通过成倍增长的方式,只递推状态空间中在2的整数次幂位置上的值作为代表。当需要其他位置上的值时,我们通过“任意整数可以表示成若干个2的次幂项的和”这一性质,使用之前求出的代表值拼成所需的值。
#include <iostream>
#include <set>
#include <cmath>
#include <stdio.h>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long LL; LL m, n, t, k;
const int maxn = 5e5 + ;
LL s[maxn], b[maxn], c[maxn]; bool check(LL l, LL r, LL lr)
{
LL tot = ;
int t = l, len = r - l + ;
for(LL i = lr; i <= r; i++){
b[i] = s[i];
}
sort(b + lr, b + r + );
if(l == lr){
for(int i = l; l <= r; i++){
c[i] = b[i];
}
}
else{
int i = l, j = lr;
while(i < lr && j <= r){
if(b[i] <= b[j]){
c[t++] = b[i++];
}
else{
c[t++] = b[j++];
}
}
while(i < lr)c[t++] = b[i++];
while(j <= r)c[t++] = b[j++];
}
LL jiaoyan = ;
for(LL i = l; i <= min(l + m - , l + (r - l + ) / - ); i++){
jiaoyan += (c[r - i + l] - c[i]) * (c[r - i + l] - c[i]);
}
if(jiaoyan <= k){
for(int i = l; i <= r; i++){
b[i] = c[i];
}
return ;
}
return ;
} int main()
{
scanf("%lld", &t);
while(t--){
scanf("%lld%lld%lld", &n, &m, &k);
for(LL i = ; i <= n; i++){
scanf("%lld", &s[i]);
} memset(b, , sizeof(b));
memset(c, , sizeof(c));
LL sum = , l = , r = , p = ;
b[] = s[];
while(r < n){
if(!p){
sum++;
p = ;r++;
l = r;
b[l] = s[l];
continue;
}
if(p){
if(r + p <= n && check(l, r + p, r + )){
r += p;
p *= ;
if(r == n)break;
}
else{
p /= ;
}
}
//cout<<sum<<endl; }
if(r == n)sum++; printf("%lld\n", sum);
}
}
CH0601 Genius ACM【倍增】【归并排序】的更多相关文章
- $CH0601\ Genius\ ACM$ 倍增优化DP
ACWing Description 给定一个长度为N的数列A以及一个整数T.我们要把A分成若干段,使得每一段的'校验值'都不超过N.求最少需要分成几段. Sol 首先是校验值的求法: 要使得'每对数 ...
- hihocoder--1384 -- Genius ACM (倍增 归并)
题目链接 1384 -- Genius ACM 给定一个整数 m,对于任意一个整数集合 S,定义“校验值”如下:从集合 S 中取出 m 对数(即 2*M 个数,不能重复使用集合中的数,如果 S 中的整 ...
- hihocoder1384/CH0601 Genius ACM[贪心+倍增+归并排序]
提交地址. 关于lyd给的倍增方法,即从当前枚举向后的$2^k$长度($k$从$1$开始),如果可行就将$k$加一以扩大范围,不可行时将范围不断减半直至$0$. 举个例子,假设当下在1,目标答案是13 ...
- Contest Hunter 0601 Genius ACM
Genius ACM Advanced CPU Manufacturer (ACM) is one of the best CPU manufacturer in the world. Every d ...
- ACM-ICPC Beijing 2016 Genius ACM(倍增+二分)
描述 给定一个整数 M,对于任意一个整数集合 S,定义“校验值”如下: 从集合 S 中取出 M 对数(即 2∗M 个数,不能重复使用集合中的数,如果 S 中的整 数不够 M 对,则取到不能取为止),使 ...
- [hihocoder #1384] Genius ACM 解题报告(倍增)
题目链接:http://hihocoder.com/problemset/problem/1384 题目大意: 给定一个整数 M,对于任意一个整数集合 S,定义“校验值”如下: 从集合 S 中取出 M ...
- AcWing:109. 天才ACM(倍增 + 归并排序)
给定一个整数 MM,对于任意一个整数集合 SS,定义“校验值”如下: 从集合 SS 中取出 MM 对数(即 2∗M2∗M 个数,不能重复使用集合中的数,如果 SS 中的整数不够 MM 对,则取到不能取 ...
- XJOI 7191 Genius ACM
二分+倍增 题目 题目中的最大校验值应由数组排序后,取出最大值和最小值,次大值和次小值--进行做差平方取和 所以在加入一个新的数时,校验值是不会下降的 那么可以发现,校验值是单调递增的,所以可以用二分 ...
- hihoCoder#1384 : Genius ACM
对于一个固定的区间$[l,r]$,显然只要将里面的数字从小到大排序后将最小的$m$个和最大的$m$个配对即可. 如果固定左端点,那么随着右端点的右移,$SPD$值单调不降,所以尽量把右端点往右移,贪心 ...
随机推荐
- labview事件结构学习
编程的主要目的是为了实现用户的某种功能,用户通过用鼠标.键盘.程序内部等触发某种程序动作,从而达到某种结果,这些操作都被称作为事件,LabVIEW中相应这些事件最常用的结构就是“事件结构”.事件结构内 ...
- Windows下安装Scrapy
安装python 根据你的需求下载python安装包,安装python(本文基于python27)https://www.python.org/downloads/ 在 环境变量---"Pa ...
- <img/>标签onerror事件在IE下的bug和解决方法
IE下打开网页时,会弹出“Stack overflow at line: 0”的弹框.经分析,这个bug是由于img标签的onerror事件引起的.程序中用到的代码片段如下:正常情况下显示src所指路 ...
- apk 反编译工具的使用
在学习android 开发的时候,我们经常回尝试使用到别人的apk,希望能了解别人怎么编写的代码,于是想要一个能实现其反编译的软件,将软件反编译出来,查看其代码. 工具/原料 反编译软件dex2jar ...
- Unity3D使用经验总结 编辑器扩展篇【转】
一个引擎,最重要的就是工具,工具除了提升开发速度,提供可视化操作环境以外,还带了容错功能. 它使得大家的工作局限在一定的范围内,比如一个变量的配置,或者是一些类型的选择. 使用编辑器,使得既使不太明白 ...
- PHPCMS v9设置文章的审核功能
对于新建的站点,如果想设置会员发布的文章必须通过审核后才能发布,则需要以下几步来完成: 1.根据需要自定义管理员角色或选择已有角色. 步骤:设置->管理员设置->角色管理->权限设置 ...
- ubuntu压缩
.tar解包:tar xvf FileName.tar打包:tar cvf FileName.tar DirName(注:tar是打包,不是压缩!)-------------------------- ...
- C#字符串二进制互换
static void Main(string[] args) { string str = "宋军辉"; Cons ...
- zoj 1022 - Parallel Expectations
题目:有两个指令序列,在执行时,能够执行随意指令序列的下一条指令.每条指令要一直执行到结束. 求两组指令执行结束后,每一个变量里面存储值的期望. 分析:dp,模拟.这道题算不上难题,只是算得上的麻烦题 ...
- day17<集合框架+>
集合框架(HashSet存储字符串并遍历) 集合框架(HashSet存储自定义对象保证元素唯一性) 集合框架(HashSet存储自定义对象保证元素唯一性图解及代码优化) 集合框架(HashSet如何保 ...