Genius ACM(倍增+归并排序)

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:

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.
样例输入 Copy
2 
5 1 49 
8 2 1 7 9 
5 1 64 
8 2 1 7 9

 

样例输出 Copy
2 
1

 

提示
T≤12
1≤n,m≤5×10 5
0≤k≤10 18
0≤Pi≤2 20
ps:倍增+归并排序。就卡归并排序。题意是,给你一个序列a,求最小的段数,使得每一段的校检值不超过k,校检值
定义“校验值”如下:
从集合 S 中取出 M 对数(即 2∗M 个数,不能重复使用集合中的数,如果 S 中的整 数不够 M 对,则取到不能取为止),使得“每对数的差的平方”之和最大,这个最大值 就称为集合 S 的“校验值”。
现在给定一个长度为 N 的数列 A 以及一个整数 T。我们要把 A 分成若干段,使得 每一段的“校验值”都不超过 T。求最少需要分成几段。
从第一个开始,len =1;每次乘2,如果可以+len,len*2,else len/2;这样可以得到最大的。
#pragma GCC optimize(3,"Ofast","inline")
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
const int maxn = 5e5+10;
ll n,m,k;

ll a[maxn],b[maxn],c[maxn],d[maxn];

bool check(ll b[],ll l1,ll c[],ll l2) {
    int p1 = 1,p2 = 1,cnt = 0;
    while(p1<= l1||p2<= l2)
    {
        if(p1> l1)
            d[++cnt] = c[p2++];
        else if(p2> l2)
            d[++cnt] = b[p1++];
        else
        {
            if(b[p1]< c[p2])
                d[++cnt] = b[p1++];
            else
                d[++cnt] = c[p2++];
        }
    }
    ll sum = 0;
    for(int i=1;i<=m&&cnt-i+1>i;i++) {
        sum+=(d[cnt-i+1]-d[i])*(d[cnt-i+1]-d[i]);
    }
    if(sum>k) return 0;
    for(int i=1;i<=cnt;i++) b[i] = d[i];
    return 1;
}

int work(int x) {
    int l = x,r = x;
    int len = 1;
    b[1]=a[x];
    while(len>0){
        if(r+len>n) len = n-r;
        for(int i=r+1;i<=r+len;i++) c[i-r]=a[i];
        sort(c+1,c+1+len);
        if(check(b,r-l+1,c,len)) {
            r+=len;
            len<<=1;
        }
        else {
            len>>=1;
        }
        if(r>=n) break;
    }
    return r;
}
int main()
{
    //freopen("R.txt","r",stdin);
    int _;
    scanf("%d",&_);
    while(_--) {
        scanf("%lld%lld%lld",&n,&m,&k);
        //cout<<n<<m<<k<<endl;
        for(int i=1;i<=n;i++) scanf("%lld",&a[i]);
        int ans = 0;
        for(int i=1;i<=n;++i) {
            i = work(i);
            ans++;
        }

        printf("%d\n",ans);
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值