题意:区间调度问题

解法:应用贪心算法,贪心的规则:

在可选的节目中,选取结束时间早的节目。

   1:  #include<stdlib.h>
   2:  #include<string.h>
   3:  #include<stdio.h>
   4:  #define N 101
   5:  struct time{
   6:      int s,t;
   7:  }timer[N];
   8:  int comp(const void *p,const void *q){
   9:      struct time a=*(struct time *)p;
  10:      struct time b=*(struct time *)q;
  11:      return a.t-b.t;
  12:  }
  13:  int main(){
  14:      int n,i,ans=0,stamp=0;
  15:      while(scanf("%d",&n)!=EOF&&n){
  16:          for(i=0;i<n;i++)
  17:              scanf("%d %d",&timer[i].s,&timer[i].t);
  18:          qsort(timer,n,sizeof(struct time),comp);//按照结束的时间从小到大排序
  19:          ans=0,stamp=0;
  20:          for(i=0;i<n;i++){
  21:              if(stamp<=timer[i].s){     //stamp:时间戳
  22:                  stamp=timer[i].t;
  23:                  ans++;
  24:              }
  25:          }
  26:          printf("%d\n",ans);
  27:      }
  28:  }
  29:      

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

05-11 14:45