楼房
題目描述
地平線(x軸)上有n個矩(lou)形(fang),用三個整數h[i],l[i],r[i]來表示第i個矩形:矩形左下角為(l[i],0),右上角為(r[i],h[i])。地平線高度為0。在輪廓線長度最小的前提下,從左到右輸出輪廓線。
下圖為樣例2。
輸入輸出格式
輸入格式:第一行一個整數n,表示矩形個數
以下n行,每行3個整數h[i],l[i],r[i]表示第i個矩形。
輸出格式:第一行一個整數m,表示節點個數
以下m行,每行一個坐標表示輪廓線上的節點。從左到右遍歷輪廓線并順序輸出節點。第一個和最后一個節點的y坐標必然為0。
輸入輸出樣例
輸入樣例#1:【樣例輸入1】 2 3 0 2 4 1 3【樣例輸入2】 5 3 -3 0 2 -1 1 4 2 4 2 3 7 3 6 8輸出樣例#1:
【樣例輸出1】 6 0 0 0 3 1 3 1 4 3 4 3 0【樣例輸出2】 14 -3 0 -3 3 0 3 0 2 1 2 1 0 2 0 2 4 4 4 4 2 6 2 6 3 8 3 8 0
說明
【數據范圍】
對于30%的數據,n<=100
對于另外30%的數據,n<=100000,1<=h[i],l[i],r[i]<=1000
對于100%的數據,1<=n<=100000,1<=h[i]<=10^9,-10^9<=l[i]<r[i]<=10^9
思路
參見 樓房 洛谷1382 && codevs2995 by:Soda 順膜.
有一個叫掃描線的東西,我沒用到~
有一種線段樹的做法,我不會~
然后就用堆模擬,手生的直接敲不對。。。
代碼實現
1 #include<cstdio> 2 #include<algorithm> 3 using namespace std; 4 const int maxn=3e6; 5 int n,s,lat,net,idd; 6 int a,b,c; 7 bool v[maxn]; 8 struct nate{int num,id,h;}f[maxn]; 9 bool cmp(const nate&x,const nate&y){return x.id<y.id;} 10 int top; 11 int h[maxn]; 12 void add(int x){ 13 h[++top]=x,a=top,b=a>>1; 14 while(f[h[a]].h>f[h[b]].h&&b){ 15 h[a]^=h[b],h[b]^=h[a],h[a]^=h[b]; 16 a=b,b=a>>1; 17 } 18 } 19 void dle(){ 20 a=1,b=a<<1,c=b|1; 21 h[a]=h[top],h[top--]=0; 22 while(1){ 23 if(f[h[c]].h>f[h[b]].h) b=c; 24 if(f[h[b]].h>f[h[a]].h) h[a]^=h[b],h[b]^=h[a],h[a]^=h[b]; 25 else break; 26 a=b,b=a<<1,c=b|1; 27 } 28 } 29 int anss,ans[2][maxn]; 30 int main(){ 31 scanf("%d",&n); 32 for(int i=1;i<=n;i++){ 33 scanf("%d%d%d",&a,&b,&c); 34 f[++s]=(nate){i,b,a}; 35 f[++s]=(nate){i,c,a}; 36 } 37 sort(f+1,f+s+1,cmp); 38 for(int i=1;i<=s;){ 39 lat=f[h[1]].h; 40 idd=f[i].id; 41 while(idd==f[i].id){ 42 v[f[i].num]^=1; 43 if(v[f[i].num]) add(i); 44 else while(!v[f[h[1]].num]&&h[1]) dle(); 45 i++; 46 } 47 net=f[h[1]].h; 48 if(lat==net) continue; 49 ans[0][anss]=idd,ans[1][anss++]=lat; 50 ans[0][anss]=idd,ans[1][anss++]=net; 51 } 52 printf("%d\n",anss); 53 for(int i=0;i<anss;i++) 54 printf("%d %d\n",ans[0][i],ans[1][i]); 55 return 0; 56 }
?
轉載于:https://www.cnblogs.com/J-william/p/7162305.html
總結
- 上一篇: 花灯交易
- 下一篇: 摅字开头的四字成语有哪些?