Poj 2376 greedy range coverage
Question:
Select as few intervals as possible for n intervals to cover 1 ~ T this interval
If output-1 cannot be overwritten
Ideas:
Classic greedy interval coverage
Sort all intervals from small to large to take the interval at the rightmost of the end point.
Code:
#include
#include
#include
#include#include
#include
#include
#include
#include
#include
using namespace std;#define INF 0x3f3f3f3f#define PI acos(-1.0)#define mem(a, b) memset(a, b, sizeof(a))typedef pair
pii;typedef long long LL;//------------------------------const int maxn = 25005;int n, T;struct node{ int s,e; bool operator < (const node nt) const{ return s < nt.s; }}cow[maxn];void init(){ for(int i = 0; i < n; i++){ scanf("%d%d",&cow[i].s,&cow[i].e); } sort(cow, cow + n);}void solve(){ if(cow[0].s > 1){ printf("-1\n"); return; } int start_ = 1, end_ = 1, cnt = 1; for(int i = 0; i < n; i++){ if(cow[i].s <= start_) end_ = max(end_, cow[i].e); else{ cnt++; start_ = end_ + 1; if(cow[i].s <= start_) end_ = max(end_, cow[i].e); else{printf("-1\n"); return; } } if(end_ >= T) break; } if(end_ >= T) printf("%d\n",cnt); else printf("-1\n");}int main(){ scanf("%d%d",&n,&T); init(); solve(); return 0;}
An error occurred when T_T was returned because a condition end> = T... sigh... was rarely judged in the solve function...