Ural1306 sequence median (card memory)

Source: Internet
Author: User

Returns the N number, n <= 250000, calculates the median of the N number, and the memory limit is 1 MB.

Card memory God question, with the array to save just 1 MB, coupled with the runtime consumption of memory, immediately burst. Therefore, we use the priority queue to store half of the data. Some code on the Internet uses priority_queue to blow up the memory. The 125000-length array I saved, plus the STL make_heap ()

# Include <cstdio> # include <queue> using namespace STD; int A [125010]; int main () {int n, x; double ans; scanf ("% d ", & N); For (INT I = 0; I <n/2 + 1; I ++) scanf ("% d", & A [I]); make_heap (A, A + n/2 + 1); For (INT I = n/2 + 1; I <n; I ++) {scanf ("% d ", & X); If (x <A [0]) {pop_heap (A, A + n/2 + 1); A [n/2] = x; push_heap (, A + n/2 + 1) ;}}if (N & 1) ans = (double) A [0]; else {ans = (double) A [0]; pop_heap (A, A + n/2 + 1); ans + = (double) A [0]; ans = ANS/2.0;} printf ("%. 1lf \ n ", ANS );}


Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

Ural1306 sequence median (card memory)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.