Codeforces 732E Sockets【贪心】

E. Sockets
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

The ICM ACPC World Finals is coming! Unfortunately, the organizers of the competition were so busy preparing tasks that totally missed an important technical point — the organization of electricity supplement for all the participants workstations.

There are n computers for participants, the i-th of which has power equal to positive integer pi. At the same time there are m sockets available, the j-th of which has power euqal to positive integer sj. It is possible to connect the i-th computer to the j-th socket if and only if their powers are the same: pi = sj. It is allowed to connect no more than one computer to one socket. Thus, if the powers of all computers and sockets are distinct, then no computer can be connected to any of the sockets.

In order to fix the situation professor Puch Williams urgently ordered a wagon of adapters — power splitters. Each adapter has one plug and one socket with a voltage divider between them. After plugging an adapter to a socket with power x, the power on the adapter's socket becomes equal to , it means that it is equal to the socket's power divided by two with rounding up, for example and .

Each adapter can be used only once. It is possible to connect several adapters in a chain plugging the first to a socket. For example, if two adapters are plugged one after enother to a socket with power 10, it becomes possible to connect one computer with power 3 to this socket.

The organizers should install adapters so that it will be possible to supply with electricity the maximum number of computers c at the same time. If there are several possible connection configurations, they want to find the one that uses the minimum number of adapters u to connect c computers.

Help organizers calculate the maximum number of connected computers c and the minimum number of adapters u needed for this.

The wagon of adapters contains enough of them to do the task. It is guaranteed that it's possible to connect at least one computer.

Input

The first line contains two integers n and m (1 ≤ n, m ≤ 200 000) — the number of computers and the number of sockets.

The second line contains n integers p1, p2, ..., pn (1 ≤ pi ≤ 109) — the powers of the computers.

The third line contains m integers s1, s2, ..., sm (1 ≤ si ≤ 109) — the power of the sockets.

Output

In the first line print two numbers c and u — the maximum number of computers which can at the same time be connected to electricity and the minimum number of adapters needed to connect c computers.

In the second line print m integers a1, a2, ..., am (0 ≤ ai ≤ 109), where ai equals the number of adapters orginizers need to plug into the i-th socket. The sum of all ai should be equal to u.

In third line print n integers b1, b2, ..., bn (0 ≤ bi ≤ m), where the bj-th equals the number of the socket which the j-th computer should be connected to. bj = 0 means that the j-th computer should not be connected to any socket. All bj that are different from 0 should be distinct. The power of the j-th computer should be equal to the power of the socket bj after plugging in abj adapters. The number of non-zero bj should be equal to c.

If there are multiple answers, print any of them.

Examples
Input
2 2
1 1
2 2
Output
2 2
1 1
1 2
Input
2 1
2 100
99
Output
1 6
6
1 0

题目大意:

一共有N个插座,有M台电脑,一台电脑和插座可以相连的要求是:pi==si.现在希望尽可能多的电脑能够连在插排上。

现在有一种分流插座,可以使得一台电脑的si从si变成【si/2】【15/2】=8.

对于一台电脑,我们也可以使用多个分流插座。

问最多能够有几台电脑连在插排上,同时问需要多少个分流插座。

第二行输出每个电脑用的分流插座的数量。

第三行输出每个插排连接的电脑编号。


思路:


首先我们将插排和电脑按照值从小到大排序。

然后我们暴力处理,将每个电脑不断的/2.如果遇到了一个插排可以与之匹配,那么直接相连接即可。

常数大的话是会TLE的,做好底层优化,剩下的vector和map乱搞乱存就行。


Ac代码:

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<vector>
#include<map>
using namespace std;
#define ll __int64
struct node
{
    ll pos,val;
}b[200050],a[200050];
vector<ll >mp[200050];
ll from[200050];
ll use[200050];
ll ned[200050];
ll ans[200050];
ll cmp(node a,node b)
{
    return a.val<b.val;
}
int main()
{
    ll n,m;
    while(~scanf("%I64d%I64d",&n,&m))
    {
        map<ll ,ll >s;
        memset(from,0,sizeof(from));
        memset(use,0,sizeof(use));
        memset(ans,-1,sizeof(ans));
        for(int i=0;i<=n+m;i++)mp[i].clear();
        for(ll i=0;i<n;i++)scanf("%I64d",&a[i].val),a[i].pos=i;
        for(ll i=0;i<m;i++)scanf("%I64d",&b[i].val),b[i].pos=i;
        sort(a,a+n,cmp);
        sort(b,b+m,cmp);
        int cnt=1;
        for(int i=0;i<n;i++)
        {
            if(s[a[i].val]==0)
            {
                s[a[i].val]=cnt;
                cnt++;
            }
            mp[s[a[i].val]].push_back(a[i].pos);
        }
        int num=0,tot=0;
        for(int i=0;i<m;i++)
        {
            int contz=0;
            while(b[i].val)
            {
                int ok=0;
                if(s[b[i].val]!=0)
                {
                    for(int j=from[s[b[i].val]];j<mp[s[b[i].val]].size();j++)
                    {
                        int v=mp[s[b[i].val]][j];
                        if(ans[v]==-1)
                        {
                            from[s[b[i].val]]=j+1;
                            ok=1;
                            num++;
                            tot+=contz;
                            ned[b[i].pos]=contz;
                            ans[v]=b[i].pos;
                            break;
                        }
                    }
                }
                if(ok==1)break;
                if(b[i].val==1)break;
                if(b[i].val%2==1)b[i].val++;
                b[i].val/=2;
                contz++;
            }
        }
        printf("%d %d\n",num,tot);
        for(int i=0;i<m;i++)
        {
            printf("%d ",ned[i]);
        }
        printf("\n");
        for(int i=0;i<n;i++)
        {
            printf("%d ",ans[i]+1);
        }
        printf("\n");
    }
}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值