poj2376 Cleaning Shifts(区间贪心,理解题意)

时间:2023-03-09 03:21:06
poj2376 Cleaning Shifts(区间贪心,理解题意)

https://vjudge.net/problem/POJ-2376

题意理解错了!!真是要仔细看题啊!!

看了poj的discuss才发现,如果前一头牛截止到3,那么下一头牛可以从4开始!!!

 #include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<set>
#define INF 0x3f3f3f3f
typedef long long ll;
using namespace std;
typedef struct{
int x, y;
}Node;
Node node[];
bool cmp(const Node a, const Node b)
{
if(a.x != b.x)
return a.x<b.x;
else
return a.y>b.y;
}//起点相同则返回终点大的,否则返回起点小的
int main()
{
int n, t, cnt=, flag=;
cin >> n >> t;
for(int i = ; i < n; i++){
cin >> node[i].x >> node[i].y;
}
sort(node, node+n, cmp);
if(node[].x != ){
cout << "-1" << endl;
return ;
}
else if(node[].y == t){
cout << "" << endl;
return ;
}
else {
int end = node[].y, pre, maxm = node[].y;
for(int i = ; i < n; i++){
pre = maxm;
while(i < n&&node[i].x<=end+){//这里理解错了!!
if(maxm < node[i].y){
maxm = node[i].y;
}
i++;
}
i--;
//cout << maxm << endl;
if(maxm == t){
cnt++;
flag=;
break;
}
else if(pre == maxm){
break;
}
else{
end = maxm;
cnt++;
}
}
if(!flag) cout << "-1" << endl;
else
cout << cnt << endl;
}
return ;
}