Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <fstream>
- #define Nmax 201
- using namespace std;
- const int nota=21;
- const int grup=19;
- 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;
- int sol[21]; /// Daca vreau s-o afisez
- void citire()
- {
- ifstream f("materom.in");
- f>>n>>m;
- for(int i=0;i<n;++i)
- f>>mat[i]>>rom[i];
- f.close();
- }
- void rezolva()
- {
- int i,j,k,p,p2,v;
- for(i=0;i<=m;++i)
- for(j=0;j<=2*nota*m+1;++j)
- l[i][j]=-1;
- /// s=l rezolv pentru un elev
- for(int i=0;i<m;++i)
- for(j=0;j<=2*nota*m+1;++j)
- s[i][j]=l[i][j];
- for(i=0;i<=n-1;i++)
- if(mat[i]+rom[i]>s[0][m*nota+mat[i]-rom[i]])
- {
- l[0][m*nota+mat[i]-rom[i]]=i;
- s[0][m*nota+mat[i]-rom[i]]=mat[i]+rom[i];
- }
- /// restul
- for(j=0;j<=m-2;j++)
- for(k=0;k<=2*nota*m-1;++k)
- if(l[j][k]>=0)
- for(i=0;i<=n-1;++i)
- if(s[j+1][k+mat[i]-rom[i]]<s[j][k]+mat[i]+rom[i])
- {
- p2=k;p=j; /// cautam daca l-am mai folosit
- while((p>=0)&&(l[p][p2]!=i))
- {
- p2=p2-(mat[l[p][p2]]-rom[l[p][p2]]);
- p=p-1;
- }
- /// daca nu il folosim
- if(p<0)
- {
- l[j+1][k+mat[i]-rom[i]]=i;
- s[j+1][k+mat[i]-rom[i]]=s[j][k]+mat[i]+rom[i];
- }
- }
- /// determinare diferenta minima
- v=nota*m+1;
- for(i=0;i<=nota*m;++i)
- if((s[m-1][nota*m+i]>=0)||(s[m-1][nota*m-i]>=0))
- {
- if(s[m-1][nota*m+i]>s[m-1][nota*m-i])
- v=nota*m+i;
- else
- v=nota*m-i;
- break;
- }
- /// determinam solutia
- sm=0;
- sr=0;
- for(j=m-1;j>=0;--j)
- {
- sol[j]=l[j][v]+1;
- sm=sm+mat[l[j][v]];
- sr=sr+rom[l[j][v]];
- v=v-(mat[l[j][v]]-rom[l[j][v]]);
- }
- }
- void scrie()
- {
- ofstream g("materom.out");
- suma=sr+sm; /// calculeaza suma
- if(sr>sm) /// calculeaza diferenta
- diferenta=sr-sm;
- else
- diferenta=sm-sr;
- g<<diferenta<<'\n'<<suma<<'\n';
- g.close();
- }
- int main()
- {
- citire();
- rezolva();
- scrie();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement