【CCPC-2019】【江西省赛】J- Worker

简介: 【CCPC-2019】【江西省赛】J- Worker

题目

Avin meets a rich customer today. He will earn 1 million dollars if he can solve a hard problem. There are n warehouses and m workers. Any worker in the i-th warehouse can handle ai orders per day. The customer wonders whether there exists one worker assignment method satisfying that every warehouse handles the same number of orders every day. Note that each worker should be assigned to exactly one warehouse and no worker is lazy when working.

Input

The first line contains two integers n (1 ≤ n ≤ 1, 000), m (1 ≤ m ≤ 1018). The second line contains n integers. The i-th integer ai (1 ≤ ai ≤ 10) represents one worker in the i-th warehouse can handle ai orders per day.

Output

If there is a feasible assignment method, print “Yes” in the first line. Then, in the second line, print n integers with the i-th integer representing the number of workers assigned to the i-th warehouse.

Otherwise, print “No” in one line. If there are multiple solutions, any solution is accepted.

Sample Input

2 6
1 2
2 5
1 2

Sample Output

Yes
4 2
No

题意:

公司有n个仓库和m个工人。在第i个仓库中,每个工人能完成a_i个订单。每个仓库不限工人数量,但是工人的工作效率会受到仓库的影响。

问:能否合理的分配每个工人,使得每个仓库一天的派单量相同。若能,输出分配方法

输入样例:

第一行给出两个整数n,m。(即告诉你有n个仓库和m个工人)

第二行给出n个整数,表示第i个仓库中每个工人每天能完成的订单数量。(也就是工人的工作效率会受到仓库的影响的数据)

输出样例:

如果能合理分配所有的工人,而且能让所有仓库每天完成的总订单数相同。就在一行中输出Yes,并且在第二行中给出每个仓库应该有多少个工人。

如果不能合理分配所有的工人,就直接在一行中输出No。

思路:

根据仓库对于工人的影响给的比例来与工人人数比较。如果能最后能除尽,这符合,反之为no


代码(有误):

(这个题目样例可以通过,但是最后得到的是Wrong Answer)

#include <cstdio>
#include <iostream>
using namespace std;
int main ()
{
    int m,n;
    while(~scanf("%d%d",&n,&m) && n>=1&&n<=1000&&m>=1&&m<=1018)//cin>>n>>m;
    {
        int a[1005]={0};
        int i,t;
        for(i=1;i<=n;i++)
        { cin>>a[i]; }
        int min=a[1];
        for(i=1;i<=n;i++)
        {
            if(min>a[i]) min = a[i];
        }
        int sum = 0;
        for(i=1;i<=n;i++)
            sum+=a[i];
        if(sum%min==0)
        { sum=sum/min; }
        if(m%sum==0)
        {
            printf("Yes\n");
            int k = m/sum;
            for(i=n;i>=1;i--)
            {
                if(i==1)
                    printf("%d \n",a[1]*k);
                else
                    printf("%d ",a[i]*k);
            }
        }
        else printf("No\n");
     }
    return 0;
}

改进后代码:

题解:

算出每个仓库工人效率共同的最小公倍数lcm(每个仓库共同的最小总单数)

每个仓库工人效率共同的最小公倍数/每个仓库工人的效率)

就是每个仓库所需要的最小人数

代码如下:

#include<iostream>
using namespace std;
long long n, m;
long long p[1005];
long long gcd(long long a, long long b)//最大公约数
{
    return b == 0 ? a : gcd(b, a % b);
}
long long lcm(long long a, long long b)//最小公倍数
{
    return a / gcd(a, b) * b;
}
int main()
{
    while (cin >> n >> m)
    {
        for (long long i = 1; i <= n; i++)
            cin >> p[i];
        long long temp = p[1];
        for (long long i = 2; i <= n; i++)
        {
            temp = lcm(temp, p[i]);
        }
        long long cnt = 0, flag = 0, d;
        for (long long i = 1; i <= n; i++)
            cnt = cnt + temp / p[i];
        if (m%cnt != 0)
            flag = 1;
        else
            d = m / cnt;
        if (flag == 0)
        {
            cout << "Yes" << endl;
            for (long long i = 1; i <= n; i++)
            {
                if (i != n)
                    cout << temp / p[i] * d << ' ';
                else
                    cout << temp / p[i] * d << endl;
            }
        }
        else
            cout << "No" << endl;
    }
    return 0;
}


相关文章
|
22天前
|
人工智能 测试技术
【“红旗杯”第十四届吉林省大学生程序设计竞赛】 之 C - String Game
【“红旗杯”第十四届吉林省大学生程序设计竞赛】 之 C - String Game
|
22天前
|
开发框架 .NET Windows
2019 CCPC网络选拔赛题
2019 CCPC网络选拔赛题
|
22天前
|
C++
【PTA】​L1-079 天梯赛的善良​ (C++)
【PTA】​L1-079 天梯赛的善良​ (C++)
74 0
【PTA】​L1-079 天梯赛的善良​ (C++)
|
22天前
(2021 ICPC)亚洲区域赛(昆明)I.Mr. Main and Windmills
(2021 ICPC)亚洲区域赛(昆明)I.Mr. Main and Windmills
14 0
(2021 ICPC)亚洲区域赛(昆明)I.Mr. Main and Windmills
|
10月前
2022天梯赛三月冲刺——PAT (Advanced Level)1013 Battle Over Cities (并查集找连通块)
2022天梯赛三月冲刺——PAT (Advanced Level)1013 Battle Over Cities (并查集找连通块)
59 0
|
11月前
|
人工智能 机器人
第10届山东省赛Wandering Robot(详细思路)
第10届山东省赛Wandering Robot(详细思路)
60 0
|
11月前
|
机器学习/深度学习
山东第一届省赛 Balloons (dfs)
山东第一届省赛 Balloons (dfs)
37 0
|
11月前
|
测试技术 C++
【PTA天梯赛】L1-001 L1-002 L1-003 L-004 L-005 L-006 L-007 L-008 L-009 L1-010 c++
【PTA天梯赛】L1-001 L1-002 L1-003 L-004 L-005 L-006 L-007 L-008 L-009 L1-010 c++
181 1
第一届ACC(AcWing Cup)全国高校联赛初赛:个人版题解
第一届ACC(AcWing Cup)全国高校联赛初赛:个人版题解
136 0
第一届ACC(AcWing Cup)全国高校联赛初赛:个人版题解

热门文章

最新文章