当前位置:网站首页>6.26CF模拟赛E:价格最大化题解
6.26CF模拟赛E:价格最大化题解
2022-07-04 16:54:00 【bj_hacker】
题目描述
链接
文字描述
E. 价格最大化
time limit per test2 s.
memory limit per test256 MB
inputstandard input
outputstandard output
A batch of n goods (n — an even number) is brought to the store, i-th of which has weight ai. Before selling the goods, they must be packed into packages. After packing, the following will be done:
There will be n2 packages, each package contains exactly two goods;
The weight of the package that contains goods with indices i and j (1≤i,j≤n) is ai+aj.
商店里进货了 n (n 是偶数) 件商品,第 i 件重量是 ai。卖出之前,商品需要被打包:
所有商品总计会被打成 n2 个包裹, 每个包裹都恰好包含 2 件商品;
如果一个包裹中包含 第 i 和 第 j (1≤i,j≤n) 件商品,则这个包裹的重量是 ai+aj。
With this, the cost of a package of weight x is always ⌊xk⌋ burles (rounded down), where k — a fixed and given value.
一个重量为 x 的包裹,价格是 ⌊xk⌋ (下取整),其中 k 是一个给定的值。
Pack the goods to the packages so that the revenue from their sale is maximized. In other words, make such n2 pairs of given goods that the sum of the values ⌊xik⌋, where xi is the weight of the package number i (1≤i≤n2), is maximal.
如何打包才能使总利润最大?换句话说,将商品组成 n2 对,使得 ⌊xik⌋ 之和 最大,其中 xi 是 第 i (1≤i≤n2) 个包裹的重量。
For example, let n=6,k=3, weights of goods a=[3,2,7,1,4,8]. Let’s pack them into the following packages.
In the first package we will put the third and sixth goods. Its weight will be a3+a6=7+8=15. The cost of the package will be ⌊153⌋=5 burles.
In the second package put the first and fifth goods, the weight is a1+a5=3+4=7. The cost of the package is ⌊73⌋=2 burles.
In the third package put the second and fourth goods, the weight is a2+a4=2+1=3. The cost of the package is ⌊33⌋=1 burle.
With this packing, the total cost of all packs would be 5+2+1=8 burles.
例如,假设 n=6,k=3,商品重量是 a=[3,2,7,1,4,8]。 我们按如下方法打包:
第一个包裹包含第 3 和 第 6 件商品。则包裹重量是 a3+a6=7+8=15。价格是 ⌊153⌋=5。
第二个包裹包含第 1 和 第 5 件商品。则包裹重量是 a1+a5=3+4=7。价格是 ⌊73⌋=2。
第三个包裹包含第 2 和 第 4 件商品。则包裹重量是 a2+a4=2+1=3。价格是 ⌊33⌋=1。
所有包裹总价格是 5+2+1=8。
Input
The first line of the input contains an integer t (1≤t≤104) —the number of test cases in the test.
第一行包含一个整数 t (1≤t≤104) — 代表测试数据组数。
The descriptions of the test cases follow.
以下描述每个测试数据。
The first line of each test case contains two integers n (2≤n≤2⋅105) and k (1≤k≤1000). The number n — is even.
第一行包含两个整数 n (2≤n≤2⋅105) 和 k (1≤k≤1000)。n 是偶数。
The second line of each test case contains exactly n integers a1,a2,…,an (0≤ai≤109).
第二行包含 n 个整数 a1,a2,…,an (0≤ai≤109)。
It is guaranteed that the sum of n over all the test cases does not exceed 2⋅105.
数据保证所有测试数据的 n 之和不超过 2⋅105。
Output
For each test case, print on a separate line a single number — the maximum possible total cost of all the packages.
对于每组数据,输出一个整数代表最大价格。
Example
inputCopy
6
6 3
3 2 7 1 4 8
4 3
2 1 5 6
4 12
0 0 0 0
2 1
1 1
6 10
2 0 0 5 9 4
6 5
5 3 8 6 3 2
outputCopy
8
4
0
2
1
5
Note
The first test case is analyzed in the statement.
对于第一个样例,题面描述中已经分析了。
In the second test case, you can get a total value equal to 4 if you put the first and second goods in the first package and the third and fourth goods in the second package.
对于第二个样例,我们可以将第一和第二件商品打包、第三和第四件商品打包,总价格可以达到 4。
In the third test case, the cost of each item is 0, so the total cost will also be 0.
对于第三个样例,每个包裹价值都是 0,所以总价值也是 0。
题目分析
- 题目数据非常复杂应先化简缩小问题规模
- 主要运用数学思维整除,将所有数/k向下取整的数相加求和,这部分肯定有。
- 将余数保存
- 局部最优解贪心将i和k-i凑
- 接下来从剩余最大数和能跟他凑的最小数凑
代码实现
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int maxn=2e5+10;
int t,n,k,x;
ll ans;
int cnt[1000+10];
int main(){
scanf("%d",&t);
while(t--){
ans=0;
memset(cnt,0,sizeof(cnt));
scanf("%d%d",&n,&k);
//余数记录
for(int i=1;i<=n;i++){
scanf("%d",&x);
ans=ans+x/k;
cnt[x%k]++;
}
//printf("%d\n",ans);
//第一轮
for(int i=1;i<=k/2;i++){
if(cnt[i]==0)continue;
if(i+i==k){
ans=ans+cnt[i]/2;
cnt[i]%=2;
break;
}
int op=min(cnt[i],cnt[k-i]);
ans=ans+op;
cnt[i]=cnt[i]-op;
cnt[k-i]=cnt[k-i]-op;
}
//printf("%d\n",ans);
//第二轮
for(int i=k-1;i>=1;i--){
if(cnt[i]==0)continue;
for(int j=1;j<=i;j++){
if(cnt[i]==0||cnt[j]==0)continue;
if(i+j>k){
if(i==j){
//printf("%d\n",cnt[i]);
ans=ans+cnt[i]/2;
cnt[i]%=2;
continue;
}
int op=min(cnt[i],cnt[j]);
ans=ans+op;
cnt[i]=cnt[i]-op;
cnt[j]=cnt[j]-op;
}
}
}
printf("%lld\n",ans);
}
return 0;
}
边栏推荐
- “在越南,钱就像躺在街上”
- 【每日一题】871. 最低加油次数
- [cloud voice suggestion collection] cloud store renewal and upgrading: provide effective suggestions, win a large number of code beans, Huawei AI speaker 2!
- 78岁华科教授冲击IPO,丰年资本有望斩获数十倍回报
- [mathematical modeling of graduate students in Jiangxi Province in 2022] analysis and code implementation of haze removal by nucleation of water vapor supersaturation
- Imitation of numpy 2
- 谷粒商城(一)
- Li Kou brush question diary /day6/6.28
- Open source PostgreSQL extension age for graph database was announced as the top-level project of Apache Software Foundation
- Machine learning concept drift detection method (Apria)
猜你喜欢
[HCIA continuous update] network management and operation and maintenance
The block:usdd has strong growth momentum
同事悄悄告诉我,飞书通知还能这样玩
输入的查询SQL语句,是如何执行的?
Nature Microbiology | 可感染阿斯加德古菌的六种深海沉积物中的病毒基因组
庆贺!科蓝SUNDB与中创软件完成七大产品的兼容性适配
Unity makes revolving door, sliding door, cabinet door drawer, click the effect of automatic door opening and closing, and automatically play the sound effect (with editor extension code)
DB engines database ranking in July 2022: Microsoft SQL Server rose sharply, Oracle fell sharply
12 - explore the underlying principles of IOS | runtime [isa details, class structure, method cache | t]
输入的查询SQL语句,是如何执行的?
随机推荐
一、C语言入门基础
创业两年,一家小VC的自我反思
网上开户安全吗?是真的吗?
How to open an account is safe,
华为云ModelArts的使用教程(附详细图解)
Pytoch deep learning environment construction
2022 national CMMI certification subsidy policy | Changxu consulting
fopen、fread、fwrite、fseek 的文件处理示例
The money circle boss, who is richer than Li Ka Shing, has just bought a building in Saudi Arabia
Detailed explanation of the maturity classification of ITSS operation and maintenance capability | one article clarifies the ITSS certificate
中国农科院基因组所汪鸿儒课题组诚邀加入
vbs或vbe如何修改图标
Improve the accuracy of 3D reconstruction of complex scenes | segmentation of UAV Remote Sensing Images Based on paddleseg
The controversial line of energy replenishment: will fast charging lead to reunification?
谷粒商城(一)
【云端心声 建议征集】云商店焕新升级:提有效建议,赢海量码豆、华为AI音箱2!
【每日一题】871. 最低加油次数
Li Kou brush question diary /day7/6.30
[210] usage of PHP delimiter
【系统盘转回U盘】记录系统盘转回U盘的操作