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, thei-th of which has power equal to positive integerpi. At the same time there arem sockets available, the j-th of which has power euqal to positive integer sj. It is possible to connect thei-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 powerx, 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 power10, it becomes possible to connect one computer with power3 to this socket.
The organizers should install adapters so that it will be possible to supply with electricity the maximum number of computersc at the same time. If there are several possible connection configurations, they want to find the one that uses the minimum number of adaptersu 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.
InputThe first line contains two integers n andm (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.
OutputIn the first line print two numbers c andu — the maximum number of computers which can at the same time be connected to electricity and the minimum number of adapters needed to connectc 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 thei-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 thej-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 from0 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-zerobj should be equal toc.
If there are multiple answers, print any of them.
ExamplesInput2 21 12 2Output2 21 11 2Input2 12 10099Output1 661 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 __int64struct 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"); }}
新聞熱點
疑難解答