zoukankan      html  css  js  c++  java
  • 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的次幂项的和”这一性质,使用之前求出的代表值拼成所需的值。

     1 #include <iostream>
     2 #include <set>
     3 #include <cmath>
     4 #include <stdio.h>
     5 #include <cstring>
     6 #include <algorithm>
     7 using namespace std;
     8 typedef long long LL;
     9 
    10 LL m, n, t, k;
    11 const int maxn = 5e5 + 5;
    12 LL s[maxn], b[maxn], c[maxn];
    13 
    14 bool check(LL l, LL r, LL lr)
    15 {
    16     LL tot = 0;
    17     int t = l, len = r - l + 1;
    18     for(LL i = lr; i <= r; i++){
    19         b[i] = s[i];
    20     }
    21     sort(b + lr, b + r + 1);
    22     if(l == lr){
    23         for(int i = l; l <= r; i++){
    24             c[i] = b[i];
    25         }
    26     }
    27     else{
    28         int i = l, j = lr;
    29         while(i < lr && j <= r){
    30             if(b[i] <= b[j]){
    31                 c[t++] = b[i++];
    32             }
    33             else{
    34                 c[t++] = b[j++];
    35             }
    36         }
    37         while(i < lr)c[t++] = b[i++];
    38         while(j <= r)c[t++] = b[j++];
    39     }
    40     LL jiaoyan = 0;
    41     for(LL i = l; i <= min(l + m - 1, l + (r - l + 1) / 2 - 1); i++){
    42         jiaoyan += (c[r - i + l] - c[i]) * (c[r - i + l] - c[i]);
    43     }
    44     if(jiaoyan <= k){
    45         for(int i = l; i <= r; i++){
    46             b[i] = c[i];
    47         }
    48         return 1;
    49     }
    50     return 0;
    51 }
    52 
    53 int main()
    54 {
    55     scanf("%lld", &t);
    56     while(t--){
    57         scanf("%lld%lld%lld", &n, &m, &k);
    58         for(LL i = 1; i <= n; i++){
    59             scanf("%lld", &s[i]);
    60         }
    61 
    62         memset(b, 0, sizeof(b));
    63         memset(c, 0, sizeof(c));
    64         LL sum = 0, l = 1, r = 1, p = 1;
    65         b[1] = s[1];
    66         while(r < n){
    67             if(!p){
    68                 sum++;
    69                 p = 1;r++;
    70                 l = r;
    71                 b[l] = s[l];
    72                 continue;
    73             }
    74             if(p){
    75                 if(r + p <= n && check(l, r + p, r + 1)){
    76                     r += p;
    77                     p *= 2;
    78                     if(r == n)break;
    79                 }
    80                 else{
    81                     p /= 2;
    82                 }
    83             }
    84             //cout<<sum<<endl;
    85 
    86         }
    87         if(r == n)sum++;
    88 
    89         printf("%lld
    ", sum);
    90     }
    91 }
  • 相关阅读:
    idea实现快捷批量修改替换
    接口自动化测试:利用环境变量管理测试环境的切换
    接口自动化测试:pytest自定义fixture传参request
    接口自动化测试:python连接mysql方法封装
    接口自动化测试:yaml文件中变量替换
    接口自动化测试:测试数据生成的一个小技巧
    接口自动化测试:抓包方式理解三次握手与四次挥手
    selenium相关:selenium grid的简单用法
    APP测试学习:弱网测试
    APP测试学习:耗电量测试
  • 原文地址:https://www.cnblogs.com/wyboooo/p/9806896.html
Copyright © 2011-2022 走看看