fork download
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.util.ArrayList;
  5. import java.util.Collections;
  6. import java.util.List;
  7. import java.util.Map.Entry;
  8. import java.util.StringTokenizer;
  9. import java.util.TreeMap;
  10.  
  11. /*
  12. * To change this license header, choose License Headers in Project Properties.
  13. * To change this template file, choose Tools | Templates
  14. * and open the template in the editor.
  15. */
  16. /**
  17. *
  18. * @author Nidhi
  19. */
  20. //http://w...content-available-to-author-only...f.com/problems/CHFQUEUE
  21. class CHFQUEUE {
  22.  
  23. public static void main(String[] args) throws IOException {
  24. StringTokenizer st = new StringTokenizer(br.readLine(), " ");
  25. long ans = 1;
  26. TreeMap<Integer, List<Integer>> tr = new TreeMap<Integer, List<Integer>>(Collections.reverseOrder());
  27.  
  28. int n = Integer.parseInt(st.nextToken());
  29. int k = Integer.parseInt(st.nextToken());
  30.  
  31. st = new StringTokenizer(br.readLine(), " ");
  32. int index = 1;
  33. while (st.hasMoreTokens()) {
  34. int a =Integer.parseInt(st.nextToken());
  35. for (Entry<Integer, List<Integer>> e : tr.entrySet()) {
  36. if (e.getKey() <= a) {
  37. break;
  38. }
  39. for (long m : e.getValue()) {
  40. ans = (long) ((ans * (index - m + 1)) % (10e9 + 7));
  41. }
  42. tr.put(e.getKey(),new ArrayList<Integer>());
  43. }
  44.  
  45. if (a != 1) {
  46. if (!tr.containsKey(a)) {
  47. tr.put(a, new ArrayList<Integer>());
  48. }
  49. tr.get(a).add(index);
  50.  
  51. }
  52.  
  53. index++;
  54. }
  55.  
  56. System.out.println(ans);
  57. }
  58. }
Success #stdin #stdout 0.08s 380224KB
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
4495557338