日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

【每日一题】4月9日题目精讲 Running Median

發布時間:2023/12/3 编程问答 40 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【每日一题】4月9日题目精讲 Running Median 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

文章目錄

    • 題目:
    • 題意:
    • 題解一:
    • 題解二:

題目:

–>鏈接<—

時間限制:C/C++ 5秒,其他語言10秒 空間限制:C/C++ 65536K,其他語言131072K 64bit
IO Format:%lld

題目描述

For this problem, you will write a program that reads in a sequence of
32-bit signed integers. After each odd-indexed value is read, output
the median (middle value) of the elements received so far.

輸入描述:

The first line of input contains a single integerP(1≤P≤1000), which is the number of data sets that follow. The
first line of each data set contains the data set number, followed by
a space, followed by an odd decimal integer (1≤M≤9999), giving the total number of signed integers to be
processed. The remaining line(s) in the dataset consists of the
values, 10 per line, separated by a single space. The last line in the
dataset may contain less than 10 values.

輸出描述:

For each data set the first line of output contains the data set
number, a single space and the number of medians output (which should
be one-half the number of input values plus one). The output medians
will be on the following lines, 10 per line separated by a single
space. The last line may have less than 10 elements, but at least 1
element. There should be no blank lines in the output.

示例1
輸入
3
1 9
1 2 3 4 5 6 7 8 9
2 9
9 8 7 6 5 4 3 2 1
3 23
23 41 13 22 -3 24 -31 -11 -8 -7
3 5 103 211 -311 -45 -67 -73 -81 -99
-33 24 56
輸出
1 5
1 2 3 4 5
2 5
9 8 7 6 5
3 12
23 23 22 22 13 3 5 5 3 -3
-7 -3

題意:

給你m個數,一次輸入,每當輸入的個數為奇數時,輸出按大小排列最中間的數
比如1 5 6 7 8
一開始輸入1,輸出1
然后輸入1 5,不輸出
輸入1 5 6,輸出5
輸入1 5 6 7,不輸出
輸入1 5 6 7 8,輸出6

題解一:

可以用堆來做
w1為大堆,w1用于存放小值
w2為小堆,w2存放大值
比如上面那個例子1 5 6 7 8
奇數位存在w1,偶數存在w2
如果w1.top()>w2.top(),就是w1的最大比w2的最小值大,就將這兩個值互換,始終保證,w1的值比w2的任意一個都小,這樣無論數據怎么讀入,w1的最大值始終都是最中間的數
看下面模擬:
第一輪:
w1:1
w2:0
二:
w1:1
w2:5
三:
w1:1 6
w2:5
6>5
w1:1 5
w2:6

w1:1 5
w2: 7 6

w1:1 5 8
w2: 7 6
8>6
w1:1 5 6
w2; 7 8
這樣每奇數輪,w1的top位就是答案

#include <iostream> #include<queue> #include<cstdio> using namespace std;priority_queue<int>w1;//默認為大堆,從大到小priority_queue<int,vector<int>,greater<int> >w2;//默認為小堆 int main() {// freopen("in.txt","r",stdin);//ios::sync_with_stdio(0);int n;cin>>n;int case1=0;int p,m,x;int minn,maxx;while(n--){cin>>p>>m;case1=0;printf("%d %d\n",p,m/2+1);for(int i=1;i<=m;i++){cin>>x;if(i%2==1)w1.push(x);else w2.push(x);if(i!=1){minn=w1.top();maxx=w2.top();if(minn>maxx){w1.pop();w2.pop();w1.push(maxx);w2.push(minn);}}if(case1!=0&&case1%10==0){cout<<endl;case1=0;}if(i%2==1){if(i==m)cout<<w1.top();elseprintf("%d ",w1.top());case1++;}}cout<<endl;while(!w1.empty())w1.pop();while(!w2.empty())w2.pop();}return 0; }

(這個題的格式我一開始一直沒注意。。。)

題解二:

我看很多人都是這么做的,但是只能過50%的數據。。包括我自己
我看了清楚姐姐的代碼稍微改一下:
我們在讀入一個數后,直接與w1.top比較,小于就存進去,大了就存w2里
當w1的數量多了,就把堆頂拿出給w2(小根堆)
w2多了就給大根堆
這樣維護出來其實和上面的方法差不多
因為總數是奇數,兩個堆數量一定不一樣,多的那方,堆頂就是答案
代碼:
清楚阿姨(狗頭 )代碼:

#include<bits/stdc++.h> using namespace std; priority_queue<int,vector<int>, greater<int> > q2; //小根堆 ,存較大的一半的數 priority_queue<int> q1; //大根堆 ,存較小的一半的數 int main() {int t;scanf("%d", &t);while(t--){while(!q1.empty()) q1.pop();while(!q2.empty()) q2.pop(); //優先隊列沒有clear函數,要一個一個彈出int m, n;scanf("%d %d", &m ,&n);printf("%d %d\n", m, (n+1)/ 2);int x;scanf("%d", &x);q1.push(x);printf("%d ", x);for(int i = 2; i <= n; i++){int x;scanf("%d", &x);if (x <= q1.top()) q1.push(x); //如果當前值比大根堆堆頂小,說明在小的這二分之一,塞進大根堆else q2.push(x);int num1= q1.size();int num2= q2.size();if (num1 - num2 > 1) //大根堆里面元素多了,把堆頂拿出來塞近小根堆{q2.push(q1.top());q1.pop();}else if(num2 - num1 >1) //小根堆里面元素多了,把堆頂拿出來塞近大根堆{q1.push(q2.top());q2.pop();}if (i % 2 == 1) //目前的元素個數是奇數{num1 = q1.size();num2 = q2.size();if(num1 > num2) printf("%d ", q1.top()); //中位數在大根堆else printf("%d ", q2.top()); //中位數在小根堆if (i % 20 == 19 && i!=n) printf("\n");}}printf("\n");} return 0; }

總結

以上是生活随笔為你收集整理的【每日一题】4月9日题目精讲 Running Median的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。