POJ 3104: Drying

POJ 3104

Problem Statement

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 3 2 3 9 5

sample input #2 3 2 3 6 5 Sample Output

sample output #1 3

sample output #2 2

The Algorithm

This is a classic binary search problem. Given the total wetness of the objects, we use the object with maximal wetness as the upper bound of binary search and 0 as the lower bound of binary search. We check the times for the average of the upper bound and the lower bound by simulating how drying would work with a certain time limit.
The checking algorithm considers everything from the radiator's point of view, not Jane's point of view, to optimize the process. The radiator can only be on or off at a time, so we track how many periods of time the radiator has to be on so that the time is below the limit. If the resulting time is STILL above the limit, then the limit clearly is impossible, so the upper bound becomes the value of the limit minus 1, if the resulting time is below, however, the lower bound becomes the value of the limit.

The Code

#include <cstdio>
#include <algorithm>
using namespace std;
int wetness[100001], n, k;

bool chck(int m)
{
    long long cnt = 0;
    for(int i=0;i<n;i++)
    {
        if(wetness[i]>m)
        {
            cnt += (wetness[i]-m)/(k-1);
            if ((wetness[i] - m) % (k-1) != 0)
                cnt++;
        }
    }
    return cnt <= m;
}

int main()
{
    int sum=0;
    scanf("%d",&n);
    for(int i=0;i<n;i++)
    {
        scanf("%d",&wetness[i]);
        sum=max(sum,wetness[i]);
    }
    scanf("%d",&k);
    if(k==1)
    {
        printf("%d\n",sum);
        return 0;
    }
    int up = sum;
    int lo = 0;
    while(up - lo > 1)
    {
        int mid = (up+lo)/2;
        if(chck(mid)) up = mid;
        else lo = mid;
    }
    printf("%d\n",up);
}

Choppy Prefix Sums Continued

CPS image.

Continued from last post post on BITS. The choppy array, which only stores benchmarks.Only storing benchmarks allow a roughly O(n/x+x) speedup to commands. Adding a element. Mark each index in the details up to the next benchmark. Only some things are stored!
Example
First, let's increment index 2. Here, 2,3,4,5,6,7,8,9 are all incremented. Then, only the benchmarks are incremented (10,20,30,40). We can think of it like this: The 2,3,4, and 5 represent a small stream, where at 9 the stream merges into a river only containing benchmarks. So we can imagine a choppy tree/array like so:

0~~~~~~~~~~~~~~10~~~~~~~~~~~~~~~~~~~20~~~~~~~~~~~~~~~~~~~30
               /                    /                    /
 1~2~3~~~~~8~9~  11~12~13~~~~~18~19~  21~22~23~~~~~28~29~
We increment 2 to 9, then 10, 20 and 30:
0~~~~~~~~~~~~~~~1~~~~~~~~~~~~~~~1~~~~~~~~~~~~~~~1
               /               /               /
 0~1~1~~~~~1~1~  0~0~0~~~~~0~0~  0~0~0~~~~~0~0~

Notice how 11 is not changed. A boat can only row downstream, not upstream. Let's do the same for 13.

0~~~~~~~~~~~~~~~1~~~~~~~~~~~~~~~2~~~~~~~~~~~~~~~2
               /               /               /
 0~1~1~~~~~1~1~  0~0~1~~~~~1~1~  0~0~0~~~~~0~0~
As before, notice how the third stream is not changed! To query, we take the corresponding index in the current STREAM and sum it with the last benchmark in the RIVER. For example, querying 11: The value in the stream is 0, and the value of 10 in the river is 1. So, the sum is 1. Let's try 15. The value in the stream is 1. and the value of 10 in the river is 1. So, the sum is 2. Last but not least, let's try 22. The value in the stream is 0. and the value of 20 in the river is 2. So, the sum is 2.

TIMUS 1028: Stars

TIMUS 1028 / POJ 2352: Stars

Problem Statement

  1. Stars Time limit: 0.25 second Memory limit: 64 MB Astronomers often examine star maps where stars are represented by points on a plane and each star has Cartesian coordinates. Let the level of a star be an amount of the stars that are not higher and not to the right of the given star. Astronomers want to know the distribution of the levels of the stars. Problem illustration For example, look at the map shown on the figure above. Level of the star number 5 is equal to 3 (it's formed by three stars with a numbers 1, 2 and 4). And the levels of the stars numbered by 2 and 4 are 1. At this map there are only one star of the level 0, two stars of the level 1, one star of the level 2, and one star of the level 3. You are to write a program that will count the amounts of the stars of each level on a given map. Input The first line of the input contains a number of stars N (1 ≤ N ≤ 15000). The following N lines describe coordinates of stars (two integers X and Y per line separated by a space, 0 ≤ X,Y ≤ 32000). There can be only one star at one point of the plane. Stars are listed in ascending order of Y coordinate. Stars with equal Y coordinates are listed in ascending order of X coordinate. Output The output should contain N lines, one number per line. The first line contains amount of stars of the level 0, the second does amount of stars of the level 1 and so on, the last line contains amount of stars of the level N−1.

The Algorithm

Since values are given in ascending Y order, if we consider the stars on demand (i.e. in parallel with the input), we don't need to consider the Y axis. We only need to consider how many stars are before the current star in respect of the X axis because it is impossible for any stars to be above it, for those had not be read yet. We can use a Fenwick tree to store all of the stars so we obtain an log(n) comptational efficiency.
The Fenwick tree is fairly traditional to implement. For every object, we increment 1 to it and its parents.

The Code

#include <cstdio>
#define Mx 32001
#define Mn 15001
using namespace std;
int c[Mx+1], a[Mn+1];
inline int lb(int x){ return x & (-x); }
void update(int x)
{
    while (x<=Mx)
    {
        c[x]++;
        x+=lb(x);
    }
}
int sum(int x)
{
    int res=0;
    while (x>=1)
    {
        res+=c[x];
        x-=lb(x);
    }
    return res;
}
int main()
{
    int n,x,y; scanf("%d", &n);
    for(int i=1;i<=n;i++)
    {
        scanf("%d %d",&x,&y);
        a[sum(++x)]++;
        update(x);
    }
    for(int i=0;i<n;i++) printf("%d\n",a[i]);
}

GitHub – testitem

Darren Li

New York City