codeforce 6E 线段树+枚举
来源:互联网 发布:淘宝微淘如何发买家秀 编辑:程序博客网 时间:2024/05/01 00:33
http://codeforces.com/problemset/problem/6/E
There are several days left before the fiftieth birthday of a famous Berland's writer Berlbury. In this connection the local library decided to make an exposition of the works of this famous science-fiction writer. It was decided as well that it is necessary to include into the exposition only those books that were published during a particular time period. It is obvious that if the books differ much in size, the visitors will not like it. That was why the organizers came to the opinion, that the difference between the highest and the lowest books in the exposition should be not more than k millimeters.
The library has n volumes of books by Berlbury, arranged in chronological order of their appearance. The height of each book in millimeters is know, it is hi. As Berlbury is highly respected in the city, the organizers want to include into the exposition as many books as possible, and to find out what periods of his creative work they will manage to cover. You are asked to help the organizers cope with this hard task.
The first line of the input data contains two integer numbers separated by a space n (1 ≤ n ≤ 105) and k (0 ≤ k ≤ 106) — the amount of books by Berlbury in the library, and the maximum allowed height difference between the lowest and the highest books. The second line contains n integer numbers separated by a space. Each number hi (1 ≤ hi ≤ 106) is the height of the i-th book in millimeters.
In the first line of the output data print two numbers a and b (separate them by a space), where a is the maximum amount of books the organizers can include into the exposition, and b — the amount of the time periods, during which Berlbury published a books, and the height difference between the lowest and the highest among these books is not more than k milllimeters.
In each of the following b lines print two integer numbers separated by a space — indexes of the first and the last volumes from each of the required time periods of Berlbury's creative work.
3 314 12 10
2 21 22 3
2 010 10
2 11 2
4 58 19 10 13
2 13 4
简单思路:利用线段树求出每个区间段的数的最大最小值,再依次枚举每个区间即可。
AC代码:
#include <stdio.h>#include <iostream>using namespace std;const int mm=111111;int ma[mm<<2],mi[mm<<2],ql[mm],qr[mm];int i,j,k,n,ans,t;void build(int l,int r,int rt){ if(l==r) { scanf("%d",&ma[rt]); mi[rt]=ma[rt]; return; } int m=(l+r)>>1;//相当于除二; build(l,m,rt<<1); build(m+1,r,rt<<1|1); ma[rt]=max(ma[rt<<1],ma[rt<<1|1]); mi[rt]=min(mi[rt<<1],mi[rt<<1|1]);}int queryma(int L,int R,int l,int r,int rt){ if(L<=l&&R>=r) return ma[rt]; int m=(l+r)>>1,ret=0; if(L<=m) ret=max(ret,queryma(L,R,l,m,rt<<1)); if(R>m)ret=max(ret,queryma(L,R,m+1,r,rt<<1|1)); return ret;}int querymi(int L,int R,int l,int r,int rt){ if(L<=l&&R>=r) return mi[rt]; int m=(l+r)>>1,ret=2e9; if(L<=m) ret=min(ret,querymi(L,R,l,m,rt<<1)); if(R>m) ret=min(ret,querymi(L,R,m+1,r,rt<<1|1)); return ret;}int main(){ while(~scanf("%d%d",&n,&k)) { build(1,n,1); ans=t=0; for(i=j=1;j<=n;i++) { if(j<i) j=i; while(j<=n&&(queryma(i,j,1,n,1)-querymi(i,j,1,n,1)<=k)) ++j; if(j-i>ans) ans=j-i,ql[0]=i,qr[0]=j-1,t=1; else if(j-i==ans) ql[t]=i,qr[t++]=j-1; } printf("%d %d\n",ans,t); for(i=0;i<t;i++) printf("%d %d\n",ql[i],qr[i]); } return 0;}
再写一个wa的代码,我利用尺取法做的2290734YranEWrong answer on test 33 GNU C++ 4.6望高人看到指点
#include <stdio.h>#include <string.h>#include <iostream>#include <algorithm>using namespace std;int a[100005];struct note{ int sum; int x,y;}edge[100005];bool cmp(note a,note b){ if(a.sum==b.sum) return a.x<b.x; return a.sum>b.sum;}int main(){ int n,k; while(~scanf("%d%d",&n,&k)) { for(int i=1;i<=n;i++) scanf("%d",&a[i]); if(n==1) { printf("1 1\n1 1\n"); continue; } int maxn=a[1],minn=a[1],xmaxn=1,xminn=1; int x=1,y=1; int flag=0; int ip=0; for(int i=2;i<=n;i++) { if(a[i]>maxn) { xmaxn=i; maxn=a[i]; // printf("&&\n"); } if(a[i]<minn) { xminn=i; minn=a[i]; // printf("**\n"); } if(maxn-minn>k) { edge[ip].sum=y-x+1; edge[ip].x=x; edge[ip++].y=y; if(a[i]==maxn) { for(int j=xminn;j<=xmaxn;j++) { if(maxn-a[j]<=k) { xminn=j; minn=a[j]; x=j; break; } } } else { for(int j=xmaxn;j<=xminn;j++) { if(a[j]-minn<=k) { //printf("**,%d\n",a[j]); xmaxn=j; maxn=a[j]; x=j; break; } } } } if(i==n) { edge[ip].sum=y+1-x+1; edge[ip].x=x; edge[ip++].y=y+1; } y=i; } int sum1=0; /*for(int i=0;i<ip;i++) { printf("(%d,%d,%d)\n",edge[i].sum,edge[i].x,edge[i].y); }*/ sort(edge,edge+ip,cmp); int m=edge[0].sum; for(int i=0;i<ip;i++) { if(edge[i].sum>=m) sum1++; else break; } printf("%d %d\n",m,sum1); for(int i=0;i<sum1;i++) { printf("%d %d\n",edge[i].x,edge[i].y); } } return 0;}
- codeforce 6E 线段树+枚举
- Codeforce E. Lucky Queries 线段树实践
- Codeforce-242E-XOR on Segment(线段树区间更新)
- CF 260E Dividing Kingdom(枚举+线段树)
- Codeforce 52C---Circular RMQ 线段树
- E. Cubes codeforce 180/E
- COdeforce 14E
- codeforce 18E
- (121E)codeforce
- Codeforce 445E
- CodeForce 27E
- codeforce 78E
- codeforce round 399# E
- Codeforce 785E(分块)
- codeforce 761E
- codeforce 416 E floyd
- codeforce 888E
- codeforce 899E
- JAVA中获取当前系统时间
- Netty源码学习——Included transports(传输方式)
- linux用户组和权限
- 互联网金融迈入深水区
- 动态网站一(哈佛大学公开课:构建动态网站1-3课)
- codeforce 6E 线段树+枚举
- h2 database
- CSS3/jQuery自定义弹出窗口
- Java 阻塞队列 BlockingQueue
- 外链与内链重要性pk
- diqMKTp写侵投驴夜访铺
- 给iOS开发者的Android开发建议
- POJ2305 Basic remains
- MaxinumSubarray(leetcode)