Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #define dbg(i,j) cout<<"I am "<<i<<" = "<<endl<<j<<endl;
- #define dbr(name,a) cout<<name<<endl;for(auto x:a)cout<<x<<" ";cout<<endl;
- #define DBR(name,a) cout<<name<<endl;for(auto x:a){ for(auto y:x)cout<<y<<" ";cout<<endl;}
- #define dbmp(name,a) cout<<name<<endl;for(auto x:a){ cout<<x.first<<"\t"<<x.second<<endl;}
- #define dbp(name,a) cout<<name<<endl;cout<<a.first<<"\t"<<a.second<<endl;
- #define boost ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
- using namespace std;
- typedef long long int big;
- typedef long double fig;
- big power(big x, big y, big p)
- {
- big res = 1; // Initialize result
- x = x % p; // Update x if it is more than or
- // equal to p
- while (y > 0)
- {
- // If y is odd, multiply x with result
- if (y & 1)
- res = (res*x) % p;
- // y must be even now
- y = y>>1; // y = y/2
- x = (x*x) % p;
- }
- return res;
- }
- bool prime[100005];
- void SieveOfEratosthenes(big n)
- {
- // Create a boolean array "prime[0..n]" and initialize
- // all entries it as true. A value in prime[i] will
- // finally be false if i is Not a prime, else true.
- //bool prime[n+1];
- memset(prime, true, sizeof(prime));
- for (int p=2; p*p<=n; p++)
- {
- // If prime[p] is not changed, then it is a prime
- if (prime[p] == true)
- {
- // Update all multiples of p greater than or
- // equal to the square of it
- // numbers which are multiple of p and are
- // less than p^2 are already been marked.
- for (int i=p*p; i<=n; i += p)
- prime[i] = false;
- }
- }
- // Print all prime numbers
- /*for (int p=2; p<=n; p++)
- if (prime[p])
- cout << p << " ";*/
- }
- int main(){
- //freopen("i.txt","r",stdin);
- //freopen("o.txt","w",stdout);
- cout<<"Enter categories and their output: \nEnter 0 to exit\n";
- vector<pair<big,big>> arr;
- while(1){
- big cat,out;
- cin>>cat>>out;
- if(cat==0){
- break;
- }
- else{
- arr.push_back(make_pair(cat,out));
- }
- }
- map<big,pair<big,big>> mp;
- for(auto x:arr){
- if(x.second==0){
- mp[x.first].second++;
- }
- else{
- mp[x.first].first++;
- }
- }
- cout<<"map= \n";
- for(auto x:mp){
- cout<<"category = "<<x.first<<"\n";
- cout<<"Yes = "<<x.second.first<<"\tNo = "<<x.second.second<<"\n";
- }
- big sum=0;
- for(auto x:mp){
- big total=x.second.first+x.second.second;
- sum+=total;
- }
- fig ce=0;
- for(auto x:mp){
- big total=x.second.first+x.second.second;
- if(x.second.first==0 || x.second.second==0){
- ;
- }
- else{
- fig weight=(fig)((fig)(total)/(fig)(sum));
- fig first_weight=(fig)((fig)(x.second.first)/(fig)(total));
- fig first = 1-first_weight;
- fig second_weight=(fig)((fig)(x.second.second)/(fig)(total));
- fig second = 1-second_weight;
- ce+=weight*(1)*(first_weight*first+second_weight*second);
- }
- }
- cout<<"Gini Index= "<<ce<<endl;
- return 0;
- }
Add Comment
Please, Sign In to add comment