1418 動態順序統計量

來源:互聯網
上載者:User

 

描述

利用二叉排序樹,設計一個能支援快速順序統計量操作的資料結構。這個資料結構支援三種操作,Insert x 往資料集合中插入一個數x, Delete x 刪除資料集合中的一個數x, Order x 輸出目前資料集合中第x大的數。(保證資料集合中的所有元素均不相等,本題資料均為int)

輸入

第一行為初始的資料集合, 元素之間用空格隔開,第二行為操作的條數m,下面的m行有m條指令,指令的格式參照題目描述。

輸出

對應每一個order x指令,輸出目前資料集合中第x大的數。

 

簡單的尋找即可

#include <stdio.h>#include <string.h>char s[100000];int a[100000];main(){int number;int length,i,j;int p;int up;int temp;int count;char op;up=0;gets(s);length=strlen(s);i=0;p=0;for(i=0;i<length;i++){if(s[i]!=','){p=0;while(s[i] >= '0' && s[i] <= '9'&&i<length){p = p * 10 + s[i] - '0';i++;}a[up++]=p;}elsecontinue;}for(i=0;i<up;i++)for(j=i+1;j<up;j++){if(a[i]>a[j]){temp=a[i];a[i]=a[j];a[j]=temp;}}scanf("%d",&count);while(count--){getchar();scanf("%c",&op);if(op=='O'){for(i=0;i<5;i++)scanf("%c",&op);scanf("%d",&temp);printf("%d\n",a[temp-1]);}if(op=='I'){for(i=0;i<6;i++)                      scanf("%c",&op);scanf("%d",&temp);if(temp>a[up-1])a[up++]=temp;else{for(i=0;i<up;i++){if(a[i]>temp){  j=i;   break;}elsecontinue;}for(i=up;i>j;i--)a[i]=a[i-1];a[j]=temp;                   up++;}}if(op=='D'){for(i=0;i<6;i++)scanf("%c",&op);scanf("%d",&temp);for(i=0;i<up;i++)if(a[i]==temp)break;for(j=i;j<up-1;j++)a[j]=a[j+1];up--;}}}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.