Knobody

Untitled

Sep 10th, 2019
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.06 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define dbg(i,j) cout<<"I am "<<i<<" = "<<endl<<j<<endl;
  3. #define dbr(name,a) cout<<name<<endl;for(auto x:a)cout<<x<<" ";cout<<endl;
  4. #define DBR(name,a) cout<<name<<endl;for(auto x:a){ for(auto y:x)cout<<y<<" ";cout<<endl;}
  5. #define dbmp(name,a) cout<<name<<endl;for(auto x:a){ cout<<x.first<<"\t"<<x.second<<endl;}
  6. #define dbp(name,a) cout<<name<<endl;cout<<a.first<<"\t"<<a.second<<endl;
  7. #define boost ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
  8. using namespace std;
  9.  
  10. typedef long long int big;
  11.  
  12. typedef  long double fig;
  13.  
  14.  
  15. big power(big x, big y, big p)
  16. {
  17.     big res = 1;      // Initialize result
  18.  
  19.     x = x % p;  // Update x if it is more than or  
  20.                 // equal to p
  21.  
  22.     while (y > 0)
  23.     {
  24.         // If y is odd, multiply x with result
  25.         if (y & 1)
  26.             res = (res*x) % p;
  27.  
  28.         // y must be even now
  29.         y = y>>1; // y = y/2
  30.         x = (x*x) % p;  
  31.     }
  32.     return res;
  33. }
  34.  
  35.  
  36. bool prime[100005];
  37.  
  38.  
  39. void SieveOfEratosthenes(big n)
  40. {
  41.     // Create a boolean array "prime[0..n]" and initialize
  42.     // all entries it as true. A value in prime[i] will
  43.     // finally be false if i is Not a prime, else true.
  44.      
  45.     //bool prime[n+1];
  46.     memset(prime, true, sizeof(prime));
  47.  
  48.     for (int p=2; p*p<=n; p++)
  49.     {
  50.         // If prime[p] is not changed, then it is a prime
  51.         if (prime[p] == true)
  52.         {
  53.             // Update all multiples of p greater than or  
  54.             // equal to the square of it
  55.             // numbers which are multiple of p and are
  56.             // less than p^2 are already been marked.  
  57.             for (int i=p*p; i<=n; i += p)
  58.                 prime[i] = false;
  59.         }
  60.     }
  61.  
  62.     // Print all prime numbers
  63.     /*for (int p=2; p<=n; p++)
  64.        if (prime[p])
  65.           cout << p << " ";*/
  66. }
  67.  
  68.  
  69.  
  70.  
  71. int main(){
  72.     //freopen("i.txt","r",stdin);
  73.     //freopen("o.txt","w",stdout);
  74.     cout<<"Enter categories and their output: \nEnter 0 to exit\n";
  75.     vector<pair<big,big>> arr;
  76.     while(1){
  77.         big cat,out;
  78.         cin>>cat>>out;
  79.         if(cat==0){
  80.             break;             
  81.         }
  82.         else{
  83.             arr.push_back(make_pair(cat,out));
  84.         }
  85.     }
  86.     map<big,pair<big,big>> mp;
  87.     for(auto x:arr){
  88.         if(x.second==0){
  89.             mp[x.first].second++;
  90.         }
  91.         else{
  92.             mp[x.first].first++;
  93.         }
  94.     }
  95.     cout<<"map= \n";
  96.     for(auto x:mp){
  97.         cout<<"category = "<<x.first<<"\n";
  98.         cout<<"Yes = "<<x.second.first<<"\tNo = "<<x.second.second<<"\n";
  99.     }
  100.     big sum=0;
  101.     for(auto x:mp){
  102.         big total=x.second.first+x.second.second;
  103.         sum+=total;
  104.     }
  105.     fig ce=0;  
  106.     for(auto x:mp){
  107.         big total=x.second.first+x.second.second;
  108.         if(x.second.first==0 || x.second.second==0){
  109.             ;
  110.         }
  111.         else{
  112.             fig weight=(fig)((fig)(total)/(fig)(sum));
  113.             fig first_weight=(fig)((fig)(x.second.first)/(fig)(total));
  114.             fig first = 1-first_weight;
  115.             fig second_weight=(fig)((fig)(x.second.second)/(fig)(total));
  116.             fig second = 1-second_weight;
  117.             ce+=weight*(1)*(first_weight*first+second_weight*second);
  118.         }
  119.        
  120.     }
  121.     cout<<"Gini Index= "<<ce<<endl;
  122.    
  123.    
  124.     return 0;
  125. }
Add Comment
Please, Sign In to add comment