Poj 2182-Lost Cows(Treap||樹狀數組+二分答案),pojtreap

來源:互聯網
上載者:User

Poj 2182-Lost Cows(Treap||樹狀數組+二分答案),pojtreap

題目連結:點擊開啟連結

題意:n個牛編號為1-n 現在編號順序已經打亂,給出a[i] ,a[i] 代表i位置前面有幾個小於它的編號,求編號順序。

倒著推,對於最後一個a[i] , 最後位置編號肯定是 a[i]+1,然後在1-n個編號中刪掉當前編號,繼續往前推。。即求第 a[i]+1小數,初始容器中有n個數(1-n) ,每求出來一個就刪掉。先用平衡樹水了一發。。明天寫樹狀數組解法。

#include <algorithm>#include <iostream>#include <cstring>#include <cstdlib>#include <string>#include <cctype>#include <vector>#include <cstdio>#include <cmath>#include <queue>#include <stack>#include <map>#include <set>#define maxn 8005#define _ll __int64#define ll long long#define INF 0x3f3f3f3f#define Mod 1<<40+10#define pp pair<int,int>#define ull unsigned long longusing namespace std;int n,ans[maxn];struct node{node *ch[2];int r,v,s;node (){}node (int v){ch[0]=NULL;ch[1]=NULL;r=rand();this->v=v;s=1;}bool operator <(const node& c)const{return r<c.r;}int cmp(int x)const {if(x==v)return -1;return x<v?0:1;}void maintain(){s=1;if(ch[0]!=NULL)s+=ch[0]->s;if(ch[1]!=NULL)s+=ch[1]->s;}};void rotate(node* &o,int d){node *k=o->ch[d^1];o->ch[d^1]=k->ch[d];k->ch[d]=o;o->maintain();k->maintain();o=k;}void insert(node* &o,int x){if(o==NULL) o=new node(x);else{int d=o->cmp(x);insert(o->ch[d],x);if(o->ch[d]->r>o->r)rotate(o,d^1);}o->maintain();}void remove(node* &o,int x){int d=o->cmp(x);if(d==-1){node* u=o;if(o->ch[0]!=NULL&&o->ch[1]!=NULL){int d2=(o->ch[0]->r>o->ch[1]->r?1:0);rotate(o,d2);remove(o->ch[d2],x);}else{if(o->ch[0]==NULL)o=o->ch[1];else o=o->ch[0];delete u;}}elseremove(o->ch[d],x);if(o!=NULL) o->maintain();}bool find(node* o,int x){while(o!=NULL){int d=o->cmp(x);if(d==-1)return 1;else o=o->ch[d];}return 0;}int find_kth(node* o,int k){if(o==NULL||k>o->s)return -1;int s=(o->ch[0]==NULL?0:o->ch[0]->s);if(k==s+1)return o->v;else if(k<=s) return find_kth(o->ch[0],k);else return find_kth(o->ch[1],k-s-1);}void solve(){node *root=NULL;for(int i=1;i<=n;i++)insert(root,i);for(int i=0;i<n-1;i++)scanf("%d",&ans[i]);for(int i=n-2;i>=0;i--){//cout<<"ans["<<i<<"]=="<<ans[i]<<endl;ans[i]=find_kth(root,ans[i]+1);//cout<<"ans["<<i<<"]=="<<ans[i]<<endl;remove(root,ans[i]);}printf("%d\n",root->v);for(int i=0;i<=n-2;i++)printf("%d\n",ans[i]);}int main(){while(~scanf("%d",&n))solve();return 0;}

樹狀數組+二分逼近。。Log(n)^2 還有一種Log(n)的寫法至今還沒會。。

#include <algorithm>#include <iostream>#include <cstring>#include <cstdlib>#include <string>#include <cctype>#include <vector>#include <cstdio>#include <cmath>#include <queue>#include <stack>#include <map>#include <set>#define maxn 8003#define _ll __int64#define ll long long#define INF 0x3f3f3f3f#define Mod 10000007#define pp pair<int,int>#define ull unsigned long longusing namespace std;int s[maxn],a[maxn],n;int lowbit(int x){return x&(-x);}void update(int x,int num){while(x<=maxn){s[x]+=num;x+=lowbit(x);}}int getsum(int x){int ans=0;while(x>=1){ans+=s[x];x-=lowbit(x);}return ans;}int find_kth(int k){int l=1,r=maxn,mid;while(l<=r){mid=(l+r)/2;if(getsum(mid)>=k)r=mid-1;elsel=mid+1;}if(l>maxn)return -1;return l;}void solve(){for(int i=1;i<=n;i++)update(i,1);//for(int i=1;i<=n;i++)cout<<s[i]<<endl;for(int i=0;i<n-1;i++)scanf("%d",a+i);for(int i=n-2;i>=0;i--){a[i]=find_kth(a[i]+1);update(a[i],-1);}printf("%d\n",find_kth(1));for(int i=0;i<n-1;i++)printf("%d\n",a[i]);}int main(){while (~scanf("%d", &n)) {solve();}return 0;}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.