Helvetic Coding Contest 2018 D1(分数类的使用 && hash)

problem

The Rebel fleet is on the run. It consists of m ships currently gathered around a single planet. Just a few seconds ago, the vastly more powerful Empire fleet has appeared in the same solar system, and the Rebels will need to escape into hyperspace. In order to spread the fleet, the captain of each ship has independently come up with the coordinate to which that ship will jump. In the obsolete navigation system used by the Rebels, this coordinate is given as the value of an arithmetic expression of the form .

To plan the future of the resistance movement, Princess Heidi needs to know, for each ship, how many ships are going to end up at the same coordinate after the jump. You are her only hope!

Input

The first line of the input contains a single integer m (1 ≤ m ≤ 200 000) – the number of ships. The next m lines describe one jump coordinate each, given as an arithmetic expression. An expression has the form (a+b)/c. Namely, it consists of: an opening parenthesis (, a positive integer a of up to two decimal digits, a plus sign +, a positive integer b of up to two decimal digits, a closing parenthesis ), a slash /, and a positive integer c of up to two decimal digits.

Output

Print a single line consisting of m space-separated integers. The i-th integer should be equal to the number of ships whose coordinate is equal to that of the i-th ship (including the i-th ship itself).

Example

input

4
(99+98)/97
(26+4)/10
(12+33)/15
(5+1)/7

output

1 2 2 1

Note

In the sample testcase, the second and the third ship will both end up at the coordinate 3.

Note that this problem has only two versions – easy and hard.

solution

使用分数类+字符串hash判重

code example

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int debug_num=0;
#define debug cout<<"debug "<<++debug_num<<" :"

unordered_map<string,int> mp;

const int maxn=2e5+10;

ll gcd(ll a,ll b){
    if(b==0) return a;
    return gcd(b,a%b);
}

struct Fraction{
    ll num;
    ll den;
    Fraction (ll num=0,ll den=1){
        if(den<0){
            num=-num;
            den=-den;
        }
        assert(den != 0);
        ll g=gcd(abs(num),den);
        this->num=num/g;
        this->den=den/g;
    }
    Fraction operator + (const Fraction &o) const{
        return Fraction(num*o.den+den*o.num,den*o.den);
    }
    Fraction operator - (const Fraction &o) const{
        return Fraction(num*o.den-den*o.num,den*o.den);
    }
    Fraction operator * (const Fraction &o) const{
        return Fraction(num*o.num,den*o.den);
    }
    Fraction operator / (const Fraction &o) const{
        return Fraction(num*o.den,den*o.num);
    }
    bool operator < (const Fraction &o) const{
        return num*o.den<den*o.num;
    }
    bool operator == (const Fraction &o) const{
        return num*o.den==den*o.num;
    }
}no[maxn];



int main()
{
    //freopen("in.txt","r",stdin);
    //ios::sync_with_stdio(false);
    int m;
    cin>>m;
    //scanf("%d",&m);
    for(int i=0;i<m;++i){
        ll a,b,c;
        //scanf("(%d+%d)/%d",&a,&b,&c);
        //cout<<a<<" "<<b<<" "<<c<<endl;
        string s;
        cin>>s;
        ll sum=0;
        int j;
        for(j=1;j<s.size();++j){
            if(s[j]=='+') break;
            sum=sum*10;
            sum+=s[j]-'0';
        }
        a=sum;
        sum=0;
        j++;
        for(;j<s.size();++j){
            if(s[j]==')') break;
            sum=sum*10;
            sum+=s[j]-'0';
        }
        b=sum;
        sum=0;
        j=j+2;
        for(;j<s.size();++j){
            sum=sum*10;
            sum+=s[j]-'0';
        }
        c=sum;
        no[i]=Fraction(a+b,c);
        s="";
        int tp=no[i].num;
        while(tp){
            s+=tp%10+'0';
            tp/=10;
        }
        s+='_';
        tp=no[i].den;
        while(tp){//逆序就逆序吧...
            s+=tp%10+'0';
            tp/=10;
        }
        mp[s]++;
        //cout<<s<<endl;
    }
    for(int i=0;i<m;++i){
        string s="";
        int tp=no[i].num;
        while(tp){
            s+=tp%10+'0';
            tp/=10;
        }
        s+='_';
        tp=no[i].den;
        while(tp){//逆序就逆序吧...
            s+=tp%10+'0';
            tp/=10;
        }
        cout<<mp[s]<<" ";
    }
    return 0;
}
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值