Guest User

Untitled

a guest
Dec 14th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.71 KB | None | 0 0
  1. #include<iostream>
  2. using namespace std;
  3. int main()
  4. {
  5. int n;
  6. cin >> n;
  7. int arr[1000] = { 0 };
  8.  
  9. for (int i = 0; i < n; i++)
  10. {
  11. cin >> arr[i];
  12. }
  13. int sum = 0;
  14. //因為邊界的地方不會積水,所以由 1 掃描到 n-2 即可。
  15. for (int pos = 1; pos < n - 1; pos++)
  16. {
  17. //檢查左邊最大值
  18. int lmax = 0;
  19. for (int j = 0; j <= pos; j++)
  20. {
  21. if (arr[j] > lmax)
  22. {
  23. lmax = arr[j];
  24. }
  25. }
  26. //檢查右邊最大值
  27. int rmax = 0;
  28. for (int j = pos; j < n; j++)
  29. {
  30. if (arr[j] > rmax)
  31. {
  32. rmax = arr[j];
  33. }
  34. }
  35. int minmax = 0;
  36. if (rmax > lmax)
  37. minmax = lmax;
  38. else
  39. minmax = rmax;
  40. //sum = sum + (minmax - arr[pos]);
  41. sum += minmax - arr[pos];
  42. }
  43. cout << sum << endl;
  44. return 0;
  45. }
Add Comment
Please, Sign In to add comment