Advertisement
Korotkodul

N1. Innoplis

Dec 11th, 2021 (edited)
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.80 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <vector>
  4. #include <queue>
  5. #include <algorithm>
  6. #include <string>
  7. #include <stack>
  8. #define pii pair <int,int>
  9. using namespace std;
  10. using ll = long long;
  11. void cv(vector <int> &v){
  12. for (auto x: v) cout<<x<<' ';
  13. cout<<"\n\n";
  14. }
  15. void cvv(vector <vector<int>> &v){
  16. for (int i=0;i<v.size();++i){
  17. for (int j=0;j<v[i].size();++j){
  18. cout<<v[i][j]<<' ';
  19. }cout<<"\n";
  20. }cout<<"\n\n";
  21. }
  22.  
  23. int n;
  24. ll S = 0,tot=-66;
  25. vector <int> a;
  26. vector <vector <int> > dp;
  27. vector <bool> fst;
  28.  
  29. void fnd(int i, int j){
  30. cout<<"ij= "<<i<<' '<<j<<"\n";
  31. if (dp[i][j] == 0) return;
  32. if (dp[i][j] == dp[i-1][j]) {
  33. fst[i] = 0;
  34. fnd(i-1, j);
  35. }
  36. else {
  37. fst[i] = 1;
  38. fnd(i-1, j - a[i]);
  39. }
  40. }
  41.  
  42.  
  43. int main()
  44. {
  45. cin>>n;
  46. a.resize(n+1);
  47. fst.resize(n+1);
  48. for (int i=1;i<=n;++i) {
  49. cin>>a[i];
  50. S+=a[i];
  51. }
  52. tot = S;
  53. S = S / 2;//рюкзак
  54. cout<<"tot= "<<tot<<" S= "<<S<<"\n";
  55. dp.resize(n+1);
  56. for (int i=0;i<n+1;++i) dp[i].resize(S+1,0);
  57. for (int i=1;i<=n;++i){
  58. for (int j=1;j<=S;++j){
  59. if (j >= a[i] ){
  60. dp[i][j] = max(dp[i-1][j], dp[i-1][j - a[i] ] + a[i]);
  61. }else{
  62. dp[i][j] = dp[i-1][j];
  63. }
  64. }
  65. }
  66. cvv(dp);
  67. //cout<<"tot= "<<tot<<"\n";
  68. //cout<<"dp = "<<dp[n][S]<<"\n";
  69. //восстан ответ
  70. fnd(n, S);
  71. int dif = abs( (tot - dp[n][S]) - dp[n][S]);
  72. cout<<dif<<"\n";
  73. for (int i = 1; i <= n; ++i){
  74. if (fst[i]) cout<<1<<' ';
  75. else cout<<2<<' ';
  76. }
  77. }
  78. /*
  79. 5
  80. 1 2 1 4 3
  81.  
  82. 2
  83. 1 1
  84.  
  85. 12
  86. 10 13 14 1 20
  87. 11 12 15 4 5 6 5
  88.  
  89. 12
  90. 11 12 15 4 5 6 5
  91. 10 13 14 1 20
  92.  
  93. */
  94.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement