Advertisement
a53

m_a_t_e_r_o_m

a53
Jun 16th, 2018
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.31 KB | None | 0 0
  1. #include <fstream>
  2. #define Nmax 201
  3. using namespace std;
  4. const int nota=21;
  5. const int grup=19;
  6. int n,m,mat[Nmax],rom[Nmax],l[grup+1][2*nota*grup+2],s[grup+1][2*nota*grup+2],sm,sr,suma,diferenta;
  7. int sol[21]; /// Daca vreau s-o afisez
  8.  
  9. void citire()
  10. {
  11. ifstream f("materom.in");
  12. f>>n>>m;
  13. for(int i=0;i<n;++i)
  14. f>>mat[i]>>rom[i];
  15. f.close();
  16. }
  17.  
  18. void rezolva()
  19. {
  20. int i,j,k,p,p2,v;
  21. for(i=0;i<=m;++i)
  22. for(j=0;j<=2*nota*m+1;++j)
  23. l[i][j]=-1;
  24. /// s=l rezolv pentru un elev
  25. for(int i=0;i<m;++i)
  26. for(j=0;j<=2*nota*m+1;++j)
  27. s[i][j]=l[i][j];
  28. for(i=0;i<=n-1;i++)
  29. if(mat[i]+rom[i]>s[0][m*nota+mat[i]-rom[i]])
  30. {
  31. l[0][m*nota+mat[i]-rom[i]]=i;
  32. s[0][m*nota+mat[i]-rom[i]]=mat[i]+rom[i];
  33. }
  34. /// restul
  35. for(j=0;j<=m-2;j++)
  36. for(k=0;k<=2*nota*m-1;++k)
  37. if(l[j][k]>=0)
  38. for(i=0;i<=n-1;++i)
  39. if(s[j+1][k+mat[i]-rom[i]]<s[j][k]+mat[i]+rom[i])
  40. {
  41. p2=k;p=j; /// cautam daca l-am mai folosit
  42. while((p>=0)&&(l[p][p2]!=i))
  43. {
  44. p2=p2-(mat[l[p][p2]]-rom[l[p][p2]]);
  45. p=p-1;
  46. }
  47. /// daca nu il folosim
  48. if(p<0)
  49. {
  50. l[j+1][k+mat[i]-rom[i]]=i;
  51. s[j+1][k+mat[i]-rom[i]]=s[j][k]+mat[i]+rom[i];
  52. }
  53. }
  54. /// determinare diferenta minima
  55. v=nota*m+1;
  56. for(i=0;i<=nota*m;++i)
  57. if((s[m-1][nota*m+i]>=0)||(s[m-1][nota*m-i]>=0))
  58. {
  59. if(s[m-1][nota*m+i]>s[m-1][nota*m-i])
  60. v=nota*m+i;
  61. else
  62. v=nota*m-i;
  63. break;
  64. }
  65. /// determinam solutia
  66. sm=0;
  67. sr=0;
  68. for(j=m-1;j>=0;--j)
  69. {
  70. sol[j]=l[j][v]+1;
  71. sm=sm+mat[l[j][v]];
  72. sr=sr+rom[l[j][v]];
  73. v=v-(mat[l[j][v]]-rom[l[j][v]]);
  74. }
  75. }
  76.  
  77. void scrie()
  78. {
  79. ofstream g("materom.out");
  80. suma=sr+sm; /// calculeaza suma
  81. if(sr>sm) /// calculeaza diferenta
  82. diferenta=sr-sm;
  83. else
  84. diferenta=sm-sr;
  85. g<<diferenta<<'\n'<<suma<<'\n';
  86. g.close();
  87. }
  88.  
  89. int main()
  90. {
  91. citire();
  92. rezolva();
  93. scrie();
  94. return 0;
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement