NC50943. Genius ACM
描述
给定一个整数 。对于任意一个整数集合
,定义“校验值”如下:
从集合 中取出
对数(即
个数,不能重复使用集合中的数,如果
中的整数不够
对,则取到不能取为止),使得“每对数的差的平方”之和最大,这个最大值就称为集合
的“校验值”。
现在给定一个长度为 的数列
以及一个整数
。我们要把数列
分成若干段,使得每一段的“校验值”都不超过
。求最少需要分成几段。
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 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,
输出描述
For each test case, print the answer in a single line.
示例1
输入:
2 5 1 49 8 2 1 7 9 5 1 64 8 2 1 7 9
输出:
2 1
C++14(g++5.4) 解法, 执行用时: 733ms, 内存消耗: 12024K, 提交时间: 2019-09-19 21:14:32
#include<bits/stdc++.h> using namespace std; typedef long long ll; const int Maxn=500020; ll n,m,k,a[Maxn],b[Maxn],c[Maxn],ans,l,r,p; void merge(ll s,ll e,ll mid){ ll i=s,j=mid; for(ll k=s;k<=e;k++){ if(j>e || (i<mid && b[i]<b[j])) c[k]=b[i++]; else c[k]=b[j++]; } } void sort1(ll s,ll e){ for(int i=r+1;i<=e;i++)b[i]=a[i]; sort(b+r+1,b+e+1); merge(s,e,r+1); } bool calc(ll s,ll e){ sort1(s,e); ll tot=0,cnt=0; for(ll i=s,j=e;i<j;i++,j--){ tot+=(c[i]-c[j])*(c[i]-c[j]); cnt++;if(cnt==m)break; } return tot<=k; } int main(){ int T;scanf("%d",&T); while(T--){ scanf("%lld %lld %lld",&n,&m,&k); for(ll i=1;i<=n;i++)scanf("%lld",&a[i]); ans=0;l=r=p=1; while(l<=n){ ans++;p=1; b[l]=a[l]; while(p!=0){ if(r+p<=n && calc(l,r+p)){ r+=p; p<<=1; for(ll i=l;i<=r;i++) b[i]=c[i]; } else p>>=1; } l=r+1; r=l; } printf("%lld\n",ans); } return 0; }
C++(clang++ 11.0.1) 解法, 执行用时: 1166ms, 内存消耗: 32304K, 提交时间: 2023-07-06 16:33:16
#include<bits/stdc++.h> using namespace std; typedef long long ll; const int N=5e5+10; ll n,m,t,tmp[N],a[N],b[N]; bool check(ll l,ll mid,ll r){ for(int i=mid;i<r;i++){ a[i]=b[i]; } sort(a+mid,a+r); int i=l,j=mid,k=0; while(i<mid&&j<r){ if(a[i]<a[j]){ tmp[k++]=a[i++]; } else{ tmp[k++]=a[j++]; } } while(i<mid){ tmp[k++]=a[i++]; } while(j<r){ tmp[k++]=a[j++]; } ll sum=0; for(int i=0;i<m&&i<k;i++,k--){ sum+=(tmp[i]-tmp[k-1])*(tmp[i]-tmp[k-1]); } return sum<=t; } int main(){ ll T; cin>>T; while(T--){ cin>>n>>m>>t; for(int i=0;i<n;i++){ cin>>b[i]; } ll ans=0,start=0,end=0; while(end<n){ ll len=1; while(len){ if(end+len<=n&&check(start,end,end+len)){ end+=len; len<<=1; for(int i=start;i<end;i++){ a[i]=tmp[i-start]; } } else{ len>>=1; } } start=end; ans++; } cout<<ans<<endl; } return 0; }