Tailor Shop--水题

Your submission will run against only preliminary test cases. Full test cases will run at the end of the day.

Jaime the Tailor's new customer wants to add  distinct clusters of buttons to her dress, where each cluster is filled with a distinct number of buttons of a certain color.

The customer also has specific ideas about how much she wants to pay for each cluster in that for some cluster , she wants to pay at least  dollars. In addition, she wants each cluster to contain a distinct number of buttons. And, finally, she wants to minimize her total cost.

Jaime stocks an infinite number of buttons in an infinite number of colors at his shop, and each button costs  dollars. Given , and the amount of money that the customer wants to pay for each respective cluster, help Jaime by finding and printing a long integer denoting the minimum number of buttons he can use to satisfy her request.

Input Format

The first line contains two space-separated integers denoting the respective values of  and 
The second line contains  space-separated integers where each integer  denotes the value of  (i.e., the minimum amount of money she wants to spend on cluster ).

Constraints

Output Format

Print a single long integer denoting the minimum number of buttons required for Jaime to satisfy his customer's request.

Sample Input 0

3 2
4 6 6

Sample Output 0

9

Explanation 0 
Let's say we use red buttons for the first cluster, green buttons for the second cluster, and yellow buttons for the third cluster:

  1. The customer's requested minimum cost for the first cluster is  dollars, and Jaime needs at least two buttons priced at  dollars apiece to satisfy this cost. Jaime chooses two red buttons such that the cost of the first cluster is  dollars.
  2. The customer's requested minimum cost for the second cluster is  dollars, and Jaime needs at least three buttons priced at  dollars apiece to satisfy this minimum cost. Jaime chooses three green buttons such that the cost of the second cluster is  dollars.
  3. The customer's requested minimum cost for the third cluster is  dollars, and Jaime needs at least three buttons priced at  dollars apiece to satisfy this minimum cost. Jaime's second cluster already contains three buttons, so he chooses four yellow buttons such that the cost of the third cluster is  dollars.

Each cluster has a distinct number of buttons, so we print a long integer denoting the total number of buttons required, which is .

Girl demand.png

Sample Input 1

2 3
4 5

Sample Output 1

5

Explanation 1 
Let's say we use red buttons for the first cluster and green buttons for the second cluster.

  1. The customer's requested minimum cost for the first cluster is  dollars, and Jaime needs at least two buttons priced at  dollars apiece to satisfy this cost. Jaime chooses two red buttons such that the cost of the first cluster is  dollars.
  2. The customer's requested minimum cost for the second is  dollars, and Jaime needs at least two buttons priced at  dollars apiece to satisfy this minimum cost. Jaime chooses three green buttons such that the cost of the second cluster is  dollars.

Each cluster has a distinct number of buttons, so we print a long integer denoting the total number of buttons required, which is .

Girl demand(2).png


题目链接:https://www.hackerrank.com/contests/w27/challenges/tailor-shop

题目大意:输入一个n,代表有n位客人,输入一个p,代表每个装饰品的钱数,然后每位客人要求要有不同数量的装饰品。


思路:很简单的一个水题,第一反应是哈希思想水一下,本以为会超时,但是竟然没有超时,还有好多思路,比如先把每位客人的要求从小到大排序,先算出最小的需求,在用二分查看有没有这个需求,应该比哈希思想水一下要快,当然,这个比赛持续7天,现在还没有结束,终判不知道能不能过。。。


代码:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
int a[233333];
bool b[233333];
int main(){
    int n,p;
    while(~scanf("%d%d",&n,&p)){
        memset(b,false,sizeof(b));
        for(int i=0;i<n;i++){
            scanf("%d",&a[i]);
        }
        sort(a,a+n);
        int h;
        long long sum=0;
        for(int i=0;i<n;i++){
            if(a[i]%p==0){
                h=a[i]/p;
            }
            else{
                h=a[i]/p+1;
            }
            if(b[h]==false){
                b[h]=true;
                sum+=h;
            }
            else{
                for(int j=h;;j++){
                    if(b[j]==false){
                        b[j]=true;
                        sum+=j;
                        break;
                    }
                }
            }
        }
        cout<<sum<<endl;
    }
    return 0;
}







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值