Skip to content

Instantly share code, notes, and snippets.

@HaiyangXu
Created January 5, 2015 14:50
Show Gist options
  • Save HaiyangXu/51257ec691a757ce5f51 to your computer and use it in GitHub Desktop.
Save HaiyangXu/51257ec691a757ce5f51 to your computer and use it in GitHub Desktop.
题目2 : Disk Storage
#include <iostream>
#include <queue>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <ctime>
#include <cstdio>
#include <cstring>
#include <cstdlib>
using namespace std;
typedef vector<int>::iterator IT;
vector<int> disks;
vector<bool> flag;
int M;
int maxdisk(int H,int R,int x,int counts){
if(H==0||counts>=disks.size())return 0;
int maxval=0;
IT it= upper_bound(disks.begin(), disks.end(), min(R,x));
for(IT i=disks.begin();i!=it;i++){
int loc=int(i-disks.begin());
if (flag[loc]) {
flag[loc]=false;
maxval=max(maxval,1+maxdisk(H-1, R+1, *i+M,counts+1));
flag[loc]=true;
}
}
return maxval;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
#endif
int N,R,H;
cin>>N>>M>>H>>R;
int disk;
while(N--){
cin>>disk;
disks.push_back(disk);
}
flag.resize(disks.size());
fill(flag.begin(),flag.end(),true);
cout<<maxdisk(H,R,R,0);
}
@HaiyangXu
Copy link
Author

http://hihocoder.com/contest/mstest2015jan1/problem/2

题目2 : Disk Storage
时间限制:10000ms
单点时限:1000ms
内存限制:256MB
描述
1.png

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

@Insulator
Copy link

复杂度这么高,果然TLE= =。。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment