heisenberg0820

Untitled

Jun 29th, 2017
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.25 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. #define wt while(t--)
  4. #define fast ios_base::sync_with_stdio(false)
  5. #define pb push_back
  6. #define pob pop_back
  7. #define pf push_front
  8. #define pof pop_front
  9. #define mp make_pair
  10. #define MOD 1000000007
  11. #define EPS 1e-5
  12. #define INF (1<<28)
  13. #define pi 3.141593
  14. typedef unsigned long long int ull;
  15. typedef long long int ll;
  16.  
  17. using namespace std;
  18.  
  19. ll arr[2000];
  20. ll n;
  21.  
  22. int findMin()
  23. {
  24. int sum = 0;
  25. for (ll i = 0; i < n; i++)
  26. sum += arr[i];
  27. bool dp[n+1][sum+1];
  28. for (ll i = 0; i <= n; i++)
  29. dp[i][0] = true;
  30. for (ll i = 1; i <= sum; i++)
  31. dp[0][i] = false;
  32. for (ll i=1; i<=n; i++)
  33. {
  34. for (ll j=1; j<=sum; j++)
  35. {
  36. dp[i][j] = dp[i-1][j];
  37. if (arr[i-1] <= j)
  38. dp[i][j] |= dp[i-1][j-arr[i-1]];
  39. }
  40. }
  41. ll diff = LLONG_MAX;
  42. for (ll j=sum/2; j>=0; j--)
  43. {
  44. if (dp[n][j] == true)
  45. {
  46. diff = sum-2*j;
  47. break;
  48. }
  49. }
  50. return diff;
  51. }
  52.  
  53. int main()
  54. {
  55. ll t,x,y,i;
  56. cin>>t;
  57. wt
  58. {
  59. cin>>n;
  60. i = 0;
  61. while(i<n)
  62. {
  63. cin>>x>>y;
  64. arr[i++] = abs(x-y);
  65. }
  66. cout<<findMin()<<endl;
  67. }
  68. return 0;
  69. }
Add Comment
Please, Sign In to add comment