当前位置:网站首页>408 true question - division sequence
408 true question - division sequence
2022-06-13 00:49:00 【Feiyichai】
Problem description
Known by n A set of positive integers A, Divide it into two subsets that do not want to intersect A1 and A2, The number of elements is n1 and n2,A1 and A2 The sum of the elements in is S1 and S2, Design an algorithm as efficient as possible , Satisfy |n1-n2| Minimum and |S1-S2| Maximum .
Algorithmic thought
n2-n1 The minimum is that the two subsequences are half each , And all the elements in half are smaller than any element in the other half
Here you can take advantage of the idea of quick sorting , When the final position of the current pivot is n/2 when , Then stop sorting , Sum and difference the elements of the left and right sequences
Algorithm implementation
void partition(int a[],int low,int high,int n){
int pivot=a[low];
int temp_low=low;
int temp_high=high;
while(low<high){
while(low<high&&pivot<=a[high])--high;
a[low]=a[high];
while(low<high&&pivot>a[low])++low;
a[high]=a[low];
}
a[low]=pivot;
if(low==n/2)// find n/2 Element number
return;
partition(a,temp_low,low-1,n);
partition(a,low+1,temp_high,n);
}The test case
#include<stdio.h>
void partition(int a[],int low,int high,int n){
int pivot=a[low];
int temp_low=low;
int temp_high=high;
while(low<high){
while(low<high&&pivot<=a[high])--high;
a[low]=a[high];
while(low<high&&pivot>a[low])++low;
a[high]=a[low];
}
a[low]=pivot;
if(low==n/2)// find n/2 Element number
return;
partition(a,temp_low,low-1,n);
partition(a,low+1,temp_high,n);
}
int main(){
int a[]={10,2,5,6,88,23,25,9};
partition(a,0,7,8);
int i,s1=0,s2=0;
for(i=0;i<4;i++)
s1+=a[i];
for(;i<8;i++)
s2+=a[i];
printf("%d\n",s2-s1);
printf("%d\n",s2);
}Running results

边栏推荐
- What is dummy change?
- 草在结种子了
- The grass is bearing seeds
- Hard (magnetic) disk (I)
- What is meebits? A brief explanation
- . The way to prove the effect of throwing exceptions on performance in. Net core
- Target recognition gadget
- Kotlin coroutine suspend function suspend keyword
- Three kinds of thinking make me reborn
- Aof persistence
猜你喜欢

ROS2之OpenCV人脸识别foxy~galactic~humble

1. Google grpc framework source code analysis Hello World

JPA execution failed in scheduled task -executing an update/delete query transactionrequiredexception

Arduino control tm1637 common positive four digit nixie tube
![[GYCTF2020]Ezsqli --BUUCTF](/img/8b/3c8b48daf7719482a235fd622737aa.png)
[GYCTF2020]Ezsqli --BUUCTF

Cve-2021-24078 vulnerability analysis

Assembly language learning

Comparison of disk partition modes (MBR and GPT)

Aunt learning code sequel: ability to sling a large number of programmers

kotlin 协程withContext切换线程
随机推荐
Arduino control tm1637 common positive four digit nixie tube
Arduino uses esp8266+ lighting technology + Xiaoai audio to realize voice control switch
杂记:intel11代和12代移动版支持原生Thunderbolt4接口,桌面版不支持
Canvas random bubbling background
Composite key relationships using Sqlalchemy - relationships on composite keys using Sqlalchemy
Androi天气
Paper reading and sharing
Androi weather
硬(磁)盘(二)
[error] invalid use of incomplete type uses an undefined type
今日在家休息
[network protocol] problems and solutions in the use of LwIP
The seventh finals of the Blue Bridge Cup
ROS2之OpenCV人脸识别foxy~galactic~humble
[gxyctf2019] no dolls -- detailed explanation
从ADK的WinPE自己手动构建自己的PE
The grass is bearing seeds
Arduino controls tb6600 driver +42 stepper motor
[North Asia server data recovery] data recovery case of Hyper-V service paralysis caused by virtual machine file loss
[Error] invalid use of incomplete type 使用了未定义的类型