Save the dwarfs

Save the dwarfs

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 954    Accepted Submission(s): 294


Problem Description
Several dwarfs are trapped in a deep well. They are not tall enough to climb out of the well, so they want to make a human-pyramid, that is, one dwarf stands on another's shoulder, until the dwarf on the top can reach the top of the well when he raise his arms up. More precisely speaking, we know the i-th dwarf's height from feet to shoulder is Ai, and his arm length Bi. And we know the height of the well is H. If we can build a dwarf-tower consists of dwarf 1, dwarf 2, ..., dwarf k from bottom to top, such that A1 + A2 + ... + Ak-1 + Ak + Bk >= H, then dwarf k can escape from the well. Obviously, the escaped dwarf can't be used to build tower again.

We want the escaped dwarfs as many as possible. Please write a program to help the dwarfs.
 

Input
The first line of each test case contains N, (1 <= N <= 2000) the number of trapped dwarfs. Each of the following N lines contains two integers Ai and Bi. The last line is H, the height of the well. All the integers are less than 100,000.
 

Output
Output one line for each test case, indicating the number of dwarfs escaped at most.
 

Sample Input
  
  
2 20 10 5 5 30 2 20 10 5 5 35
 

Sample Output
   
   
2 1
Hint
For the first case, the tall dwarf can help the other dwarf escape at first. For the second case, only the tall dwarf can escape.
 


#include <iostream>


using namespace std;
struct Node{
int a;
int b;
int priority;
bool state;
Node(int a1,int b1,int priority1,bool state1):a(a1),b(b1),priority(priority1),state(state1)
{}
Node()
{
a=b=priority=-1;
state=false;
}
bool operator<(const Node & rhs)
{
if(a<rhs.a)
return true;
if(a>rhs.a)
return false;
if(b<rhs.b)
return true;
else
return false;
}
};
Node data[2000];
int size=0;int H=0;
int partition(Node * p,int start,int end)
{
int a=start,b=end+1;
int middle=start+(end-start)/2;
Node temp=p[middle];
p[middle]=p[start];
p[start]=temp;
while(a<=b)
{
while(p[++a]<temp&&a<end);
while(temp<p[--b]);
if(a<b)
{ Node t=p[a];
 p[a]=p[b];
 p[b]=t;
}
}
p[start]=p[a];p[a]=temp;return a;
}
void qSort(int start,int end)
{
if(start<end)
{
int t=partition(data,start,end);
qSort(start,t-1);
qSort(t+1,end);
}


}
void BubbleSort()
{
int exchange=size-1;int bound=size-1;
while(exchange!=-1)
{
bound=exchange;
exchange=-1;
for(int i=0;i<bound;++i)
if(data[i]<data[i+1])
{
Node t=data[i+1];
data[i+1]=data[i];
data[i]=t;
exchange=i;
}
}
}


void SetPriority()
{
static int sum;
sum=0;
for(int i=0;i!=size;++i)
{
if(data[i].state==true)
sum+=data[i].a;
}
for(int i=0;i!=size;++i)
{
if(data[i].state)
{
int temp=sum;int num=-1;
if(data[i].b+sum>=H) num=1;
for(int j=size-1;j>=0;--j)
{
if(j!=i&&data[j].state)
{
if(data[i].b+temp-data[j].a>=H)
{
num++;
temp-=data[j].a;
}
else
break;
}


}
data[i].priority=num;
}
}
}
int MinNum()
{
int result=-1;
for(int i=0;i<size;++i)
{
if(data[i].state==true&&data[i].priority>0)
{
result=i;
break;
}
}
for(int i=0;i<size;++i)
if(data[i].state&&data[i].priority>0&&data[i].priority<data[result].priority)
result=i;
else if(data[i].state&&data[i].priority==data[result].priority&&data[i].a<data[result].a)
result=i;
return result;

}
int MaxEscape()
{
//qSort(0,size-1);
BubbleSort();
int result=0;
SetPriority();
while(1)
{
int min=MinNum();
if(min!=-1)
{
data[min].state=false;
result++;
SetPriority();
}
else
break;
}
return result;
}
void main()
{
while(1)
{
int n;
cin>>n;size=n;
while(n-- >0)
{
cin>>data[n].a>>data[n].b;
data[n].state=true;
}
cin>>H;
n=MaxEscape();
cout<<n<<endl;
}
}






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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值