【BZOJ1071】[SCOI2007]組隊

【BZOJ1071】[SCOI2007]組隊

Time Limit: 3 Sec Memory Limit: 128 MB
Submit: 2216 Solved: 692

Description

  NBA每年都有球員選秀環節。通常用速度和身高兩項數據來衡量一個籃球運動員的基本素質。假如一支球隊裏
速度最慢的球員速度爲minV,身高最矮的球員高度爲minH,那麼這支球隊的所有隊員都應該滿足: A * ( height
– minH ) + B * ( speed – minV ) <= C 其中A和B,C爲給定的經驗值。這個式子很容易理解,如果一個球隊的
球員速度和身高差距太大,會造成配合的不協調。 請問作爲球隊管理層的你,在N名選秀球員中,最多能有多少名
符合條件的候選球員。

Input

  第一行四個數N、A、B、C 下接N行每行兩個數描述一個球員的height和speed

Output

  最多候選球員數目。

Sample Input

4 1 2 10
5 1
3 2
2 3
2 1

Sample Output

4

HINT

  數據範圍: N <= 5000 ,height和speed不大於10000。A、B、C在長整型以內。
2016.3.26 數據加強 Nano_ape 程序未重測

Source

[Submit]

思路

這道題可以O(nlog2n) 複雜度處理。

題目的意思是求滿足A(ai.hhmin)+B(ai.ssmin)C 的最大個數。既然要求這個的話那可以在讀入每個ai.h 的時候乘以一個A,讀入每個ai.s 的時候乘以一個B,然後既然要按照上文不等式中左邊兩項的和比較,不妨設一個ai.v 表示(ai.s+ai.h)

循環i ,按照ai.s 從小到大排序,用ai.ssmin ,同時更新hmin ,對於堆中的某個x ,如果x.v>C+hmin+smin 那麼把x 移除堆。答案就是最後堆中元素個數的最小值。

這樣,按照上文的循環邏輯,最壞複雜度O(nlog2n)

代碼

#include <algorithm>
#include <cctype>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>

int nextInt()
{
    int num = 0;
    char c;
    bool flag = false;
    while ((c = std::getchar()) == ' ' || c == '\r' || c == '\t' || c == '\n');
    if (c == '-')
        flag = true;
    else
        num = c - 48;
    while (std::isdigit(c = std::getchar()))
        num = num * 10 + c - 48;
    return (flag ? -1 : 1) * num;
}

struct stru
{
    long long w, h, v;
    bool operator<(const stru &t)
    {
        return v < t.v;
    }
} a[100001];

std::priority_queue<long long> q;
long long n, A, B, C;

bool cmp(const stru x, const stru y)
{
    return x.w > y.w;
}

int main(int argc, char ** argv)
{
    n = nextInt();
    A = nextInt();
    B = nextInt();
    C = nextInt();
    for (int i = 1; i <= n; i++)
    {
        a[i].h = nextInt();
        a[i].w = nextInt();
        a[i].h *= A;
        a[i].w *= B;
        a[i].v = a[i].h + a[i].w;
    }
    long long ans = 1;
    std::sort(a + 1, a + n + 1, cmp);
    for (int i = 1; i <= n; i++)
    {
        long long minh = a[i].h,
            minw = a[i].w;
        while (!q.empty())
            q.pop();
        q.push(a[i].v);
        for (int j = 1; j <= n; j++)
            if (i != j && a[j].h >= minh)
            {
                minw = std::min(minw, a[j].w);
                if (a[i].v > C + minh + minw)
                    break;
                while (!q.empty() && q.top() > C + minh + minw)
                    q.pop();
                if (a[j].v <= C + minh + minw)
                {
                    q.push(a[j].v);
                    ans = std::max(ans, (long long)q.size());
                }
            }
    }
    std::cout << ans << std::endl;
#ifdef __EDWARD_EDIT
    std::cin.get();
    std::cin.get();
#endif
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章