Advertisement
nontawat1996

1083

Sep 23rd, 2011
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.97 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. int main()
  4. {
  5.     int* cost;
  6.     int n,i,q,a,b,k,total=0,sell;
  7.     cost=(int*)malloc(sizeof(int)*1000000);
  8.     scanf("%d",&n);
  9.     for(i=0; i<n; i++) scanf("%d",&cost[i]);
  10.     scanf("%d",&q);
  11.     for(k=0; k<q; k++)
  12.     {
  13.         scanf("%d%d",&a,&b);
  14.         a--;
  15.         b--;
  16.         for(i=a; i<b; i++)
  17.         {
  18.             if(i>=b) break;
  19.             //printf("\n--test %d\n",cost[i]);
  20.             if(cost[i]<cost[i+1])
  21.             {
  22.                 sell=i+1;
  23.                 while(1)
  24.                 {
  25.                     if(sell>=b) break;
  26.                     if(cost[sell]<cost[sell+1])
  27.                     {
  28.                         sell++;
  29.                     }
  30.                     else break;
  31.                 }
  32.                 total+=cost[sell]-cost[i];
  33.                 i=sell;
  34.                 //printf("i== %d",i);
  35.             }
  36.         }
  37.         printf("%d",total);
  38.         total=0;
  39.     }
  40.  
  41.     return 0;
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement