Advertisement
a53

Vizita

a53
Dec 27th, 2017
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.34 KB | None | 0 0
  1. /*
  2. Folosirea unui vector de 1000 de elemente de tip long long pe care aplicam un procedeu de programare dinamica,
  3. astfel:
  4. V – vectorul, X – elementul citit, j – pozitia curenta din vector (coloana a matricei),
  5. i – capatul drept al vectorului(linie a matricei).
  6.  
  7. daca pozitia curenta este 1, atunci V[j] = V[j] + x;
  8. daca pozitia curenta este i, atunci V[j] = V[j - 1] + x;
  9. altfel V[j] = min(V[j], V[j - 1]) + x.
  10. */
  11. #include <fstream>
  12. #include <iostream>
  13. using namespace std;
  14. ifstream f("vizita.in");
  15. ofstream g("vizita.out");
  16. int n;
  17. long long x,v[2000];
  18.  
  19. int main()
  20. {
  21. f>>n;
  22. int i,j;
  23. for(i=1;i<=n;++i)
  24. {
  25. for(j=1;j<=i;++j)
  26. {
  27. f>>x;
  28. if(j==1)
  29. v[j]=v[j]+x;
  30. else
  31. {
  32. if(j==i)
  33. v[j]=v[j-1]+x;
  34. else
  35. v[j]=min(v[j],v[j-1])+x;
  36. }
  37. }
  38. }
  39. int n2=n*2-1;
  40. for(int ii=i;ii<=n2;++ii)
  41. {
  42. for(int jj=j-1;jj<=ii;++jj)
  43. {
  44. f>>x;
  45. if(jj==j-1)
  46. v[jj]=v[jj]+x;
  47. else
  48. {
  49. if(jj==ii)
  50. v[jj]=v[jj-1]+x;
  51. else
  52. v[jj]=min(v[jj],v[jj-1])+x;
  53. }
  54. }
  55. }
  56. g<<v[n2];
  57. return 0;
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement