比赛的一个水题,英语不好,一开始把题意都错了, 后来改了下就对了,很简单的一个算是贪心的吧,代码如下
#include <iostream>
#include <cstdio>
#include <queue>
#include <algorithm>
#include <string>
#include <cmath>
using namespace std;
struct people
{
int come, go;
bool operator < (const people & p)
{
return come < p.come ||(come == p.come && go <p.go);
}
}person[300000+1];
int main()
{
int m, n;
while(scanf("%d%d", &m, &n) != EOF)
{
priority_queue< int, vector <int>, greater<int> >q;
int sum = 0, lock = 0;
int come, go;
for(int i = 0; i < m; i++)
{
scanf("%d%d", &person[i].come, &person[i].go);
}
sort(person, person + m);
for(int i = 0;i < m; i++)
{
come = person[i].come;
go = person[i].go;
if(!q.empty() && come >= q.top() && come <= q.top() + n )
{
q.pop();
lock++;
}
else if(!q.empty() && come > q.top() + n )
{
while(come > q.top() + n && q.size() > 0)
q.pop();
if(q.size() != 0)
{
q.pop();
lock++;
}
}
q.push(come + go);
}
cout << lock << endl;
}
return 0;
}
转载请注明原文地址: https://ju.6miu.com/read-162.html