不知道为什么原来的思路就是错的 ,那也贴上代码,希望大牛指点
#include<stdio.h>
#include<iostream>
#include<math.h>
#include<stdlib.h>
#include<ctype.h>
#include<algorithm>
#include<vector>
#include<string.h>
#include<queue>
#include<stack>
#include<set>
#include<map>
#include<sstream>
#include<time.h>
#include<malloc.h>
using namespace std;
int n;
int pp,l;
struct qq
{
int ps;
int ml;
}p[10100];
bool cmp (qq a ,qq b)
{
return a.ps < b.ps;
}
int main ()
{
while (scanf ("%d",&n)!=EOF)
{
priority_queue<int,vector<int>,less<int> >q;
for(int i=0;i<n;i++)
{
scanf("%d %d",&p[i].ps , &p[i].ml );
}
scanf("%d %d",&l,&pp);
sort(p,p+n,cmp);
p[n].ps = l;
p[n].ml = 0;
int ans =0;
int pos =0;
int tank=pp;
int ok=1;
for(int i=0 ;i<n;i++)
{
int d =p[i].ps - pos;
while (tank - d <0)
{
if ( q.empty() )
{
ok=0;
break;
}
tank+=q.top();
q.pop();
ans ++;
}
tank-=d;
pos = p[i].ps ;
q.push( p[i].ml );
}
if (ok)
printf("%d\n",ans);
else
puts("-1");
}
return 0;
}
#include<stdio.h>
#include<iostream>
#include<math.h>
#include<stdlib.h>
#include<ctype.h>
#include<algorithm>
#include<vector>
#include<string.h>
#include<queue>
#include<stack>
#include<set>
#include<map>
#include<sstream>
#include<time.h>
#include<malloc.h>
using namespace std;
int n;
int pp,l;
struct qq
{
int ps;
int ml;
}p[10100];
bool cmp (qq a ,qq b)
{
return a.ps > b.ps;
}
int main ()
{
while (scanf ("%d",&n)!=EOF)
{
priority_queue<int,vector<int>,less<int> >q;
for(int i=0;i<n;i++)
{
scanf("%d %d",&p[i].ps , &p[i].ml );
}
scanf("%d %d",&l,&pp);
sort(p,p+n,cmp);
p[n].ps = l;
p[n].ml = 0;
int pos = 0;
int ans = 0;
q.push(pp);
while ( l>0 && !q.empty() )
{
ans ++;
int t = q.top();
q.pop();
l-=t;
while ( pos < n && l<= p[pos].ps )
{
q.push( p[pos++].ml );
}
}
if (l<=0)
printf("%d\n",ans-1);
else
puts("-1");
}
return 0;
}