北大ACM2104——K-th Number

题目的意思是:给你一个N个数的数组,有M条询问,每一次输入3个数,i, j ,k  意思是数组中从第 i 个到第 j 个中的数从小到大排序,第k个数是哪个?输出来。

题目时间限制是20000MS,相对比较宽松,但是如果你用普通的方法来做,还是超时,没询问一次,你就要排一次序,消耗很多时间。我们可以想另外一种方法,也就是只用排一次序的。

我们可以用这样的方法:每一个数,输入的时候,记录它原本的位置,然后将数列排序,询问的时候,只需要遍历一次整个数组,找原本坐标在 i 和 j 之间的的第 k 个,就可以了。

这样的算法跑了 7100 MS。

下面的是AC的代码:

#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;

class data                   //一个是数,一个是原本位置
{
public:
	int num;
	int id;
};
data D[100005];

int cmp(const data &a, const data &b)  //从小到大排
{
	return a.num < b.num;
}

int main()
{
	int m, n;
	int a, b, c;
	while(scanf("%d%d", &n, &m) != EOF)
	{
		for(int i = 0; i < n; i++)  //输入
		{
			scanf("%d", &D[i].num);
			D[i].id = i;
		}
		sort(D, D + n, cmp);       //排序
		for(int j = 0; j < m; j++)
		{
			scanf("%d%d%d", &a, &b, &c);
			for(int k = 0; k <= n; k++)
			{
				if(D[k].id >= a - 1 && D[k].id <= b - 1)  //找第 c 个数
					c--;
				if(!c)
				{
					printf("%d\n", D[k].num);
					break;
				}
			}
		}
	}
	return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-02 11:34:13

北大ACM2104——K-th Number的相关文章

ACM-ICPC 2018 沈阳赛区网络预赛 K. Supreme Number

A prime number (or a prime) is a natural number greater than 11 that cannot be formed by multiplying two smaller natural numbers. Now lets define a number NN as the supreme number if and only if each number made up of an non-empty subsequence of all

K Best(最大化平均数)_二分搜索

Description Demy has n jewels. Each of her jewels has some value vi and weight wi. Since her husband John got broke after recent financial crises, Demy has decided to sell some jewels. She has decided that she would keep k best jewels for herself. Sh

Codeforces Round #350 (Div. 2) F. Restore a Number 模拟构造题

F. Restore a Number Vasya decided to pass a very large integer n to Kate. First, he wrote that number as a string, then he appended to the right integer k — the number of digits in n. Magically, all the numbers were shuffled in arbitrary order while

lintcode: ugly number

问题描述 Ugly number is a number that only have factors 3, 5 and 7. Design an algorithm to find the Kth ugly number. The first 5 ugly numbers are 3, 5, 7, 9, 15 - 问题分析 这个题,有多种方法求解,常见的是有辅助数据结构,比如priority_queue,或是常说的最大最小堆也可. 解法一:时间复杂度O(k log k),空间复杂度O(k) 使

Lintcode: Kth Prime Number

Design an algorithm to find the kth number such that the only prime factors are 3, 5, and 7. The eligible numbers are like 3, 5, 7, 9, 15 ... Example If k=4, return 9. Challenge O(n log n) or O(n) time 这是丑数问题(Ugly Number), 思路参看: http://www.cppblog.co

POJ3111 K Best —— 01分数规划 二分法

题目链接:http://poj.org/problem?id=3111 K Best Time Limit: 8000MS   Memory Limit: 65536K Total Submissions: 11380   Accepted: 2935 Case Time Limit: 2000MS   Special Judge Description Demy has n jewels. Each of her jewels has some value vi and weight wi.

poj 3111 K Best (二分搜索之最大化平均值之01分数规划)

Description Demy has n jewels. Each of her jewels has some value vi and weight wi. Since her husband John got broke after recent financial crises, Demy has decided to sell some jewels. She has decided that she would keep k best jewels for herself. Sh

ZOJ 3436 July Number(DFS)

题意   把一个数替换为这个数相邻数字差组成的数  知道这个数只剩一位数  若最后的一位数是7  则称原来的数为 July Number  给你一个区间  求这个区间中July Number的个数 从7开始DFS  位数多的数总能由位数小的数推出 #include <bits/stdc++.h> using namespace std; const int N = 1e6; int july[N], n; set<int> ans; int pw[] = {1, 10, 100,

POJ-2104-K-th Number(函数式线段树)

Description You are working for Macrohard company in data structures department. After failing your previous task about key insertion you were asked to write a new data structure that would be able to return quickly k-th order statistics in the array