描述
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.

T≤12
1≤n,m≤5×105
0≤k≤1018
0≤Pi≤220

输出
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
.
.
.
.
.

分析

固定区间左侧,类似倍增的思想枚举右侧,
设当前的左侧是ll,然后[l,l+2r][l,l+2r]是合法的,[l,l+2r+1][l,l+2r+1]是不合法的,
那么在我们此时在[l+2r,l+2r+1][l+2r,l+2r+1]二分
然后每个区间的合法就是要满足那个小于kk,就是就是最大的mm个数跟最小的mm个数一一对应然后计算得到的结果小于kk,因为这样计算最大,而最大值小于kk则这个区间合法。
.
.
.
.
.

程序:
#include<iostream>
#include<algorithm>
using namespace std;
long long a[500005],b[500005],k;
int n,m,tj;bool cmp(int x,int y)
{return a[x]<a[y];
}bool check(int l,int r)
{int num=1;while (l+num-1<=r) {b[num]=a[l+num-1];num++;}num--;sort(b+1,b+num+1);int x=1,y=num,cnt=0;long long rp=0;while (x<y&&cnt<m){rp+=(b[y]-b[x])*(b[y]-b[x]);if (rp>k) return 0;cnt++;x++;y--;}if (rp<=k) return true;else return false;
}bool work(int w)
{int x=1,y=tj,c=0;long long r=0;while (x<y&&c<m){c++;while (x<y&&b[x]>w) x++;while (x<y&&b[y]>w) y--;if (x>=y) break;r+=(a[b[y]]-a[b[x]])*(a[b[y]]-a[b[x]]);x++;y--;if (r>k) return 0;}if (r<=k) return true;else return false;
}
int main()
{int t;cin>>t;while (t--){cin>>n>>m>>k;for (int i=1;i<=n;i++) cin>>a[i];int ans=0,l=1;while (l<=n){int num=1;while (l+num<=n&&check(l,l+num)) num<<=1;int x=l+num/2,y=l+num;if (y>n) y=n;tj=0;for (int i=l;i<=y;i++) b[++tj]=i;sort(b+1,b+tj+1,cmp);int now=x;while (x<=y){int mid=(x+y)>>1;if (work(mid)) {now=mid;x=mid+1;}else y=mid-1;}ans++;l=now+1;}cout<<ans<<endl;}return 0;
}

转载于:https://www.cnblogs.com/YYC-0304/p/9499902.html

hihoCoder #1384 : Genius ACM [枚举+倍增]相关推荐

  1. hihoCoder#1384 : Genius ACM

    对于一个固定的区间$[l,r]$,显然只要将里面的数字从小到大排序后将最小的$m$个和最大的$m$个配对即可. 如果固定左端点,那么随着右端点的右移,$SPD$值单调不降,所以尽量把右端点往右移,贪心 ...

  2. [hihoCoder 1384]Genius ACM

    Description 题库链接 给定一个整数 \(M\),对于任意一个整数集合 \(S\),定义"校验值"如下: 从集合 \(S\) 中取出 \(M\) 对数(即 \(2\tim ...

  3. CH - 0601 Genius ACM(倍增+归并排序)

    题目链接:点击查看 题目大意: 给定一个正整数M,对于任意一个整数集合S,定义"检验值"如下: 从集合S中取出M对数(即2*M个数,不能重复使用集合中的数,如果S中的整数不够M对, ...

  4. HDU 4081 Qin Shi Huang‘s National Road System(枚举+倍增优化)

    题目链接 题目链接:给你n个点,每个点都有权值,可以免费连接两个点,然后再选择n-2条边构成一个生成树,使得免费连接的两个点的权值和/n-2条边的长度和比值最大.输出这个最大值. 分析:很容易可以想到 ...

  5. 0x06.基本算法 — 倍增

    目录 一.倍增 0.例题引入 1.AcWing 109.Genius ACM(归并+倍增) 二.ST算法 2.luogu P3865 [模板]ST表 三.求LCA(Least Common Ances ...

  6. ST算法 - RMQ(区间最值问题)—— 倍增

    文章目录 引入倍增: 例题1:区间和 例题2:Genius ACM 应用: ST算法 求解 RMQ(区间最值问题) 模板Code: 练习题: ST算法 维护区间最大公约数 例题:Pair of Num ...

  7. 《算法竞赛进阶指南》刷题记录

    总算闲下来一些辣!然后最近发现其实看书是真真很有效但是一直没有落实!所以决定落实一下这段时间把这本书看完题目做完! 然后发现还有挺多题目挺巧妙的于是一堆博客预警,,,可能最近会写很多比较水(但是我还是 ...

  8. 《算法竞赛进阶》学习笔记

    N数码判定性问题 大意就是给你两个N数码(N为奇数),判定是否能互相转化的问题 首先展开成一维,空格不计 如 1 2 3 4 5 6 7 0 8转化为1 2 3 4 5 6 7 8 然后判断两个序列的 ...

  9. 【读书笔记】《算法竞赛进阶指南》读书笔记——0x00基本算法

    to-do: 例题: POJ 1845 Sumdiv 所有的课后习题: 随缘~~~ 位运算 对于一个二进制数,通常称其最低位为第0位,从右往左依此类推. 补码 unsigned int 直接将其看作3 ...

最新文章

  1. 使用Python,OpenCV线程化方式提高视频FPS(每秒帧数)
  2. Access sql语句创建表及字段类型
  3. 怎么跑都不累?自然医学揭秘微生物组提升运动表现
  4. 【直播回放】100分钟全面剖析图像分割任务,学习CV必知
  5. 舍选法抽样matlab,12 重要抽样法 | 统计计算
  6. PHP中 下列哪个操作符用来连接字符串,PHP试题带答案
  7. The content of the adapter has changed but ListView did not receive a notification
  8. 【兼容封装】addEventListener()和attachEvent()跨浏览器的兼容性处理
  9. linux安装安卓fastboot,Android的fastboot协议
  10. ant design vue折叠面板自定义header
  11. 爬取分析拉勾网招聘信息
  12. COLD:中文冒犯性语言检测数据集
  13. uefi模式下重装系统
  14. 剑指offer 手刷python 汇总整理版本~
  15. hevc AMVP模式
  16. IP网络主动测评系统——X-Vision
  17. mac 微信客户端破解版下载地址
  18. 隐私政策协议和人脸服务协议
  19. html5保存到桌面,win8.1将IE浏览器网页内容保存到电脑桌面的两种方法
  20. MATLAB|awgn函数的说明

热门文章

  1. 我与计算机编程的不解之缘(2015.9~2022.1)
  2. [云炬mysql数据库笔记] Work2
  3. VisualStudioCode 中设置中文语言【图文教程】
  4. 返回值类型与函数类型不匹配_C++返回值类型后置(跟踪返回值类型)
  5. 蓝桥杯大学组python试题_第十届蓝桥杯2019年C/C++ 大学A组省赛试题
  6. 自编码器深度分析+定制特征描述子构建初探
  7. SegNet 语义分割网络以及其变体 基于贝叶斯后验推断的 SegNet
  8. vscode + plantuml实现uml的编写
  9. BUUCTF-Reverce:不一样的flag
  10. 数据段描述符和代码段描述符(二)——《x86汇编语言:从实模式到保护模式》读书笔记11