Resource Distribution (思维+贪心好题)

Resource Distribution

One department of some software company has n servers of different specifications. Servers are indexed with consecutive integers from 1 to n. Suppose that the specifications of the j-th server may be expressed with a single integer number cj

of artificial resource units.

In order for production to work, it is needed to deploy two services S1

and S2 to process incoming requests using the servers of the department. Processing of incoming requests of service Si takes xi

resource units.

The described situation happens in an advanced company, that is why each service may be deployed using not only one server, but several servers simultaneously. If service Si

is deployed using ki servers, then the load is divided equally between these servers and each server requires only xi/ki

(that may be a fractional number) resource units.

Each server may be left unused at all, or be used for deploying exactly one of the services (but not for two of them simultaneously). The service should not use more resources than the server provides.

Determine if it is possible to deploy both services using the given servers, and if yes, determine which servers should be used for deploying each of the services.

Input

The first line contains three integers n

, x1, x2 ( 2n300000, 1x1,x2109

) — the number of servers that the department may use, and resource units requirements for each of the services.

The second line contains n

space-separated integers c1,c2,,cn ( 1ci109

) — the number of resource units provided by each of the servers.

Output

If it is impossible to deploy both services using the given servers, print the only word "No" (without the quotes).

Otherwise print the word "Yes" (without the quotes).

In the second line print two integers k1

and k2 ( 1k1,k2n

) — the number of servers used for each of the services.

In the third line print k1

integers, the indices of the servers that will be used for the first service.

In the fourth line print k2

integers, the indices of the servers that will be used for the second service.

No index may appear twice among the indices you print in the last two lines. If there are several possible answers, it is allowed to print any of them.

Examples
Input
6 8 16
3 5 2 9 8 7
Output
Yes
3 2
1 2 6
5 4
Input
4 20 32
21 11 11 12
Output
Yes
1 3
1
2 3 4
Input
4 11 32
5 5 16 16
Output
No
Input
5 12 20
7 8 4 11 9
Output
No
Note

In the first sample test each of the servers 1, 2 and 6 will will provide 8/3=2.(6)

resource units and each of the servers 5, 4 will provide 16/2=8

resource units.

In the second sample test the first server will provide 20

resource units and each of the remaining servers will provide 32/3=10.(6) resource units.

网上一篇博客写的很好,很清晰直接载过来了

思路传送门

两遍贪心

code:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 3e5+100;
struct node{
    int c;
    int id;
    bool operator < (const node &other)const{
        return c > other.c;
    }
}s[maxn];
int main(){
    int n,x1,x2;
    int k1,k2;
    int c[maxn];
    cin >> n >> x1 >> x2;
    for(int i = 1; i <= n; i++){
        cin >> s[i].c;
        s[i].id = i;
    }
    sort(s+1,s+1+n);//从大到小排序
    //尝试先分配X1再分配X2
    for(int i = 1; i <= n; i++){
        //以服务器i为下限组成X1需要的钱K1个
        k1 = x1 / s[i].c;
        if(x1 % s[i].c != 0) k1++;//算出以ci为最小值需要的最少数量
        if(k1 >= i) continue; // 如果往前的总数量不够这个最小数量继续寻找,虽然对于X1来说等于是可以的,但是如果等于就没办法给X2分配了,所以仍然要继续下一次寻找
        //否则给X2分配
        k2 = x2 / s[i-k1].c;
        if(x2 % s[i-k1].c != 0) k2++;
        if(k2 > i - k1) continue; //这次直接大于就行了
        //分配成功
        printf("Yes\n%d %d\n",k1,k2);
        for(int j = i-k1+1; j <= i; j++){
            printf("%d%c",s[j].id,j == i ? '\n' : ' ');
        }
        for(int j = i-k1-k2+1; j <= i-k1; j++){
            printf("%d%c",s[j].id,j == i-k1 ? '\n' : ' ');
        }
        return 0;
    }
    //先分配X2原理同上不在赘述
    for(int i = 1; i <= n; i++){
        k2 = x2 / s[i].c;
        if(x2 % s[i].c != 0) k2++;
        if(k2 >= i) continue;
        k1 = x1 / s[i-k2].c;
        if(x1 % s[i-k2].c != 0) k1++;
        if(k1 > i - k2) continue;
        printf("Yes\n%d %d\n",k1,k2);
        for(int j = i-k2-k1+1; j <= i-k2; j++){
            printf("%d%c",s[j].id,j == i-k2 ? '\n' : ' ');
        }
        for(int j = i-k2+1; j <= i; j++){
            printf("%d%c",s[j].id,j == i ? '\n' : ' ');
        }
        return 0;
    }
    printf("No\n");
    return 0;
}


猜你喜欢

转载自blog.csdn.net/codeswarrior/article/details/80286278
今日推荐