Black Box Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 8637   Accepted: 3542 Description Our Black Box represents a primitive database. It can save an integer array and has a special i variable. At the initial moment Black Box is empt…
求一组N个数中的第k个最大者,设k=N/2. import java.util.Random; public class K_Max { /** * @param args */ //求第K大的数,保证K大于等于1,小于等于array.length/2哦 public static int TopK(int array[],int K) { int topk[] = new int [K]; for(int i = 0; i<topk.length;i++) topk[i] = array[i]…
问题描述 无序数组求第K大的数,其中K从1开始算. 例如:[0,3,1,8,5,2]这个数组,第2大的数是5 OJ可参考:LeetCode_0215_KthLargestElementInAnArray 堆解法 设置一个小根堆,先把前K个数放入小根堆,对于这前K个数来说,堆顶元素一定是第K大的数,接下来的元素继续入堆,但是每入一个就弹出一个,最后,堆顶元素就是整个数组的第K大元素.代码如下: public static int findKthLargest3(int[] nums, int k)…
这个题目一开始感觉还是有点难的,这个模数这么大,根本就不知道怎么写,然后去搜了题解,知道了怎么去求当x很大的时候x的平方对一个数取模怎么样不会爆掉. 然后还顺便发现了一个规律就是当一个数更新一定次数之后就不会变化了. 然后这个题目就很好写了,就是一个区间求和和一个区间修改.现在还不确定如果不加一个找到的规律是不是会超时. 现在写完了,写的过程你会发现,这个每次必须更新到叶节点才可以,不然这个是有问题的,因为我们要求和, 所以如果不更新到叶节点,那就无法求和,然后我们再计算一下复杂度,如果直接是m…
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication9 { class Program { static void Main(string[] args) { // sql如下,我把它翻译成代码,加深理解 // SELECT MAX(id)FROM sys_men…
//下面两种part效率比较:相同运算量下part比part2快5倍左右,part2写法简单但是效率低 #include "stdafx.h" #include <iostream> #include <stdio.h> using namespace std; int part(int *arr, int l , int r) { c_num += r - l; swap(arr[r],arr[l+rand()%(r-l)]); int q = r--; wh…
传送门 The k-th Largest Group Time Limit: 2000MS   Memory Limit: 131072K Total Submissions: 8690   Accepted: 2847 Description Newman likes playing with cats. He possesses lots of cats in his home. Because the number of cats is really huge, Newman wants…
问题描述:给定一系列数{a1,a2,...,an},这些数无序的,现在求第k大的数. 看到这个问题,首先想到的是先排序,然后直接输出第k大的数,于是得到啦基于排序的算法 算法一: #include<iostream>#include<algorithm>using namespace std;bool cmp(int a, int b){ return a > b; }int main(){ int k; int a[9] = { 6, 5, 9, 8, 2, 1, 7, 3…
传送门 Description There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)). Example 1: nums1 = [1, 3] nums2 = [2] The median is 2.0 Exampl…
题目链接:第k个数 题意:求n个数中第k小的数 题解: //由快速排序算法演变而来的快速选择算法 #include<iostream> using namespace std; const int N=1e5+10; int a[N]; //k是整个区间中的第k小的数. void quick(int l,int r,int k) {//快速选择算法保证每次递归时都递归到答案所在的区间. int i=l-1,j=r+1,x=a[l+r>>1]; if(l>=r)return a…