loj 10001 種樹
阿新 • • 發佈:2018-09-09
== esp () span return sort image cmp nod
*********貪心,把需要的路段終止點排序,然後在每個區間內判斷是否已經滿足條件,不滿足的從區間右端向左端種樹。
1 #include<cstdio> 2 #include<algorithm> 3 #include<cstring> 4 #include<cmath> 5 using namespace std; 6 int i,j,n,h,vis[30005] = {0},ans = 0,tot; 7 struct node 8 { 9 int b; 10 int e; 11 int t; 12 }a[30005]; 13 int cmp(node a,node b) 14 { 15 return a.e < b.e; 16 } 17 int main() 18 { 19 scanf("%d",&n); 20 scanf("%d",&h); 21 for(i = 1;i <= h;i++) 22 { 23 scanf("%d %d %d",&a[i].b,&a[i].e,&a[i].t); 24 } 25 sort(a + 1,a + 1 + h,cmp); 26 for(i = 1;i <= h;i++) 27 { 28 tot = 0; 29 for(j = a[i].b;j <= a[i].e;j++) 30 { 31 if(vis[j] == 1) 32 { 33 tot++; 34 } 35 if(tot == a[i].t) 36 break; 37 } 38 if(tot < a[i].t) 39 {40 for(j = a[i].e;j >= a[i].b;j--) 41 { 42 if(vis[j] == 0) 43 { 44 vis[j] = 1; 45 tot++; 46 ans++; 47 } 48 if(tot == a[i].t) 49 break; 50 } 51 } 52 } 53 printf("%d",ans); 54 return 0; 55 }
loj 10001 種樹