牛客网Mask Allocation
题目描述 :
Nowadays, the Kingdom of Dreamgrid is suffering from a national pandemic.
Fortunately,president Baobao is working effectively with the Center for Disease Control (CDC) and they are trying their best to make everything under control.
President Baobao has received n×mn \times mn×m medical masks from his friend Reku, an extremely rich billionaire.
As the chief of the CDC, you are required to allocate these masks properly.
There are 2 kinds of hospitals in the Kingdom of Dreamgrid, n senior hospitals for critically ill patients and m mobile cabin hospitals for patients with mild symptoms.
Before allocating them to hospitals, you have to pack them into boxes.
Please note that boxes must not be opened in order to prevent contamination and you only know these boxes will be allocated to either senior hospitals or mobile cabin hospitals.
That is to say, there should be a way of dividing masks boxes into m groups of n masks, and a way of dividing into n groups of m masks.
You want the number of boxes to be minimal and please also provide a lexicographically greatest sequence of the numbers of masks in boxes. We say a sequence a is lexicographically greater than another b of the same length if there exists an integer i, such that
- aj=bj,for all j < i; and
- ai>bi.
首先判断n是否大于m,若大于,则使得n<m。
由于要求字典序最大,装口罩最多的盒子不超过n。
医院数量为n的时候,需要给每个医院安排m个口罩,至多给每个医院(m/n)个(向下取整(这很重要!)),每个盒子装着n个,总的就是(m/n)×n个盒子,每个医院还要再拿m%n个口罩。
这时候我们考虑医院数量为m的情况,此时需要给每个医院分配n个口罩,那显然已经有(m/n)×n个医院满足条件了,还有m%n个医院没拿到。
这样,我们就把问题转化成了n’= m % n,m’= n的子问题,不断循环即可。
上代码(灵魂!)
#include<bits/stdc++.h> using namespace std; int t,n,m,ans,a[100010]; int main() { scanf("%d",&t); while(t--) { scanf("%d%d",&n,&m); ans=0; while(n>0) { if(n<m) swap(n,m); for(int i=1; i<=m; i++) a[++ans]=m; n-=m; } printf("%d",ans); printf("\n"); for(int i=1; i<=ans; i++) printf("%d ",a[i]); printf("\n"); } }

浙公网安备 33010602011771号