Advertisement
Guest User

Untitled

a guest
Dec 15th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.51 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int atualizaVendendo (int array[], int size) {
  6. for (int i = 0; i < size; i++) {
  7. if (array[i] < 0) {
  8. return i;
  9. }
  10. }
  11. return -1;
  12. }
  13.  
  14. int atualizaComprando (int array[], int size) {
  15. for (int i = 0; i < size; i++) {
  16. if (array[i] > 0) {
  17. return i;
  18. }
  19. }
  20. return -1;
  21. }
  22. int main() {
  23.  
  24.  
  25.  
  26.  
  27. while (1) {
  28.  
  29. int vizinhos;
  30. cin >> vizinhos;
  31.  
  32. long long trabalho = 0;
  33.  
  34. if (vizinhos == 0) {
  35. break;
  36. }
  37.  
  38. int * array = new int[vizinhos];
  39. for (int i=0; i<vizinhos; i++)
  40. cin >> array[i];
  41.  
  42. int vendendo = 0;
  43. int comprando = 0;
  44.  
  45. while (vendendo != -1 && comprando != -1) {
  46.  
  47. vendendo = atualizaVendendo(array, vizinhos);
  48. comprando = atualizaComprando(array, vizinhos);
  49.  
  50.  
  51. if (abs(array[vendendo]) >= array[comprando]) {
  52. trabalho += array[comprando] * abs(comprando - vendendo);
  53.  
  54. array[vendendo] = (abs(array[vendendo]) - array[comprando]) * -1;
  55. array[comprando] = 0;
  56. } else {
  57. trabalho += abs(array[vendendo]) * abs(comprando - vendendo);
  58. array[comprando] = array[comprando] - abs(array[vendendo]);
  59. array[vendendo] = 0;
  60. }
  61.  
  62.  
  63. }
  64.  
  65. std::cout << trabalho << std::endl;
  66.  
  67.  
  68. delete[]array;
  69. }
  70.  
  71. return 0;
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement