Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 18180 | Accepted: 4630 |
Descriptionios
Input算法
Outputide
Sample Inputspa
3 10 1 7 3 6 6 10
Sample Outputcode
2
Hintblog
Sourceip
區間覆蓋問題。數軸上有n個閉區間 [ai, bi],選擇儘可能少的區間覆蓋一條指定線段[s,t]。題目意義的區間與通常的區間不太相同,由於按照題意,[1,t]與[t+1,n]是能夠覆蓋[1,n]的。ci
見《算法競賽入門經典(第二版)》第8章。get
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 }