3104 (greed): Happy algorithms
- Meaning:
There are n freshly washed clothes, each given a moisture content. There is a dryer. You can choose a piece of clothes to dry every minute, and you can dry k the water every minute. Each garment can automatically evaporate 1% of the water per minute, and it will not evaporate when using the dryer. Ask how much time it takes to dry all the clothes at least.
Input:
The first line: n indicates n clothes;
The second line: n numbers, indicating the water content of N clothes;
The third line: K, indicating the amount of water removed by the dryer per minute.
Output:
How many minutes to dry n clothes. - Idea:
(1) Greedy algorithm; Every minute, there must be one piece of clothes drying and other clothes drying. Finally, the total water content of all clothes becomes 0.
(2) It should be noted that the description and examples may not be consistent, and the description shall prevail. - Greedy steps:
Considering greed, bake the one with the largest amount of water and air the rest every minute.
(1) Sort so that the clothes with the most water are always the last (n-1);
(2) Subtract K from the water volume n-1, and all the others are reduced by 1;
(3) Then insert the Nth-1 into the position where it should be in the front. Those with more water than it will be moved back one bit. In this way, the order will be maintained. The Nth-1 has the most water.
(4) Until the N-1 water volume is 0, exit.
In the middle, only the treated water volume is not negative (0). - Light rain Code:
#include
#include
#include
using namespace std;
const int N = 100010;
long long num[N];
long long k;
int main()
{
int i,j,n,tmp;
long long ans;
while(cin>>n)
{
long long max_value = 0;
memset(num,0,sizeof(num));
for(i = 0; i < n; i++)
{
cin>>num[i];
if(num[i] > max_value)
max_value = num[i];
}
cin>>k;
if(k == 1)
{
cout<<max_value<<endl;
continue;
}
sort(num,num+n);
ans = 0;
while(1)
{
if(num[n-1]==0)
break;
num[n-1] = num[n-1] - k;
if(num[n-1]<0)
num[n-1] = 0;
for(i=0;i<n-1;i++)
if(num[i]>0)
num[i]–;
ans++;
tmp = num[n-1];
for(i=n-1;i>=1;i–)
{
if(tmp>=num[i-1])
break;
else
num[i]=num[i-1];
}
num[i] = tmp;
/*
for(i=0;i<n;i++)
cout<<num[i]<<" ";
cout<<endl;
system(“pause”);
*/
}
cout<<ans<<endl;
}
system(“pause”);
return 0;
}
4、3104 Drying
Description
It is very hard to wash and especially to dry clothes in winter. But Jane is a very smart girl. She is not afraid of this boring process. Jane has decided to use a radiator to make drying faster. But the radiator is small, so it can hold only one thing at a time.
Jane wants to perform drying in the minimal possible time. She asked you to write a program that will calculate the minimal time for a given set of clothes.
There are n clothes Jane has just washed. Each of them took ai water during washing. Every minute the amount of water contained in each thing decreases by one (of course, only if the thing is not completely dry yet). When amount of water contained becomes zero the cloth becomes dry and is ready to be packed.
Every minute Jane can select one thing to dry on the radiator. The radiator is very hot, so the amount of water in this thing decreases by k this minute (but not less than zero — if the thing contains less than k water, the resulting amount of water will be zero).
The task is to minimize the total time of drying by means of using the radiator effectively. The drying process ends when all the clothes are dry.
Input
The first line contains a single integer n (1 ≤ n ≤ 100 000). The second line contains ai separated by spaces (1 ≤ ai ≤ 109). The third line contains k (1 ≤ k ≤ 109).
Output
Output a single integer — the minimal possible number of minutes required to dry all clothes.
Sample Input
sample input #1
three
2 3 9
five
sample input #2
three
2 3 6
five
Sample Output
sample output #1
three
sample output #2
two