标签:scan cin inter long miss sub mat mini ide
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 32561 | Accepted: 7972 |
Description
Input
Output
Sample Input
3 10 1 7 3 6 6 10
Sample Output
2
Hint
Source
https://www.cnblogs.com/wyboooo/p/9808378.html
和poj3171基本一样,改一下输入和范围即可。
1 #include <iostream> 2 #include <set> 3 #include <cmath> 4 #include <stdio.h> 5 #include <cstring> 6 #include <algorithm> 7 using namespace std; 8 typedef long long LL; 9 #define inf 0x7f7f7f7f 10 11 const int maxn = 25000 + 5; 12 const int maxtime = 1e6 + 5; 13 struct node{ 14 int st, ed, cost; 15 }cow[maxn]; 16 bool cmp(node a, node b) 17 { 18 return a.ed < b.ed; 19 } 20 LL tree[maxtime << 2];//区间中f[]最小值 21 int n, L, R; 22 23 void pushup(int rt) 24 { 25 tree[rt] = min(tree[rt << 1], tree[rt << 1|1]); 26 } 27 28 void build(int rt, int l, int r) 29 { 30 if(l == r){ 31 tree[maxn] = inf; 32 return; 33 } 34 int mid = (l + r) / 2; 35 build(rt<<1, l, mid); 36 build(rt<<1|1, mid + 1, r); 37 pushup(rt); 38 } 39 40 void update(int x, LL val, int l, int r, int rt) 41 { 42 if(l == r){ 43 tree[rt] = min(tree[rt], val); 44 return; 45 } 46 int m = (l + r) / 2; 47 if(x <= m){ 48 update(x, val, l, m, rt<<1); 49 } 50 else{ 51 update(x, val, m + 1, r, rt<<1|1); 52 } 53 pushup(rt); 54 } 55 56 LL query(int L, int R, int l, int r, int rt) 57 { 58 if(L <= l && R >= r){ 59 return tree[rt]; 60 } 61 int m = (l + r) / 2; 62 LL ans = inf; 63 if(L <= m){ 64 ans = min(ans, query(L, R, l, m, rt<< 1)); 65 } 66 if(R > m){ 67 ans = min(ans, query(L, R, m + 1, r, rt<<1|1)); 68 } 69 pushup(rt); 70 return ans; 71 } 72 73 int main() 74 { 75 while(scanf("%d%d", &n, &R) != EOF){ 76 R+=1; 77 memset(tree, 0x7f, sizeof(tree)); 78 for(int i = 1; i <= n; i++){ 79 scanf("%d%d", &cow[i].st, &cow[i].ed); 80 cow[i].st+=1;cow[i].ed+=1; 81 cow[i].cost = 1; 82 } 83 sort(cow + 1, cow + 1 + n, cmp); 84 85 build(1, 1, R); 86 87 update(1, 0, 1, R, 1); 88 //cout<<"yes"<<endl; 89 //int far = L; 90 bool flag = true; 91 for(int i = 1; i <= n; i++){ 92 /*if(cow[i].st > far + 1){ 93 flag = false; 94 // break; 95 }*/ 96 int a = max(1, cow[i].st - 1); 97 int b = min(R, cow[i].ed); 98 //cout<<a<<" "<<b<<endl; 99 LL f = query(a, b, 1, R, 1); 100 f += cow[i].cost; 101 //cout<<f<<endl; 102 update(b, f, 1, R, 1); 103 //far = max(far, cow[i].ed); 104 //cout<<far<<endl; 105 } 106 //cout<<"yes"<<endl; 107 108 LL ans = query(R, R, 1, R, 1); 109 if(ans >= inf){ 110 printf("-1\n"); 111 } 112 else{ 113 printf("%lld\n", ans); 114 115 //else{ 116 // printf("-1\n"); 117 } 118 119 } 120 121 }
poj2376 Cleaning Shifts【线段树】【DP】
标签:scan cin inter long miss sub mat mini ide
原文地址:https://www.cnblogs.com/wyboooo/p/9808395.html