sort排序 c++

发表于 算法模板 分类,标签:
 c++的sort排序默认是进行升序,也可以自定义cmp()排序函数,根据需要进行排序。排序函数的形式为:boolcmp(inta,intb){    .........}a,b是需要排序的数组中的值,a在数组中的位置比b小,即a的下标比b小。如果返回true,则a,b的值不进行交换,若返回false,则交换a,b的值。从大到小排序的cmp()boolcmp(inta,intb){    returna>b;}vector也能用sort进行排序,一般调用sort的方法为:sort(s,s+n,cmp);s是一个int型数组,n为数组的大小,cmp为排序规则。用sort对vector进行排序是:sort(vc.begin(),vc.end(),cmp)vc是一个vector数组。...

PAT (Advanced Level)1114. Family Property (25)

发表于 并查集 分类,标签: pat
 Thistime,youaresupposedtohelpuscollectthedataforfamily-ownedproperty.Giveneachperson'sfamilymembers,andtheestate(房产)infounderhis/herownname,weneedtoknowthesizeofeachfamily,andtheaverageareaandnumberofsetsoftheirrealestate.InputSpecification:Eachinputfilecontainsonetestcase.Foreachcase,thefirstlinegivesapositiveintegerN(<...

PAT (Advanced Level)1113. Integer Set Partition (25)

发表于 模拟 分类,标签: pat
 GivenasetofN(>1)positiveintegers,youaresupposedtopartitionthemintotwodisjointsetsA1 andA2 ofn1 andn2 numbers,respectively.LetS1 andS2 denotethesumsofallthenumbersinA1 andA2,respectively.Youaresupposedtomakethepartitionsothat|n1 -n2|isminimizedfirst,andthen|S1 -S2|ismaximized.InputSpecific...

PAT (Advanced Level)1112. Stucked Keyboard (20)

发表于 模拟 分类,标签: pat
 Onabrokenkeyboard,someofthekeysarealwaysstucked.Sowhenyoutypesomesentences,thecharacterscorrespondingtothosekeyswillappearrepeatedlyonscreenforktimes.Nowgivenaresultingstringonscreen,youaresupposedtolistallthepossiblestuckedkeys,andtheoriginalstring.Noticethattheremightbesomecharactersthataretypedrepeatedly.Thestuckedkeywillalw...

PAT (Advanced Level)1119. Pre- and Post-order Traversals (30)

发表于 分类,标签: pat二叉树
 Supposethatallthekeysinabinarytreearedistinctpositiveintegers.Auniquebinarytreecanbedeterminedbyagivenpairofpostorderandinordertraversalsequences,orpreorderandinordertraversalsequences.However,ifonlythepostorderandpreordertraversalsequencesaregiven,thecorrespondingtreemaynolongerbeunique.Nowgivenapairofpostorderandpreordertraversa...

PAT (Advanced Level) 1111. Online Map (30)

发表于 图论 分类,标签: pat
 Inputourcurrentpositionandadestination,anonlinemapcanrecommendseveralpaths.Nowyourjobistorecommendtwopathstoyouruser:oneistheshortest,andtheotheristhefastest.Itisguaranteedthatapathexistsforanyrequest.InputSpecification:Eachinputfilecontainsonetestcase.Foreachcase,thefirstlinegivestwopositiveintegersN(2<=N<=500),andM,...

1103. Integer Factorization (30)(pat甲级)

发表于 搜索 分类,标签: pat
 TheK-PfactorizationofapositiveintegerNistowriteNasthesumoftheP-thpowerofKpositiveintegers.YouaresupposedtowriteaprogramtofindtheK-PfactorizationofNforanypositiveintegersN,KandP.InputSpecification:EachinputfilecontainsonetestcasewhichgivesinalinethethreepositiveintegersN(<=400),K(<=N)andP(1<P<=7).Thenumbersina...

1102. Invert a Binary Tree (25)(pat甲级)

发表于 分类,标签: pat二叉树
 ThefollowingisfromMaxHowell@twitter:Google:90%ofourengineersusethesoftwareyouwrote(Homebrew),butyoucan'tinvertabinarytreeonawhiteboardsofuckoff.Nowit'syourturntoprovethatYOUCANinvertabinarytree!InputSpecification:Eachinputfilecontainsonetestcase.Foreachcase,thefirstlinegivesapositiveintegerN(<=10)whichisthetotalnumbe...

1101. Quick Sort (25)(pat甲级)

发表于 排序 分类,标签: pat
 Thereisaclassicalprocessnamed partition inthefamousquicksortalgorithm.Inthisprocesswetypicallychooseoneelementasthepivot.Thentheelementslessthanthepivotaremovedtoitsleftandthoselargerthanthepivottoitsright.GivenNdistinctpositiveintegersafterarunofpartition,couldyoutellhowmanyelementscouldbetheselectedpivotforthispart...

1100. Mars Numbers (20)(pat甲级)

发表于 模拟 分类,标签: pat
 PeopleonMarscounttheirnumberswithbase13:ZeroonEarthiscalled"tret"onMars.Thenumbers1to12onEarchiscalled"jan,feb,mar,apr,may,jun,jly,aug,sep,oct,nov,dec"onMars,respectively.Forthenexthigherdigit,Marspeoplenamethe12numbersas"tam,hel,maa,huh,tou,kes,hei,elo,syy,lok,mer,jou",respectively.Forexamples,the...