HDU 5200
来源:互联网 发布:java hashtable 编辑:程序博客网 时间:2024/04/20 01:34
恩。。。。看了题解之后不知道线段树应该怎么实现,由于后面附了更简便的方法,所以好像没有看到用线段树实现的题解QAQ。。想知道。。。
然后仔细看了最后的题解,发现………………自己刚开始写的就差一点点就可以优化了嘛QAQ!摔!每次对比的时候只看前后的位置是否有树,所以只要比对这两个就好了啊。。。然后就不用再用队列来存放那些加进来的树了。。。虽然自己在写之前算过复杂度。。但是。。。貌似忘了考虑每次后移的n^2的时间。。。。华丽丽的T。。。
这是之前的版本:
#include <stdio.h>#include <iostream>#include <algorithm>using namespace std;#define maxn 51000struct node{ int val; int pos,vis;}a[maxn],q[maxn],que[maxn];int ans[maxn];int cmp(node a,node b){ if(a.val==b.val) return a.pos>b.pos; return a.val>b.val;}int erfen(int l,int r,int x){ int mid=(l+r)>>1; while(l<r) { if(que[mid].pos<x) l=mid+1; else r=mid; mid=(l+r)>>1; } return mid;}void print(node *a,int len){ for(int i=0;i<len;i++) printf("%d ",a[i].val);printf("\n");}int main(){ int n,m; while(scanf("%d%d",&n,&m)!=EOF) { int i,j,k; for(i=0;i<n;i++) {scanf("%d",&a[i].val);a[i].vis=0;a[i].pos=i;} sort(a,a+n,cmp); for(i=0;i<m;i++) {scanf("%d",&q[i].val);q[i].pos=i;que[i].vis=0;} sort(q,q+m,cmp); i=0; int num=0; int top=0; // print(a,n);print(q,m); //debug for(k=0;k<m;k++) { while(a[i].val>q[k].val&&i<n) { j=erfen(0,top,a[i].pos);//printf("pos is %d\n",j); int tmp=a[i].pos; if(j==0) { if(tmp==que[j].pos-1) que[j].vis=a[i].vis=1; } else if(j==top) {if(tmp==que[j-1].pos+1) que[j].vis=a[i].vis=1;} else if(tmp==que[j].pos-1&&tmp==que[j-1].pos+1) {num--;que[j-1].vis=que[j].vis=a[i].vis=1;} if(!a[i].vis) num++; for(int p=top;p>j;p--) que[p]=que[p-1]; que[j]=a[i]; top++; // printf("a[%d]=%d q[%d]=%d\n",i,a[i].val,k,q[k].val); //debug i++; } // for(i=0;i<top;i++) printf("%d ",que[i].pos);printf("\n"); //debug ans[q[k].pos]=num; } for(i=0;i<m;i++) printf("%d\n",ans[i]); } return 0;}
这是AC的:(顿觉MAP大法好)
#include <stdio.h>#include <iostream>#include <algorithm>#include <map>using namespace std;#define maxn 50100struct node{ int val,pos;}a[maxn],q[maxn];int ans[maxn];int cmp(node x,node y){ if(x.val==y.val) return x.pos<y.pos; return x.val>y.val;}int main(){ int n,m; while(scanf("%d%d",&n,&m)!=EOF) { int i; map<int,int> match; for(i=0;i<n;i++) {scanf("%d",&a[i].val);a[i].pos=i;match[i]=0;} sort(a,a+n,cmp); for(i=0;i<m;i++) {scanf("%d",&q[i].val);q[i].pos=i;} sort(q,q+m,cmp); int num=0; int st=0; //cout<<"sort a:"<<endl;for(i=0;i<n;i++) cout<<a[i].val<<" ";cout<<endl; //cout<<"sort q:"<<endl;for(i=0;i<m;i++) cout<<q[i].val<<" ";cout<<endl; for(i=0;i<m;i++) { //cout<<"a[st]="<<a[st].val<<endl; while(a[st].val>q[i].val&&st<n) { num++; if(match[a[st].pos+1]==1||match[a[st].pos-1]==1) num--; if(match[a[st].pos+1]==1&&match[a[st].pos-1]==1) num--; match[a[st].pos]=1;//printf("pos=%d val=%d\n",a[st].pos,a[st].val); st++; // printf("***%d",num); }//printf("\n"); ans[q[i].pos]=num;//printf("num=%d\n",num); } for(i=0;i<m;i++) printf("%d\n",ans[i]); } return 0;}
0 0
- HDU 5200
- hdu-5200
- HDU 5200 Trees
- hdu 5200 Trees
- hdu 5200(离线处理)
- hdu
- hdu
- HDU
- hdu ()
- hdu
- hdu
- HDU
- HDU
- hdu
- hdu
- HDU
- Hdu
- hdu
- 半小时教你使用hexo建立一个漂亮的个人博客
- 程序设计C 实验三 题目四 数字根的计算(0284)
- IIS6应用程序池配置研究
- 做简历(4-6)
- poj 3480 John anti-SG博弈
- HDU 5200
- 解决Loadrunner报not writing pre_cci.ci问题
- 最小公倍数和最大公约数(新解法)
- 设计模式是什么
- python中有关赋值的问题
- HTML第十三节(CSS基础详解二)
- 项目:网上报修系统(4-7~4-12)
- Android编译服务器的管理之一:搭建环境
- 面试技巧