CodeForces - 958D1( STL-map )

题目链接:http://codeforces.com/problemset/problem/958/D1

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.


题目:

    给出m个算式,依次输出每个算法算出的值总共出现的次数。

思路:

    数组的下标一般只能是int,当你需要用double或其他数据类型对应一个值(或其他数据类型)的时候,map就是最好的选择。下面就是map的基操。

AC代码:


Time 779ms
Memory 2032kB
Length 649

#include <cstdio>
#include <cstring>
#include <map>
#include <iostream>
using namespace std;
double x[200008] ;
map<double,int> im;
int main( )
{
     int n;
     scanf("%d",&n);
     im.clear();
     for( int i=0; i<n; i++ )
     {
         double a[3]={0};
         char d;
         getchar();
         scanf("%c%lf%c%lf%c%c%lf",&d,&a[0],&d,&a[1],&d,&d,&a[2]);
         x[ i ] = (a[0] + a[1] )/ a[2] ;
         im[ x[i] ] ++ ;
     }
     for( int i=0; i<n; i++ )
     {
         cout << im.find(x[i])->second << endl;
     }
     return 0;
}

还有一种代码,换了个map的读取形式,速度变慢了好多


Time 1263ms
Memory 2044kB
Length 409

#include <iostream>
#include <map>

#define N 200030
using namespace std;

int n;
map<double,int> m;
double vis[N];
int main(){
    cin>>n;
    for(int i=0;i<n;i++){
        char c1,c2,c3,c4;
        int a,b,c;
        cin>>c1>>a>>c2>>b>>c3>>c4>>c;
        vis[i] = ((double)(a+b))/c*1.0;
        m[vis[i]]++;
    }
    for(int i=0;i<n;i++){
        cout<<m[vis[i]]<<" ";
    }
    cout<<endl;
    return 0;
}



猜你喜欢

转载自blog.csdn.net/qq_40764917/article/details/80966041
今日推荐