优先修截至时间短的建筑!!!
大佬ac代码
设置截止时间为first,消耗时间为second,比我的巧妙
#include <iostream>
#include <queue>
#include <vector>
#include <algorithm>
using namespace std;
typedef long long ll;
const int N = 150100;
pair<ll, ll> arr[N];
priority_queue<ll> q;
ll sum = 0, ans = 0, n;
int main()
{
scanf("%lld", &n);
for (int i = 1; i <= n; i++)
{
scanf("%lld%lld", &arr[i].second, &arr[i].first);
}
sort(arr + 1, arr + 1 + n); //按截至时间排序
for (int i = 1; i <= n; i++)
{
sum += arr[i].second; //先假设能修
if (sum <= arr[i].first)
{
q.push(arr[i].second); //确实能修
ans++;
}
else
{
if (q.top() > arr[i].second)//修不了但可替换(堆顶的修理时间比当前的修理时间长)
{
sum -= q.top();
q.pop(); //不修堆顶的建筑
q.push(arr[i].second); //修当前的建筑
}
else sum -= arr[i].second; //确实不能修
}
}
printf("%lld\n", ans);
return 0;
}
菜鸡的 ac代码
按题目依次读入,一开始没考虑pair使用sort的优先级,就wa了,下面是改正代码
#include <iostream>
#include <queue>
#include <vector>
#include <algorithm>
using namespace std;
typedef long long ll;
const int N = 150100;
pair<ll,ll> arr[N];
priority_queue<ll> q;
ll sum = 0,ans=0,n;
bool cmp(pair<ll, ll> x, pair<ll, ll> y)
{
if(x.second!=y.second)
return x.second < y.second;
eles
return x.first < y.first;
}
int main()
{
scanf("%lld", &n);
for (int i = 1; i <= n; i++)
{
scanf("%lld%lld", &arr[i].first, &arr[i].second);
}
sort(arr + 1, arr + 1 + n,cmp);
for (int i = 1; i <= n; i++)
{
sum += arr[i].first;
if (sum <= arr[i].second)
{
q.push(arr[i].first);
ans++;
}
else
{
if (q.top() > arr[i].first)
{
sum -= q.top();
q.pop();
q.push(arr[i].first);
}
else sum -= arr[i].first;
}
}
printf("%lld\n", ans);
return 0;
}
关键:以截至时间排序