题意:n头牛排成一行,告诉你最高的牛的高度h以及位置i,然后有r个信息(x,y),告诉你第x头牛向某一个方向最多可以看到第y头牛,即位于[x+1,y-1]区间内的牛都比它小,问每头牛最大可能高度是多少。
题解:对每个信息(x,y)建立区间[x+1,y-1],并标记属于区间的左还是右,意味着这区间里的牛会被至少有一头牛给高度鄙视= =!然后,就是求区间的重叠数了,某个点的区间重叠数是k,那么它的最大高度就是h-k,另外需要注意的就是,要自己去重,题目可能有完全相同的冗余信息,这些信息都应该只记录一次,即哪怕一头牛身高在不堪,也不能被同一头牛给反反复复鄙视,鄙视可是要降身高的啊~~
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 using namespace std; 5 struct data 6 { 7 int x; 8 bool left; 9 bool operator<(const data &ne)const 10 { 11 if(x!=ne.x) 12 return x<ne.x; 13 else 14 return left; 15 } 16 }po[30000]; 17 int d[10005]; 18 struct que 19 { 20 int x,y; 21 bool operator<(const que &ne)const 22 { 23 if(x!=ne.x) 24 return x<ne.x; 25 else 26 return y<ne.y; 27 } 28 bool operator!=(const que &ne)const 29 { 30 return x!=ne.x||y!=ne.y; 31 } 32 }qe[10005]; 33 int main() 34 { 35 int n,i,h,r; 36 while(scanf("%d%d%d%d",&n,&i,&h,&r)!=EOF) 37 { 38 int m=0,x,y; 39 for(int i=0;i<r;i++) 40 { 41 scanf("%d%d",&x,&y); 42 if(x>y) 43 swap(x,y); 44 qe[i].x=x;qe[i].y=y; 45 } 46 sort(qe,qe+r); 47 m=0; 48 for(int i=1;i<r;i++) 49 { 50 if(qe[i]!=qe[m]) 51 qe[++m]=qe[i]; 52 } 53 r=m+1; 54 m=0; 55 for(int i=0;i<r;i++) 56 { 57 x=qe[i].x,y=qe[i].y; 58 if(x==y||x+1==y) 59 continue; 60 po[m].x=x+1;po[m].left=true;m++; 61 po[m].x=y-1;po[m].left=false;m++; 62 } 63 sort(po,po+m); 64 int lx=1,dep=0; 65 for(int i=0;i<m;i++) 66 { 67 if(po[i].x>lx) 68 { 69 while(lx<po[i].x) 70 printf("%d\n",h-dep),lx++; 71 } 72 if(po[i].left) 73 dep++; 74 else 75 { 76 while(lx<=po[i].x) 77 printf("%d\n",h-dep),lx++; 78 dep--; 79 } 80 } 81 while(lx<=n) 82 printf("%d\n",h-dep),lx++; 83 } 84 return 0; 85 }