【CF 732E】Sockets(优先队列+贪心)

来源:互联网 发布:网络直销模式典型公司 编辑:程序博客网 时间:2024/06/05 16:53

【CF 732E】Sockets(优先队列+贪心)

题目大意:
n台电脑,m个供电器。
每台电脑和每个供电器都有电量,当电脑i的电量和供电器j电量相同时,可以连接供电。

现在提供变压器,可以连接到供电器上,每个变压器会将电量变为x2变压器可以累加。提供无限个变压器。

现在问最多能使多少台电脑供上电,输出最多的可供电电脑数,最少使用的变压器数。之后输出每个电源上安装的变压器数量,以及每个电脑连接到的电源编号(0表示不连接电源)

优先队列存结构体,表示电源。
结构体中有电源编号,当前电量以及使用的变压器数量。

电量大的在堆顶,相同的变压器用的少的在堆顶。

电脑按电量从大到小排序,然后遍历一遍电脑,能按电源就安。然后就没了

代码如下:

#include <iostream>#include <cmath>#include <vector>#include <cstdlib>#include <cstdio>#include <cstring>#include <queue>#include <stack>#include <list>#include <algorithm>#include <map>#include <set>#define LL long long#define VI vector<int>#define Pr pair<int,int>#define fread(ch) freopen(ch,"r",stdin)#define fwrite(ch) freopen(ch,"w",stdout)using namespace std;const int INF = 0x3f3f3f3f;const int msz = 10000;const int mod = 1e9+7;const double eps = 1e-8;const int maxn = 212345;struct Stock{    int id,power,cnt;    bool operator >(const struct Stock a)const    {        return power == a.power? cnt > a.cnt: power < a.power;    }} s[maxn];struct Computer{    int id,power;} com[maxn];int ned[maxn];int ans[maxn];priority_queue <Stock,vector<Stock>,greater<Stock> > q;bool cmp(Computer a,Computer b){    return a.power > b.power;}int main(){    //fread("in.in");    //fread("out.out");    int n,m;    scanf("%d%d",&n,&m);    for(int i = 1; i <= n; ++i)     {        scanf("%d",&com[i].power);        com[i].id = i;    }    sort(com+1,com+n+1,cmp);    for(int i = 1; i <= m; ++i)    {        scanf("%d",&s[i].power);        s[i].id = i;        s[i].cnt = 0;        q.push(s[i]);    }    Stock tmp;    int x;    LL c,u;    c = u = 0;    for(int i = 1; i <= n; ++i)    {        while(!q.empty())        {            tmp = q.top();            if(tmp.power <= com[i].power) break;            q.pop();            tmp.cnt++;            x = (int)ceil(tmp.power*1.0/2);            if(x == tmp.power) continue;            tmp.power = x;            q.push(tmp);        }        if(!q.empty())        {            tmp = q.top();            if(tmp.power < com[i].power) continue;            q.pop();            u += tmp.cnt;            c++;            s[tmp.id].cnt = tmp.cnt;            ans[com[i].id] = tmp.id;        }    }    printf("%lld %lld\n",c,u);    for(int i = 1; i <= m; ++i) printf("%d ",s[i].cnt);    puts("");    for(int i = 1; i <= n; ++i) printf("%d ",ans[i]);    puts("");    return 0;}
0 0