}
Withthesetwoimprovements,thespeedmaybedoubled.
3mergecycles,reducejudgments,andswaploops
Example:
NOIPimprovescombinationandfruit
Two,combinedfruit(fruit.pas/DPR/C/CPP)
[problemdescription]
Inanorchard,manyhavealreadyknockeddownallthefruit,andtheyaredividedintodifferentheapsaccordingtothedifferentkindsoffruit.Manydecidedtoheapallthefruittogether.
Eachtimethemerger,manycancombinetwopilesoffruittogether,andthestrengthconsumedisequaltothesumoftheweightofthetwopilesoffruit.Asyoucansee,allthefruithasbeenleftinthepileafterthen-1merger.Whencombinedwithfruit,thetotalamountofphysicalexertionisequaltothesumoftheenergyconsumedbyeachcombination.
Asmuchefforthasbeenmadetomovethefruithome,mucheffortisneededtoconserveenergywhencombinedwithfruit.Assumethateachfruitweightis1,andthenumberofspeciesknowntofruitandthenumberofeachtypeoffruit,yourtaskistodesignacombinedorderofprograms,soalotofphysicalleastcostandphysicaloutputoftheminimumcostvalue.
Forexample,thereare3kindsoffruit,thenumberis1,2,9.Youcancombinethe1and2heapsfirst,thenewheapnumberis3,andtheenergyconsumptionis3.Next,thenewheapwasmergedwiththeoriginalthird,andthenewheapwas12,consuming12ofthephysicaleffort.Somuchlaborcosts=3+12=15.Itcanbeprovedthat15istheminimumphysicalexertionvalue.
[inputfile]
Theinputfilefruit.incontainstworows,andthefirstlineisanintegern(1<=n<=10000)indicatingthenumberoffruits.Thesecondlinecontainsnintegers,separatedbyspaces.ThefirstIintegerAI(1<=ai<=20000)isthenumberofthefirstIspecies.
[outputfile]
Theoutputfilefruit.outcontainsarowthatcontainsonlyoneinteger,thatis,theminimumphysicalcost.Enterdatatomakesurethisvalueislessthan231.
[sampleinput]
Three
129
[sampleoutput]
Fifteen
[datasize]
For30%ofthedata,ensurethatthereisn<=1000:
For50%ofthedata,ensurethatthereisn<=5000;
Foralldata,besuretohaven<=10000.
ThisproblemwithHuffmanalgorithm,eachtimetofindtwopilesofleastfruitformerger.Youcansortfirst,
Thisisequivalenttodeletingtwoofthefewestfruitsandcombiningthemintoanewpileoffruit.Andtheninsertthepileofnewfruitintoasequence.Repeatthisoperationuntilallfruitisinaheap.
Referencecode
#include
Usingnamespacestd;
Inta[10001]={2147483647};
Voidinsert(int,x,int,y)
{
While(a[x]A[x+1]=y;
}
Int,main()
{
Int,N,s=0;
Cin>>n;
For(inti=1;i<=n;i++)
{
Cin>>a[i];
Insert(i-1,a[i]);
}
For(inti=n-1;i>0;i--)
{
S+=a[i]+=a[i+1];
Insert(i-1,a[i]);
}
Cout<
System("pause");
Return0;
}
4,twowayinsertion
Theabove3descriptionsaredescriptionsofthemagnitudeofthesquaredcalculations.Aswiththetwowayofsorting,theamountofcomputationisboundtodecreaseifwealsodotwo-wayprocessingforthesortoforderlookups.
Classexercise:
programmingcompletestwo-wayinsertion,ascendingsort.IinputtheamountofdataNandMboundaryvalue,thesecondrowisNtobesorteddata.Fordatathatissmallerthanthedemarcationvalue,theoriginalinsertionsort,insteadoflessthantheboundaryvalue,isinsertedbackwardfromthea[n]tothea[1].
Theboundaryvaluehere,ifyoudividetheNdataexactly,willgetthebesttwo-wayprocessingeffect,butthedistributionisnotenoughandtheaverageeffectwillbediscounted.Pleaseevaluatetheamountofcomputationthatdividesthe100dataintothefollowingthreepatterns:
5050
3070
1090
Canyoudescribethetwo-wayinsertionsortbythemostaverageallocation?
Let'staketheCOUNTcompetitionasanexample,
NOIPimprovegroup2007
Firstquestion:
countstatistics
Enteranumbern(n<=200000)andnnaturalnumbers(eachnumberisnotmorethan1.5*10^9),pleasecountthenumberofthesenaturalnumbersappear,insmallorderoutputfromsmalltolarge.Enterdatatoensurethatthenumberofdifferentdoesnotexceed10000.
Sampleinput:
Eight
Two
Four
Two
Four
Five
Onehundred
Two
Onehundred
Sampleoutput:
23
42
51
1002
Thefollowingdescribesthetwowayinsertionusingabidirectionalchain
#include
#include
Usingnamespacestd;
Ifstreamfin("count.in");
OFSTREAMfout("count.out");
Structnode
{
Int,Data,Count;
Node,*previous,*next;
};
Voidins(node,*q,int,x)
{
Node*p=newnode;
P->Data=x;
P->Count=1;
P->previous=q;
Q->next->previous=p;
P->next=q->next;
Q->next=p;
Return;
}
Int,main()
{
Int,clockt=clock();
Node,l,m,R,*z,*t;
Z=&m;
Int,x,N,I,b=0;
Fin>>n>>m.Data;
M.Count=1;
L.Data=-1;
R.Data=1500000001;
L.next=r.previous=z;
M.previous=&l;
M.next=&r;
For(i=1;i{
Fin>>x;
If(x==z->Data)
Z->Count++;
Elseif(xData)
{t=z;
Dot=t->previous;
While(xData);
If(x==t->Data)
T->Count++;
Else
{
Ins(T,X);
B--;
}
}
Else
{t=z;
Dot=t->next;
While(x>t->Data);
If(x==t->Data)
T->Count++;
Else
{
Ins(t->previous,X);
B++;
}
}
If(b<-1)
{
Z=z->previous;
B=0;
}
Elseif(b>1)
{
Z=z->next;
B=0;
}
}
T=l.next;
While(T,=&r)
{
Fout<Data<<''<Count<T=t->next;
}
Cout<<(clock())-clockt/1000.0;
System("pause");
Return0;
}
5.InsertasecondaryhashwithachaintoresolveCOUNT
IfthedatarangeinCOUNTischangedfromtheupperlimitof0#include
Usingnamespacestd;
Ifstreamfin("count.in");
OFSTREAMfout("count.out");
int[15000000];
main()
{intn,a,I,T=clock();
鳍>n;
为(i=0;i<;;+(+))
{
对于(i=1;i<15000000;i+)
如果(b[我]>0)四<<我<<”<
四<<(clock()-T)/1000;
}
面对大范围的数据,自映射哈希法的地址空间就不够了,压缩地址空间很可能造成地址冲突,采用链地址法就是解决地址冲突的常用方法之一。
#包括<文件
#包含iostream><
使用名称空间;
ifstream鳍(“计算。
”);
ofstreamfout(“伯爵。
”);
结点
{
int数据,计数;
下一个节点;
node()
{
下一个=0;
}
};
节点h[150001];
国际main()
{
int,i,j,临时;
鳍>n;
节点p,*q;
对于(i=0;i<150000;i++)
h=新节点;
对于(i=1;i=n;i++)
{
散热片;
p=h[温度/10000];
同时(P>下一步)!
=0和温度>p>下一个>数据
下一页;
如果(下)!
=0和p>下一步>数据=温度)
下面>>计数++;
其他的
{
q=新节点;
下一步;
p=下一个q;
q=数据=温度;
q=计数=1;
}
}
对于(i=0;i<150000;i++)
如果(下一步)!
=0)
{
下一步;
而(P!
=0)
{
四<数据<<”<算<下一页;
}
}
系统(暂停);
返回0;
}
在4和5中,插入都避免了成片搬,把”插”的代价降低到了极点,但”查”的代价仍然很大。
为了降低”查”的代价,可以考虑折半查找。
6折半查找的插入排序
仍然以计数为例,下面的折半插入描述时间复杂度较低。
#包括<文件cstdlib>
#包括<文件
使用名称空间;
ifstream鳍(“伯爵。
”);
ofstreamfout(“伯爵。
”);
结点
{int数据,计数;
};
节点A[10001];
int(int,int,int,x)
{整数;
而(R-L>1)
{=(l+R)/2;
如果(x<数据)
r=m;
其他的
L=m;
}
回我;
}
intmain(intargc、argvchar*[])
{[0]。
数据=1;
A[1]。
数据=2147483647;
整数n,i,x,t,d=1;
T=sizeof(节点);
滚动条;
对于(i=1;i=n;i++)
{
鳍>X;
y=se(0,d,x);
如果(a=y)
计数++;
其他的
{memcpy(+y+2,+y+1,T*(D-Y));
++;
一个[y+1];
a[y+1]。
计数=1;
}
}
对于(i=1;i;d;i+)
四<<一个[我]。
数据<<”<<一个[我]数<返回exit_success;
}
能否在线性结构”查”优化到log2(N)同时”插”也优化到O
(1)呢?
为什么?
7在直接交换的插入排序的基础上进行多路和多轮的改进--希尔排序
原来(52741386)
看成(5)
(2)(7)(4)
(1)(3)(8)(6)
第一轮子序列下标差4:
12745386
第二轮下标差2:
12537486
第三轮下标差1:
(12345678)
用它解决计数的参考代码
#包括<文件
使用名称空间;
ifstreamCIN(“伯爵。
”);
ofstreamcout(“伯爵。
”);
int[200001];
国际main()
{
n,m,i,j,t;
CIN;
m=n;
为(i=0;i<;;+(+))
[i];
当((m=2)>0)
对于(i=m;i<;a;j,t,i+)
为(T=一个[J=我];J>=M&&T<[为了];J=m)
a[j=m];
对于(m=i=1;i=n;i++)
如果(一个[我]==一[·])
米+;
其他的
{
cout<<一[·]<<”<<我<m=1;
}
返回0;
}
ThetimecomplexityisO(n*log2(n)).Thefinalroundofthewhole