The K-th largest number(小根堆)

版权声明:本文为博主原创文章,未经博主允许不得转载,谢谢合作! https://blog.csdn.net/cleopard66/article/details/51287586

【题目要求】

Given N integer numbers, find the K-th largest number. 
Requirement: design an algorithm with the time complexity O(NlogK).

【输入要求】

The first line contains N, K. In the following N lines, each line contains an integer value. 
Note that N could be very large ( 1000000 <= N <= 10000000). You should allocate the memory dynamically and design the algorithm with the time complexity O(NlogK).

12 4
7
2
1
5
6
6
4
3
8
8
10
9
【输出要求】

Output the K-th largest number.

8
【我的程序】

#include <iostream>
using namespace std;

long int num, *a;

void swapOp(long int x, long int y)
{
    long int temp=a[x]; a[x]=a[y]; a[y]=temp;
}

void downOp(long int x)
{
    while (x*2<=num)
    {
        x*=2;
        if (x+1<=num && a[x+1]<a[x]) x++;
        if (a[x]<a[x/2]) swapOp(x,x/2); else break;
    }
}

void upOp(long int x)
{
    while (x/2>=1){ if (a[x]<a[x/2]) swapOp(x,x/2); else break; x/=2; }
}

int main()
{
    long int n,k,temp;
    cin>> n>> k;  num=k;  a=new long int [k+1];

    for (long int i=1;i<=k;i++) cin>>a[i];
    for (long int i=k/2; i>=1; i--) downOp(i);

    for (long int i=k+1; i<=n; i++)
    {
        cin>> temp;
        if (temp>a[1])
        {
            swapOp(1,num); num--; downOp(1);
            a[++num]=temp; upOp(num);
        }
    }

    cout<< a[1];
    return 0;
}

Largest prime number ever is found

12-03

http://www.newscientist.com/news/news.jsp?id=ns99994438rnrnLargest prime number ever is found rn rn rn15:11 02 December 03 rn rnNewScientist.com news service rn rnA 26-year-old graduate student in the US has made mathematical history by discovering the largest known prime number.rnrnThe new number is 6,320,430 digits long. It took just over two years to find using a distributed network of more than 200,000 computers.rnrnMichael Shafer a chemical engineering student at Michigan State University used his office computer to contribute spare processing power to the Great Internet Mersenne Prime Search (GIMPS). The project has more than 60,000 volunteers from all over the world taking part.rnrn"I had just finished a meeting with my advisor when I saw the computer had found the new prime," Shafer says. "After a short victory dance, I called up my wife and friends involved with GIMPS to share the great news." rnrnPrime numbers are positive integers that can only be divided by themselves and one. Mersenne primes are an especially rare type of prime that take the form 2 p-1, where p is also a prime number. The new number can be represented as 220,996,011-1. It is only the 40th Mersenne prime to have ever been found.rnrnrnBuilding blocks rnrnrnMersenne primes were first discussed by Euclid in 350 BC and have been central to the branch of mathematics known as number theory ever since. They are named after a 17th century French monk who first came up with an important conjecture about which values of p would yield a prime.rnrnPrimes are the building blocks of all positive numbers. They have practical uses too, for example by providing a way of exchanging the cryptographic keys that keep internet communications secure from eavesdropping. However, despite their significance, mathematicians do not understand the way prime numbers are distributed making it very difficult to identify new primes.rnrnMarcus du Sautoy, a mathematician at Oxford University and author of The Music of the Primes, says the discovery is unlikely to add much to our understanding of the way primes are distributed but is still significant. rnrn"It's a really good measure of what our computational capabilities are," he told New Scientist. "It's a really fun project. Everyone gets a different bit of the number universe to look at. It's a bit like the lottery."rnrn The World's No.1 Science & Technology News Service rn rn rn rnLargest prime number ever is found rn rn rn15:11 02 December 03 rn rnNewScientist.com news service rn rnA 26-year-old graduate student in the US has made mathematical history by discovering the largest known prime number.rnrnThe new number is 6,320,430 digits long. It took just over two years to find using a distributed network of more than 200,000 computers.rnrnMichael Shafer a chemical engineering student at Michigan State University used his office computer to contribute spare processing power to the Great Internet Mersenne Prime Search (GIMPS). The project has more than 60,000 volunteers from all over the world taking part.rnrn"I had just finished a meeting with my advisor when I saw the computer had found the new prime," Shafer says. "After a short victory dance, I called up my wife and friends involved with GIMPS to share the great news." rnrnPrime numbers are positive integers that can only be divided by themselves and one. Mersenne primes are an especially rare type of prime that take the form 2 p-1, where p is also a prime number. The new number can be represented as 220,996,011-1. It is only the 40th Mersenne prime to have ever been found.rnrnrnBuilding blocks rnrnrnMersenne primes were first discussed by Euclid in 350 BC and have been central to the branch of mathematics known as number theory ever since. They are named after a 17th century French monk who first came up with an important conjecture about which values of p would yield a prime.rnrnPrimes are the building blocks of all positive numbers. They have practical uses too, for example by providing a way of exchanging the cryptographic keys that keep internet communications secure from eavesdropping. However, despite their significance, mathematicians do not understand the way prime numbers are distributed making it very difficult to identify new primes.rnrnMarcus du Sautoy, a mathematician at Oxford University and author of The Music of the Primes, says the discovery is unlikely to add much to our understanding of the way primes are distributed but is still significant. rnrn"It's a really good measure of what our computational capabilities are," he told New Scientist. "It's a really fun project. Everyone gets a different bit of the number universe to look at. It's a bit like the lottery."rnrnMathematical curiosity rn rnThe GIMPS project uses a central computer server and free software to coordinate the activity of all its contributors. Contributing machines are each allocated different prime number candidates to test.rnrnSome people contribute to GIMPS out of mathematical curiosity or to test their computer hardware, while others just hope to go down in history as the discoverer of a massive prime. There is also a financial incentive with the Electronic Frontier Foundation, a non-profit US group, offering a $100,000 prize for the discovery of the first prime with 10 million digits.rnrnShafer's discovery was made on 17 November but it was not independently verified until now. "It's humbling to see so many people of varied lands, ages and vocations volunteering for this fun and amazing project," says Scott Kurowski, whose company Entropia manages the GIMPS server. rnrn"There are more primes out there," adds George Woltman, who started the GIMPS project in 1996. "And anyone with an internet-connected computer can participate."rn rn rnWill Knightrnrn 论坛

没有更多推荐了,返回首页