1、活动安排问题
描述:有n个活动,只能在一个场地安排,假设活动排放时间是按其结束时间非递减排列。活动i【b[i],e[i])表示其时间
b表示开始,e表示结束
int[] b = new int[] {1,3,0,5,3,5,6,8,8,2,12};
int[] e = new int[] {4,5,6,7,8,9,10,11,12,13,14 };
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace xz_贪心算法 { class Program { static void GreedySelecter(int n,int[] b,int[] e,bool[] x) { x[0] = true; Console.Write("1"+" "); int j = 0; for (int i = 1; i < n;i++ ) { if(b[i]>e[j]) { x[i] = true; Console.Write(i+1+" "); j = i; } else { x[i] = false; } } } static void Main(string[] args) { int[] b = new int[] {1,3,0,5,3,5,6,8,8,2,12}; int[] e = new int[] {4,5,6,7,8,9,10,11,12,13,14 }; bool[] x=new bool[11]; GreedySelecter(11,b,e,x); Console.ReadKey(); } } }
结果:1,4,8,11
时间: 2024-10-10 06:29:09