Advertisement
Centipede18

Bai6

Mar 28th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.72 KB | None | 0 0
  1. #include<iostream>
  2. #include<cmath>
  3. using namespace std;
  4.  
  5. void next(int a[], int n){
  6.     int position = n;
  7.     while(position >=1 && a[position] == 1) position--;
  8.     a[position] = 1;
  9.     for(int i = position + 1; i <= n; i++) a[i]=0;
  10. }
  11.  
  12. main(){
  13.     int n, s[100005], b[100005], s1[100005]={0}, s2[100005]={0};
  14.     int xmin;
  15.     cin>>n;
  16.     int mu = pow(2,n);
  17.     for(int i = 1; i <= n; i++){
  18.         cin>>s[i]>>b[i];
  19.     }
  20.     xmin = abs(s[1]-b[1]);
  21.     for(int i = 1; i < mu; i++){
  22.         next(s1, n);
  23.         next(s2, n);
  24.         long long mul = 1, sum = 0;
  25.         for(int i = 1; i <= n; i++){
  26.             if(s1[i] == 1){
  27.                 mul *= s[i];
  28.                 sum += b[i];
  29.             }
  30.             int x;
  31.             if(mul > sum) x = mul - sum;
  32.             else x = sum - mul;
  33.             if(x < xmin) xmin = x;
  34.         }
  35.     }
  36.     cout<<xmin;
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement