Advertisement
jaskaran_1

trip.c

Dec 22nd, 2012
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.95 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. inline int next_int() {
  4.  int n = 0;
  5. char c = getchar();
  6. while (!('0' <= c && c <= '9'))
  7.  {
  8.    
  9.   c = getchar();
  10.  }
  11.  while ('0' <= c && c <= '9') {
  12.  
  13.  n = n * 10 + c - '0';
  14.  
  15.  c = getchar();
  16.  
  17.  }
  18.  
  19.  return n;
  20.  
  21.  }
  22. int main()
  23. {
  24. int m,n,i;
  25.  
  26.  
  27.  
  28. n=next_int();
  29.  
  30.  
  31. m=next_int();
  32.  
  33.  
  34. int *a=(int *)malloc(n*sizeof(int));
  35.  
  36.  
  37. int *ways=(int *)malloc(n*sizeof(int));
  38.  
  39.  
  40. int *ans=(int *)malloc(n*sizeof(int));
  41.  
  42.  
  43. for(i=0;i<n;i++)
  44.  
  45.  
  46. *(a+i)=next_int();
  47.  
  48.  
  49. for(i=0;i<n;i++)
  50.  
  51.  
  52. *(ans+i)=n;
  53.  
  54.  
  55. ans[0]=0;
  56.  
  57.  
  58. ways[0]=1;
  59.  
  60.  
  61. i=0;//i points to the current location
  62.  
  63.  
  64. int j=i+1;//j points to the location just after i
  65.  
  66.  
  67. while(i<=n-1)
  68. {
  69.  
  70. while(a[j]-a[i]<=m)
  71.  
  72.  
  73. {if(ans[j]==ans[i]+1)
  74.  
  75.  
  76.  ways[j]++;
  77.  
  78.  
  79.  else if(ans[i]+1<ans[j])
  80.  {ans[j]=ans[i]+1;
  81.  
  82.  
  83.  ways[j]=ways[i];}
  84.  
  85.  
  86.  j++;
  87.  
  88.  }
  89.  
  90.  
  91.  i++;
  92.  
  93.  
  94.  j=i+1;
  95.  
  96.  
  97.  }
  98.  
  99.  
  100.  printf("%d %d\n",ans[n-1]-1,ways[n-1]%1000000007);
  101.  return 0;}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement