Sicily 1063. Who's the Boss

       讲的是一个薪水和身高的问题。如果A的薪水和身高都比B高,那么A就是B的上司;进一步地,如果A是比B的薪水高的人中薪水最少的,并且A的身高至少和B一样高,那么A就是B的直接上司。 
       我的策略是,首先将所有人按薪水从少到多排好序,然后依次往上检索,找到一个身高大于等于当前身高的人,那么这个人就是当前人的直接上司,下属人数是原有下属人数加上当前人的下属人数之和再加1(即当前人)。每个人都进行了以上操作后,再按ID从小到大排一次序,对当前询问的ID进行二分搜索,输出搜索到的人的直接上司的ID和下属人数。 
        这个方法时间消耗比较大,但没想到其他技巧可以减少时间消耗,就只能将就了。

Run Time: 0.45sec
Run Memory: 764 KB
Code length: 1647 Bytes
SubmitTime: 2011-12-06 23:16:28

// Problem#: 1063
// Submission#: 1042450
// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/
// All Copyright reserved by Informatic Lab of Sun Yat-sen University
#include 
#include 
using namespace std;

struct Employee {
    int ID, salary, height;
    int boss;
    int count;
};
bool cmp1( const Employee& e1, const Employee& e2 ) { return e1.salary < e2.salary; }
bool cmp2( const Employee& e1, const Employee& e2 ) { return e1.ID < e2.ID; }

int main()
{
    int n, m, q;
    int i, j;
    int bot, top;
    int id;
    Employee data[ 30000 ];

    scanf( "%d", &n );
    while ( n-- ) {
        scanf( "%d%d", &m, &q );

        for ( i = 0; i < m; i++ ) {
            scanf( "%d%d%d", &data[ i ].ID, &data[ i ].salary, &data[ i ].height );
            data[ i ].boss = 0;
            data[ i ].count = 0;
        }
        sort( data, data + m, cmp1 );

        for ( i = 0; i < m; i++ ) {
            for ( j = i + 1; j < m; j++ ) {
                if ( data[ j ].height >= data[ i ].height ) {
                    data[ i ].boss = data[ j ].ID;
                    data[ j ].count += data[ i ].count + 1;
                    break;
                }
            }
        }
        sort( data, data + m, cmp2 );

        for ( i = 0; i < q; i++ ) {
            scanf( "%d", &id );
            bot = 0;
            top = m - 1;
            while ( bot <= top ) {
                j = ( bot + top ) / 2;
                if ( data[ j ].ID == id )
                    break;
                else if ( data[ j ].ID < id )
                    bot = j + 1;
                else
                    top = j - 1;
            }
            printf( "%d %d\n", data[ j ].boss, data[ j ].count );
        }
    }
    
    return 0;

}                                 


 

你可能感兴趣的:(Sicily,sun)