[微软苏州校招 Hihocoder] Disk Storage

#1100 : Disk Storage

时间限制:
10000ms 单点时限:
1000ms 内存限制:
256MB

描述

《[微软苏州校招 Hihocoder] Disk Storage》

Little Hi and Little Ho have a disk storage. The storage’s shape is a truncated cone of height H. R+H is radius of top circle and R is radius of base circle. 
Little Ho buys N disks today. Every disk is a cylinder of height 1. Little Ho wants to put these disk into the storage under below constraints:

1. Every disk is placed horizontally. Its axis must coincide with the axis of storage.
2. Every disk is either place on the bottom surface or on another disk.
3. Between two neighboring disks in the storage, the upper one’s radius minus the lower one’s radius must be less than or equal to M.

Little Ho wants to know how many disks he can put in the storage at most.

输入

Input contains only one testcase.
The first line contains 4 integers: N(1 <= N <= 100000), M, H, R(1 <= M, R, H <= 100000000).
The second line contains N integers, each number prepresenting the radius of a disk. Each radius is no more than 100000000.

输出

Output the maximum possible number of disks can be put into the storage.

样例输入

5 1 10 3
1 3 4 5 10

样例输出

4

解决方案:

其实就是将 n 个disk先排好序,然后分组,每组都可以直接叠在一起,而满足 上面的disk – 下面的disk <= m。

Code:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <vector>

using namespace std;

class Solution {
public:

    void solve() {
        int n, m, h, r;
        cin >> n >> m >> h >> r;
        vector<int> disks(n, 0);
        for (int i = 0; i < n; i++) cin >> disks[i];
        sort(disks.begin(), disks.end());
        vector<vector<int> > splitedDisks;
        for (int i = 0; i < n; i++) {
            if (i == 0 || disks[i] - disks[i-1] > m) {
                vector<int> one;
                one.push_back(disks[i]);
                splitedDisks.push_back(one);
            } else {
                splitedDisks[splitedDisks.size()-1].push_back(disks[i]);
            }
        }
        
        //print(splitedDisks);
        
        int ans = 0;
        for (int i = splitedDisks.size()-1; i >= 0; i--) {
            if (splitedDisks[i][0] <= r) {
                for (int j = 0; j < splitedDisks[i].size(); j++) {
                    if (splitedDisks[i][j] <= r+j) ans++;
                    else break;
                }
                for (int j = 0; j < i; j++) ans += splitedDisks[j].size();
                break;
            }
        }
        ans = min(ans, h);
        printf("%d\n", ans);
    }

    void print(vector<int> &v1) {
        for (int i = 0; i < v1.size(); i++) printf("%d ", v1[i]); printf("\n");
    }

    void print(vector<vector<int> > &v2) {
        for (int i = 0; i < v2.size(); i++) print(v2[i]);
    }
};

int main() {
    Solution solution;
    solution.solve();
    return 0;
}

点赞