【“盛大遊戲杯”第15屆上海大學程序設計聯賽 H】【水題預處理】調和序列

調和序列

發佈時間: 2017年7月9日 18:17   最後更新: 2017年7月9日 21:05   時間限制: 1000ms   內存限制: 128M

給定一個長度爲n的非負整數序列,下標爲0,1,…,n1

定義:sequence(K): 由下標爲K的倍數組成的子序列,即下標爲0,K,2K,...,[n1/k]k

query(K,S): 詢問sequence(K)中的第S大的數字

第一行一個整數T,表示測試組數。
對於每組數據,第一行輸入兩個整數n,m1<=n<=200001<=m<=100000n表示序列的長度,m表示詢問個數。
接下來一行是n個整數a0,a1,..,an1,0<=ai<231i=0,1,,n1,表示序列。
接下來m行,每行兩個整數K,S 
0<K<=1091<=S<=n

每組數據對於每個詢問輸出一行,若sequence(K)的元素個數小於S,輸出1;否則輸出query(K,S)

1
5 2
2 5 3 4 1
2 4
2 1
-1
 3

#include<stdio.h>
#include<iostream>
#include<string.h>
#include<string>
#include<ctype.h>
#include<math.h>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<bitset>
#include<algorithm>
#include<time.h>
using namespace std;
void fre() { freopen("c://test//input.in", "r", stdin); freopen("c://test//output.out", "w", stdout); }
#define MS(x, y) memset(x, y, sizeof(x))
#define ls o<<1
#define rs o<<1|1
typedef long long LL;
typedef unsigned long long UL;
typedef unsigned int UI;
template <class T1, class T2>inline void gmax(T1 &a, T2 b) { if (b > a)a = b; }
template <class T1, class T2>inline void gmin(T1 &a, T2 b) { if (b < a)a = b; }
const int N = 20020, M = 0, Z = 1e9 + 7, inf = 0x3f3f3f3f;
template <class T1, class T2>inline void gadd(T1 &a, T2 b) { a = (a + b) % Z; }
int casenum, casei;
int n, m;
int a[N];
vector<int>vt[N];
int main()
{
	scanf("%d", &casenum);
	for (casei = 1; casei <= casenum; ++casei)
	{
		scanf("%d%d", &n, &m);
		for (int i = 0; i < n; ++i)
		{
			scanf("%d", &a[i]);
			vt[i + 1].clear();
		}
		for (int K = 1; K <= n; ++K)
		{
			for (int x = 0; x < n; x += K)
			{
				vt[K].push_back(a[x]);
			}
			sort(vt[K].begin(), vt[K].end());
			reverse(vt[K].begin(), vt[K].end());
		}
		while (m--)
		{
			int K, S; scanf("%d%d", &K, &S);
			if (K > n)
			{
				if (S == 1)
				{
					printf("%d\n", a[0]);
				}
				else
				{
					puts("-1");
				}
				continue;
			}
			if (vt[K].size() < S)
			{
				puts("-1");
			}
			else
			{
				printf("%d\n", vt[K][S - 1]);
			}
		}
	}
	return 0;
}
/*
【題意】
http://acmoj.shu.edu.cn/problem/417/

【分析】
因爲for(i=1~n)for(j=i的倍數 && j<=n)的複雜度是nlogn
所以直接預處理就好了。
但是因爲這裏下標是從0開始,小心一下K比較大特殊情況就好啦。

*/


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章