Advertisement
Guest User

Sample text

a guest
May 24th, 2015
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.08 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. #define x first
  4. #define y second
  5. #define all(x) (x).begin(), (x).end()
  6. #define itn int
  7.  
  8. using namespace std;
  9.  
  10. inline int nxt(){
  11.     int x;
  12.     scanf("%d", &x);
  13.     return x;
  14. }
  15.  
  16. int main(){
  17.  
  18.     int n = nxt();
  19.     vector<pair<pair<int, int>, int> > a(n);
  20.     for (int i = 0; i < n; i++){
  21.         a[i].x.x = nxt();
  22.         a[i].x.y = nxt();
  23.         a[i].y = nxt();
  24.     }
  25.     int ans = -1;
  26.     int s = 0;
  27.     for (int i = 0; i < n; i++)
  28.         s += a[i].y;
  29.     clock_t start = clock();
  30.     for (int it = 0;; it++){
  31.         if (it % 1000 == 0){
  32.             if (clock() - start > 1.7 * CLOCKS_PER_SEC)
  33.                 break;
  34.         }
  35.         int q, w;
  36.         do {
  37.             q = rand() % 10000;
  38.             w = rand() % 10000;
  39.             if (rand() % 2)
  40.                 w = -w;
  41.         } while (!q && !w);
  42.         auto cmp = [q, w](const pair<pair<int, int>, int>& x, const pair<pair<int, int>, int>& y){
  43.             return q * x.x.x + w * x.x.y < q * y.x.x + w * y.x.y;
  44.         };
  45.         sort(all(a), cmp);
  46.         int cur = 0;
  47.         for (int i = 0; i < n; i++){
  48.             if (ans == -1 || abs(s - cur - cur) < ans){
  49.                 ans = abs(s - cur - cur);
  50.             }
  51.             cur += a[i].y;
  52.         }
  53.     }
  54.     printf("%d\n", ans);
  55.  
  56.     return 0;
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement