当前位置:网站首页>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;
}
边栏推荐
- Halcon模板匹配
- Nature Microbiology | 可感染阿斯加德古菌的六种深海沉积物中的病毒基因组
- [system disk back to U disk] record the operation of system disk back to U disk
- [209] go language learning ideas
- 激进技术派 vs 项目保守派的微服务架构之争
- 【云端心声 建议征集】云商店焕新升级:提有效建议,赢海量码豆、华为AI音箱2!
- 2022 national CMMI certification subsidy policy | Changxu consulting
- uni-app与uviewUI实现仿小米商城app(附源码)
- How to modify icons in VBS or VBE
- 李迟2022年6月工作生活总结
猜你喜欢
Unity 制作旋转门 推拉门 柜门 抽屉 点击自动开门效果 开关门自动播放音效 (附带编辑器扩展代码)
Grain Mall (I)
Scala基础教程--17--集合
Machine learning concept drift detection method (Apria)
线上MySQL的自增id用尽怎么办?
如何提高开发质量
Numpy 的仿制 2
【Go语言刷题篇】Go完结篇|函数、结构体、接口、错误入门学习
The controversial line of energy replenishment: will fast charging lead to reunification?
2022年全国CMMI认证补贴政策|昌旭咨询
随机推荐
2022 ByteDance daily practice experience (Tiktok)
I wrote a learning and practice tutorial for beginners!
Wireshark packet capturing TLS protocol bar displays version inconsistency
Li Kou brush question diary /day5/2022.6.27
android使用SQLiteOpenHelper闪退
. Net ORM framework hisql practice - Chapter 2 - using hisql to realize menu management (add, delete, modify and check)
2022年字节跳动日常实习面经(抖音)
Scala基础教程--15--递归
【2022年江西省研究生数学建模】冰壶运动 思路分析及代码实现
Digital "new" operation and maintenance of energy industry
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
Microservice architecture debate between radical technologists vs Project conservatives
力扣刷题日记/day2/2022.6.24
Thawte通配符SSL证书提供的类型有哪些
ISO27001认证办理流程及2022年补贴政策汇总
Behind the ultra clear image quality of NBA Live Broadcast: an in-depth interpretation of Alibaba cloud video cloud "narrowband HD 2.0" technology
Nature Microbiology | 可感染阿斯加德古菌的六种深海沉积物中的病毒基因组
力扣刷題日記/day6/6.28
Scala basic tutorial -- 13 -- advanced function
2022年全国CMMI认证补贴政策|昌旭咨询