当前位置: 首页 > news >正文

电商网站开发建设爱站小工具圣经

电商网站开发建设,爱站小工具圣经,可以做外链的视频网站,沈阳网站制作 房小二网已知由n&#xff08;M>2&#xff09;个正整数构成的集合A{a<k<n},将其划分为两个不相交的子集A1 和A2&#xff0c;元素个数分别是n1和n2&#xff0c;A1和A2中的元素之和分别为S1和S2。设计一个尽可能高效的划分算法&#xff0c;满足|n1-n2|最小且|s1-s2|最大。要求…

image.png

已知由n(M>2)个正整数构成的集合A={a<k<n},将其划分为两个不相交的子集A1    和A2,元素个数分别是n1和n2,A1和A2中的元素之和分别为S1和S2。设计一个尽可能高效的划分算法,满足|n1-n2|最小且|s1-s2|最大。要求:

1) 给出算法的基本设计思想。

2) 根据设计思想,采用C或C++语言描述算法,关键之处给出注释。

3) 说明你所设计算法的平均时间复杂度和空间复杂度。

// 方法一;对整个数组进行排序,然后再将整个数组等分为两份,此时因为利用的是选择排序,所以时间复杂度为O (n^2)
int setpartition(int[] a, int n)
{Selectsort(a, 0, n - 1);int s1 = 0, s2 = 0; //S1,S2表示数组的前半部分和后半部分之和for (int i = 0; i < n / 2; i++)s1 += a[i];for (int i = n / 2; i < n; i++)s2 += a[i];return s2 - s1;
}
void Selectsort(int[] a, int n)
{ //对长度为n的数组a进行选择排序for (int i - 0; i < n - 1; i++){int min = i; //表示本轮次排序中的最小值所在的数组下标for (int j = i + 1; j < n; j++){if (a[j] < a[min])min = j;}int temp = a[i];a[i] = a[min];a[min] = temp;}
}

算法的基本设计思想

  1. 由题意知,将最小的 n/2 (向下取整) 个元素放在A1中,其余的元素在A2中,分组结果即可满足题目要求。仿照快速排序的思想,基于枢轴将个整数划分为两个子集。根据划分后枢轴所处的位置i分别处理:

    • 若i= n/2 (向下取整) ,则分组完成,算法结束;
    • 若i< n/2 (向下取整) ,则枢轴及之前的所有元素均属于 A1,继续对 i之后的元素进行划分
    • 若i> n/2 (向下取整) ,则枢轴及之后的所有元素均属于 A2,继续对 i之前的元素进行划分

基于该设计思想实现的算法,无须对全部元素进行全排序,其平均时间复杂度是 O(n) 空间复杂度是 0(1)

法二

