Tailor Shop
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:
- 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.
- 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.
- 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 .
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.
- 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.
- 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 .
题目链接: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;
}