标签:
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 18180 | Accepted: 4630 |
Description
Input
Output
Sample Input
3 10 1 7 3 6 6 10
Sample Output
2
Hint
Source
区间覆盖问题。数轴上有n个闭区间 [ai, bi],选择尽量少的区间覆盖一条指定线段[s,t]。题目意义的区间与一般的区间不太相同,因为按照题意,[1,t]与[t+1,n]是可以覆盖[1,n]的。
见《算法竞赛入门经典(第二版)》第8章。
1 #include <iostream> 2 #include <algorithm> 3 #include <map> 4 #include <vector> 5 #include <functional> 6 #include <string> 7 #include <cstring> 8 #include <queue> 9 #include <set> 10 #include <cmath> 11 #include <cstdio> 12 using namespace std; 13 #define IOS ios_base::sync_with_stdio(false) 14 #define TIE std::cin.tie(0) 15 #define MIN2(a,b) (a<b?a:b) 16 #define MIN3(a,b) (a<b?(a<c?a:c):(b<c?b:c)) 17 #define MAX2(a,b) (a>b?a:b) 18 #define MAX3(a,b,c) (a>b?(a>c?a:c):(b>c?b:c)) 19 typedef long long LL; 20 typedef unsigned long long ULL; 21 const int INF = 0x3f3f3f3f; 22 const double PI = 4.0*atan(1.0); 23 24 int n, t, ans; 25 typedef struct Interval{ 26 int st, et; 27 bool operator<(const Interval &a) const{ 28 return st < a.st; 29 } 30 }I; 31 I a[25005]; 32 33 bool solve() 34 { 35 int i = 0, et = 0, tmp; 36 ans = 0; 37 while (i < n&&et < t){ 38 if (a[i].st>et + 1) return false; 39 tmp = -1; 40 while (i < n&&a[i].st <= et + 1){ 41 tmp = MAX2(tmp, a[i].et); i++; 42 } 43 et = tmp; 44 ans++; 45 } 46 if (et < t) return false; 47 else return true; 48 } 49 50 int main() 51 { 52 scanf("%d%d", &n, &t); 53 for (int i = 0; i < n; i++) 54 scanf("%d%d", &a[i].st, &a[i].et); 55 sort(a, a + n); 56 printf("%d\n", solve() ? ans : -1); 57 }
标签:
原文地址:http://www.cnblogs.com/cumulonimbus/p/5856904.html