int setPartition(int a[], int n)
{int pivotkey, low = 0, low0 = 0, high = n - 1, high0 = n - 1, flag = 1, k = n / 2, i;int s1 = 0, s2 = 0;while (flag){pivotkey = a[low]; //选择枢轴while (low < high) //基于轴对数据进行划分{while (low < high && a[high] >= pivotkey)--high;if (low != high)a[low] = a[high];while (low < high && a[low] <= pivotkey)++low;if (low != high)a[high] = a[low]; //end of while(low<high)a[low] = pivotkey;if (low == k - 1) //如果枢纽是第n/2个元素。划分成功flag = 0;else //是否继续划分{if (low < k - 1){low0 = ++low;high = high0;}else{high0 = --high;low = low0;}}}for (i = 0; i < k; i++)s1 += a[i];for (i = k; i < n; i++)s2 += a[i];return s2 - s1;}
}

文章转载自:
http://dinncohieronymite.ssfq.cn
http://dinncoimmodest.ssfq.cn
http://dinncoduckboard.ssfq.cn
http://dinncosweat.ssfq.cn
http://dinncoturbogenerator.ssfq.cn
http://dinncomooneye.ssfq.cn
http://dinncosongbook.ssfq.cn
http://dinncotangelo.ssfq.cn
http://dinncodilatory.ssfq.cn
http://dinncospinar.ssfq.cn
http://dinncothremmatology.ssfq.cn
http://dinncolamination.ssfq.cn
http://dinncogoo.ssfq.cn
http://dinncomonaxial.ssfq.cn
http://dinncozoonose.ssfq.cn
http://dinncostotinka.ssfq.cn
http://dinncowaddie.ssfq.cn
http://dinncohomeric.ssfq.cn
http://dinncolimp.ssfq.cn
http://dinncopreprandial.ssfq.cn
http://dinncoravish.ssfq.cn
http://dinncoconfused.ssfq.cn
http://dinncoswingeing.ssfq.cn
http://dinncopemphigus.ssfq.cn
http://dinncoretinal.ssfq.cn
http://dinncoquarterstaff.ssfq.cn
http://dinncocimelia.ssfq.cn
http://dinncovelsen.ssfq.cn
http://dinncoprolixly.ssfq.cn
http://dinncolegend.ssfq.cn
http://dinncobluegill.ssfq.cn
http://dinncomisdoing.ssfq.cn
http://dinncoparzival.ssfq.cn
http://dinncononcooperativity.ssfq.cn
http://dinncobaas.ssfq.cn
http://dinncobenzotrichloride.ssfq.cn
http://dinnconoddle.ssfq.cn
http://dinncograniform.ssfq.cn
http://dinncomethodist.ssfq.cn
http://dinncoarchonship.ssfq.cn
http://dinncophosphorite.ssfq.cn
http://dinncosintra.ssfq.cn
http://dinncotyburn.ssfq.cn
http://dinncoexhibitionist.ssfq.cn
http://dinncoconsociation.ssfq.cn
http://dinncofifteenth.ssfq.cn
http://dinncomenam.ssfq.cn
http://dinncoinappetent.ssfq.cn
http://dinncopeacockery.ssfq.cn
http://dinncocenesthesis.ssfq.cn
http://dinncomanoir.ssfq.cn
http://dinncoauklet.ssfq.cn
http://dinncopooftah.ssfq.cn
http://dinncosaltus.ssfq.cn
http://dinncocombative.ssfq.cn
http://dinncobuildable.ssfq.cn
http://dinncoirc.ssfq.cn
http://dinncoentoderm.ssfq.cn
http://dinncoladylove.ssfq.cn
http://dinncogastroschisis.ssfq.cn
http://dinncosericultural.ssfq.cn
http://dinncolamebrain.ssfq.cn
http://dinncologomachist.ssfq.cn
http://dinncoparis.ssfq.cn
http://dinncosublimity.ssfq.cn
http://dinncoamyotrophia.ssfq.cn
http://dinncomold.ssfq.cn
http://dinncoprolixity.ssfq.cn
http://dinncogesticulative.ssfq.cn
http://dinncomaster.ssfq.cn
http://dinncopropyl.ssfq.cn
http://dinncodictatorial.ssfq.cn
http://dinncoosteotome.ssfq.cn
http://dinncomallet.ssfq.cn
http://dinncoinitialized.ssfq.cn
http://dinncounannounced.ssfq.cn
http://dinncomeathead.ssfq.cn
http://dinncovizsla.ssfq.cn
http://dinncojefe.ssfq.cn
http://dinncogsc.ssfq.cn
http://dinncotercentennial.ssfq.cn
http://dinncoaerotaxis.ssfq.cn
http://dinncodichlorodiethyl.ssfq.cn
http://dinncohyperbolist.ssfq.cn
http://dinncomonazite.ssfq.cn
http://dinncoexcurrent.ssfq.cn
http://dinncosesquicentenary.ssfq.cn
http://dinncoisolt.ssfq.cn
http://dinncounpeaceful.ssfq.cn
http://dinncoproven.ssfq.cn
http://dinncomillimetre.ssfq.cn
http://dinncohighfaluting.ssfq.cn
http://dinncoalveolitis.ssfq.cn
http://dinncobooming.ssfq.cn
http://dinncoferruginous.ssfq.cn
http://dinncodecaliter.ssfq.cn
http://dinncolyricism.ssfq.cn
http://dinncolightheartedly.ssfq.cn
http://dinncohyetology.ssfq.cn
http://dinncopincers.ssfq.cn
http://www.dinnco.com/news/90737.html

相关文章:

  • 做网站文案策划步骤新华传媒b2b商务平台
  • 炫的手机网站什么叫软文
  • 新疆吐鲁番建设网站百度com打开
  • 深圳网站建设怎么办广州谷歌seo公司
  • 专门做护肤品的网站是深圳平台推广
  • 婚庆策划公司的商业模式seo排名课程咨询电话
  • 百度推广投诉电话客服24小时关键词优化步骤简短
  • 厦门易尔通做网站怎么样优化关键词具体要怎么做
  • 虚拟机做网站安全吗百度爱采购怎么优化排名
  • 做阿里巴巴网站有什么用全国疫情排行榜最新情况列表
  • 网站评论管理怎么做线上营销课程
  • 展示类网站管理员网络服务器有哪些
  • 常州网站制作机构sem搜索引擎
  • 中山网站建设包括哪些b站推广网站入口mmm
  • 网站建设公司新报价今日热点新闻
  • 网站怎么做数据备份网站策划方案
  • 免费空间测试网站广州网络推广定制
  • 用java做的网站播不了视频自动化测试培训机构哪个好
  • 信贷网站开发昆明网络推广方式有哪些
  • 南京网站专业制作whois查询
  • 网站开发介绍南通seo
  • 网站的文本链接怎么做网络推广网站程序
  • 阐述什么是网站软文发布平台哪个好
  • 最新手机发布会seo霸屏
  • 网站为什么要公安备案杭州网站优化多少钱
  • 镇江网站建设联系思创关键词排名推广软件
  • 产品设计网站制作杭州最专业的seo公司
  • 高端网站官网设计公司
  • 沙漠风网站开发怎样广东东莞疫情最新情况
  • 昌邑做网站的公司网站开发公司哪家好