fork download
  1. #include <iostream>
  2. #include <map>
  3. #include <stack>
  4. #define BIGNUM 1000000007
  5.  
  6. using namespace std;
  7.  
  8.  
  9. typedef struct {
  10. int index;
  11. int val;
  12. }entry;
  13.  
  14. int main()
  15. {
  16. int n, k;
  17. unsigned long long soln;
  18. int i,j;
  19. int e;
  20. stack<entry>sa;
  21. int sacount;
  22. int sacurrindex, sacurrval;
  23. unsigned long long p;
  24. entry se;
  25. cin >> n;
  26. cin >> k;
  27. //the basic idea is as follows
  28. //we need to keep indices only when seniority is increasing
  29. //when seniority decreases we map all values to lower value found
  30. soln = 1;
  31. for(i=0;i<n;i++) {
  32. cin >> e;
  33. //Go over the array from high seniority to low
  34. while(!sa.empty()) {
  35. sacurrindex = sa.top().index;
  36. sacurrval = sa.top().val;
  37. if (e >= sacurrval) {
  38. break;
  39. }
  40. else {
  41. p = i - sacurrindex + 1;
  42. soln = soln * p;
  43. soln = soln % BIGNUM;
  44. }
  45. sa.pop();
  46. }
  47. //add to sa
  48. se.index = i;
  49. se.val = e;
  50. sa.push(se);
  51. }
  52. cout << soln << '\n';
  53. return 0;
  54. }
  55.  
Success #stdin #stdout 0s 3436KB
stdin
50 10 
1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10
stdout
202566790