当前位置:网站首页>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;
}
边栏推荐
- ByteDance dev better technology salon was successfully held, and we joined hands with Huatai to share our experience in improving the efficiency of web research and development
- mysql5.7安装教程图文详解
- Imitation of numpy 2
- 一、C语言入门基础
- Behind the ultra clear image quality of NBA Live Broadcast: an in-depth interpretation of Alibaba cloud video cloud "narrowband HD 2.0" technology
- Interpretation of SIGMOD '22 hiengine paper
- 华为云ModelArts的使用教程(附详细图解)
- Digital "new" operation and maintenance of energy industry
- Scala基础教程--18--集合(二)
- 力扣刷题日记/day5/2022.6.27
猜你喜欢
被忽视的问题:测试环境配置管理
2022 national CMMI certification subsidy policy | Changxu consulting
【2022年江西省研究生数学建模】水汽过饱和的核化除霾 思路分析及代码实现
Journal des problèmes de brosse à boutons de force / day6 / 6.28
Imitation of numpy 2
Tutorial on the use of Huawei cloud modelarts (with detailed illustrations)
如何提高开发质量
一种将Tree-LSTM的强化学习用于连接顺序选择的方法
力扣刷题日记/day7/2022.6.29
[2022 Jiangxi graduate mathematical modeling] curling movement idea analysis and code implementation
随机推荐
[2022 Jiangxi graduate mathematical modeling] curling movement idea analysis and code implementation
Uni app and uviewui realize the imitation of Xiaomi mall app (with source code)
【2022年江西省研究生数学建模】水汽过饱和的核化除霾 思路分析及代码实现
Scala basic tutorial -- 13 -- advanced function
Li Kou brush question diary /day1/2022.6.23
Unity 制作旋转门 推拉门 柜门 抽屉 点击自动开门效果 开关门自动播放音效 (附带编辑器扩展代码)
Li Kou brush question diary /day8/7.1
Grain Mall (I)
Wireshark packet capturing TLS protocol bar displays version inconsistency
TorchDrug教程
一种将Tree-LSTM的强化学习用于连接顺序选择的方法
PB的扩展DLL开发(超级篇)(七)
VMware Tools和open-vm-tools的安装与使用:解决虚拟机不全屏和无法传输文件的问题
Li Kou brush question diary /day7/2022.6.29
TCP waves twice, have you seen it? What about four handshakes?
1、 Introduction to C language
字节跳动Dev Better技术沙龙成功举办,携手华泰分享Web研发效能提升经验
基于NCF的多模块协同实例
Li Kou brush question diary /day5/2022.6.27
力扣刷题日记/day3/2022.6.25