prestige

Untitled

Mar 21st, 2014
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.56 KB | None | 0 0
  1. #include <algorithm>
  2. #include <bitset>
  3. #include <cassert>
  4. #include <cmath>
  5. #include <cstdio>
  6. #include <cstdlib>
  7. #include <cstring>
  8. #include <iostream>
  9. #include <list>
  10. #include <map>
  11. #include <queue>
  12. #include <set>
  13. #include <string>
  14. #include <sstream>
  15. #include <vector>
  16. #include <complex>
  17. #include <ctime>
  18. #include <stack>
  19.  
  20. using namespace std;
  21.  
  22. typedef long long ll;
  23. typedef unsigned long long ull;
  24. typedef vector<int> VI;
  25. typedef vector< VI > VVI;
  26. typedef pair<int, int> PII;
  27. typedef vector<PII> VPII;
  28.  
  29. #define REP(i, n) for(int i = 0; i < n; ++i)
  30. #define RREP(i, n) for(int i = n - 1; i >= 0; --i)
  31. #define FOR(i, x, y) for(int i = x; i <= y; ++i)
  32. #define RFOR(i, x, y) for(int i = x; i >= y; --i)
  33. #define SZ(a) (int)(a).size()
  34. #define ALL(a) (a).begin(),(a).end()
  35. #define SORT(a) sort(ALL(a))
  36. #define CLEAR(x) memset(x, 0, sizeof x);
  37. #define COPY(FROM, TO) memcpy(TO, FROM, sizeof TO);
  38. #define UNIQUE(c) SORT(c),(c).resize(unique(ALL(c))-(c).begin())
  39. #define pb push_back
  40. #define sqr(x) (x)*(x)
  41. #define X first
  42. #define Y second
  43. const long double pi=acos(-1.0);
  44. const long double eps = 1e-9;
  45. const int INF = 2 * (1e+9);
  46.  
  47. PII dp[51][51][51];
  48. class TaroFriends {
  49. public:
  50. int getNumber(vector <int> c, int x) {
  51. int n = SZ(c);
  52. if (n == 1) return 0;
  53. SORT (c);
  54. dp[1][0][1] = make_pair(c[0] + x, c[0] + x);
  55. dp[1][1][0] = make_pair(c[0] - x, c[0] - x);
  56. FOR (cnt, 2, n) {
  57. FOR (i, 0, cnt) {
  58. int j = cnt - i;
  59. int l = c[cnt - 1] - x, r = c[cnt - 1] + x;
  60. VI res (2, 0);
  61. res[0] = l, res[1] = r;
  62. int ans = INF, left = -INF, right = INF;
  63. int i1 = i - 1, j1 = j;
  64. if (i - 1 >= 0) {
  65. int cur_l = dp[cnt - 1][i1][j1].X, cur_r = dp[cnt - 1][i1][j1].Y;
  66. int new_l = min (cur_l, res[0]), new_r = max (cur_r, res[0]);
  67. int dist = abs(new_l - new_r);
  68. if (dist < ans) {
  69. ans = dist, left = new_l, right = new_r;
  70. }
  71. }
  72. i1 = i, j1 = j - 1;
  73. if (j - 1 >= 0) {
  74. int cur_l = dp[cnt - 1][i1][j1].X, cur_r = dp[cnt - 1][i1][j1].Y;
  75. int new_l = min (cur_l, res[1]), new_r = max (cur_r, res[1]);
  76. int dist = abs(new_l - new_r);
  77. if (dist < ans) {
  78. ans = dist, left = new_l, right = new_r;
  79. }
  80. }
  81. dp[cnt][i][j] = make_pair(left, right);
  82. }
  83. }
  84. int minn = INF;
  85. FOR (i, 0, n) {
  86. int j = n - i;
  87. minn = min (minn, abs(dp[n][i][j].X - dp[n][i][j].Y));
  88. }
  89. return minn;
  90. }
  91. };
Advertisement
Add Comment
Please, Sign In to add comment