Advertisement
newb_ie

a

Sep 2nd, 2021
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.95 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int optimal (vector<int> a,int n) {
  5. int max_sum = INT_MIN,sum = 0;
  6. for (int i = 0; i < n; ++i) {
  7. sum += a[i];
  8. max_sum = max(max_sum,sum);
  9. sum = max(sum,0);
  10. }
  11. return max_sum;
  12. }
  13.  
  14. int brute (vector<int> a,int n) {
  15. int max_sum = 0;
  16. for (int i = 0; i < n; ++i) {
  17. for (int j = i; j < n; ++j) {
  18. int sum = 0;
  19. for (int k = i; k <= j; ++k) {
  20. sum += a[k];
  21. }
  22. max_sum = max(max_sum,sum);
  23. }
  24. }
  25. return max_sum;
  26. }
  27.  
  28. void test () {
  29. for (int test_case = 1; test_case <= 100; ++test_case) {
  30. int n = rand() % 100 + 1;
  31. vector<int> a(n);
  32. for (int i = 0; i < n; ++i) {
  33. a[i] = rand() % 10000;
  34. int p = rand() % 2;
  35. if (p != 1) {
  36. a[i] = -a[i];
  37. }
  38. }
  39. if (brute(a,n) != optimal(a,n)) {
  40. for (auto i : a) cout << i << " ";
  41. return;
  42. }
  43. }
  44. }
  45.  
  46. int main () {
  47. ios::sync_with_stdio(false);
  48. cin.tie(nullptr);
  49. cout.tie(nullptr);
  50. test();
  51. }
  52.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement