当前位置:网站首页>6.26cf simulation race e: solution to the problem of price maximization
6.26cf simulation race e: solution to the problem of price maximization
2022-07-04 18:53:00 【bj_ hacker】
6.26CF Simulation game E: Price maximization solution
Title Description
link
CF Simulation game E: Price maximization solution
A word description
E. Price maximization
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.
There is stock in the store n (n It's even ) Commodity , The first i The weight of each piece is ai. Before selling , Goods need to be packed :
All items will be marked as n2 Parcel , Each package happens to contain 2 Commodity ;
If a package contains The first i and The first j (1≤i,j≤n) Commodity , Then the weight of this package is 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.
One weighs x Package , price is ⌊xk⌋ ( Round down ), among k Is a given value .
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.
How to package to maximize the total profit ? let me put it another way , Combine products n2 Yes , bring ⌊xik⌋ The sum of the Maximum , among xi yes The first i (1≤i≤n2) The weight of a package .
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.
for example , hypothesis n=6,k=3, The weight of the goods is a=[3,2,7,1,4,8]. We pack it as follows :
The first package contains 3 and The first 6 Commodity . Then the package weight is a3+a6=7+8=15. price is ⌊153⌋=5.
The second package contains 1 and The first 5 Commodity . Then the package weight is a1+a5=3+4=7. price is ⌊73⌋=2.
The third package contains 2 and The first 4 Commodity . Then the package weight is a2+a4=2+1=3. price is ⌊33⌋=1.
The total price of all packages is 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.
The first line contains an integer t (1≤t≤104) — Number of representative test data groups .
The descriptions of the test cases follow.
The following describes each test data .
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.
The first line contains two integers n (2≤n≤2⋅105) and k (1≤k≤1000).n It's even .
The second line of each test case contains exactly n integers a1,a2,…,an (0≤ai≤109).
The second line contains n It's an integer a1,a2,…,an (0≤ai≤109).
It is guaranteed that the sum of n over all the test cases does not exceed 2⋅105.
Data guarantees the accuracy of all test data n The sum does not exceed 2⋅105.
Output
For each test case, print on a separate line a single number — the maximum possible total cost of all the packages.
For each set of data , Output an integer representing the maximum price .
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.
For the first example , The problem has been analyzed in the description .
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.
For the second example , We can pack the first and second items 、 The third and fourth items are packed , The total price can reach 4.
In the third test case, the cost of each item is 0, so the total cost will also be 0.
For the third example , The value of each package is 0, So the total value is 0.
Topic analysis
- The subject data is very complex, so we should simplify and reduce the scale of the small problem first
- Mainly use mathematical thinking to divide , All numbers /k Sum the numbers rounded down , There must be .
- Save the remainder
- The local optimal solution will be greedy i and k-i Gather together
- Next, from the remaining maximum number and the minimum number that can be counted with him
Code implementation
#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);
// Remainder record
for(int i=1;i<=n;i++){
scanf("%d",&x);
ans=ans+x/k;
cnt[x%k]++;
}
//printf("%d\n",ans);
// The first round
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);
// The second round
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;
}
边栏推荐
- Blue bridge: sympodial plant
- 2022年字节跳动日常实习面经(抖音)
- 6.26CF模拟赛B:数组缩减题解
- 学习路之PHP--phpstudy创建项目时“hosts文件不存在或被阻止打开”
- 怎么开户才是安全的,
- 6.26CF模拟赛E:价格最大化题解
- Pb extended DLL development (super chapter) (VII)
- 78 year old professor Huake impacts the IPO, and Fengnian capital is expected to reap dozens of times the return
- vbs或vbe如何修改图标
- 输入的查询SQL语句,是如何执行的?
猜你喜欢
Once the "king of color TV", he sold pork before delisting
同事悄悄告诉我,飞书通知还能这样玩
Li Kou brush question diary /day6/6.28
Weima, which is going to be listed, still can't give Baidu confidence
输入的查询SQL语句,是如何执行的?
[cloud native] what is the "grid" of service grid?
Installation and use of VMware Tools and open VM tools: solve the problems of incomplete screen and unable to transfer files of virtual machines
Just today, four experts from HSBC gathered to discuss the problems of bank core system transformation, migration and reconstruction
How to improve development quality
2022 national CMMI certification subsidy policy | Changxu consulting
随机推荐
Detailed explanation of the maturity classification of ITSS operation and maintenance capability | one article clarifies the ITSS certificate
Once the "king of color TV", he sold pork before delisting
[cloud voice suggestion collection] cloud store renewal and upgrading: provide effective suggestions, win a large number of code beans, Huawei AI speaker 2!
Unity 制作旋转门 推拉门 柜门 抽屉 点击自动开门效果 开关门自动播放音效 (附带编辑器扩展代码)
[2022 Jiangxi graduate mathematical modeling] curling movement idea analysis and code implementation
I always thought that excel and PPT could only be used for making statements until I saw this set of templates (attached)
How to modify icons in VBS or VBE
激进技术派 vs 项目保守派的微服务架构之争
Mxnet implementation of googlenet (parallel connection network)
General environmental instructions for the project
Improve the accuracy of 3D reconstruction of complex scenes | segmentation of UAV Remote Sensing Images Based on paddleseg
Blue bridge: sympodial plant
Numpy 的仿制 2
一种将Tree-LSTM的强化学习用于连接顺序选择的方法
Li Kou brush question diary /day4/6.26
Reptile elementary learning
uni-app与uviewUI实现仿小米商城app(附源码)
Machine learning concept drift detection method (Apria)
未来几年中,软件测试的几大趋势是什么?
项目通用环境使用说明