NYOJ  2355 点兵点将(贪心)_i++

题解:

        题目可以理解为当你打败一第i个敌人时你就获得了一个任意时刻制造bi单位的机会,然后每次当打不过某个敌人的时候贪心的选择属性最强的即可,这里采用优先队列。

#include<set>  
#include<map>         
#include<stack>                
#include<queue>                
#include<vector>        
#include<string>      
#include<math.h>       
#include<stdio.h>                
#include<iostream>                
#include<string.h>                
#include<stdlib.h>        
#include<algorithm>       
#include<functional>        
using namespace std;
typedef long long ll;
#define inf 1000000000           
#define mod 1000000007                 
#define maxn  500005     
#define PI 3.1415926  
#define lowbit(x) (x&-x)     
#define eps 1e-9   
priority_queue<ll>q;
ll a[maxn], b[maxn];
int main(void)
{
	ll n, m, i, ans, p;
	while (scanf("%lld%lld", &n, &m) != EOF)
	{
		ans = 0;p = n;
		while (q.empty() == 0)
			q.pop();
		for (i = 1;i <= m;i++)
			scanf("%lld", &a[i]);
		for (i = 1;i <= m;i++)
			scanf("%lld", &b[i]);
		for (i = 1;i <= m;i++)
		{
			while (p < a[i] && q.empty() == 0)
			{
				ll now = q.top();
				p += now;q.pop();ans++;
			}
			if (p < a[i])
			{
				ans = -1;
				break;
			}
			p -= a[i];
			q.push(b[i]);
		}
		printf("%lld/n", ans);
	}
	return 0;
}