Advertisement
Guest User

Untitled

a guest
Mar 25th, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.59 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.BufferedWriter;
  3. import java.io.IOException;
  4. import java.io.InputStreamReader;
  5. import java.io.OutputStreamWriter;
  6.  
  7. public class Main{
  8. public static int max(int a, int b) {
  9. if(a>=b) return a;
  10. else return b;
  11. }
  12. public static void main(String args[]) throws IOException{
  13. BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  14. BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
  15.  
  16. int cass = Integer.parseInt(br.readLine());
  17. String[] arr = br.readLine().split(" ");
  18. int[] res = new int[cass];
  19.  
  20. for(int i=0;i<cass;i++) {
  21. res[i] = Integer.parseInt(arr[i]);
  22. }
  23.  
  24. int[] inc = new int[cass];
  25. inc[0] = 1;
  26. for(int i=1;i<cass;i++) {
  27. for(int j=i-1;j>=0;j--) {
  28. int max = 1;
  29. if(res[j]<res[i]) {
  30. max = max(max,inc[j]);
  31. inc[i] = max(inc[i],max+1);
  32. }else inc[i] = max(inc[i],1);
  33. }
  34. }
  35.  
  36. int[] dec = new int[cass];
  37. dec[cass-1] = 1;
  38. for(int i=cass-1;i>=0;i--) {
  39. for(int j=i+1;j<cass;j++) {
  40. int max = 1;
  41. if(res[j]<res[i]) {
  42. max = max(max,dec[j]);
  43. dec[i] = max(dec[i],max+1);
  44. }else dec[i] = max(dec[i],1);
  45. }
  46. }
  47.  
  48. int[] ans = new int[cass];
  49. int max = 1;
  50. for(int i=0;i<cass;i++) {
  51. ans[i] = inc[i]+dec[i]-1;
  52. max = max(max,ans[i]);
  53. }
  54.  
  55. bw.write(max+"");
  56. bw.flush();
  57. bw.close();
  58. }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement