[微软苏州校招 Hihocoder] Disk Storage

35 篇文章 0 订阅
6 篇文章 0 订阅

#1100 : Disk Storage

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

描述

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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值