usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;namespaceseqlistsort{/// <summary> /// <ather> ///Lihonlin/// </ather> /// <content> ///The problem of moving tables///A teaching building has n classrooms on the first floor, numbered 1, 2 、...、 n from left to right. Now we have to move some desks from some classrooms///in other classrooms, each table is moved from a smaller classroom to a numbered classroom, and every trip goes from left to right,///after you have finished moving a desk, you can continue to move from the current position or to the right to take another table. Input data: First enter N, M, and then immediately///The m-line enters the starting classroom and target classroom for the M-card to move the desks. Output data: You need to run a few trips at least. ///Sample Input///Ten 5///1 3///3 9///4 6///6 Ten///7 8///Sample Output///3/// </content> /// </summary> Public structThe hostel { Public intstart; Public intend; } classGreedy_desk {Const intSize = -; Static intm; Static intN; StaticRoom[] TheNewRoom[size]; Static BOOL[] Ismove =New BOOL[size]; Static intMin =0; Static voidInitData () {Console.WriteLine ("Please enter n classrooms"); N=Convert.ToInt32 (Console.ReadLine ()); Console.WriteLine ("Please enter the M-line desk"); M=Convert.ToInt32 (Console.ReadLine ()); Console.WriteLine ("Enter the starting classroom and target classroom for the M-card to move the desks"); for(inti =0; I < m; ++i) {Room[i].start=Convert.ToInt32 (Console.ReadLine ()); Room[i].end=Convert.ToInt32 (Console.ReadLine ()); } } Static voidSort () { for(inti =0; I < m; ++i) { for(intj =0; J < M-i-1; ++j) {if(Room[j].start > Room[j +1].start) {inttemp =Room[j].start; Room[j].start= Room[j +1].start; Room[j+1].start =temp; } } } } Public Static voidMovedesk () {initdata (); Sort (); intnum =0; while(Num <m) {inttemp =0; for(inti =0; I < m; ++i) {if(Temp <= room[i].start &&!)Ismove[i]) {Temp=Room[i].end; Ismove[i]=true; Num++; }} min++; } Console.WriteLine ("minimum run of {0} trips required", Min); } }}
The problem of moving tables with greedy algorithm