Search is not available for this dataset
name
stringlengths 2
88
| description
stringlengths 31
8.62k
| public_tests
dict | private_tests
dict | solution_type
stringclasses 2
values | programming_language
stringclasses 5
values | solution
stringlengths 1
983k
|
|---|---|---|---|---|---|---|
1227_D1. Optimal Subsequences (Easy Version)
|
This is the easier version of the problem. In this version 1 β€ n, m β€ 100. You can hack this problem only if you solve and lock both problems.
You are given a sequence of integers a=[a_1,a_2,...,a_n] of length n. Its subsequence is obtained by removing zero or more elements from the sequence a (they do not necessarily go consecutively). For example, for the sequence a=[11,20,11,33,11,20,11]:
* [11,20,11,33,11,20,11], [11,20,11,33,11,20], [11,11,11,11], [20], [33,20] are subsequences (these are just some of the long list);
* [40], [33,33], [33,20,20], [20,20,11,11] are not subsequences.
Suppose that an additional non-negative integer k (1 β€ k β€ n) is given, then the subsequence is called optimal if:
* it has a length of k and the sum of its elements is the maximum possible among all subsequences of length k;
* and among all subsequences of length k that satisfy the previous item, it is lexicographically minimal.
Recall that the sequence b=[b_1, b_2, ..., b_k] is lexicographically smaller than the sequence c=[c_1, c_2, ..., c_k] if the first element (from the left) in which they differ less in the sequence b than in c. Formally: there exists t (1 β€ t β€ k) such that b_1=c_1, b_2=c_2, ..., b_{t-1}=c_{t-1} and at the same time b_t<c_t. For example:
* [10, 20, 20] lexicographically less than [10, 21, 1],
* [7, 99, 99] is lexicographically less than [10, 21, 1],
* [10, 21, 0] is lexicographically less than [10, 21, 1].
You are given a sequence of a=[a_1,a_2,...,a_n] and m requests, each consisting of two numbers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j). For each query, print the value that is in the index pos_j of the optimal subsequence of the given sequence a for k=k_j.
For example, if n=4, a=[10,20,30,20], k_j=2, then the optimal subsequence is [20,30] β it is the minimum lexicographically among all subsequences of length 2 with the maximum total sum of items. Thus, the answer to the request k_j=2, pos_j=1 is the number 20, and the answer to the request k_j=2, pos_j=2 is the number 30.
Input
The first line contains an integer n (1 β€ n β€ 100) β the length of the sequence a.
The second line contains elements of the sequence a: integer numbers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9).
The third line contains an integer m (1 β€ m β€ 100) β the number of requests.
The following m lines contain pairs of integers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j) β the requests.
Output
Print m integers r_1, r_2, ..., r_m (1 β€ r_j β€ 10^9) one per line: answers to the requests in the order they appear in the input. The value of r_j should be equal to the value contained in the position pos_j of the optimal subsequence for k=k_j.
Examples
Input
3
10 20 10
6
1 1
2 1
2 2
3 1
3 2
3 3
Output
20
10
20
10
20
10
Input
7
1 2 1 3 1 2 1
9
2 1
2 2
3 1
3 2
3 3
1 1
7 1
7 7
7 4
Output
2
3
2
3
2
3
1
1
3
Note
In the first example, for a=[10,20,10] the optimal subsequences are:
* for k=1: [20],
* for k=2: [10,20],
* for k=3: [10,20,10].
|
{
"input": [
"3\n10 20 10\n6\n1 1\n2 1\n2 2\n3 1\n3 2\n3 3\n",
"7\n1 2 1 3 1 2 1\n9\n2 1\n2 2\n3 1\n3 2\n3 3\n1 1\n7 1\n7 7\n7 4\n"
],
"output": [
"20\n10\n20\n10\n20\n10\n",
"2\n3\n2\n3\n2\n3\n1\n1\n3\n"
]
}
|
{
"input": [
"2\n1 10\n3\n2 2\n2 1\n1 1\n",
"2\n3922 3922\n3\n2 2\n2 1\n1 1\n",
"1\n1000000000\n1\n1 1\n",
"1\n1\n3\n1 1\n1 1\n1 1\n",
"5\n3 1 4 1 2\n15\n5 5\n5 4\n5 3\n5 2\n5 1\n4 4\n4 3\n4 2\n4 1\n3 3\n3 2\n3 1\n2 2\n2 1\n1 1\n",
"2\n392222 322\n3\n2 2\n2 1\n1 1\n"
],
"output": [
"10\n1\n10\n",
"3922\n3922\n3922\n",
"1000000000\n",
"1\n1\n1\n",
"2\n1\n4\n1\n3\n2\n4\n1\n3\n2\n4\n3\n4\n3\n4\n",
"322\n392222\n392222\n"
]
}
|
CORRECT
|
python3
|
n = int(input())
arr = list(map(int, input().split()))
m = int(input())
q = []
for i in range(m):
a, b = map(int, input().split())
q.append((a, b))
def f(arr, m):
vis = [False] * len(arr)
arr2 = []
for i in range(len(arr)):
arr2.append([arr[i], n - i])
arr2.sort(reverse=True)
arr2 = arr2[:m]
for i in range(len(arr2)):
arr2[i][1] = n - arr2[i][1]
arr2.sort(key=lambda x: x[1])
res = []
for i in range(len(arr2)):
res.append(arr2[i][0])
return res
for i in q:
x = f(arr, i[0])
print(x[i[1] - 1])
|
1227_D1. Optimal Subsequences (Easy Version)
|
This is the easier version of the problem. In this version 1 β€ n, m β€ 100. You can hack this problem only if you solve and lock both problems.
You are given a sequence of integers a=[a_1,a_2,...,a_n] of length n. Its subsequence is obtained by removing zero or more elements from the sequence a (they do not necessarily go consecutively). For example, for the sequence a=[11,20,11,33,11,20,11]:
* [11,20,11,33,11,20,11], [11,20,11,33,11,20], [11,11,11,11], [20], [33,20] are subsequences (these are just some of the long list);
* [40], [33,33], [33,20,20], [20,20,11,11] are not subsequences.
Suppose that an additional non-negative integer k (1 β€ k β€ n) is given, then the subsequence is called optimal if:
* it has a length of k and the sum of its elements is the maximum possible among all subsequences of length k;
* and among all subsequences of length k that satisfy the previous item, it is lexicographically minimal.
Recall that the sequence b=[b_1, b_2, ..., b_k] is lexicographically smaller than the sequence c=[c_1, c_2, ..., c_k] if the first element (from the left) in which they differ less in the sequence b than in c. Formally: there exists t (1 β€ t β€ k) such that b_1=c_1, b_2=c_2, ..., b_{t-1}=c_{t-1} and at the same time b_t<c_t. For example:
* [10, 20, 20] lexicographically less than [10, 21, 1],
* [7, 99, 99] is lexicographically less than [10, 21, 1],
* [10, 21, 0] is lexicographically less than [10, 21, 1].
You are given a sequence of a=[a_1,a_2,...,a_n] and m requests, each consisting of two numbers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j). For each query, print the value that is in the index pos_j of the optimal subsequence of the given sequence a for k=k_j.
For example, if n=4, a=[10,20,30,20], k_j=2, then the optimal subsequence is [20,30] β it is the minimum lexicographically among all subsequences of length 2 with the maximum total sum of items. Thus, the answer to the request k_j=2, pos_j=1 is the number 20, and the answer to the request k_j=2, pos_j=2 is the number 30.
Input
The first line contains an integer n (1 β€ n β€ 100) β the length of the sequence a.
The second line contains elements of the sequence a: integer numbers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9).
The third line contains an integer m (1 β€ m β€ 100) β the number of requests.
The following m lines contain pairs of integers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j) β the requests.
Output
Print m integers r_1, r_2, ..., r_m (1 β€ r_j β€ 10^9) one per line: answers to the requests in the order they appear in the input. The value of r_j should be equal to the value contained in the position pos_j of the optimal subsequence for k=k_j.
Examples
Input
3
10 20 10
6
1 1
2 1
2 2
3 1
3 2
3 3
Output
20
10
20
10
20
10
Input
7
1 2 1 3 1 2 1
9
2 1
2 2
3 1
3 2
3 3
1 1
7 1
7 7
7 4
Output
2
3
2
3
2
3
1
1
3
Note
In the first example, for a=[10,20,10] the optimal subsequences are:
* for k=1: [20],
* for k=2: [10,20],
* for k=3: [10,20,10].
|
{
"input": [
"3\n10 20 10\n6\n1 1\n2 1\n2 2\n3 1\n3 2\n3 3\n",
"7\n1 2 1 3 1 2 1\n9\n2 1\n2 2\n3 1\n3 2\n3 3\n1 1\n7 1\n7 7\n7 4\n"
],
"output": [
"20\n10\n20\n10\n20\n10\n",
"2\n3\n2\n3\n2\n3\n1\n1\n3\n"
]
}
|
{
"input": [
"2\n1 10\n3\n2 2\n2 1\n1 1\n",
"2\n3922 3922\n3\n2 2\n2 1\n1 1\n",
"1\n1000000000\n1\n1 1\n",
"1\n1\n3\n1 1\n1 1\n1 1\n",
"5\n3 1 4 1 2\n15\n5 5\n5 4\n5 3\n5 2\n5 1\n4 4\n4 3\n4 2\n4 1\n3 3\n3 2\n3 1\n2 2\n2 1\n1 1\n",
"2\n392222 322\n3\n2 2\n2 1\n1 1\n"
],
"output": [
"10\n1\n10\n",
"3922\n3922\n3922\n",
"1000000000\n",
"1\n1\n1\n",
"2\n1\n4\n1\n3\n2\n4\n1\n3\n2\n4\n3\n4\n3\n4\n",
"322\n392222\n392222\n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const long long int mod = 1e9 + 7;
const long double error = 2e-6;
const long double PI = acosl(-1);
inline long long int MOD(long long int x, long long int m = mod) {
long long int y = x % m;
return (y >= 0) ? y : y + m;
}
const int inf = 1e9;
const long long int infl = 1061109567;
const int nmax = 1000 + 10;
bool cmp(pair<int, int> p1, pair<int, int> p2) {
if (p1.first != p2.first) return p1.first < p2.first;
return p1.second > p2.second;
}
int main() {
int n;
cin >> n;
int i;
vector<pair<int, int> > vc(n);
vector<int> ara;
for (i = 0; i < n; i++) {
int xx;
cin >> xx;
ara.push_back(xx);
vc[i] = {xx, i};
}
sort(vc.begin(), vc.end(), cmp);
int m;
cin >> m;
for (i = 1; i <= m; i++) {
int k, pos;
cin >> k >> pos;
vector<int> sv;
int j;
for (j = n - 1; j >= n - k; j--) {
sv.push_back(vc[j].second);
}
sort(sv.begin(), sv.end());
cout << ara[sv[pos - 1]] << "" << endl;
}
return 0;
}
|
1227_D1. Optimal Subsequences (Easy Version)
|
This is the easier version of the problem. In this version 1 β€ n, m β€ 100. You can hack this problem only if you solve and lock both problems.
You are given a sequence of integers a=[a_1,a_2,...,a_n] of length n. Its subsequence is obtained by removing zero or more elements from the sequence a (they do not necessarily go consecutively). For example, for the sequence a=[11,20,11,33,11,20,11]:
* [11,20,11,33,11,20,11], [11,20,11,33,11,20], [11,11,11,11], [20], [33,20] are subsequences (these are just some of the long list);
* [40], [33,33], [33,20,20], [20,20,11,11] are not subsequences.
Suppose that an additional non-negative integer k (1 β€ k β€ n) is given, then the subsequence is called optimal if:
* it has a length of k and the sum of its elements is the maximum possible among all subsequences of length k;
* and among all subsequences of length k that satisfy the previous item, it is lexicographically minimal.
Recall that the sequence b=[b_1, b_2, ..., b_k] is lexicographically smaller than the sequence c=[c_1, c_2, ..., c_k] if the first element (from the left) in which they differ less in the sequence b than in c. Formally: there exists t (1 β€ t β€ k) such that b_1=c_1, b_2=c_2, ..., b_{t-1}=c_{t-1} and at the same time b_t<c_t. For example:
* [10, 20, 20] lexicographically less than [10, 21, 1],
* [7, 99, 99] is lexicographically less than [10, 21, 1],
* [10, 21, 0] is lexicographically less than [10, 21, 1].
You are given a sequence of a=[a_1,a_2,...,a_n] and m requests, each consisting of two numbers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j). For each query, print the value that is in the index pos_j of the optimal subsequence of the given sequence a for k=k_j.
For example, if n=4, a=[10,20,30,20], k_j=2, then the optimal subsequence is [20,30] β it is the minimum lexicographically among all subsequences of length 2 with the maximum total sum of items. Thus, the answer to the request k_j=2, pos_j=1 is the number 20, and the answer to the request k_j=2, pos_j=2 is the number 30.
Input
The first line contains an integer n (1 β€ n β€ 100) β the length of the sequence a.
The second line contains elements of the sequence a: integer numbers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9).
The third line contains an integer m (1 β€ m β€ 100) β the number of requests.
The following m lines contain pairs of integers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j) β the requests.
Output
Print m integers r_1, r_2, ..., r_m (1 β€ r_j β€ 10^9) one per line: answers to the requests in the order they appear in the input. The value of r_j should be equal to the value contained in the position pos_j of the optimal subsequence for k=k_j.
Examples
Input
3
10 20 10
6
1 1
2 1
2 2
3 1
3 2
3 3
Output
20
10
20
10
20
10
Input
7
1 2 1 3 1 2 1
9
2 1
2 2
3 1
3 2
3 3
1 1
7 1
7 7
7 4
Output
2
3
2
3
2
3
1
1
3
Note
In the first example, for a=[10,20,10] the optimal subsequences are:
* for k=1: [20],
* for k=2: [10,20],
* for k=3: [10,20,10].
|
{
"input": [
"3\n10 20 10\n6\n1 1\n2 1\n2 2\n3 1\n3 2\n3 3\n",
"7\n1 2 1 3 1 2 1\n9\n2 1\n2 2\n3 1\n3 2\n3 3\n1 1\n7 1\n7 7\n7 4\n"
],
"output": [
"20\n10\n20\n10\n20\n10\n",
"2\n3\n2\n3\n2\n3\n1\n1\n3\n"
]
}
|
{
"input": [
"2\n1 10\n3\n2 2\n2 1\n1 1\n",
"2\n3922 3922\n3\n2 2\n2 1\n1 1\n",
"1\n1000000000\n1\n1 1\n",
"1\n1\n3\n1 1\n1 1\n1 1\n",
"5\n3 1 4 1 2\n15\n5 5\n5 4\n5 3\n5 2\n5 1\n4 4\n4 3\n4 2\n4 1\n3 3\n3 2\n3 1\n2 2\n2 1\n1 1\n",
"2\n392222 322\n3\n2 2\n2 1\n1 1\n"
],
"output": [
"10\n1\n10\n",
"3922\n3922\n3922\n",
"1000000000\n",
"1\n1\n1\n",
"2\n1\n4\n1\n3\n2\n4\n1\n3\n2\n4\n3\n4\n3\n4\n",
"322\n392222\n392222\n"
]
}
|
CORRECT
|
python3
|
import sys
input = sys.stdin.buffer.readline
mx = 2*10**5 + 1
bit = [0]*(mx)
def add(idx):
idx += 1
while idx < mx:
bit[idx] += 1
idx += idx & - idx
# log(n) !!!
def lower_bound(val):
pos = 0
tot = 0
for i in range(20,-1,-1):
if pos + (1 << i) < mx and tot + bit[pos + (1 << i)] < val:
tot += bit[pos + (1 << i)]
pos += (1 << i)
return pos
n = int(input())
a = list(map(int,input().split()))
new_el = sorted(list(range(n)), key = lambda i: (-a[i], i))
m = int(input())
queries = []
answers = [0]*m
for i in range(m):
k,p = map(int,input().split())
queries.append([k,p,i])
queries.sort()
curr_len = 0
for i in range(m):
k,pos,query_idx = queries[i]
while curr_len < k:
add(new_el[curr_len])
curr_len += 1
answers[query_idx] = a[lower_bound(pos)]
print(*answers)
|
1227_D1. Optimal Subsequences (Easy Version)
|
This is the easier version of the problem. In this version 1 β€ n, m β€ 100. You can hack this problem only if you solve and lock both problems.
You are given a sequence of integers a=[a_1,a_2,...,a_n] of length n. Its subsequence is obtained by removing zero or more elements from the sequence a (they do not necessarily go consecutively). For example, for the sequence a=[11,20,11,33,11,20,11]:
* [11,20,11,33,11,20,11], [11,20,11,33,11,20], [11,11,11,11], [20], [33,20] are subsequences (these are just some of the long list);
* [40], [33,33], [33,20,20], [20,20,11,11] are not subsequences.
Suppose that an additional non-negative integer k (1 β€ k β€ n) is given, then the subsequence is called optimal if:
* it has a length of k and the sum of its elements is the maximum possible among all subsequences of length k;
* and among all subsequences of length k that satisfy the previous item, it is lexicographically minimal.
Recall that the sequence b=[b_1, b_2, ..., b_k] is lexicographically smaller than the sequence c=[c_1, c_2, ..., c_k] if the first element (from the left) in which they differ less in the sequence b than in c. Formally: there exists t (1 β€ t β€ k) such that b_1=c_1, b_2=c_2, ..., b_{t-1}=c_{t-1} and at the same time b_t<c_t. For example:
* [10, 20, 20] lexicographically less than [10, 21, 1],
* [7, 99, 99] is lexicographically less than [10, 21, 1],
* [10, 21, 0] is lexicographically less than [10, 21, 1].
You are given a sequence of a=[a_1,a_2,...,a_n] and m requests, each consisting of two numbers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j). For each query, print the value that is in the index pos_j of the optimal subsequence of the given sequence a for k=k_j.
For example, if n=4, a=[10,20,30,20], k_j=2, then the optimal subsequence is [20,30] β it is the minimum lexicographically among all subsequences of length 2 with the maximum total sum of items. Thus, the answer to the request k_j=2, pos_j=1 is the number 20, and the answer to the request k_j=2, pos_j=2 is the number 30.
Input
The first line contains an integer n (1 β€ n β€ 100) β the length of the sequence a.
The second line contains elements of the sequence a: integer numbers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9).
The third line contains an integer m (1 β€ m β€ 100) β the number of requests.
The following m lines contain pairs of integers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j) β the requests.
Output
Print m integers r_1, r_2, ..., r_m (1 β€ r_j β€ 10^9) one per line: answers to the requests in the order they appear in the input. The value of r_j should be equal to the value contained in the position pos_j of the optimal subsequence for k=k_j.
Examples
Input
3
10 20 10
6
1 1
2 1
2 2
3 1
3 2
3 3
Output
20
10
20
10
20
10
Input
7
1 2 1 3 1 2 1
9
2 1
2 2
3 1
3 2
3 3
1 1
7 1
7 7
7 4
Output
2
3
2
3
2
3
1
1
3
Note
In the first example, for a=[10,20,10] the optimal subsequences are:
* for k=1: [20],
* for k=2: [10,20],
* for k=3: [10,20,10].
|
{
"input": [
"3\n10 20 10\n6\n1 1\n2 1\n2 2\n3 1\n3 2\n3 3\n",
"7\n1 2 1 3 1 2 1\n9\n2 1\n2 2\n3 1\n3 2\n3 3\n1 1\n7 1\n7 7\n7 4\n"
],
"output": [
"20\n10\n20\n10\n20\n10\n",
"2\n3\n2\n3\n2\n3\n1\n1\n3\n"
]
}
|
{
"input": [
"2\n1 10\n3\n2 2\n2 1\n1 1\n",
"2\n3922 3922\n3\n2 2\n2 1\n1 1\n",
"1\n1000000000\n1\n1 1\n",
"1\n1\n3\n1 1\n1 1\n1 1\n",
"5\n3 1 4 1 2\n15\n5 5\n5 4\n5 3\n5 2\n5 1\n4 4\n4 3\n4 2\n4 1\n3 3\n3 2\n3 1\n2 2\n2 1\n1 1\n",
"2\n392222 322\n3\n2 2\n2 1\n1 1\n"
],
"output": [
"10\n1\n10\n",
"3922\n3922\n3922\n",
"1000000000\n",
"1\n1\n1\n",
"2\n1\n4\n1\n3\n2\n4\n1\n3\n2\n4\n3\n4\n3\n4\n",
"322\n392222\n392222\n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> arr(n);
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
int m;
cin >> m;
while (m--) {
map<int, pair<int, vector<int>>> mp;
int k, pos;
cin >> k >> pos;
vector<int> temp;
mp[0] = make_pair(0, temp);
for (int i = 0; i < n; i++) {
vector<pair<int, vector<int>>> toadd;
for (auto& a : mp) {
if (a.first >= k) continue;
int nsm = a.second.first + arr[i];
vector<int> temp = a.second.second;
;
temp.push_back(arr[i]);
toadd.push_back(make_pair(nsm, temp));
}
for (auto& a : toadd) {
if (!mp.count(a.second.size()) || mp[a.second.size()].first < a.first ||
(mp[a.second.size()].first == a.first &&
mp[a.second.size()].second > a.second)) {
mp[a.second.size()] = a;
}
}
}
auto e = mp.end();
e--;
cout << e->second.second[pos - 1] << "\n";
}
return 0;
}
|
1227_D1. Optimal Subsequences (Easy Version)
|
This is the easier version of the problem. In this version 1 β€ n, m β€ 100. You can hack this problem only if you solve and lock both problems.
You are given a sequence of integers a=[a_1,a_2,...,a_n] of length n. Its subsequence is obtained by removing zero or more elements from the sequence a (they do not necessarily go consecutively). For example, for the sequence a=[11,20,11,33,11,20,11]:
* [11,20,11,33,11,20,11], [11,20,11,33,11,20], [11,11,11,11], [20], [33,20] are subsequences (these are just some of the long list);
* [40], [33,33], [33,20,20], [20,20,11,11] are not subsequences.
Suppose that an additional non-negative integer k (1 β€ k β€ n) is given, then the subsequence is called optimal if:
* it has a length of k and the sum of its elements is the maximum possible among all subsequences of length k;
* and among all subsequences of length k that satisfy the previous item, it is lexicographically minimal.
Recall that the sequence b=[b_1, b_2, ..., b_k] is lexicographically smaller than the sequence c=[c_1, c_2, ..., c_k] if the first element (from the left) in which they differ less in the sequence b than in c. Formally: there exists t (1 β€ t β€ k) such that b_1=c_1, b_2=c_2, ..., b_{t-1}=c_{t-1} and at the same time b_t<c_t. For example:
* [10, 20, 20] lexicographically less than [10, 21, 1],
* [7, 99, 99] is lexicographically less than [10, 21, 1],
* [10, 21, 0] is lexicographically less than [10, 21, 1].
You are given a sequence of a=[a_1,a_2,...,a_n] and m requests, each consisting of two numbers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j). For each query, print the value that is in the index pos_j of the optimal subsequence of the given sequence a for k=k_j.
For example, if n=4, a=[10,20,30,20], k_j=2, then the optimal subsequence is [20,30] β it is the minimum lexicographically among all subsequences of length 2 with the maximum total sum of items. Thus, the answer to the request k_j=2, pos_j=1 is the number 20, and the answer to the request k_j=2, pos_j=2 is the number 30.
Input
The first line contains an integer n (1 β€ n β€ 100) β the length of the sequence a.
The second line contains elements of the sequence a: integer numbers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9).
The third line contains an integer m (1 β€ m β€ 100) β the number of requests.
The following m lines contain pairs of integers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j) β the requests.
Output
Print m integers r_1, r_2, ..., r_m (1 β€ r_j β€ 10^9) one per line: answers to the requests in the order they appear in the input. The value of r_j should be equal to the value contained in the position pos_j of the optimal subsequence for k=k_j.
Examples
Input
3
10 20 10
6
1 1
2 1
2 2
3 1
3 2
3 3
Output
20
10
20
10
20
10
Input
7
1 2 1 3 1 2 1
9
2 1
2 2
3 1
3 2
3 3
1 1
7 1
7 7
7 4
Output
2
3
2
3
2
3
1
1
3
Note
In the first example, for a=[10,20,10] the optimal subsequences are:
* for k=1: [20],
* for k=2: [10,20],
* for k=3: [10,20,10].
|
{
"input": [
"3\n10 20 10\n6\n1 1\n2 1\n2 2\n3 1\n3 2\n3 3\n",
"7\n1 2 1 3 1 2 1\n9\n2 1\n2 2\n3 1\n3 2\n3 3\n1 1\n7 1\n7 7\n7 4\n"
],
"output": [
"20\n10\n20\n10\n20\n10\n",
"2\n3\n2\n3\n2\n3\n1\n1\n3\n"
]
}
|
{
"input": [
"2\n1 10\n3\n2 2\n2 1\n1 1\n",
"2\n3922 3922\n3\n2 2\n2 1\n1 1\n",
"1\n1000000000\n1\n1 1\n",
"1\n1\n3\n1 1\n1 1\n1 1\n",
"5\n3 1 4 1 2\n15\n5 5\n5 4\n5 3\n5 2\n5 1\n4 4\n4 3\n4 2\n4 1\n3 3\n3 2\n3 1\n2 2\n2 1\n1 1\n",
"2\n392222 322\n3\n2 2\n2 1\n1 1\n"
],
"output": [
"10\n1\n10\n",
"3922\n3922\n3922\n",
"1000000000\n",
"1\n1\n1\n",
"2\n1\n4\n1\n3\n2\n4\n1\n3\n2\n4\n3\n4\n3\n4\n",
"322\n392222\n392222\n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10, mod = 1e9 + 7;
int a[N];
vector<pair<int, int> > vec;
bool cmp(pair<int, int> a, pair<int, int> b) {
if (a.first != b.first)
return a.first > b.first;
else
return a.second < b.second;
}
void solve() {
int k, pos;
cin >> k >> pos;
vector<int> ans;
for (int i = 0; i < k; i++) {
ans.push_back(vec[i].second);
}
sort(ans.begin(), ans.end());
cout << a[ans[pos - 1]] << endl;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
vec.push_back({a[i], i});
}
int m;
cin >> m;
sort(vec.begin(), vec.end(), cmp);
while (m--) solve();
}
|
1227_D1. Optimal Subsequences (Easy Version)
|
This is the easier version of the problem. In this version 1 β€ n, m β€ 100. You can hack this problem only if you solve and lock both problems.
You are given a sequence of integers a=[a_1,a_2,...,a_n] of length n. Its subsequence is obtained by removing zero or more elements from the sequence a (they do not necessarily go consecutively). For example, for the sequence a=[11,20,11,33,11,20,11]:
* [11,20,11,33,11,20,11], [11,20,11,33,11,20], [11,11,11,11], [20], [33,20] are subsequences (these are just some of the long list);
* [40], [33,33], [33,20,20], [20,20,11,11] are not subsequences.
Suppose that an additional non-negative integer k (1 β€ k β€ n) is given, then the subsequence is called optimal if:
* it has a length of k and the sum of its elements is the maximum possible among all subsequences of length k;
* and among all subsequences of length k that satisfy the previous item, it is lexicographically minimal.
Recall that the sequence b=[b_1, b_2, ..., b_k] is lexicographically smaller than the sequence c=[c_1, c_2, ..., c_k] if the first element (from the left) in which they differ less in the sequence b than in c. Formally: there exists t (1 β€ t β€ k) such that b_1=c_1, b_2=c_2, ..., b_{t-1}=c_{t-1} and at the same time b_t<c_t. For example:
* [10, 20, 20] lexicographically less than [10, 21, 1],
* [7, 99, 99] is lexicographically less than [10, 21, 1],
* [10, 21, 0] is lexicographically less than [10, 21, 1].
You are given a sequence of a=[a_1,a_2,...,a_n] and m requests, each consisting of two numbers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j). For each query, print the value that is in the index pos_j of the optimal subsequence of the given sequence a for k=k_j.
For example, if n=4, a=[10,20,30,20], k_j=2, then the optimal subsequence is [20,30] β it is the minimum lexicographically among all subsequences of length 2 with the maximum total sum of items. Thus, the answer to the request k_j=2, pos_j=1 is the number 20, and the answer to the request k_j=2, pos_j=2 is the number 30.
Input
The first line contains an integer n (1 β€ n β€ 100) β the length of the sequence a.
The second line contains elements of the sequence a: integer numbers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9).
The third line contains an integer m (1 β€ m β€ 100) β the number of requests.
The following m lines contain pairs of integers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j) β the requests.
Output
Print m integers r_1, r_2, ..., r_m (1 β€ r_j β€ 10^9) one per line: answers to the requests in the order they appear in the input. The value of r_j should be equal to the value contained in the position pos_j of the optimal subsequence for k=k_j.
Examples
Input
3
10 20 10
6
1 1
2 1
2 2
3 1
3 2
3 3
Output
20
10
20
10
20
10
Input
7
1 2 1 3 1 2 1
9
2 1
2 2
3 1
3 2
3 3
1 1
7 1
7 7
7 4
Output
2
3
2
3
2
3
1
1
3
Note
In the first example, for a=[10,20,10] the optimal subsequences are:
* for k=1: [20],
* for k=2: [10,20],
* for k=3: [10,20,10].
|
{
"input": [
"3\n10 20 10\n6\n1 1\n2 1\n2 2\n3 1\n3 2\n3 3\n",
"7\n1 2 1 3 1 2 1\n9\n2 1\n2 2\n3 1\n3 2\n3 3\n1 1\n7 1\n7 7\n7 4\n"
],
"output": [
"20\n10\n20\n10\n20\n10\n",
"2\n3\n2\n3\n2\n3\n1\n1\n3\n"
]
}
|
{
"input": [
"2\n1 10\n3\n2 2\n2 1\n1 1\n",
"2\n3922 3922\n3\n2 2\n2 1\n1 1\n",
"1\n1000000000\n1\n1 1\n",
"1\n1\n3\n1 1\n1 1\n1 1\n",
"5\n3 1 4 1 2\n15\n5 5\n5 4\n5 3\n5 2\n5 1\n4 4\n4 3\n4 2\n4 1\n3 3\n3 2\n3 1\n2 2\n2 1\n1 1\n",
"2\n392222 322\n3\n2 2\n2 1\n1 1\n"
],
"output": [
"10\n1\n10\n",
"3922\n3922\n3922\n",
"1000000000\n",
"1\n1\n1\n",
"2\n1\n4\n1\n3\n2\n4\n1\n3\n2\n4\n3\n4\n3\n4\n",
"322\n392222\n392222\n"
]
}
|
CORRECT
|
python3
|
import sys
def main():
n = int(input())
al = [[int(x),n-i] for i,x in enumerate(input().split())]
al.sort()
al.reverse()
arr = [[[]for i in range(n)]for i in range(n)]
for i in range(1,n+1):
for j in range(i-1,n):
arr[j][n-al[i-1][1]] = al[i-1][0]
for i in range(n):
arr[i] = list(filter(None, arr[i]))
q = int(input())
for _ in range(q):
k,ind = map(int,input().split())
print(arr[k-1][ind-1])
main()
|
1227_D1. Optimal Subsequences (Easy Version)
|
This is the easier version of the problem. In this version 1 β€ n, m β€ 100. You can hack this problem only if you solve and lock both problems.
You are given a sequence of integers a=[a_1,a_2,...,a_n] of length n. Its subsequence is obtained by removing zero or more elements from the sequence a (they do not necessarily go consecutively). For example, for the sequence a=[11,20,11,33,11,20,11]:
* [11,20,11,33,11,20,11], [11,20,11,33,11,20], [11,11,11,11], [20], [33,20] are subsequences (these are just some of the long list);
* [40], [33,33], [33,20,20], [20,20,11,11] are not subsequences.
Suppose that an additional non-negative integer k (1 β€ k β€ n) is given, then the subsequence is called optimal if:
* it has a length of k and the sum of its elements is the maximum possible among all subsequences of length k;
* and among all subsequences of length k that satisfy the previous item, it is lexicographically minimal.
Recall that the sequence b=[b_1, b_2, ..., b_k] is lexicographically smaller than the sequence c=[c_1, c_2, ..., c_k] if the first element (from the left) in which they differ less in the sequence b than in c. Formally: there exists t (1 β€ t β€ k) such that b_1=c_1, b_2=c_2, ..., b_{t-1}=c_{t-1} and at the same time b_t<c_t. For example:
* [10, 20, 20] lexicographically less than [10, 21, 1],
* [7, 99, 99] is lexicographically less than [10, 21, 1],
* [10, 21, 0] is lexicographically less than [10, 21, 1].
You are given a sequence of a=[a_1,a_2,...,a_n] and m requests, each consisting of two numbers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j). For each query, print the value that is in the index pos_j of the optimal subsequence of the given sequence a for k=k_j.
For example, if n=4, a=[10,20,30,20], k_j=2, then the optimal subsequence is [20,30] β it is the minimum lexicographically among all subsequences of length 2 with the maximum total sum of items. Thus, the answer to the request k_j=2, pos_j=1 is the number 20, and the answer to the request k_j=2, pos_j=2 is the number 30.
Input
The first line contains an integer n (1 β€ n β€ 100) β the length of the sequence a.
The second line contains elements of the sequence a: integer numbers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9).
The third line contains an integer m (1 β€ m β€ 100) β the number of requests.
The following m lines contain pairs of integers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j) β the requests.
Output
Print m integers r_1, r_2, ..., r_m (1 β€ r_j β€ 10^9) one per line: answers to the requests in the order they appear in the input. The value of r_j should be equal to the value contained in the position pos_j of the optimal subsequence for k=k_j.
Examples
Input
3
10 20 10
6
1 1
2 1
2 2
3 1
3 2
3 3
Output
20
10
20
10
20
10
Input
7
1 2 1 3 1 2 1
9
2 1
2 2
3 1
3 2
3 3
1 1
7 1
7 7
7 4
Output
2
3
2
3
2
3
1
1
3
Note
In the first example, for a=[10,20,10] the optimal subsequences are:
* for k=1: [20],
* for k=2: [10,20],
* for k=3: [10,20,10].
|
{
"input": [
"3\n10 20 10\n6\n1 1\n2 1\n2 2\n3 1\n3 2\n3 3\n",
"7\n1 2 1 3 1 2 1\n9\n2 1\n2 2\n3 1\n3 2\n3 3\n1 1\n7 1\n7 7\n7 4\n"
],
"output": [
"20\n10\n20\n10\n20\n10\n",
"2\n3\n2\n3\n2\n3\n1\n1\n3\n"
]
}
|
{
"input": [
"2\n1 10\n3\n2 2\n2 1\n1 1\n",
"2\n3922 3922\n3\n2 2\n2 1\n1 1\n",
"1\n1000000000\n1\n1 1\n",
"1\n1\n3\n1 1\n1 1\n1 1\n",
"5\n3 1 4 1 2\n15\n5 5\n5 4\n5 3\n5 2\n5 1\n4 4\n4 3\n4 2\n4 1\n3 3\n3 2\n3 1\n2 2\n2 1\n1 1\n",
"2\n392222 322\n3\n2 2\n2 1\n1 1\n"
],
"output": [
"10\n1\n10\n",
"3922\n3922\n3922\n",
"1000000000\n",
"1\n1\n1\n",
"2\n1\n4\n1\n3\n2\n4\n1\n3\n2\n4\n3\n4\n3\n4\n",
"322\n392222\n392222\n"
]
}
|
CORRECT
|
python3
|
n = int(input())
b = list(map(int,input().split()))
a = [[0] * 2 for i in range(n)]
for i in range(n):
a[i][0] = b[i]
a[i][1] = i
for i in range(n-1):
for j in range(n-i-1):
if a[j][0] > a[j+1][0]:
a[j], a[j+1] = a[j+1], a[j]
elif (a[j][0] == a[j + 1][0]) and (a[j][1] < a[j + 1][1]):
a[j], a[j+1] = a[j+1], a[j]
m = int(input())
for k in range(m):
k, ind = map(int,input().split())
ans = [[0] * 2 for i in range(k)]
for i in range(k):
ans[i][0] = a[n - i - 1][1]
ans[i][1] = a[n - i - 1][0]
ans.sort()
print(ans[ind - 1][1])
|
1227_D1. Optimal Subsequences (Easy Version)
|
This is the easier version of the problem. In this version 1 β€ n, m β€ 100. You can hack this problem only if you solve and lock both problems.
You are given a sequence of integers a=[a_1,a_2,...,a_n] of length n. Its subsequence is obtained by removing zero or more elements from the sequence a (they do not necessarily go consecutively). For example, for the sequence a=[11,20,11,33,11,20,11]:
* [11,20,11,33,11,20,11], [11,20,11,33,11,20], [11,11,11,11], [20], [33,20] are subsequences (these are just some of the long list);
* [40], [33,33], [33,20,20], [20,20,11,11] are not subsequences.
Suppose that an additional non-negative integer k (1 β€ k β€ n) is given, then the subsequence is called optimal if:
* it has a length of k and the sum of its elements is the maximum possible among all subsequences of length k;
* and among all subsequences of length k that satisfy the previous item, it is lexicographically minimal.
Recall that the sequence b=[b_1, b_2, ..., b_k] is lexicographically smaller than the sequence c=[c_1, c_2, ..., c_k] if the first element (from the left) in which they differ less in the sequence b than in c. Formally: there exists t (1 β€ t β€ k) such that b_1=c_1, b_2=c_2, ..., b_{t-1}=c_{t-1} and at the same time b_t<c_t. For example:
* [10, 20, 20] lexicographically less than [10, 21, 1],
* [7, 99, 99] is lexicographically less than [10, 21, 1],
* [10, 21, 0] is lexicographically less than [10, 21, 1].
You are given a sequence of a=[a_1,a_2,...,a_n] and m requests, each consisting of two numbers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j). For each query, print the value that is in the index pos_j of the optimal subsequence of the given sequence a for k=k_j.
For example, if n=4, a=[10,20,30,20], k_j=2, then the optimal subsequence is [20,30] β it is the minimum lexicographically among all subsequences of length 2 with the maximum total sum of items. Thus, the answer to the request k_j=2, pos_j=1 is the number 20, and the answer to the request k_j=2, pos_j=2 is the number 30.
Input
The first line contains an integer n (1 β€ n β€ 100) β the length of the sequence a.
The second line contains elements of the sequence a: integer numbers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9).
The third line contains an integer m (1 β€ m β€ 100) β the number of requests.
The following m lines contain pairs of integers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j) β the requests.
Output
Print m integers r_1, r_2, ..., r_m (1 β€ r_j β€ 10^9) one per line: answers to the requests in the order they appear in the input. The value of r_j should be equal to the value contained in the position pos_j of the optimal subsequence for k=k_j.
Examples
Input
3
10 20 10
6
1 1
2 1
2 2
3 1
3 2
3 3
Output
20
10
20
10
20
10
Input
7
1 2 1 3 1 2 1
9
2 1
2 2
3 1
3 2
3 3
1 1
7 1
7 7
7 4
Output
2
3
2
3
2
3
1
1
3
Note
In the first example, for a=[10,20,10] the optimal subsequences are:
* for k=1: [20],
* for k=2: [10,20],
* for k=3: [10,20,10].
|
{
"input": [
"3\n10 20 10\n6\n1 1\n2 1\n2 2\n3 1\n3 2\n3 3\n",
"7\n1 2 1 3 1 2 1\n9\n2 1\n2 2\n3 1\n3 2\n3 3\n1 1\n7 1\n7 7\n7 4\n"
],
"output": [
"20\n10\n20\n10\n20\n10\n",
"2\n3\n2\n3\n2\n3\n1\n1\n3\n"
]
}
|
{
"input": [
"2\n1 10\n3\n2 2\n2 1\n1 1\n",
"2\n3922 3922\n3\n2 2\n2 1\n1 1\n",
"1\n1000000000\n1\n1 1\n",
"1\n1\n3\n1 1\n1 1\n1 1\n",
"5\n3 1 4 1 2\n15\n5 5\n5 4\n5 3\n5 2\n5 1\n4 4\n4 3\n4 2\n4 1\n3 3\n3 2\n3 1\n2 2\n2 1\n1 1\n",
"2\n392222 322\n3\n2 2\n2 1\n1 1\n"
],
"output": [
"10\n1\n10\n",
"3922\n3922\n3922\n",
"1000000000\n",
"1\n1\n1\n",
"2\n1\n4\n1\n3\n2\n4\n1\n3\n2\n4\n3\n4\n3\n4\n",
"322\n392222\n392222\n"
]
}
|
CORRECT
|
python3
|
n = int(input())
a = list(map(int, input().split()))
m = int(input())
for i in range(m):
k, pos = map(int, input().split())
save = []
for j in range(len(a)):
save.append(a[j])
ans = []
for x in range(k):
maximum = 0
pr = -1
for j in range(len(save)):
if save[j] > maximum:
maximum = save[j]
pr = j
ans.append(maximum)
save.pop(pr)
ans.sort()
answer = []
for i in range(len(a)):
if a[i] in ans:
answer.append(a[i])
ans.pop(ans.index(a[i]))
print(answer[pos - 1])
|
1227_D1. Optimal Subsequences (Easy Version)
|
This is the easier version of the problem. In this version 1 β€ n, m β€ 100. You can hack this problem only if you solve and lock both problems.
You are given a sequence of integers a=[a_1,a_2,...,a_n] of length n. Its subsequence is obtained by removing zero or more elements from the sequence a (they do not necessarily go consecutively). For example, for the sequence a=[11,20,11,33,11,20,11]:
* [11,20,11,33,11,20,11], [11,20,11,33,11,20], [11,11,11,11], [20], [33,20] are subsequences (these are just some of the long list);
* [40], [33,33], [33,20,20], [20,20,11,11] are not subsequences.
Suppose that an additional non-negative integer k (1 β€ k β€ n) is given, then the subsequence is called optimal if:
* it has a length of k and the sum of its elements is the maximum possible among all subsequences of length k;
* and among all subsequences of length k that satisfy the previous item, it is lexicographically minimal.
Recall that the sequence b=[b_1, b_2, ..., b_k] is lexicographically smaller than the sequence c=[c_1, c_2, ..., c_k] if the first element (from the left) in which they differ less in the sequence b than in c. Formally: there exists t (1 β€ t β€ k) such that b_1=c_1, b_2=c_2, ..., b_{t-1}=c_{t-1} and at the same time b_t<c_t. For example:
* [10, 20, 20] lexicographically less than [10, 21, 1],
* [7, 99, 99] is lexicographically less than [10, 21, 1],
* [10, 21, 0] is lexicographically less than [10, 21, 1].
You are given a sequence of a=[a_1,a_2,...,a_n] and m requests, each consisting of two numbers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j). For each query, print the value that is in the index pos_j of the optimal subsequence of the given sequence a for k=k_j.
For example, if n=4, a=[10,20,30,20], k_j=2, then the optimal subsequence is [20,30] β it is the minimum lexicographically among all subsequences of length 2 with the maximum total sum of items. Thus, the answer to the request k_j=2, pos_j=1 is the number 20, and the answer to the request k_j=2, pos_j=2 is the number 30.
Input
The first line contains an integer n (1 β€ n β€ 100) β the length of the sequence a.
The second line contains elements of the sequence a: integer numbers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9).
The third line contains an integer m (1 β€ m β€ 100) β the number of requests.
The following m lines contain pairs of integers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j) β the requests.
Output
Print m integers r_1, r_2, ..., r_m (1 β€ r_j β€ 10^9) one per line: answers to the requests in the order they appear in the input. The value of r_j should be equal to the value contained in the position pos_j of the optimal subsequence for k=k_j.
Examples
Input
3
10 20 10
6
1 1
2 1
2 2
3 1
3 2
3 3
Output
20
10
20
10
20
10
Input
7
1 2 1 3 1 2 1
9
2 1
2 2
3 1
3 2
3 3
1 1
7 1
7 7
7 4
Output
2
3
2
3
2
3
1
1
3
Note
In the first example, for a=[10,20,10] the optimal subsequences are:
* for k=1: [20],
* for k=2: [10,20],
* for k=3: [10,20,10].
|
{
"input": [
"3\n10 20 10\n6\n1 1\n2 1\n2 2\n3 1\n3 2\n3 3\n",
"7\n1 2 1 3 1 2 1\n9\n2 1\n2 2\n3 1\n3 2\n3 3\n1 1\n7 1\n7 7\n7 4\n"
],
"output": [
"20\n10\n20\n10\n20\n10\n",
"2\n3\n2\n3\n2\n3\n1\n1\n3\n"
]
}
|
{
"input": [
"2\n1 10\n3\n2 2\n2 1\n1 1\n",
"2\n3922 3922\n3\n2 2\n2 1\n1 1\n",
"1\n1000000000\n1\n1 1\n",
"1\n1\n3\n1 1\n1 1\n1 1\n",
"5\n3 1 4 1 2\n15\n5 5\n5 4\n5 3\n5 2\n5 1\n4 4\n4 3\n4 2\n4 1\n3 3\n3 2\n3 1\n2 2\n2 1\n1 1\n",
"2\n392222 322\n3\n2 2\n2 1\n1 1\n"
],
"output": [
"10\n1\n10\n",
"3922\n3922\n3922\n",
"1000000000\n",
"1\n1\n1\n",
"2\n1\n4\n1\n3\n2\n4\n1\n3\n2\n4\n3\n4\n3\n4\n",
"322\n392222\n392222\n"
]
}
|
CORRECT
|
python3
|
n=int(input())
s=sorted([[v,-i] for i,v in enumerate(map(int,input().split()))])
for _ in range(int(input())):
k,i=map(int,input().split())
ans=sorted(s[-k:],key=lambda x:-x[1])
print(ans[i-1][0])
|
1227_D1. Optimal Subsequences (Easy Version)
|
This is the easier version of the problem. In this version 1 β€ n, m β€ 100. You can hack this problem only if you solve and lock both problems.
You are given a sequence of integers a=[a_1,a_2,...,a_n] of length n. Its subsequence is obtained by removing zero or more elements from the sequence a (they do not necessarily go consecutively). For example, for the sequence a=[11,20,11,33,11,20,11]:
* [11,20,11,33,11,20,11], [11,20,11,33,11,20], [11,11,11,11], [20], [33,20] are subsequences (these are just some of the long list);
* [40], [33,33], [33,20,20], [20,20,11,11] are not subsequences.
Suppose that an additional non-negative integer k (1 β€ k β€ n) is given, then the subsequence is called optimal if:
* it has a length of k and the sum of its elements is the maximum possible among all subsequences of length k;
* and among all subsequences of length k that satisfy the previous item, it is lexicographically minimal.
Recall that the sequence b=[b_1, b_2, ..., b_k] is lexicographically smaller than the sequence c=[c_1, c_2, ..., c_k] if the first element (from the left) in which they differ less in the sequence b than in c. Formally: there exists t (1 β€ t β€ k) such that b_1=c_1, b_2=c_2, ..., b_{t-1}=c_{t-1} and at the same time b_t<c_t. For example:
* [10, 20, 20] lexicographically less than [10, 21, 1],
* [7, 99, 99] is lexicographically less than [10, 21, 1],
* [10, 21, 0] is lexicographically less than [10, 21, 1].
You are given a sequence of a=[a_1,a_2,...,a_n] and m requests, each consisting of two numbers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j). For each query, print the value that is in the index pos_j of the optimal subsequence of the given sequence a for k=k_j.
For example, if n=4, a=[10,20,30,20], k_j=2, then the optimal subsequence is [20,30] β it is the minimum lexicographically among all subsequences of length 2 with the maximum total sum of items. Thus, the answer to the request k_j=2, pos_j=1 is the number 20, and the answer to the request k_j=2, pos_j=2 is the number 30.
Input
The first line contains an integer n (1 β€ n β€ 100) β the length of the sequence a.
The second line contains elements of the sequence a: integer numbers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9).
The third line contains an integer m (1 β€ m β€ 100) β the number of requests.
The following m lines contain pairs of integers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j) β the requests.
Output
Print m integers r_1, r_2, ..., r_m (1 β€ r_j β€ 10^9) one per line: answers to the requests in the order they appear in the input. The value of r_j should be equal to the value contained in the position pos_j of the optimal subsequence for k=k_j.
Examples
Input
3
10 20 10
6
1 1
2 1
2 2
3 1
3 2
3 3
Output
20
10
20
10
20
10
Input
7
1 2 1 3 1 2 1
9
2 1
2 2
3 1
3 2
3 3
1 1
7 1
7 7
7 4
Output
2
3
2
3
2
3
1
1
3
Note
In the first example, for a=[10,20,10] the optimal subsequences are:
* for k=1: [20],
* for k=2: [10,20],
* for k=3: [10,20,10].
|
{
"input": [
"3\n10 20 10\n6\n1 1\n2 1\n2 2\n3 1\n3 2\n3 3\n",
"7\n1 2 1 3 1 2 1\n9\n2 1\n2 2\n3 1\n3 2\n3 3\n1 1\n7 1\n7 7\n7 4\n"
],
"output": [
"20\n10\n20\n10\n20\n10\n",
"2\n3\n2\n3\n2\n3\n1\n1\n3\n"
]
}
|
{
"input": [
"2\n1 10\n3\n2 2\n2 1\n1 1\n",
"2\n3922 3922\n3\n2 2\n2 1\n1 1\n",
"1\n1000000000\n1\n1 1\n",
"1\n1\n3\n1 1\n1 1\n1 1\n",
"5\n3 1 4 1 2\n15\n5 5\n5 4\n5 3\n5 2\n5 1\n4 4\n4 3\n4 2\n4 1\n3 3\n3 2\n3 1\n2 2\n2 1\n1 1\n",
"2\n392222 322\n3\n2 2\n2 1\n1 1\n"
],
"output": [
"10\n1\n10\n",
"3922\n3922\n3922\n",
"1000000000\n",
"1\n1\n1\n",
"2\n1\n4\n1\n3\n2\n4\n1\n3\n2\n4\n3\n4\n3\n4\n",
"322\n392222\n392222\n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
long long N = 200000;
vector<bool> visited(N + 1, false);
bool isprime(long long x) {
for (long long i = 2; i <= sqrt(x); i++) {
if (x % i == 0) return false;
}
return true;
}
void findfact(long long x, map<long long, vector<long long>>& m) {
for (long long i = 3; i * i <= x; i += 2) {
if (x % i == 0) {
if (i == x / i)
m[x].push_back(i);
else {
m[x].push_back(i);
m[x].push_back(x / i);
}
break;
}
}
return;
}
struct cmp {
bool operator()(const pair<int, int>& a, const pair<int, int>& b) {
if (a.first > b.first)
return false;
else if (a.first < b.first)
return true;
if (a.second < b.second) return false;
return true;
}
};
bool isPalindrome(string t) {
long long st = 0;
long long end = t.size() - 1;
while (st <= end) {
if (t[st] != t[end]) return false;
st++;
end--;
}
return true;
}
long long gcd(long long a, long long b) {
if (b == 0) return a;
return gcd(b, a % b);
}
bool pow2(long long x) { return x && (!(x & (x - 1))); }
unsigned long long factorial(unsigned long long n) {
return (n == 1 || n == 0) ? 1 : n * factorial(n - 1);
}
unsigned long long binomialCoeff(unsigned long long n, unsigned long long k) {
unsigned long long res = 1;
if (k > n - k) k = n - k;
for (unsigned long long i = 0; i < k; ++i) {
res *= (n - i);
res /= (i + 1);
}
return res;
}
vector<long long> hp;
void primeFactors(long long n) {
while (n % 2 == 0) {
hp.push_back(2);
n = n / 2;
}
for (int i = 3; i <= sqrt(n); i = i + 2) {
while (n % i == 0) {
hp.push_back(i);
n = n / i;
}
}
if (n > 2) hp.push_back(n);
}
long long minFlipsMonoIncr(string S) {
int flip = 0;
int ones = 0;
bool flag = false;
for (int i = 0; i < S.size(); i++) {
if (S[i] - '0' == 1) flag = true;
if (flag && S[i] - '0' == 0)
flip++;
else if (S[i] == '1')
ones++;
if (flip > ones) flip = ones;
}
return flip;
}
void dfs() {}
int main() {
ios_base::sync_with_stdio(0);
long long n;
cin >> n;
vector<long long> arr(n + 1);
for (long long i = 1; i <= n; i++) cin >> arr[i];
long long m;
cin >> m;
while (m--) {
long long k, pos;
cin >> k >> pos;
priority_queue<pair<long long, long long>,
vector<pair<long long, long long>>, cmp>
pq;
for (long long i = 1; i <= n; i++) pq.push(make_pair(arr[i], i));
vector<long long> temp(n + 1, -1);
while (k--) {
temp[pq.top().second] = pq.top().first;
pq.pop();
}
long long ele = 0;
long long i;
for (i = 1; i <= n; i++) {
if (temp[i] == -1)
continue;
else
ele++;
if (ele == pos) break;
}
cout << temp[i] << "\n";
}
}
|
1227_D1. Optimal Subsequences (Easy Version)
|
This is the easier version of the problem. In this version 1 β€ n, m β€ 100. You can hack this problem only if you solve and lock both problems.
You are given a sequence of integers a=[a_1,a_2,...,a_n] of length n. Its subsequence is obtained by removing zero or more elements from the sequence a (they do not necessarily go consecutively). For example, for the sequence a=[11,20,11,33,11,20,11]:
* [11,20,11,33,11,20,11], [11,20,11,33,11,20], [11,11,11,11], [20], [33,20] are subsequences (these are just some of the long list);
* [40], [33,33], [33,20,20], [20,20,11,11] are not subsequences.
Suppose that an additional non-negative integer k (1 β€ k β€ n) is given, then the subsequence is called optimal if:
* it has a length of k and the sum of its elements is the maximum possible among all subsequences of length k;
* and among all subsequences of length k that satisfy the previous item, it is lexicographically minimal.
Recall that the sequence b=[b_1, b_2, ..., b_k] is lexicographically smaller than the sequence c=[c_1, c_2, ..., c_k] if the first element (from the left) in which they differ less in the sequence b than in c. Formally: there exists t (1 β€ t β€ k) such that b_1=c_1, b_2=c_2, ..., b_{t-1}=c_{t-1} and at the same time b_t<c_t. For example:
* [10, 20, 20] lexicographically less than [10, 21, 1],
* [7, 99, 99] is lexicographically less than [10, 21, 1],
* [10, 21, 0] is lexicographically less than [10, 21, 1].
You are given a sequence of a=[a_1,a_2,...,a_n] and m requests, each consisting of two numbers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j). For each query, print the value that is in the index pos_j of the optimal subsequence of the given sequence a for k=k_j.
For example, if n=4, a=[10,20,30,20], k_j=2, then the optimal subsequence is [20,30] β it is the minimum lexicographically among all subsequences of length 2 with the maximum total sum of items. Thus, the answer to the request k_j=2, pos_j=1 is the number 20, and the answer to the request k_j=2, pos_j=2 is the number 30.
Input
The first line contains an integer n (1 β€ n β€ 100) β the length of the sequence a.
The second line contains elements of the sequence a: integer numbers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9).
The third line contains an integer m (1 β€ m β€ 100) β the number of requests.
The following m lines contain pairs of integers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j) β the requests.
Output
Print m integers r_1, r_2, ..., r_m (1 β€ r_j β€ 10^9) one per line: answers to the requests in the order they appear in the input. The value of r_j should be equal to the value contained in the position pos_j of the optimal subsequence for k=k_j.
Examples
Input
3
10 20 10
6
1 1
2 1
2 2
3 1
3 2
3 3
Output
20
10
20
10
20
10
Input
7
1 2 1 3 1 2 1
9
2 1
2 2
3 1
3 2
3 3
1 1
7 1
7 7
7 4
Output
2
3
2
3
2
3
1
1
3
Note
In the first example, for a=[10,20,10] the optimal subsequences are:
* for k=1: [20],
* for k=2: [10,20],
* for k=3: [10,20,10].
|
{
"input": [
"3\n10 20 10\n6\n1 1\n2 1\n2 2\n3 1\n3 2\n3 3\n",
"7\n1 2 1 3 1 2 1\n9\n2 1\n2 2\n3 1\n3 2\n3 3\n1 1\n7 1\n7 7\n7 4\n"
],
"output": [
"20\n10\n20\n10\n20\n10\n",
"2\n3\n2\n3\n2\n3\n1\n1\n3\n"
]
}
|
{
"input": [
"2\n1 10\n3\n2 2\n2 1\n1 1\n",
"2\n3922 3922\n3\n2 2\n2 1\n1 1\n",
"1\n1000000000\n1\n1 1\n",
"1\n1\n3\n1 1\n1 1\n1 1\n",
"5\n3 1 4 1 2\n15\n5 5\n5 4\n5 3\n5 2\n5 1\n4 4\n4 3\n4 2\n4 1\n3 3\n3 2\n3 1\n2 2\n2 1\n1 1\n",
"2\n392222 322\n3\n2 2\n2 1\n1 1\n"
],
"output": [
"10\n1\n10\n",
"3922\n3922\n3922\n",
"1000000000\n",
"1\n1\n1\n",
"2\n1\n4\n1\n3\n2\n4\n1\n3\n2\n4\n3\n4\n3\n4\n",
"322\n392222\n392222\n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
const LL INF = INT_MAX;
const int N = 1e5 + 7;
const LL MOD = 1e9 + 7;
void ArrayIn(int size, int a[]) {
for (int i = 0; i < size; i++) scanf("%d", &a[i]);
}
void ArrayOut(int size, int a[]) {
for (int i = 0; i < size; i++) printf("%d ", a[i]);
printf("\n");
}
int main() {
int n, m, i;
scanf("%d", &n);
vector<pair<int, int>> ar(n + 1);
for (i = 1; i <= n; i++) {
int item;
scanf("%d", &item);
ar[i] = make_pair(item, i);
}
sort(ar.rbegin(), ar.rend() - 1);
for (i = 1; i <= n; i++) {
int j = i;
while (ar[j].first == ar[j + 1].first && j < n) j++;
reverse(ar.begin() + i, ar.begin() + j + 1);
i = j;
}
scanf("%d", &m);
while (m--) {
vector<pair<int, int>> cur = ar;
int k, pos;
scanf("%d%d", &k, &pos);
for (i = 1; i <= k; i++) {
swap(cur[i].first, cur[i].second);
}
sort(cur.begin() + 1, cur.begin() + 1 + k);
printf("%d\n", cur[pos].second);
}
return 0;
}
|
1227_D1. Optimal Subsequences (Easy Version)
|
This is the easier version of the problem. In this version 1 β€ n, m β€ 100. You can hack this problem only if you solve and lock both problems.
You are given a sequence of integers a=[a_1,a_2,...,a_n] of length n. Its subsequence is obtained by removing zero or more elements from the sequence a (they do not necessarily go consecutively). For example, for the sequence a=[11,20,11,33,11,20,11]:
* [11,20,11,33,11,20,11], [11,20,11,33,11,20], [11,11,11,11], [20], [33,20] are subsequences (these are just some of the long list);
* [40], [33,33], [33,20,20], [20,20,11,11] are not subsequences.
Suppose that an additional non-negative integer k (1 β€ k β€ n) is given, then the subsequence is called optimal if:
* it has a length of k and the sum of its elements is the maximum possible among all subsequences of length k;
* and among all subsequences of length k that satisfy the previous item, it is lexicographically minimal.
Recall that the sequence b=[b_1, b_2, ..., b_k] is lexicographically smaller than the sequence c=[c_1, c_2, ..., c_k] if the first element (from the left) in which they differ less in the sequence b than in c. Formally: there exists t (1 β€ t β€ k) such that b_1=c_1, b_2=c_2, ..., b_{t-1}=c_{t-1} and at the same time b_t<c_t. For example:
* [10, 20, 20] lexicographically less than [10, 21, 1],
* [7, 99, 99] is lexicographically less than [10, 21, 1],
* [10, 21, 0] is lexicographically less than [10, 21, 1].
You are given a sequence of a=[a_1,a_2,...,a_n] and m requests, each consisting of two numbers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j). For each query, print the value that is in the index pos_j of the optimal subsequence of the given sequence a for k=k_j.
For example, if n=4, a=[10,20,30,20], k_j=2, then the optimal subsequence is [20,30] β it is the minimum lexicographically among all subsequences of length 2 with the maximum total sum of items. Thus, the answer to the request k_j=2, pos_j=1 is the number 20, and the answer to the request k_j=2, pos_j=2 is the number 30.
Input
The first line contains an integer n (1 β€ n β€ 100) β the length of the sequence a.
The second line contains elements of the sequence a: integer numbers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9).
The third line contains an integer m (1 β€ m β€ 100) β the number of requests.
The following m lines contain pairs of integers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j) β the requests.
Output
Print m integers r_1, r_2, ..., r_m (1 β€ r_j β€ 10^9) one per line: answers to the requests in the order they appear in the input. The value of r_j should be equal to the value contained in the position pos_j of the optimal subsequence for k=k_j.
Examples
Input
3
10 20 10
6
1 1
2 1
2 2
3 1
3 2
3 3
Output
20
10
20
10
20
10
Input
7
1 2 1 3 1 2 1
9
2 1
2 2
3 1
3 2
3 3
1 1
7 1
7 7
7 4
Output
2
3
2
3
2
3
1
1
3
Note
In the first example, for a=[10,20,10] the optimal subsequences are:
* for k=1: [20],
* for k=2: [10,20],
* for k=3: [10,20,10].
|
{
"input": [
"3\n10 20 10\n6\n1 1\n2 1\n2 2\n3 1\n3 2\n3 3\n",
"7\n1 2 1 3 1 2 1\n9\n2 1\n2 2\n3 1\n3 2\n3 3\n1 1\n7 1\n7 7\n7 4\n"
],
"output": [
"20\n10\n20\n10\n20\n10\n",
"2\n3\n2\n3\n2\n3\n1\n1\n3\n"
]
}
|
{
"input": [
"2\n1 10\n3\n2 2\n2 1\n1 1\n",
"2\n3922 3922\n3\n2 2\n2 1\n1 1\n",
"1\n1000000000\n1\n1 1\n",
"1\n1\n3\n1 1\n1 1\n1 1\n",
"5\n3 1 4 1 2\n15\n5 5\n5 4\n5 3\n5 2\n5 1\n4 4\n4 3\n4 2\n4 1\n3 3\n3 2\n3 1\n2 2\n2 1\n1 1\n",
"2\n392222 322\n3\n2 2\n2 1\n1 1\n"
],
"output": [
"10\n1\n10\n",
"3922\n3922\n3922\n",
"1000000000\n",
"1\n1\n1\n",
"2\n1\n4\n1\n3\n2\n4\n1\n3\n2\n4\n3\n4\n3\n4\n",
"322\n392222\n392222\n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const long long int no = 3e6 + 5, modulo = 1e9 + 7, inf = 1e18, N = 3e3 + 1;
long long int ar[no], br[no], cr[no];
void solve() {
long long int n = 0, m = 0, a = 0, b = 0, c = 0, d = 0, x = 0, y = 0, z = 0,
w = 0, k = 0;
cin >> n;
vector<long long int> vv;
for (long long int i = 1; i < n + 1; i++) cin >> ar[i], vv.push_back(ar[i]);
cin >> m;
sort(vv.rbegin(), vv.rend());
while (m--) {
cin >> x >> y;
a = vv[x - 1];
vector<long long int> ans, v;
map<long long int, long long int> mapp;
for (long long int i = 0; i < x; i++) {
if (vv[i] > a) v.push_back(vv[i]), mapp[vv[i]]++;
}
z = 0;
for (long long int i = 1; i < n + 1; i++) {
if (ans.size() == x) break;
if (ar[i] == a && z < (x - v.size())) {
ans.push_back(a);
z++;
} else if (mapp[ar[i]] > 0) {
mapp[ar[i]]--;
ans.push_back(ar[i]);
if (mapp[ar[i]] == 0) mapp.erase(ar[i]);
}
}
cout << ans[y - 1] << "\n";
}
}
inline void runn() {
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
long long int t = 1;
for (long long int i = 1; i < t + 1; i++) {
solve();
}
}
|
1227_D1. Optimal Subsequences (Easy Version)
|
This is the easier version of the problem. In this version 1 β€ n, m β€ 100. You can hack this problem only if you solve and lock both problems.
You are given a sequence of integers a=[a_1,a_2,...,a_n] of length n. Its subsequence is obtained by removing zero or more elements from the sequence a (they do not necessarily go consecutively). For example, for the sequence a=[11,20,11,33,11,20,11]:
* [11,20,11,33,11,20,11], [11,20,11,33,11,20], [11,11,11,11], [20], [33,20] are subsequences (these are just some of the long list);
* [40], [33,33], [33,20,20], [20,20,11,11] are not subsequences.
Suppose that an additional non-negative integer k (1 β€ k β€ n) is given, then the subsequence is called optimal if:
* it has a length of k and the sum of its elements is the maximum possible among all subsequences of length k;
* and among all subsequences of length k that satisfy the previous item, it is lexicographically minimal.
Recall that the sequence b=[b_1, b_2, ..., b_k] is lexicographically smaller than the sequence c=[c_1, c_2, ..., c_k] if the first element (from the left) in which they differ less in the sequence b than in c. Formally: there exists t (1 β€ t β€ k) such that b_1=c_1, b_2=c_2, ..., b_{t-1}=c_{t-1} and at the same time b_t<c_t. For example:
* [10, 20, 20] lexicographically less than [10, 21, 1],
* [7, 99, 99] is lexicographically less than [10, 21, 1],
* [10, 21, 0] is lexicographically less than [10, 21, 1].
You are given a sequence of a=[a_1,a_2,...,a_n] and m requests, each consisting of two numbers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j). For each query, print the value that is in the index pos_j of the optimal subsequence of the given sequence a for k=k_j.
For example, if n=4, a=[10,20,30,20], k_j=2, then the optimal subsequence is [20,30] β it is the minimum lexicographically among all subsequences of length 2 with the maximum total sum of items. Thus, the answer to the request k_j=2, pos_j=1 is the number 20, and the answer to the request k_j=2, pos_j=2 is the number 30.
Input
The first line contains an integer n (1 β€ n β€ 100) β the length of the sequence a.
The second line contains elements of the sequence a: integer numbers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9).
The third line contains an integer m (1 β€ m β€ 100) β the number of requests.
The following m lines contain pairs of integers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j) β the requests.
Output
Print m integers r_1, r_2, ..., r_m (1 β€ r_j β€ 10^9) one per line: answers to the requests in the order they appear in the input. The value of r_j should be equal to the value contained in the position pos_j of the optimal subsequence for k=k_j.
Examples
Input
3
10 20 10
6
1 1
2 1
2 2
3 1
3 2
3 3
Output
20
10
20
10
20
10
Input
7
1 2 1 3 1 2 1
9
2 1
2 2
3 1
3 2
3 3
1 1
7 1
7 7
7 4
Output
2
3
2
3
2
3
1
1
3
Note
In the first example, for a=[10,20,10] the optimal subsequences are:
* for k=1: [20],
* for k=2: [10,20],
* for k=3: [10,20,10].
|
{
"input": [
"3\n10 20 10\n6\n1 1\n2 1\n2 2\n3 1\n3 2\n3 3\n",
"7\n1 2 1 3 1 2 1\n9\n2 1\n2 2\n3 1\n3 2\n3 3\n1 1\n7 1\n7 7\n7 4\n"
],
"output": [
"20\n10\n20\n10\n20\n10\n",
"2\n3\n2\n3\n2\n3\n1\n1\n3\n"
]
}
|
{
"input": [
"2\n1 10\n3\n2 2\n2 1\n1 1\n",
"2\n3922 3922\n3\n2 2\n2 1\n1 1\n",
"1\n1000000000\n1\n1 1\n",
"1\n1\n3\n1 1\n1 1\n1 1\n",
"5\n3 1 4 1 2\n15\n5 5\n5 4\n5 3\n5 2\n5 1\n4 4\n4 3\n4 2\n4 1\n3 3\n3 2\n3 1\n2 2\n2 1\n1 1\n",
"2\n392222 322\n3\n2 2\n2 1\n1 1\n"
],
"output": [
"10\n1\n10\n",
"3922\n3922\n3922\n",
"1000000000\n",
"1\n1\n1\n",
"2\n1\n4\n1\n3\n2\n4\n1\n3\n2\n4\n3\n4\n3\n4\n",
"322\n392222\n392222\n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const long double pi = 2 * acos(0.0);
struct arr {
long long index, val;
};
struct query {
long long n, ind, index, ans;
};
bool compare(arr a1, arr a2) {
if (a1.val == a2.val) return (a1.index < a2.index);
return (a1.val > a2.val);
}
bool compare2(arr a1, arr a2) { return (a1.index < a2.index); }
bool compare1(query q1, query q2) { return (q1.n < q2.n); }
bool compare3(query q1, query q2) { return (q1.index < q2.index); }
int main() {
long long n, i;
cin >> n;
arr A[n];
for (i = 0; i < n; ++i) {
cin >> A[i].val;
A[i].index = i + 1;
}
sort(A, A + n, compare);
long long m;
cin >> m;
query q[m];
for (i = 0; i < m; ++i) {
cin >> q[i].n >> q[i].ind;
q[i].index = i;
}
sort(q, q + m, compare1);
for (i = 0; i < m; ++i) {
if (i != 0) {
if (q[i].n != q[i - 1].n) {
sort(A, A + q[i - 1].n, compare);
sort(A, A + q[i].n, compare2);
q[i].ans = A[q[i].ind - 1].val;
} else
q[i].ans = A[q[i].ind - 1].val;
} else {
sort(A, A + q[i].n, compare2);
q[i].ans = A[q[i].ind - 1].val;
}
}
sort(q, q + m, compare3);
for (i = 0; i < m; ++i) cout << q[i].ans << "\n";
return 0;
}
|
1227_D1. Optimal Subsequences (Easy Version)
|
This is the easier version of the problem. In this version 1 β€ n, m β€ 100. You can hack this problem only if you solve and lock both problems.
You are given a sequence of integers a=[a_1,a_2,...,a_n] of length n. Its subsequence is obtained by removing zero or more elements from the sequence a (they do not necessarily go consecutively). For example, for the sequence a=[11,20,11,33,11,20,11]:
* [11,20,11,33,11,20,11], [11,20,11,33,11,20], [11,11,11,11], [20], [33,20] are subsequences (these are just some of the long list);
* [40], [33,33], [33,20,20], [20,20,11,11] are not subsequences.
Suppose that an additional non-negative integer k (1 β€ k β€ n) is given, then the subsequence is called optimal if:
* it has a length of k and the sum of its elements is the maximum possible among all subsequences of length k;
* and among all subsequences of length k that satisfy the previous item, it is lexicographically minimal.
Recall that the sequence b=[b_1, b_2, ..., b_k] is lexicographically smaller than the sequence c=[c_1, c_2, ..., c_k] if the first element (from the left) in which they differ less in the sequence b than in c. Formally: there exists t (1 β€ t β€ k) such that b_1=c_1, b_2=c_2, ..., b_{t-1}=c_{t-1} and at the same time b_t<c_t. For example:
* [10, 20, 20] lexicographically less than [10, 21, 1],
* [7, 99, 99] is lexicographically less than [10, 21, 1],
* [10, 21, 0] is lexicographically less than [10, 21, 1].
You are given a sequence of a=[a_1,a_2,...,a_n] and m requests, each consisting of two numbers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j). For each query, print the value that is in the index pos_j of the optimal subsequence of the given sequence a for k=k_j.
For example, if n=4, a=[10,20,30,20], k_j=2, then the optimal subsequence is [20,30] β it is the minimum lexicographically among all subsequences of length 2 with the maximum total sum of items. Thus, the answer to the request k_j=2, pos_j=1 is the number 20, and the answer to the request k_j=2, pos_j=2 is the number 30.
Input
The first line contains an integer n (1 β€ n β€ 100) β the length of the sequence a.
The second line contains elements of the sequence a: integer numbers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9).
The third line contains an integer m (1 β€ m β€ 100) β the number of requests.
The following m lines contain pairs of integers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j) β the requests.
Output
Print m integers r_1, r_2, ..., r_m (1 β€ r_j β€ 10^9) one per line: answers to the requests in the order they appear in the input. The value of r_j should be equal to the value contained in the position pos_j of the optimal subsequence for k=k_j.
Examples
Input
3
10 20 10
6
1 1
2 1
2 2
3 1
3 2
3 3
Output
20
10
20
10
20
10
Input
7
1 2 1 3 1 2 1
9
2 1
2 2
3 1
3 2
3 3
1 1
7 1
7 7
7 4
Output
2
3
2
3
2
3
1
1
3
Note
In the first example, for a=[10,20,10] the optimal subsequences are:
* for k=1: [20],
* for k=2: [10,20],
* for k=3: [10,20,10].
|
{
"input": [
"3\n10 20 10\n6\n1 1\n2 1\n2 2\n3 1\n3 2\n3 3\n",
"7\n1 2 1 3 1 2 1\n9\n2 1\n2 2\n3 1\n3 2\n3 3\n1 1\n7 1\n7 7\n7 4\n"
],
"output": [
"20\n10\n20\n10\n20\n10\n",
"2\n3\n2\n3\n2\n3\n1\n1\n3\n"
]
}
|
{
"input": [
"2\n1 10\n3\n2 2\n2 1\n1 1\n",
"2\n3922 3922\n3\n2 2\n2 1\n1 1\n",
"1\n1000000000\n1\n1 1\n",
"1\n1\n3\n1 1\n1 1\n1 1\n",
"5\n3 1 4 1 2\n15\n5 5\n5 4\n5 3\n5 2\n5 1\n4 4\n4 3\n4 2\n4 1\n3 3\n3 2\n3 1\n2 2\n2 1\n1 1\n",
"2\n392222 322\n3\n2 2\n2 1\n1 1\n"
],
"output": [
"10\n1\n10\n",
"3922\n3922\n3922\n",
"1000000000\n",
"1\n1\n1\n",
"2\n1\n4\n1\n3\n2\n4\n1\n3\n2\n4\n3\n4\n3\n4\n",
"322\n392222\n392222\n"
]
}
|
CORRECT
|
python3
|
n = int(input())
a = list(map(int, input().split()))
m = int(input())
for i in range(m):
d = dict()
k, pos = map(int, input().split())
b = a + []
for i in range(k):
d[b.index(max(b))] = max(b)
b[b.index(max(b))] = 0
print(d[sorted(d.keys())[pos - 1]])
|
1227_D1. Optimal Subsequences (Easy Version)
|
This is the easier version of the problem. In this version 1 β€ n, m β€ 100. You can hack this problem only if you solve and lock both problems.
You are given a sequence of integers a=[a_1,a_2,...,a_n] of length n. Its subsequence is obtained by removing zero or more elements from the sequence a (they do not necessarily go consecutively). For example, for the sequence a=[11,20,11,33,11,20,11]:
* [11,20,11,33,11,20,11], [11,20,11,33,11,20], [11,11,11,11], [20], [33,20] are subsequences (these are just some of the long list);
* [40], [33,33], [33,20,20], [20,20,11,11] are not subsequences.
Suppose that an additional non-negative integer k (1 β€ k β€ n) is given, then the subsequence is called optimal if:
* it has a length of k and the sum of its elements is the maximum possible among all subsequences of length k;
* and among all subsequences of length k that satisfy the previous item, it is lexicographically minimal.
Recall that the sequence b=[b_1, b_2, ..., b_k] is lexicographically smaller than the sequence c=[c_1, c_2, ..., c_k] if the first element (from the left) in which they differ less in the sequence b than in c. Formally: there exists t (1 β€ t β€ k) such that b_1=c_1, b_2=c_2, ..., b_{t-1}=c_{t-1} and at the same time b_t<c_t. For example:
* [10, 20, 20] lexicographically less than [10, 21, 1],
* [7, 99, 99] is lexicographically less than [10, 21, 1],
* [10, 21, 0] is lexicographically less than [10, 21, 1].
You are given a sequence of a=[a_1,a_2,...,a_n] and m requests, each consisting of two numbers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j). For each query, print the value that is in the index pos_j of the optimal subsequence of the given sequence a for k=k_j.
For example, if n=4, a=[10,20,30,20], k_j=2, then the optimal subsequence is [20,30] β it is the minimum lexicographically among all subsequences of length 2 with the maximum total sum of items. Thus, the answer to the request k_j=2, pos_j=1 is the number 20, and the answer to the request k_j=2, pos_j=2 is the number 30.
Input
The first line contains an integer n (1 β€ n β€ 100) β the length of the sequence a.
The second line contains elements of the sequence a: integer numbers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9).
The third line contains an integer m (1 β€ m β€ 100) β the number of requests.
The following m lines contain pairs of integers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j) β the requests.
Output
Print m integers r_1, r_2, ..., r_m (1 β€ r_j β€ 10^9) one per line: answers to the requests in the order they appear in the input. The value of r_j should be equal to the value contained in the position pos_j of the optimal subsequence for k=k_j.
Examples
Input
3
10 20 10
6
1 1
2 1
2 2
3 1
3 2
3 3
Output
20
10
20
10
20
10
Input
7
1 2 1 3 1 2 1
9
2 1
2 2
3 1
3 2
3 3
1 1
7 1
7 7
7 4
Output
2
3
2
3
2
3
1
1
3
Note
In the first example, for a=[10,20,10] the optimal subsequences are:
* for k=1: [20],
* for k=2: [10,20],
* for k=3: [10,20,10].
|
{
"input": [
"3\n10 20 10\n6\n1 1\n2 1\n2 2\n3 1\n3 2\n3 3\n",
"7\n1 2 1 3 1 2 1\n9\n2 1\n2 2\n3 1\n3 2\n3 3\n1 1\n7 1\n7 7\n7 4\n"
],
"output": [
"20\n10\n20\n10\n20\n10\n",
"2\n3\n2\n3\n2\n3\n1\n1\n3\n"
]
}
|
{
"input": [
"2\n1 10\n3\n2 2\n2 1\n1 1\n",
"2\n3922 3922\n3\n2 2\n2 1\n1 1\n",
"1\n1000000000\n1\n1 1\n",
"1\n1\n3\n1 1\n1 1\n1 1\n",
"5\n3 1 4 1 2\n15\n5 5\n5 4\n5 3\n5 2\n5 1\n4 4\n4 3\n4 2\n4 1\n3 3\n3 2\n3 1\n2 2\n2 1\n1 1\n",
"2\n392222 322\n3\n2 2\n2 1\n1 1\n"
],
"output": [
"10\n1\n10\n",
"3922\n3922\n3922\n",
"1000000000\n",
"1\n1\n1\n",
"2\n1\n4\n1\n3\n2\n4\n1\n3\n2\n4\n3\n4\n3\n4\n",
"322\n392222\n392222\n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e12;
int main() {
int n;
cin >> n;
vector<int> a(n), s;
for (int i = 0; i < n; ++i) {
cin >> a[i];
}
s = a;
sort(s.begin(), s.end());
int m;
cin >> m;
for (int i = 0; i < m; ++i) {
int k, pos;
cin >> k >> pos;
int c = 0;
vector<int> ans, st(k);
while (c < k) {
st[c] = s[n - 1 - c];
++c;
}
sort(st.begin(), st.end());
for (int q = 0; q < n; ++q) {
bool flag = false;
for (int j = 0; j < st.size(); ++j) {
if (a[q] == st[j]) {
flag = true;
st.erase(st.begin() + j);
break;
}
}
if (flag && ans.size() < k) {
ans.push_back(a[q]);
}
}
cout << ans[pos - 1] << "\n";
}
}
|
1227_D1. Optimal Subsequences (Easy Version)
|
This is the easier version of the problem. In this version 1 β€ n, m β€ 100. You can hack this problem only if you solve and lock both problems.
You are given a sequence of integers a=[a_1,a_2,...,a_n] of length n. Its subsequence is obtained by removing zero or more elements from the sequence a (they do not necessarily go consecutively). For example, for the sequence a=[11,20,11,33,11,20,11]:
* [11,20,11,33,11,20,11], [11,20,11,33,11,20], [11,11,11,11], [20], [33,20] are subsequences (these are just some of the long list);
* [40], [33,33], [33,20,20], [20,20,11,11] are not subsequences.
Suppose that an additional non-negative integer k (1 β€ k β€ n) is given, then the subsequence is called optimal if:
* it has a length of k and the sum of its elements is the maximum possible among all subsequences of length k;
* and among all subsequences of length k that satisfy the previous item, it is lexicographically minimal.
Recall that the sequence b=[b_1, b_2, ..., b_k] is lexicographically smaller than the sequence c=[c_1, c_2, ..., c_k] if the first element (from the left) in which they differ less in the sequence b than in c. Formally: there exists t (1 β€ t β€ k) such that b_1=c_1, b_2=c_2, ..., b_{t-1}=c_{t-1} and at the same time b_t<c_t. For example:
* [10, 20, 20] lexicographically less than [10, 21, 1],
* [7, 99, 99] is lexicographically less than [10, 21, 1],
* [10, 21, 0] is lexicographically less than [10, 21, 1].
You are given a sequence of a=[a_1,a_2,...,a_n] and m requests, each consisting of two numbers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j). For each query, print the value that is in the index pos_j of the optimal subsequence of the given sequence a for k=k_j.
For example, if n=4, a=[10,20,30,20], k_j=2, then the optimal subsequence is [20,30] β it is the minimum lexicographically among all subsequences of length 2 with the maximum total sum of items. Thus, the answer to the request k_j=2, pos_j=1 is the number 20, and the answer to the request k_j=2, pos_j=2 is the number 30.
Input
The first line contains an integer n (1 β€ n β€ 100) β the length of the sequence a.
The second line contains elements of the sequence a: integer numbers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9).
The third line contains an integer m (1 β€ m β€ 100) β the number of requests.
The following m lines contain pairs of integers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j) β the requests.
Output
Print m integers r_1, r_2, ..., r_m (1 β€ r_j β€ 10^9) one per line: answers to the requests in the order they appear in the input. The value of r_j should be equal to the value contained in the position pos_j of the optimal subsequence for k=k_j.
Examples
Input
3
10 20 10
6
1 1
2 1
2 2
3 1
3 2
3 3
Output
20
10
20
10
20
10
Input
7
1 2 1 3 1 2 1
9
2 1
2 2
3 1
3 2
3 3
1 1
7 1
7 7
7 4
Output
2
3
2
3
2
3
1
1
3
Note
In the first example, for a=[10,20,10] the optimal subsequences are:
* for k=1: [20],
* for k=2: [10,20],
* for k=3: [10,20,10].
|
{
"input": [
"3\n10 20 10\n6\n1 1\n2 1\n2 2\n3 1\n3 2\n3 3\n",
"7\n1 2 1 3 1 2 1\n9\n2 1\n2 2\n3 1\n3 2\n3 3\n1 1\n7 1\n7 7\n7 4\n"
],
"output": [
"20\n10\n20\n10\n20\n10\n",
"2\n3\n2\n3\n2\n3\n1\n1\n3\n"
]
}
|
{
"input": [
"2\n1 10\n3\n2 2\n2 1\n1 1\n",
"2\n3922 3922\n3\n2 2\n2 1\n1 1\n",
"1\n1000000000\n1\n1 1\n",
"1\n1\n3\n1 1\n1 1\n1 1\n",
"5\n3 1 4 1 2\n15\n5 5\n5 4\n5 3\n5 2\n5 1\n4 4\n4 3\n4 2\n4 1\n3 3\n3 2\n3 1\n2 2\n2 1\n1 1\n",
"2\n392222 322\n3\n2 2\n2 1\n1 1\n"
],
"output": [
"10\n1\n10\n",
"3922\n3922\n3922\n",
"1000000000\n",
"1\n1\n1\n",
"2\n1\n4\n1\n3\n2\n4\n1\n3\n2\n4\n3\n4\n3\n4\n",
"322\n392222\n392222\n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e9 + 123, MAXN = 5e5 + 47, MEGAINF = 1e18 + 228;
template <class T>
inline istream& operator>>(istream& in, vector<T>& a) {
for (auto& i : a) in >> i;
return in;
}
template <class T>
inline ostream& operator<<(ostream& out, vector<T>& a) {
for (auto i : a) out << i << " ";
return out;
}
template <class T, class U>
inline istream& operator>>(istream& in, vector<pair<T, U>>& a) {
for (auto& i : a) in >> i.first >> i.second;
return in;
}
template <class T, class U>
inline ostream& operator<<(ostream& out, vector<pair<T, U>>& a) {
for (auto& i : a) out << i.first << " " << i.second << "\n";
return out;
}
signed main() {
setlocale(LC_ALL, "rus");
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long n;
cin >> n;
vector<long long> a(n);
cin >> a;
vector<pair<long long, long long>> p;
for (long long i = 0; i < n; ++i) p.push_back({a[i], i});
sort(p.begin(), p.end(),
[&](pair<long long, long long> one, pair<long long, long long> two) {
if (one.first == two.first) return one.second < two.second;
return one.first > two.first;
});
long long m;
cin >> m;
while (m--) {
long long k, pos;
cin >> k >> pos;
--pos;
vector<long long> have;
for (long long i = 0; i < k; ++i) have.push_back(p[i].second);
sort(have.begin(), have.end());
long long ind = have[pos];
cout << a[ind] << "\n";
}
}
|
1227_D1. Optimal Subsequences (Easy Version)
|
This is the easier version of the problem. In this version 1 β€ n, m β€ 100. You can hack this problem only if you solve and lock both problems.
You are given a sequence of integers a=[a_1,a_2,...,a_n] of length n. Its subsequence is obtained by removing zero or more elements from the sequence a (they do not necessarily go consecutively). For example, for the sequence a=[11,20,11,33,11,20,11]:
* [11,20,11,33,11,20,11], [11,20,11,33,11,20], [11,11,11,11], [20], [33,20] are subsequences (these are just some of the long list);
* [40], [33,33], [33,20,20], [20,20,11,11] are not subsequences.
Suppose that an additional non-negative integer k (1 β€ k β€ n) is given, then the subsequence is called optimal if:
* it has a length of k and the sum of its elements is the maximum possible among all subsequences of length k;
* and among all subsequences of length k that satisfy the previous item, it is lexicographically minimal.
Recall that the sequence b=[b_1, b_2, ..., b_k] is lexicographically smaller than the sequence c=[c_1, c_2, ..., c_k] if the first element (from the left) in which they differ less in the sequence b than in c. Formally: there exists t (1 β€ t β€ k) such that b_1=c_1, b_2=c_2, ..., b_{t-1}=c_{t-1} and at the same time b_t<c_t. For example:
* [10, 20, 20] lexicographically less than [10, 21, 1],
* [7, 99, 99] is lexicographically less than [10, 21, 1],
* [10, 21, 0] is lexicographically less than [10, 21, 1].
You are given a sequence of a=[a_1,a_2,...,a_n] and m requests, each consisting of two numbers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j). For each query, print the value that is in the index pos_j of the optimal subsequence of the given sequence a for k=k_j.
For example, if n=4, a=[10,20,30,20], k_j=2, then the optimal subsequence is [20,30] β it is the minimum lexicographically among all subsequences of length 2 with the maximum total sum of items. Thus, the answer to the request k_j=2, pos_j=1 is the number 20, and the answer to the request k_j=2, pos_j=2 is the number 30.
Input
The first line contains an integer n (1 β€ n β€ 100) β the length of the sequence a.
The second line contains elements of the sequence a: integer numbers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9).
The third line contains an integer m (1 β€ m β€ 100) β the number of requests.
The following m lines contain pairs of integers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j) β the requests.
Output
Print m integers r_1, r_2, ..., r_m (1 β€ r_j β€ 10^9) one per line: answers to the requests in the order they appear in the input. The value of r_j should be equal to the value contained in the position pos_j of the optimal subsequence for k=k_j.
Examples
Input
3
10 20 10
6
1 1
2 1
2 2
3 1
3 2
3 3
Output
20
10
20
10
20
10
Input
7
1 2 1 3 1 2 1
9
2 1
2 2
3 1
3 2
3 3
1 1
7 1
7 7
7 4
Output
2
3
2
3
2
3
1
1
3
Note
In the first example, for a=[10,20,10] the optimal subsequences are:
* for k=1: [20],
* for k=2: [10,20],
* for k=3: [10,20,10].
|
{
"input": [
"3\n10 20 10\n6\n1 1\n2 1\n2 2\n3 1\n3 2\n3 3\n",
"7\n1 2 1 3 1 2 1\n9\n2 1\n2 2\n3 1\n3 2\n3 3\n1 1\n7 1\n7 7\n7 4\n"
],
"output": [
"20\n10\n20\n10\n20\n10\n",
"2\n3\n2\n3\n2\n3\n1\n1\n3\n"
]
}
|
{
"input": [
"2\n1 10\n3\n2 2\n2 1\n1 1\n",
"2\n3922 3922\n3\n2 2\n2 1\n1 1\n",
"1\n1000000000\n1\n1 1\n",
"1\n1\n3\n1 1\n1 1\n1 1\n",
"5\n3 1 4 1 2\n15\n5 5\n5 4\n5 3\n5 2\n5 1\n4 4\n4 3\n4 2\n4 1\n3 3\n3 2\n3 1\n2 2\n2 1\n1 1\n",
"2\n392222 322\n3\n2 2\n2 1\n1 1\n"
],
"output": [
"10\n1\n10\n",
"3922\n3922\n3922\n",
"1000000000\n",
"1\n1\n1\n",
"2\n1\n4\n1\n3\n2\n4\n1\n3\n2\n4\n3\n4\n3\n4\n",
"322\n392222\n392222\n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }
const int maxn = 100100;
using namespace std;
int ans[110][110];
struct node {
int nub, pos;
} a[110];
bool cmp(node c, node b) {
if (c.nub == b.nub) return c.pos < b.pos;
return c.nub > b.nub;
}
bool cmp2(node c, node b) { return c.pos < b.pos; }
int main() {
int n, m;
cin >> n;
for (int i = 1; i <= n; ++i) {
cin >> a[i].nub;
a[i].pos = i;
}
sort(a + 1, a + 1 + n, cmp);
ans[1][1] = a[1].nub;
for (int i = 2; i <= n; ++i) {
sort(a + 1, a + 1 + i, cmp2);
for (int j = 1; j <= i; ++j) {
ans[i][j] = a[j].nub;
}
}
cin >> m;
for (int i = 1; i <= m; ++i) {
int x, pos;
cin >> x >> pos;
cout << ans[x][pos] << endl;
}
}
|
1227_D1. Optimal Subsequences (Easy Version)
|
This is the easier version of the problem. In this version 1 β€ n, m β€ 100. You can hack this problem only if you solve and lock both problems.
You are given a sequence of integers a=[a_1,a_2,...,a_n] of length n. Its subsequence is obtained by removing zero or more elements from the sequence a (they do not necessarily go consecutively). For example, for the sequence a=[11,20,11,33,11,20,11]:
* [11,20,11,33,11,20,11], [11,20,11,33,11,20], [11,11,11,11], [20], [33,20] are subsequences (these are just some of the long list);
* [40], [33,33], [33,20,20], [20,20,11,11] are not subsequences.
Suppose that an additional non-negative integer k (1 β€ k β€ n) is given, then the subsequence is called optimal if:
* it has a length of k and the sum of its elements is the maximum possible among all subsequences of length k;
* and among all subsequences of length k that satisfy the previous item, it is lexicographically minimal.
Recall that the sequence b=[b_1, b_2, ..., b_k] is lexicographically smaller than the sequence c=[c_1, c_2, ..., c_k] if the first element (from the left) in which they differ less in the sequence b than in c. Formally: there exists t (1 β€ t β€ k) such that b_1=c_1, b_2=c_2, ..., b_{t-1}=c_{t-1} and at the same time b_t<c_t. For example:
* [10, 20, 20] lexicographically less than [10, 21, 1],
* [7, 99, 99] is lexicographically less than [10, 21, 1],
* [10, 21, 0] is lexicographically less than [10, 21, 1].
You are given a sequence of a=[a_1,a_2,...,a_n] and m requests, each consisting of two numbers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j). For each query, print the value that is in the index pos_j of the optimal subsequence of the given sequence a for k=k_j.
For example, if n=4, a=[10,20,30,20], k_j=2, then the optimal subsequence is [20,30] β it is the minimum lexicographically among all subsequences of length 2 with the maximum total sum of items. Thus, the answer to the request k_j=2, pos_j=1 is the number 20, and the answer to the request k_j=2, pos_j=2 is the number 30.
Input
The first line contains an integer n (1 β€ n β€ 100) β the length of the sequence a.
The second line contains elements of the sequence a: integer numbers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9).
The third line contains an integer m (1 β€ m β€ 100) β the number of requests.
The following m lines contain pairs of integers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j) β the requests.
Output
Print m integers r_1, r_2, ..., r_m (1 β€ r_j β€ 10^9) one per line: answers to the requests in the order they appear in the input. The value of r_j should be equal to the value contained in the position pos_j of the optimal subsequence for k=k_j.
Examples
Input
3
10 20 10
6
1 1
2 1
2 2
3 1
3 2
3 3
Output
20
10
20
10
20
10
Input
7
1 2 1 3 1 2 1
9
2 1
2 2
3 1
3 2
3 3
1 1
7 1
7 7
7 4
Output
2
3
2
3
2
3
1
1
3
Note
In the first example, for a=[10,20,10] the optimal subsequences are:
* for k=1: [20],
* for k=2: [10,20],
* for k=3: [10,20,10].
|
{
"input": [
"3\n10 20 10\n6\n1 1\n2 1\n2 2\n3 1\n3 2\n3 3\n",
"7\n1 2 1 3 1 2 1\n9\n2 1\n2 2\n3 1\n3 2\n3 3\n1 1\n7 1\n7 7\n7 4\n"
],
"output": [
"20\n10\n20\n10\n20\n10\n",
"2\n3\n2\n3\n2\n3\n1\n1\n3\n"
]
}
|
{
"input": [
"2\n1 10\n3\n2 2\n2 1\n1 1\n",
"2\n3922 3922\n3\n2 2\n2 1\n1 1\n",
"1\n1000000000\n1\n1 1\n",
"1\n1\n3\n1 1\n1 1\n1 1\n",
"5\n3 1 4 1 2\n15\n5 5\n5 4\n5 3\n5 2\n5 1\n4 4\n4 3\n4 2\n4 1\n3 3\n3 2\n3 1\n2 2\n2 1\n1 1\n",
"2\n392222 322\n3\n2 2\n2 1\n1 1\n"
],
"output": [
"10\n1\n10\n",
"3922\n3922\n3922\n",
"1000000000\n",
"1\n1\n1\n",
"2\n1\n4\n1\n3\n2\n4\n1\n3\n2\n4\n3\n4\n3\n4\n",
"322\n392222\n392222\n"
]
}
|
CORRECT
|
python3
|
def mergesort(l, r, arr, pos):
if r - l == 1:
return arr, pos
m = (l + r) // 2
arr, pos = mergesort(l, m, arr, pos)
arr, pos = mergesort(m, r, arr, pos)
c = [0 for i in range(r)]
d = [0 for i in range(r)]
poi_a = l
poi_b = m
for i in range(l, r):
if poi_a == m:
c[i] = arr[poi_b]
d[i] = pos[poi_b]
poi_b += 1
elif poi_b == r:
c[i] = arr[poi_a]
d[i] = pos[poi_a]
poi_a += 1
elif a[poi_a] > arr[poi_b]:
c[i] = arr[poi_a]
d[i] = pos[poi_a]
poi_a += 1
else:
c[i] = arr[poi_b]
d[i] = pos[poi_b]
poi_b += 1
for i in range(l, r):
arr[i] = c[i]
pos[i] = d[i]
return arr, pos
n = int(input())
a = list(map(int, input().split()))
p = [i for i in range(n)]
temp = a[:]
a, p = mergesort(0, n, a, p)
for m in range(int(input())):
k, pos = map(int, input().split())
j = k
while j < n and a[j - 1] == a[j]:
j += 1
i = k - 1
l = 1
while i > 0 and a[i - 1] == a[i]:
i -= 1
l += 1
m = sorted(p[i:j])
res = sorted(m[:l] + p[:i])
print(temp[res[pos - 1]])
|
1227_D1. Optimal Subsequences (Easy Version)
|
This is the easier version of the problem. In this version 1 β€ n, m β€ 100. You can hack this problem only if you solve and lock both problems.
You are given a sequence of integers a=[a_1,a_2,...,a_n] of length n. Its subsequence is obtained by removing zero or more elements from the sequence a (they do not necessarily go consecutively). For example, for the sequence a=[11,20,11,33,11,20,11]:
* [11,20,11,33,11,20,11], [11,20,11,33,11,20], [11,11,11,11], [20], [33,20] are subsequences (these are just some of the long list);
* [40], [33,33], [33,20,20], [20,20,11,11] are not subsequences.
Suppose that an additional non-negative integer k (1 β€ k β€ n) is given, then the subsequence is called optimal if:
* it has a length of k and the sum of its elements is the maximum possible among all subsequences of length k;
* and among all subsequences of length k that satisfy the previous item, it is lexicographically minimal.
Recall that the sequence b=[b_1, b_2, ..., b_k] is lexicographically smaller than the sequence c=[c_1, c_2, ..., c_k] if the first element (from the left) in which they differ less in the sequence b than in c. Formally: there exists t (1 β€ t β€ k) such that b_1=c_1, b_2=c_2, ..., b_{t-1}=c_{t-1} and at the same time b_t<c_t. For example:
* [10, 20, 20] lexicographically less than [10, 21, 1],
* [7, 99, 99] is lexicographically less than [10, 21, 1],
* [10, 21, 0] is lexicographically less than [10, 21, 1].
You are given a sequence of a=[a_1,a_2,...,a_n] and m requests, each consisting of two numbers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j). For each query, print the value that is in the index pos_j of the optimal subsequence of the given sequence a for k=k_j.
For example, if n=4, a=[10,20,30,20], k_j=2, then the optimal subsequence is [20,30] β it is the minimum lexicographically among all subsequences of length 2 with the maximum total sum of items. Thus, the answer to the request k_j=2, pos_j=1 is the number 20, and the answer to the request k_j=2, pos_j=2 is the number 30.
Input
The first line contains an integer n (1 β€ n β€ 100) β the length of the sequence a.
The second line contains elements of the sequence a: integer numbers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9).
The third line contains an integer m (1 β€ m β€ 100) β the number of requests.
The following m lines contain pairs of integers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j) β the requests.
Output
Print m integers r_1, r_2, ..., r_m (1 β€ r_j β€ 10^9) one per line: answers to the requests in the order they appear in the input. The value of r_j should be equal to the value contained in the position pos_j of the optimal subsequence for k=k_j.
Examples
Input
3
10 20 10
6
1 1
2 1
2 2
3 1
3 2
3 3
Output
20
10
20
10
20
10
Input
7
1 2 1 3 1 2 1
9
2 1
2 2
3 1
3 2
3 3
1 1
7 1
7 7
7 4
Output
2
3
2
3
2
3
1
1
3
Note
In the first example, for a=[10,20,10] the optimal subsequences are:
* for k=1: [20],
* for k=2: [10,20],
* for k=3: [10,20,10].
|
{
"input": [
"3\n10 20 10\n6\n1 1\n2 1\n2 2\n3 1\n3 2\n3 3\n",
"7\n1 2 1 3 1 2 1\n9\n2 1\n2 2\n3 1\n3 2\n3 3\n1 1\n7 1\n7 7\n7 4\n"
],
"output": [
"20\n10\n20\n10\n20\n10\n",
"2\n3\n2\n3\n2\n3\n1\n1\n3\n"
]
}
|
{
"input": [
"2\n1 10\n3\n2 2\n2 1\n1 1\n",
"2\n3922 3922\n3\n2 2\n2 1\n1 1\n",
"1\n1000000000\n1\n1 1\n",
"1\n1\n3\n1 1\n1 1\n1 1\n",
"5\n3 1 4 1 2\n15\n5 5\n5 4\n5 3\n5 2\n5 1\n4 4\n4 3\n4 2\n4 1\n3 3\n3 2\n3 1\n2 2\n2 1\n1 1\n",
"2\n392222 322\n3\n2 2\n2 1\n1 1\n"
],
"output": [
"10\n1\n10\n",
"3922\n3922\n3922\n",
"1000000000\n",
"1\n1\n1\n",
"2\n1\n4\n1\n3\n2\n4\n1\n3\n2\n4\n3\n4\n3\n4\n",
"322\n392222\n392222\n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
namespace kotespace {
template <class T>
class duplet {
private:
public:
T x, y;
duplet(){};
duplet(T a, T b) : x(a), y(b){};
bool operator<(const duplet P) const {
return (x < P.x || (x == P.x && y < P.y));
}
bool operator>(const duplet P) const {
return (x > P.x || (x == P.x && y > P.y));
}
bool operator==(const duplet P) const { return (x == P.x && y == P.y); }
bool operator!=(const duplet P) const { return (x != P.x || y != P.y); }
void reverse() { std::swap(x, y); }
};
template <class P>
istream &operator>>(istream &in, duplet<P> &T) {
return (in >> T.x >> T.y);
}
template <class P>
ostream &operator<<(ostream &out, duplet<P> T) {
return (out << T.x << " " << T.y);
}
template <class T>
class triplet {
private:
public:
T x, y, z;
triplet(){};
triplet(T a, T b, T c) : x(a), y(b), z(c){};
bool operator<(const triplet P) const {
return (x < P.x || (x == P.x && y < P.y) ||
(x == P.x && y == P.y && z < P.z));
}
bool operator>(const triplet P) const {
return (x > P.x || (x == P.x && y > P.y) ||
(x == P.x && y == P.y && z > P.z));
}
bool operator==(const triplet P) const {
return (x == P.x && y == P.y && z == P.z);
}
bool operator!=(const triplet P) const {
return (x != P.x || y != P.y || z != P.z);
}
void reverse() { std::swap(x, z); }
void cycle_right(int a) {
if (a == 1) {
std::swap(x, y);
std::swap(x, z);
}
if (a == 2) {
std::swap(x, z);
std::swap(y, x);
}
}
void cycle_left(int a) {
if (a == 1) {
std::swap(x, z);
std::swap(y, x);
}
if (a == 2) {
std::swap(x, y);
std::swap(x, z);
}
}
};
template <class P>
istream &operator>>(istream &in, triplet<P> &T) {
return (in >> T.x >> T.y >> T.z);
}
template <class P>
ostream &operator<<(ostream &out, triplet<P> &T) {
return (out << T.x << " " << T.y << " " << T.z);
}
} // namespace kotespace
using namespace kotespace;
long long inf = 1000 * 1000 * 1000 + 5;
long long inf64 = inf * inf;
long long mod = 228228227;
vector<duplet<int> > a;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cerr << fixed << setprecision(10);
cout << fixed << setprecision(10);
srand(time(0));
float START_TIME = clock();
int n;
cin >> n;
a.resize(n);
for (int i = 0; i < n; ++i) {
cin >> a[i].x;
a[i].x *= -1;
a[i].y = i;
}
sort(a.begin(), a.end());
int k;
cin >> k;
for (int i = 0; i < k; ++i) {
int x, y;
cin >> x >> y;
vector<duplet<int> > b;
for (int j = 0; j < x; ++j) {
b.emplace_back(a[j].y, -a[j].x);
}
sort(b.begin(), b.end());
cout << b[y - 1].y << endl;
}
cerr << endl << (clock() - START_TIME) / CLOCKS_PER_SEC << " sec." << endl;
}
|
1227_D1. Optimal Subsequences (Easy Version)
|
This is the easier version of the problem. In this version 1 β€ n, m β€ 100. You can hack this problem only if you solve and lock both problems.
You are given a sequence of integers a=[a_1,a_2,...,a_n] of length n. Its subsequence is obtained by removing zero or more elements from the sequence a (they do not necessarily go consecutively). For example, for the sequence a=[11,20,11,33,11,20,11]:
* [11,20,11,33,11,20,11], [11,20,11,33,11,20], [11,11,11,11], [20], [33,20] are subsequences (these are just some of the long list);
* [40], [33,33], [33,20,20], [20,20,11,11] are not subsequences.
Suppose that an additional non-negative integer k (1 β€ k β€ n) is given, then the subsequence is called optimal if:
* it has a length of k and the sum of its elements is the maximum possible among all subsequences of length k;
* and among all subsequences of length k that satisfy the previous item, it is lexicographically minimal.
Recall that the sequence b=[b_1, b_2, ..., b_k] is lexicographically smaller than the sequence c=[c_1, c_2, ..., c_k] if the first element (from the left) in which they differ less in the sequence b than in c. Formally: there exists t (1 β€ t β€ k) such that b_1=c_1, b_2=c_2, ..., b_{t-1}=c_{t-1} and at the same time b_t<c_t. For example:
* [10, 20, 20] lexicographically less than [10, 21, 1],
* [7, 99, 99] is lexicographically less than [10, 21, 1],
* [10, 21, 0] is lexicographically less than [10, 21, 1].
You are given a sequence of a=[a_1,a_2,...,a_n] and m requests, each consisting of two numbers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j). For each query, print the value that is in the index pos_j of the optimal subsequence of the given sequence a for k=k_j.
For example, if n=4, a=[10,20,30,20], k_j=2, then the optimal subsequence is [20,30] β it is the minimum lexicographically among all subsequences of length 2 with the maximum total sum of items. Thus, the answer to the request k_j=2, pos_j=1 is the number 20, and the answer to the request k_j=2, pos_j=2 is the number 30.
Input
The first line contains an integer n (1 β€ n β€ 100) β the length of the sequence a.
The second line contains elements of the sequence a: integer numbers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9).
The third line contains an integer m (1 β€ m β€ 100) β the number of requests.
The following m lines contain pairs of integers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j) β the requests.
Output
Print m integers r_1, r_2, ..., r_m (1 β€ r_j β€ 10^9) one per line: answers to the requests in the order they appear in the input. The value of r_j should be equal to the value contained in the position pos_j of the optimal subsequence for k=k_j.
Examples
Input
3
10 20 10
6
1 1
2 1
2 2
3 1
3 2
3 3
Output
20
10
20
10
20
10
Input
7
1 2 1 3 1 2 1
9
2 1
2 2
3 1
3 2
3 3
1 1
7 1
7 7
7 4
Output
2
3
2
3
2
3
1
1
3
Note
In the first example, for a=[10,20,10] the optimal subsequences are:
* for k=1: [20],
* for k=2: [10,20],
* for k=3: [10,20,10].
|
{
"input": [
"3\n10 20 10\n6\n1 1\n2 1\n2 2\n3 1\n3 2\n3 3\n",
"7\n1 2 1 3 1 2 1\n9\n2 1\n2 2\n3 1\n3 2\n3 3\n1 1\n7 1\n7 7\n7 4\n"
],
"output": [
"20\n10\n20\n10\n20\n10\n",
"2\n3\n2\n3\n2\n3\n1\n1\n3\n"
]
}
|
{
"input": [
"2\n1 10\n3\n2 2\n2 1\n1 1\n",
"2\n3922 3922\n3\n2 2\n2 1\n1 1\n",
"1\n1000000000\n1\n1 1\n",
"1\n1\n3\n1 1\n1 1\n1 1\n",
"5\n3 1 4 1 2\n15\n5 5\n5 4\n5 3\n5 2\n5 1\n4 4\n4 3\n4 2\n4 1\n3 3\n3 2\n3 1\n2 2\n2 1\n1 1\n",
"2\n392222 322\n3\n2 2\n2 1\n1 1\n"
],
"output": [
"10\n1\n10\n",
"3922\n3922\n3922\n",
"1000000000\n",
"1\n1\n1\n",
"2\n1\n4\n1\n3\n2\n4\n1\n3\n2\n4\n3\n4\n3\n4\n",
"322\n392222\n392222\n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const int N = 5e3 + 2, mod = 1e9 + 7;
void solve() {
int n;
cin >> n;
vector<pair<int, int> > a(n);
for (int i = 0; i < n; i++) {
cin >> a[i].first;
a[i].second = i;
}
sort(a.begin(), a.end(), [](pair<int, int> i, pair<int, int> j) {
if (i.first == j.first) return i.second < j.second;
return i.first > j.first;
});
int m;
cin >> m;
while (m--) {
int k, pos;
cin >> k >> pos;
vector<pair<int, int> > v(a.begin(), a.begin() + k);
sort(v.begin(), v.end(), [](pair<int, int> i, pair<int, int> j) {
return i.second < j.second;
});
cout << v[pos - 1].first << '\n';
}
}
int main() {
int tt = 1;
for (int tc = 1; tc <= tt; tc++) {
solve();
}
}
|
1227_D1. Optimal Subsequences (Easy Version)
|
This is the easier version of the problem. In this version 1 β€ n, m β€ 100. You can hack this problem only if you solve and lock both problems.
You are given a sequence of integers a=[a_1,a_2,...,a_n] of length n. Its subsequence is obtained by removing zero or more elements from the sequence a (they do not necessarily go consecutively). For example, for the sequence a=[11,20,11,33,11,20,11]:
* [11,20,11,33,11,20,11], [11,20,11,33,11,20], [11,11,11,11], [20], [33,20] are subsequences (these are just some of the long list);
* [40], [33,33], [33,20,20], [20,20,11,11] are not subsequences.
Suppose that an additional non-negative integer k (1 β€ k β€ n) is given, then the subsequence is called optimal if:
* it has a length of k and the sum of its elements is the maximum possible among all subsequences of length k;
* and among all subsequences of length k that satisfy the previous item, it is lexicographically minimal.
Recall that the sequence b=[b_1, b_2, ..., b_k] is lexicographically smaller than the sequence c=[c_1, c_2, ..., c_k] if the first element (from the left) in which they differ less in the sequence b than in c. Formally: there exists t (1 β€ t β€ k) such that b_1=c_1, b_2=c_2, ..., b_{t-1}=c_{t-1} and at the same time b_t<c_t. For example:
* [10, 20, 20] lexicographically less than [10, 21, 1],
* [7, 99, 99] is lexicographically less than [10, 21, 1],
* [10, 21, 0] is lexicographically less than [10, 21, 1].
You are given a sequence of a=[a_1,a_2,...,a_n] and m requests, each consisting of two numbers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j). For each query, print the value that is in the index pos_j of the optimal subsequence of the given sequence a for k=k_j.
For example, if n=4, a=[10,20,30,20], k_j=2, then the optimal subsequence is [20,30] β it is the minimum lexicographically among all subsequences of length 2 with the maximum total sum of items. Thus, the answer to the request k_j=2, pos_j=1 is the number 20, and the answer to the request k_j=2, pos_j=2 is the number 30.
Input
The first line contains an integer n (1 β€ n β€ 100) β the length of the sequence a.
The second line contains elements of the sequence a: integer numbers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9).
The third line contains an integer m (1 β€ m β€ 100) β the number of requests.
The following m lines contain pairs of integers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j) β the requests.
Output
Print m integers r_1, r_2, ..., r_m (1 β€ r_j β€ 10^9) one per line: answers to the requests in the order they appear in the input. The value of r_j should be equal to the value contained in the position pos_j of the optimal subsequence for k=k_j.
Examples
Input
3
10 20 10
6
1 1
2 1
2 2
3 1
3 2
3 3
Output
20
10
20
10
20
10
Input
7
1 2 1 3 1 2 1
9
2 1
2 2
3 1
3 2
3 3
1 1
7 1
7 7
7 4
Output
2
3
2
3
2
3
1
1
3
Note
In the first example, for a=[10,20,10] the optimal subsequences are:
* for k=1: [20],
* for k=2: [10,20],
* for k=3: [10,20,10].
|
{
"input": [
"3\n10 20 10\n6\n1 1\n2 1\n2 2\n3 1\n3 2\n3 3\n",
"7\n1 2 1 3 1 2 1\n9\n2 1\n2 2\n3 1\n3 2\n3 3\n1 1\n7 1\n7 7\n7 4\n"
],
"output": [
"20\n10\n20\n10\n20\n10\n",
"2\n3\n2\n3\n2\n3\n1\n1\n3\n"
]
}
|
{
"input": [
"2\n1 10\n3\n2 2\n2 1\n1 1\n",
"2\n3922 3922\n3\n2 2\n2 1\n1 1\n",
"1\n1000000000\n1\n1 1\n",
"1\n1\n3\n1 1\n1 1\n1 1\n",
"5\n3 1 4 1 2\n15\n5 5\n5 4\n5 3\n5 2\n5 1\n4 4\n4 3\n4 2\n4 1\n3 3\n3 2\n3 1\n2 2\n2 1\n1 1\n",
"2\n392222 322\n3\n2 2\n2 1\n1 1\n"
],
"output": [
"10\n1\n10\n",
"3922\n3922\n3922\n",
"1000000000\n",
"1\n1\n1\n",
"2\n1\n4\n1\n3\n2\n4\n1\n3\n2\n4\n3\n4\n3\n4\n",
"322\n392222\n392222\n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
using ll = int64_t;
using ld = long double;
void solve();
bool comp(const pair<int64_t, int64_t> &a, const pair<int64_t, int64_t> &b) {
if (a.first != b.first) return (a.first < b.first);
return (a.second > b.second);
}
int32_t main() {
ios_base::sync_with_stdio(0);
cout.tie(0);
cin.tie(0);
solve();
return 0;
}
void solve() {
int64_t n;
cin >> n;
pair<int64_t, int64_t> a[n];
for (int64_t i = 0; i < n; i++) {
cin >> a[i].first;
a[i].second = i;
}
sort(a, a + n, comp);
int64_t q;
cin >> q;
while (q--) {
int64_t x, y;
cin >> x >> y;
vector<pair<int64_t, int64_t>> v;
for (int64_t i = n - 1; i >= n - x; i--) {
v.push_back({a[i].second, a[i].first});
}
sort((v).begin(), (v).end());
cout << v[y - 1].second << endl;
}
}
|
1227_D1. Optimal Subsequences (Easy Version)
|
This is the easier version of the problem. In this version 1 β€ n, m β€ 100. You can hack this problem only if you solve and lock both problems.
You are given a sequence of integers a=[a_1,a_2,...,a_n] of length n. Its subsequence is obtained by removing zero or more elements from the sequence a (they do not necessarily go consecutively). For example, for the sequence a=[11,20,11,33,11,20,11]:
* [11,20,11,33,11,20,11], [11,20,11,33,11,20], [11,11,11,11], [20], [33,20] are subsequences (these are just some of the long list);
* [40], [33,33], [33,20,20], [20,20,11,11] are not subsequences.
Suppose that an additional non-negative integer k (1 β€ k β€ n) is given, then the subsequence is called optimal if:
* it has a length of k and the sum of its elements is the maximum possible among all subsequences of length k;
* and among all subsequences of length k that satisfy the previous item, it is lexicographically minimal.
Recall that the sequence b=[b_1, b_2, ..., b_k] is lexicographically smaller than the sequence c=[c_1, c_2, ..., c_k] if the first element (from the left) in which they differ less in the sequence b than in c. Formally: there exists t (1 β€ t β€ k) such that b_1=c_1, b_2=c_2, ..., b_{t-1}=c_{t-1} and at the same time b_t<c_t. For example:
* [10, 20, 20] lexicographically less than [10, 21, 1],
* [7, 99, 99] is lexicographically less than [10, 21, 1],
* [10, 21, 0] is lexicographically less than [10, 21, 1].
You are given a sequence of a=[a_1,a_2,...,a_n] and m requests, each consisting of two numbers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j). For each query, print the value that is in the index pos_j of the optimal subsequence of the given sequence a for k=k_j.
For example, if n=4, a=[10,20,30,20], k_j=2, then the optimal subsequence is [20,30] β it is the minimum lexicographically among all subsequences of length 2 with the maximum total sum of items. Thus, the answer to the request k_j=2, pos_j=1 is the number 20, and the answer to the request k_j=2, pos_j=2 is the number 30.
Input
The first line contains an integer n (1 β€ n β€ 100) β the length of the sequence a.
The second line contains elements of the sequence a: integer numbers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9).
The third line contains an integer m (1 β€ m β€ 100) β the number of requests.
The following m lines contain pairs of integers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j) β the requests.
Output
Print m integers r_1, r_2, ..., r_m (1 β€ r_j β€ 10^9) one per line: answers to the requests in the order they appear in the input. The value of r_j should be equal to the value contained in the position pos_j of the optimal subsequence for k=k_j.
Examples
Input
3
10 20 10
6
1 1
2 1
2 2
3 1
3 2
3 3
Output
20
10
20
10
20
10
Input
7
1 2 1 3 1 2 1
9
2 1
2 2
3 1
3 2
3 3
1 1
7 1
7 7
7 4
Output
2
3
2
3
2
3
1
1
3
Note
In the first example, for a=[10,20,10] the optimal subsequences are:
* for k=1: [20],
* for k=2: [10,20],
* for k=3: [10,20,10].
|
{
"input": [
"3\n10 20 10\n6\n1 1\n2 1\n2 2\n3 1\n3 2\n3 3\n",
"7\n1 2 1 3 1 2 1\n9\n2 1\n2 2\n3 1\n3 2\n3 3\n1 1\n7 1\n7 7\n7 4\n"
],
"output": [
"20\n10\n20\n10\n20\n10\n",
"2\n3\n2\n3\n2\n3\n1\n1\n3\n"
]
}
|
{
"input": [
"2\n1 10\n3\n2 2\n2 1\n1 1\n",
"2\n3922 3922\n3\n2 2\n2 1\n1 1\n",
"1\n1000000000\n1\n1 1\n",
"1\n1\n3\n1 1\n1 1\n1 1\n",
"5\n3 1 4 1 2\n15\n5 5\n5 4\n5 3\n5 2\n5 1\n4 4\n4 3\n4 2\n4 1\n3 3\n3 2\n3 1\n2 2\n2 1\n1 1\n",
"2\n392222 322\n3\n2 2\n2 1\n1 1\n"
],
"output": [
"10\n1\n10\n",
"3922\n3922\n3922\n",
"1000000000\n",
"1\n1\n1\n",
"2\n1\n4\n1\n3\n2\n4\n1\n3\n2\n4\n3\n4\n3\n4\n",
"322\n392222\n392222\n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
bool choice_first(std::vector<size_t> a, std::vector<size_t> b) {
uint64_t sum_a, sum_b;
sum_a = sum_b = 0;
for (auto elem : a) {
sum_a += elem;
}
for (auto elem : b) {
sum_b += elem;
}
return sum_a > sum_b || (sum_a == sum_b && a < b);
}
int main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(0);
std::cout.tie(0);
size_t n, m;
std::cin >> n;
std::vector<std::vector<std::vector<size_t>>> dp(
n + 1, std::vector<std::vector<size_t>>(n + 1));
std::vector<size_t> a(n);
for (size_t i = 0; i < n; i++) {
std::cin >> a[i];
}
std::cin >> m;
for (size_t i = 0; i < n; i++) {
if (i == 0) {
dp[1][i] = {a[0]};
continue;
}
dp[1][i] = std::max(std::vector<size_t>{a[i]}, dp[1][i - 1]);
}
for (size_t i = 2; i <= n; i++) {
for (size_t j = i - 1; j < n; j++) {
if (j == i - 1) {
std::vector<size_t> cur = dp[i - 1][j - 1];
cur.push_back(a[j]);
dp[i][j] = cur;
continue;
}
std::vector<size_t> first, second;
first = dp[i - 1][j - 1];
first.push_back(a[j]);
second = dp[i][j - 1];
if (choice_first(first, second)) {
dp[i][j] = first;
} else {
dp[i][j] = second;
}
}
}
for (size_t i = 0; i < m; i++) {
size_t k, pos;
std::cin >> k >> pos;
std::cout << dp[k][n - 1][pos - 1] << '\n';
}
}
|
1227_D1. Optimal Subsequences (Easy Version)
|
This is the easier version of the problem. In this version 1 β€ n, m β€ 100. You can hack this problem only if you solve and lock both problems.
You are given a sequence of integers a=[a_1,a_2,...,a_n] of length n. Its subsequence is obtained by removing zero or more elements from the sequence a (they do not necessarily go consecutively). For example, for the sequence a=[11,20,11,33,11,20,11]:
* [11,20,11,33,11,20,11], [11,20,11,33,11,20], [11,11,11,11], [20], [33,20] are subsequences (these are just some of the long list);
* [40], [33,33], [33,20,20], [20,20,11,11] are not subsequences.
Suppose that an additional non-negative integer k (1 β€ k β€ n) is given, then the subsequence is called optimal if:
* it has a length of k and the sum of its elements is the maximum possible among all subsequences of length k;
* and among all subsequences of length k that satisfy the previous item, it is lexicographically minimal.
Recall that the sequence b=[b_1, b_2, ..., b_k] is lexicographically smaller than the sequence c=[c_1, c_2, ..., c_k] if the first element (from the left) in which they differ less in the sequence b than in c. Formally: there exists t (1 β€ t β€ k) such that b_1=c_1, b_2=c_2, ..., b_{t-1}=c_{t-1} and at the same time b_t<c_t. For example:
* [10, 20, 20] lexicographically less than [10, 21, 1],
* [7, 99, 99] is lexicographically less than [10, 21, 1],
* [10, 21, 0] is lexicographically less than [10, 21, 1].
You are given a sequence of a=[a_1,a_2,...,a_n] and m requests, each consisting of two numbers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j). For each query, print the value that is in the index pos_j of the optimal subsequence of the given sequence a for k=k_j.
For example, if n=4, a=[10,20,30,20], k_j=2, then the optimal subsequence is [20,30] β it is the minimum lexicographically among all subsequences of length 2 with the maximum total sum of items. Thus, the answer to the request k_j=2, pos_j=1 is the number 20, and the answer to the request k_j=2, pos_j=2 is the number 30.
Input
The first line contains an integer n (1 β€ n β€ 100) β the length of the sequence a.
The second line contains elements of the sequence a: integer numbers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9).
The third line contains an integer m (1 β€ m β€ 100) β the number of requests.
The following m lines contain pairs of integers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j) β the requests.
Output
Print m integers r_1, r_2, ..., r_m (1 β€ r_j β€ 10^9) one per line: answers to the requests in the order they appear in the input. The value of r_j should be equal to the value contained in the position pos_j of the optimal subsequence for k=k_j.
Examples
Input
3
10 20 10
6
1 1
2 1
2 2
3 1
3 2
3 3
Output
20
10
20
10
20
10
Input
7
1 2 1 3 1 2 1
9
2 1
2 2
3 1
3 2
3 3
1 1
7 1
7 7
7 4
Output
2
3
2
3
2
3
1
1
3
Note
In the first example, for a=[10,20,10] the optimal subsequences are:
* for k=1: [20],
* for k=2: [10,20],
* for k=3: [10,20,10].
|
{
"input": [
"3\n10 20 10\n6\n1 1\n2 1\n2 2\n3 1\n3 2\n3 3\n",
"7\n1 2 1 3 1 2 1\n9\n2 1\n2 2\n3 1\n3 2\n3 3\n1 1\n7 1\n7 7\n7 4\n"
],
"output": [
"20\n10\n20\n10\n20\n10\n",
"2\n3\n2\n3\n2\n3\n1\n1\n3\n"
]
}
|
{
"input": [
"2\n1 10\n3\n2 2\n2 1\n1 1\n",
"2\n3922 3922\n3\n2 2\n2 1\n1 1\n",
"1\n1000000000\n1\n1 1\n",
"1\n1\n3\n1 1\n1 1\n1 1\n",
"5\n3 1 4 1 2\n15\n5 5\n5 4\n5 3\n5 2\n5 1\n4 4\n4 3\n4 2\n4 1\n3 3\n3 2\n3 1\n2 2\n2 1\n1 1\n",
"2\n392222 322\n3\n2 2\n2 1\n1 1\n"
],
"output": [
"10\n1\n10\n",
"3922\n3922\n3922\n",
"1000000000\n",
"1\n1\n1\n",
"2\n1\n4\n1\n3\n2\n4\n1\n3\n2\n4\n3\n4\n3\n4\n",
"322\n392222\n392222\n"
]
}
|
CORRECT
|
java
|
/*
*
* @Author Ajudiya_13(Bhargav Girdharbhai Ajudiya)
* Dhirubhai Ambani Institute of Information And Communication Technology
*
*/
import java.util.*;
import java.io.*;
import java.lang.*;
public class Code85
{
public static void main(String[] args)
{
InputReader in = new InputReader(System.in);
PrintWriter pw = new PrintWriter(System.out);
int test_case = 1;
for(int t=0;t<test_case;t++)
{
int n = in.nextInt();
int[] a = new int[n];
int[] b = new int[n];
for(int i=0;i<n;i++){
a[i] = in.nextInt();
b[i] = a[i];
}
Arrays.sort(b);
int[][] subSequence = new int[n][];
subSequence[0] = new int[n];
for(int i=0;i<n;i++)
subSequence[0][i] = a[i];
for(int i=1;i<n;i++){
subSequence[i] = new int[n-i];
int x = b[i-1];
boolean flag = false;
int index = n-i-1;
for(int j=n-i;j>=0;j--){
//System.out.println(i + " " + j + " " + index + " " + flag + " " + x + " " + b[i-1]);
if(flag) {
subSequence[i][index] = subSequence[i - 1][j];
index--;
}
else{
if(subSequence[i-1][j]==x)
flag = true;
else {
subSequence[i][index] = subSequence[i-1][j];
index--;
}
}
}
}
int m = in.nextInt();
for(int i=0;i<m;i++){
int k = in.nextInt();
int pos = in.nextInt();
pw.println(subSequence[n-k][pos-1]);
}
}
pw.flush();
pw.close();
}
static class InputReader
{
private final InputStream stream;
private final byte[] buf = new byte[8192];
private int curChar, snumChars;
private SpaceCharFilter filter;
public InputReader(InputStream stream)
{
this.stream = stream;
}
public int snext()
{
if (snumChars == -1)
throw new InputMismatchException();
if (curChar >= snumChars)
{
curChar = 0;
try
{
snumChars = stream.read(buf);
}
catch (IOException e)
{
throw new InputMismatchException();
}
if (snumChars <= 0)
return -1;
}
return buf[curChar++];
}
public int nextInt()
{
int c = snext();
while (isSpaceChar(c))
{
c = snext();
}
int sgn = 1;
if (c == '-')
{
sgn = -1;
c = snext();
}
int res = 0;
do
{
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = snext();
} while (!isSpaceChar(c));
return res * sgn;
}
public long nextLong()
{
int c = snext();
while (isSpaceChar(c))
{
c = snext();
}
int sgn = 1;
if (c == '-')
{
sgn = -1;
c = snext();
}
long res = 0;
do
{
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = snext();
} while (!isSpaceChar(c));
return res * sgn;
}
public int[] nextIntArray(int n)
{
int a[] = new int[n];
for (int i = 0; i < n; i++)
{
a[i] = nextInt();
}
return a;
}
public String readString()
{
int c = snext();
while (isSpaceChar(c))
{
c = snext();
}
StringBuilder res = new StringBuilder();
do
{
res.appendCodePoint(c);
c = snext();
} while (!isSpaceChar(c));
return res.toString();
}
public String nextLine()
{
int c = snext();
while (isSpaceChar(c))
c = snext();
StringBuilder res = new StringBuilder();
do
{
res.appendCodePoint(c);
c = snext();
} while (!isEndOfLine(c));
return res.toString();
}
public boolean isSpaceChar(int c)
{
if (filter != null)
return filter.isSpaceChar(c);
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
private boolean isEndOfLine(int c)
{
return c == '\n' || c == '\r' || c == -1;
}
public interface SpaceCharFilter
{
public boolean isSpaceChar(int ch);
}
}
public static long mod = 1000000007;
public static int d;
public static int p;
public static int q;
public static int[] suffle(int[] a,Random gen)
{
int n = a.length;
for(int i=0;i<n;i++)
{
int ind = gen.nextInt(n-i)+i;
int temp = a[ind];
a[ind] = a[i];
a[i] = temp;
}
return a;
}
public static void swap(int a, int b){
int temp = a;
a = b;
b = temp;
}
public static HashSet<Integer> primeFactorization(int n)
{
HashSet<Integer> a =new HashSet<Integer>();
for(int i=2;i*i<=n;i++)
{
while(n%i==0)
{
a.add(i);
n/=i;
}
}
if(n!=1)
a.add(n);
return a;
}
public static void sieve(boolean[] isPrime,int n)
{
for(int i=1;i<n;i++)
isPrime[i] = true;
isPrime[0] = false;
isPrime[1] = false;
for(int i=2;i*i<n;i++)
{
if(isPrime[i] == true)
{
for(int j=(2*i);j<n;j+=i)
isPrime[j] = false;
}
}
}
public static int GCD(int a,int b)
{
if(b==0)
return a;
else
return GCD(b,a%b);
}
public static long GCD(long a,long b)
{
if(b==0)
return a;
else
return GCD(b,a%b);
}
public static void extendedEuclid(int A,int B)
{
if(B==0)
{
d = A;
p = 1 ;
q = 0;
}
else
{
extendedEuclid(B, A%B);
int temp = p;
p = q;
q = temp - (A/B)*q;
}
}
public static long LCM(long a,long b)
{
return (a*b)/GCD(a,b);
}
public static int LCM(int a,int b)
{
return (a*b)/GCD(a,b);
}
public static int binaryExponentiation(int x,int n)
{
int result=1;
while(n>0)
{
if(n % 2 ==1)
result=result * x;
x=x*x;
n=n/2;
}
return result;
}
public static long binaryExponentiation(long x,long n)
{
long result=1;
while(n>0)
{
if(n % 2 ==1)
result=result * x;
x=x*x;
n=n/2;
}
return result;
}
public static int modularExponentiation(int x,int n,int M)
{
int result=1;
while(n>0)
{
if(n % 2 ==1)
result=(result * x)%M;
x=(x*x)%M;
n=n/2;
}
return result;
}
public static long modularExponentiation(long x,long n,long M)
{
long result=1;
while(n>0)
{
if(n % 2 ==1)
result=(result * x)%M;
x=(x*x)%M;
n=n/2;
}
return result;
}
public static int modInverse(int A,int M)
{
return modularExponentiation(A,M-2,M);
}
public static long modInverse(long A,long M)
{
return modularExponentiation(A,M-2,M);
}
public static boolean isPrime(int n)
{
if (n <= 1) return false;
if (n <= 3) return true;
if (n%2 == 0 || n%3 == 0)
return false;
for (int i=5; i*i<=n; i=i+6)
{
if (n%i == 0 || n%(i+2) == 0)
return false;
}
return true;
}
static class pair implements Comparable<pair>
{
Integer x, y;
pair(int x,int y)
{
this.x=x;
this.y=y;
}
public int compareTo(pair o) {
int result = x.compareTo(o.x);
if(result==0)
result = y.compareTo(o.y);
return result;
}
public String toString()
{
return x+" "+y;
}
public boolean equals(Object o)
{
if (o instanceof pair)
{
pair p = (pair)o;
return (Math.abs(p.x-x)==0 && Math.abs(p.y-y)==0);
}
return false;
}
public int hashCode()
{
return new Long(x).hashCode()*31 + new Long(y).hashCode();
}
}
}
|
1227_D1. Optimal Subsequences (Easy Version)
|
This is the easier version of the problem. In this version 1 β€ n, m β€ 100. You can hack this problem only if you solve and lock both problems.
You are given a sequence of integers a=[a_1,a_2,...,a_n] of length n. Its subsequence is obtained by removing zero or more elements from the sequence a (they do not necessarily go consecutively). For example, for the sequence a=[11,20,11,33,11,20,11]:
* [11,20,11,33,11,20,11], [11,20,11,33,11,20], [11,11,11,11], [20], [33,20] are subsequences (these are just some of the long list);
* [40], [33,33], [33,20,20], [20,20,11,11] are not subsequences.
Suppose that an additional non-negative integer k (1 β€ k β€ n) is given, then the subsequence is called optimal if:
* it has a length of k and the sum of its elements is the maximum possible among all subsequences of length k;
* and among all subsequences of length k that satisfy the previous item, it is lexicographically minimal.
Recall that the sequence b=[b_1, b_2, ..., b_k] is lexicographically smaller than the sequence c=[c_1, c_2, ..., c_k] if the first element (from the left) in which they differ less in the sequence b than in c. Formally: there exists t (1 β€ t β€ k) such that b_1=c_1, b_2=c_2, ..., b_{t-1}=c_{t-1} and at the same time b_t<c_t. For example:
* [10, 20, 20] lexicographically less than [10, 21, 1],
* [7, 99, 99] is lexicographically less than [10, 21, 1],
* [10, 21, 0] is lexicographically less than [10, 21, 1].
You are given a sequence of a=[a_1,a_2,...,a_n] and m requests, each consisting of two numbers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j). For each query, print the value that is in the index pos_j of the optimal subsequence of the given sequence a for k=k_j.
For example, if n=4, a=[10,20,30,20], k_j=2, then the optimal subsequence is [20,30] β it is the minimum lexicographically among all subsequences of length 2 with the maximum total sum of items. Thus, the answer to the request k_j=2, pos_j=1 is the number 20, and the answer to the request k_j=2, pos_j=2 is the number 30.
Input
The first line contains an integer n (1 β€ n β€ 100) β the length of the sequence a.
The second line contains elements of the sequence a: integer numbers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9).
The third line contains an integer m (1 β€ m β€ 100) β the number of requests.
The following m lines contain pairs of integers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j) β the requests.
Output
Print m integers r_1, r_2, ..., r_m (1 β€ r_j β€ 10^9) one per line: answers to the requests in the order they appear in the input. The value of r_j should be equal to the value contained in the position pos_j of the optimal subsequence for k=k_j.
Examples
Input
3
10 20 10
6
1 1
2 1
2 2
3 1
3 2
3 3
Output
20
10
20
10
20
10
Input
7
1 2 1 3 1 2 1
9
2 1
2 2
3 1
3 2
3 3
1 1
7 1
7 7
7 4
Output
2
3
2
3
2
3
1
1
3
Note
In the first example, for a=[10,20,10] the optimal subsequences are:
* for k=1: [20],
* for k=2: [10,20],
* for k=3: [10,20,10].
|
{
"input": [
"3\n10 20 10\n6\n1 1\n2 1\n2 2\n3 1\n3 2\n3 3\n",
"7\n1 2 1 3 1 2 1\n9\n2 1\n2 2\n3 1\n3 2\n3 3\n1 1\n7 1\n7 7\n7 4\n"
],
"output": [
"20\n10\n20\n10\n20\n10\n",
"2\n3\n2\n3\n2\n3\n1\n1\n3\n"
]
}
|
{
"input": [
"2\n1 10\n3\n2 2\n2 1\n1 1\n",
"2\n3922 3922\n3\n2 2\n2 1\n1 1\n",
"1\n1000000000\n1\n1 1\n",
"1\n1\n3\n1 1\n1 1\n1 1\n",
"5\n3 1 4 1 2\n15\n5 5\n5 4\n5 3\n5 2\n5 1\n4 4\n4 3\n4 2\n4 1\n3 3\n3 2\n3 1\n2 2\n2 1\n1 1\n",
"2\n392222 322\n3\n2 2\n2 1\n1 1\n"
],
"output": [
"10\n1\n10\n",
"3922\n3922\n3922\n",
"1000000000\n",
"1\n1\n1\n",
"2\n1\n4\n1\n3\n2\n4\n1\n3\n2\n4\n3\n4\n3\n4\n",
"322\n392222\n392222\n"
]
}
|
CORRECT
|
java
|
/*
TO LEARN
1-segment trees
2-euler tour
3-fenwick tree and interval tree
*/
/*
TO SOLVE
uva 1103
*/
/*
bit manipulation shit
1-Computer Systems: A Programmer's Perspective
2-hacker's delight
3-(02-03-bits-ints)
4-machine-basics
5-Bits Manipulation tutorialspoint
*/
import java.util.*;
import java.math.*;
import java.io.*;
import java.util.stream.Collectors;
public class A{
static InputStream inputStream = System.in;
static FastReader scan=new FastReader(inputStream);
public static PrintWriter out = new PrintWriter (new BufferedOutputStream(System.out));
static class comp1 implements Comparator<Pair> {
public int compare(Pair o1, Pair o2)
{
return (int)(o1.y-o2.y);
}
}
public static void main(String[] args) throws Exception
{
// scan=new FastReader("two2.in");
// out = new PrintWriter("peacefulsets.out");
/*
currently doing
1-digit dp
2-ds like fenwick and interval tree and sparse table
*/
/*
READING
1-Everything About Dynamic Programming codeforces
2-DYNAMIC PROGRAMMING: FROM NOVICE TO ADVANCED topcoder
*/
int tt=1;
//tt=scan.nextInt();
outer:while(tt-->0)
{
int n=scan.nextInt();
int arr[]=new int[n];
PriorityQueue<Pair>pq=new PriorityQueue<Pair>();
for(int i=0;i<n;i++){
arr[i]=scan.nextInt();
pq.add(new Pair(arr[i],i));
}
//out.println(pq);
//pq.poll();
//System.out.println(pq.poll());
//out.println(pq);
int m=scan.nextInt();
for(int i=0;i<m;i++)
{
int k=scan.nextInt(),pos=scan.nextInt();
PriorityQueue<Pair>tmp=new PriorityQueue<Pair>(pq);
ArrayList<Pair>list=new ArrayList<Pair>();
int idx=Integer.MAX_VALUE;
int val=-1;
while(k>0)
{
k--;
Pair p=tmp.poll();
list.add(new Pair(p.x,p.y));
}
// out.println(list);
Collections.sort(list,new comp1());
out.println(list.get(pos-1).x);
}
}
out.close();
}
static class special{
char x,y;
//int id;
special(char x,char y)
{
this.x=x;
this.y=y;
//this.id=id;
}
@Override
public int hashCode() {
return (int)(x + 31 * y);
}
@Override
public boolean equals(Object o){
if (o == this) return true;
if (o.getClass() != getClass()) return false;
special t = (special)o;
return t.x == x && t.y == y;
}
}
static long binexp(long a,long n)
{
if(n==0)
return 1;
long res=binexp(a,n/2);
if(n%2==1)
return res*res*a;
else
return res*res;
}
static long powMod(long base, long exp, long mod) {
if (base == 0 || base == 1) return base;
if (exp == 0) return 1;
if (exp == 1) return (base % mod+mod)%mod;
long R = (powMod(base, exp/2, mod) % mod+mod)%mod;
R *= R;
R %= mod;
if ((exp & 1) == 1) {
return (base * R % mod+mod)%mod;
}
else return (R %mod+mod)%mod;
}
static double dis(double x1,double y1,double x2,double y2)
{
return Math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
}
static long mod(long x,long y)
{
if(x<0)
x=x+(-x/y+1)*y;
return x%y;
}
public static long pow(long b, long e) {
long r = 1;
while (e > 0) {
if (e % 2 == 1) r = r * b ;
b = b * b;
e >>= 1;
}
return r;
}
private static void sort(long[] arr) {
List<Long> list = new ArrayList<>();
for (long object : arr) list.add(object);
Collections.sort(list);
//Collections.reverse(list);
for (int i = 0; i < list.size(); ++i) arr[i] = list.get(i);
}
private static void sort2(long[] arr) {
List<Long> list = new ArrayList<>();
for (Long object : arr) list.add(object);
Collections.sort(list);
Collections.reverse(list);
for (int i = 0; i < list.size(); ++i) arr[i] = list.get(i);
}
public static class FastReader {
BufferedReader br;
StringTokenizer root;
public FastReader(InputStream stream) {
br = new BufferedReader(new InputStreamReader(stream), 32768);
root = null;
}
FastReader(String filename)throws Exception
{
br=new BufferedReader(new FileReader(filename));
}
boolean hasNext(){
String line;
while(root.hasMoreTokens())
return true;
return false;
}
String next() {
while (root == null || !root.hasMoreTokens()) {
try {
root = new StringTokenizer(br.readLine());
} catch (Exception addd) {
addd.printStackTrace();
}
}
return root.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
long nextLong() {
return Long.parseLong(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (Exception addd) {
addd.printStackTrace();
}
return str;
}
public int[] nextIntArray(int arraySize) {
int array[] = new int[arraySize];
for (int i = 0; i < arraySize; i++) {
array[i] = nextInt();
}
return array;
}
}
static class Pair implements Comparable<Pair>{
public long x, y,z;
public Pair(long x1, long y1,long z1) {
x=x1;
y=y1;
z=z1;
}
public Pair(long x1, long y1) {
x=x1;
y=y1;
}
@Override
public int hashCode() {
return (int)(x + 31 * y);
}
public String toString() {
return x + " " + y+" "+z;
}
@Override
public boolean equals(Object o){
if (o == this) return true;
if (o.getClass() != getClass()) return false;
Pair t = (Pair)o;
return t.x == x && t.y == y&&t.z==z;
}
public int compareTo(Pair o)
{
if(x==o.x)
{
return (int)(y-o.y);
}
return (int)(o.x-x);
}
}
static class tuple{
int x,y,z;
tuple(int a,int b,int c){
x=a;
y=b;
z=c;
}
}
static class Edge{
int d,w;
Edge(int d,int w)
{
this.d=d;
this.w=w;
}
}
}
|
1227_D1. Optimal Subsequences (Easy Version)
|
This is the easier version of the problem. In this version 1 β€ n, m β€ 100. You can hack this problem only if you solve and lock both problems.
You are given a sequence of integers a=[a_1,a_2,...,a_n] of length n. Its subsequence is obtained by removing zero or more elements from the sequence a (they do not necessarily go consecutively). For example, for the sequence a=[11,20,11,33,11,20,11]:
* [11,20,11,33,11,20,11], [11,20,11,33,11,20], [11,11,11,11], [20], [33,20] are subsequences (these are just some of the long list);
* [40], [33,33], [33,20,20], [20,20,11,11] are not subsequences.
Suppose that an additional non-negative integer k (1 β€ k β€ n) is given, then the subsequence is called optimal if:
* it has a length of k and the sum of its elements is the maximum possible among all subsequences of length k;
* and among all subsequences of length k that satisfy the previous item, it is lexicographically minimal.
Recall that the sequence b=[b_1, b_2, ..., b_k] is lexicographically smaller than the sequence c=[c_1, c_2, ..., c_k] if the first element (from the left) in which they differ less in the sequence b than in c. Formally: there exists t (1 β€ t β€ k) such that b_1=c_1, b_2=c_2, ..., b_{t-1}=c_{t-1} and at the same time b_t<c_t. For example:
* [10, 20, 20] lexicographically less than [10, 21, 1],
* [7, 99, 99] is lexicographically less than [10, 21, 1],
* [10, 21, 0] is lexicographically less than [10, 21, 1].
You are given a sequence of a=[a_1,a_2,...,a_n] and m requests, each consisting of two numbers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j). For each query, print the value that is in the index pos_j of the optimal subsequence of the given sequence a for k=k_j.
For example, if n=4, a=[10,20,30,20], k_j=2, then the optimal subsequence is [20,30] β it is the minimum lexicographically among all subsequences of length 2 with the maximum total sum of items. Thus, the answer to the request k_j=2, pos_j=1 is the number 20, and the answer to the request k_j=2, pos_j=2 is the number 30.
Input
The first line contains an integer n (1 β€ n β€ 100) β the length of the sequence a.
The second line contains elements of the sequence a: integer numbers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9).
The third line contains an integer m (1 β€ m β€ 100) β the number of requests.
The following m lines contain pairs of integers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j) β the requests.
Output
Print m integers r_1, r_2, ..., r_m (1 β€ r_j β€ 10^9) one per line: answers to the requests in the order they appear in the input. The value of r_j should be equal to the value contained in the position pos_j of the optimal subsequence for k=k_j.
Examples
Input
3
10 20 10
6
1 1
2 1
2 2
3 1
3 2
3 3
Output
20
10
20
10
20
10
Input
7
1 2 1 3 1 2 1
9
2 1
2 2
3 1
3 2
3 3
1 1
7 1
7 7
7 4
Output
2
3
2
3
2
3
1
1
3
Note
In the first example, for a=[10,20,10] the optimal subsequences are:
* for k=1: [20],
* for k=2: [10,20],
* for k=3: [10,20,10].
|
{
"input": [
"3\n10 20 10\n6\n1 1\n2 1\n2 2\n3 1\n3 2\n3 3\n",
"7\n1 2 1 3 1 2 1\n9\n2 1\n2 2\n3 1\n3 2\n3 3\n1 1\n7 1\n7 7\n7 4\n"
],
"output": [
"20\n10\n20\n10\n20\n10\n",
"2\n3\n2\n3\n2\n3\n1\n1\n3\n"
]
}
|
{
"input": [
"2\n1 10\n3\n2 2\n2 1\n1 1\n",
"2\n3922 3922\n3\n2 2\n2 1\n1 1\n",
"1\n1000000000\n1\n1 1\n",
"1\n1\n3\n1 1\n1 1\n1 1\n",
"5\n3 1 4 1 2\n15\n5 5\n5 4\n5 3\n5 2\n5 1\n4 4\n4 3\n4 2\n4 1\n3 3\n3 2\n3 1\n2 2\n2 1\n1 1\n",
"2\n392222 322\n3\n2 2\n2 1\n1 1\n"
],
"output": [
"10\n1\n10\n",
"3922\n3922\n3922\n",
"1000000000\n",
"1\n1\n1\n",
"2\n1\n4\n1\n3\n2\n4\n1\n3\n2\n4\n3\n4\n3\n4\n",
"322\n392222\n392222\n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const int Z = (int)3e3 + 228;
const int N = (int)3e5 + 228;
const int INF = (int)1e9 + 228;
const int MOD = (int)1e9 + 7;
int a[N];
map<int, int> cnt;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
vector<int> b;
for (int i = 1; i <= n; i++) {
cin >> a[i];
b.push_back(a[i]);
}
sort(b.rbegin(), b.rend());
int m;
cin >> m;
while (m--) {
int k, pos;
cin >> k >> pos;
multiset<int> mn;
map<int, int> q;
for (int i = 0; i < k; i++) mn.insert(b[i]), q[b[i]]++;
vector<int> ans;
for (int i = 1; i <= n; i++) {
if (k == 0) break;
if (mn.find(a[i]) != mn.end()) {
if (a[i] == *mn.begin()) {
ans.push_back(a[i]);
q[a[i]]--;
mn.erase(mn.begin());
k--;
}
map<int, int> now;
for (int j = i + 1; j <= n; j++) now[a[j]]++;
bool f = true;
for (auto it : mn)
if (q[it] > now[it]) f = false;
if (f)
continue;
else {
ans.push_back(a[i]);
q[a[i]]--;
mn.erase(mn.find(a[i]));
k--;
}
}
}
cout << ans[pos - 1] << "\n";
}
return 0;
}
|
1227_D1. Optimal Subsequences (Easy Version)
|
This is the easier version of the problem. In this version 1 β€ n, m β€ 100. You can hack this problem only if you solve and lock both problems.
You are given a sequence of integers a=[a_1,a_2,...,a_n] of length n. Its subsequence is obtained by removing zero or more elements from the sequence a (they do not necessarily go consecutively). For example, for the sequence a=[11,20,11,33,11,20,11]:
* [11,20,11,33,11,20,11], [11,20,11,33,11,20], [11,11,11,11], [20], [33,20] are subsequences (these are just some of the long list);
* [40], [33,33], [33,20,20], [20,20,11,11] are not subsequences.
Suppose that an additional non-negative integer k (1 β€ k β€ n) is given, then the subsequence is called optimal if:
* it has a length of k and the sum of its elements is the maximum possible among all subsequences of length k;
* and among all subsequences of length k that satisfy the previous item, it is lexicographically minimal.
Recall that the sequence b=[b_1, b_2, ..., b_k] is lexicographically smaller than the sequence c=[c_1, c_2, ..., c_k] if the first element (from the left) in which they differ less in the sequence b than in c. Formally: there exists t (1 β€ t β€ k) such that b_1=c_1, b_2=c_2, ..., b_{t-1}=c_{t-1} and at the same time b_t<c_t. For example:
* [10, 20, 20] lexicographically less than [10, 21, 1],
* [7, 99, 99] is lexicographically less than [10, 21, 1],
* [10, 21, 0] is lexicographically less than [10, 21, 1].
You are given a sequence of a=[a_1,a_2,...,a_n] and m requests, each consisting of two numbers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j). For each query, print the value that is in the index pos_j of the optimal subsequence of the given sequence a for k=k_j.
For example, if n=4, a=[10,20,30,20], k_j=2, then the optimal subsequence is [20,30] β it is the minimum lexicographically among all subsequences of length 2 with the maximum total sum of items. Thus, the answer to the request k_j=2, pos_j=1 is the number 20, and the answer to the request k_j=2, pos_j=2 is the number 30.
Input
The first line contains an integer n (1 β€ n β€ 100) β the length of the sequence a.
The second line contains elements of the sequence a: integer numbers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9).
The third line contains an integer m (1 β€ m β€ 100) β the number of requests.
The following m lines contain pairs of integers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j) β the requests.
Output
Print m integers r_1, r_2, ..., r_m (1 β€ r_j β€ 10^9) one per line: answers to the requests in the order they appear in the input. The value of r_j should be equal to the value contained in the position pos_j of the optimal subsequence for k=k_j.
Examples
Input
3
10 20 10
6
1 1
2 1
2 2
3 1
3 2
3 3
Output
20
10
20
10
20
10
Input
7
1 2 1 3 1 2 1
9
2 1
2 2
3 1
3 2
3 3
1 1
7 1
7 7
7 4
Output
2
3
2
3
2
3
1
1
3
Note
In the first example, for a=[10,20,10] the optimal subsequences are:
* for k=1: [20],
* for k=2: [10,20],
* for k=3: [10,20,10].
|
{
"input": [
"3\n10 20 10\n6\n1 1\n2 1\n2 2\n3 1\n3 2\n3 3\n",
"7\n1 2 1 3 1 2 1\n9\n2 1\n2 2\n3 1\n3 2\n3 3\n1 1\n7 1\n7 7\n7 4\n"
],
"output": [
"20\n10\n20\n10\n20\n10\n",
"2\n3\n2\n3\n2\n3\n1\n1\n3\n"
]
}
|
{
"input": [
"2\n1 10\n3\n2 2\n2 1\n1 1\n",
"2\n3922 3922\n3\n2 2\n2 1\n1 1\n",
"1\n1000000000\n1\n1 1\n",
"1\n1\n3\n1 1\n1 1\n1 1\n",
"5\n3 1 4 1 2\n15\n5 5\n5 4\n5 3\n5 2\n5 1\n4 4\n4 3\n4 2\n4 1\n3 3\n3 2\n3 1\n2 2\n2 1\n1 1\n",
"2\n392222 322\n3\n2 2\n2 1\n1 1\n"
],
"output": [
"10\n1\n10\n",
"3922\n3922\n3922\n",
"1000000000\n",
"1\n1\n1\n",
"2\n1\n4\n1\n3\n2\n4\n1\n3\n2\n4\n3\n4\n3\n4\n",
"322\n392222\n392222\n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
int n, q, arr[101], sr[101];
vector<int> dp[101];
multiset<int> s;
int main() {
cin >> n;
for (long long i = 0; i < n; i++) {
cin >> arr[i];
sr[i] = arr[i];
}
sort(sr, sr + n, [](int a, int b) { return a > b; });
for (long long i = 1; i <= n; i++) {
for (long long j = 0; j < i; j++) {
s.insert(sr[j]);
}
for (long long j = 0; j < n; j++) {
if (s.count(arr[j])) {
dp[i].push_back(arr[j]);
s.erase(s.find(arr[j]));
}
}
}
cin >> q;
while (q--) {
int a, b;
cin >> a >> b;
cout << dp[a][b - 1] << endl;
}
return 0;
}
|
1227_D1. Optimal Subsequences (Easy Version)
|
This is the easier version of the problem. In this version 1 β€ n, m β€ 100. You can hack this problem only if you solve and lock both problems.
You are given a sequence of integers a=[a_1,a_2,...,a_n] of length n. Its subsequence is obtained by removing zero or more elements from the sequence a (they do not necessarily go consecutively). For example, for the sequence a=[11,20,11,33,11,20,11]:
* [11,20,11,33,11,20,11], [11,20,11,33,11,20], [11,11,11,11], [20], [33,20] are subsequences (these are just some of the long list);
* [40], [33,33], [33,20,20], [20,20,11,11] are not subsequences.
Suppose that an additional non-negative integer k (1 β€ k β€ n) is given, then the subsequence is called optimal if:
* it has a length of k and the sum of its elements is the maximum possible among all subsequences of length k;
* and among all subsequences of length k that satisfy the previous item, it is lexicographically minimal.
Recall that the sequence b=[b_1, b_2, ..., b_k] is lexicographically smaller than the sequence c=[c_1, c_2, ..., c_k] if the first element (from the left) in which they differ less in the sequence b than in c. Formally: there exists t (1 β€ t β€ k) such that b_1=c_1, b_2=c_2, ..., b_{t-1}=c_{t-1} and at the same time b_t<c_t. For example:
* [10, 20, 20] lexicographically less than [10, 21, 1],
* [7, 99, 99] is lexicographically less than [10, 21, 1],
* [10, 21, 0] is lexicographically less than [10, 21, 1].
You are given a sequence of a=[a_1,a_2,...,a_n] and m requests, each consisting of two numbers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j). For each query, print the value that is in the index pos_j of the optimal subsequence of the given sequence a for k=k_j.
For example, if n=4, a=[10,20,30,20], k_j=2, then the optimal subsequence is [20,30] β it is the minimum lexicographically among all subsequences of length 2 with the maximum total sum of items. Thus, the answer to the request k_j=2, pos_j=1 is the number 20, and the answer to the request k_j=2, pos_j=2 is the number 30.
Input
The first line contains an integer n (1 β€ n β€ 100) β the length of the sequence a.
The second line contains elements of the sequence a: integer numbers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9).
The third line contains an integer m (1 β€ m β€ 100) β the number of requests.
The following m lines contain pairs of integers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j) β the requests.
Output
Print m integers r_1, r_2, ..., r_m (1 β€ r_j β€ 10^9) one per line: answers to the requests in the order they appear in the input. The value of r_j should be equal to the value contained in the position pos_j of the optimal subsequence for k=k_j.
Examples
Input
3
10 20 10
6
1 1
2 1
2 2
3 1
3 2
3 3
Output
20
10
20
10
20
10
Input
7
1 2 1 3 1 2 1
9
2 1
2 2
3 1
3 2
3 3
1 1
7 1
7 7
7 4
Output
2
3
2
3
2
3
1
1
3
Note
In the first example, for a=[10,20,10] the optimal subsequences are:
* for k=1: [20],
* for k=2: [10,20],
* for k=3: [10,20,10].
|
{
"input": [
"3\n10 20 10\n6\n1 1\n2 1\n2 2\n3 1\n3 2\n3 3\n",
"7\n1 2 1 3 1 2 1\n9\n2 1\n2 2\n3 1\n3 2\n3 3\n1 1\n7 1\n7 7\n7 4\n"
],
"output": [
"20\n10\n20\n10\n20\n10\n",
"2\n3\n2\n3\n2\n3\n1\n1\n3\n"
]
}
|
{
"input": [
"2\n1 10\n3\n2 2\n2 1\n1 1\n",
"2\n3922 3922\n3\n2 2\n2 1\n1 1\n",
"1\n1000000000\n1\n1 1\n",
"1\n1\n3\n1 1\n1 1\n1 1\n",
"5\n3 1 4 1 2\n15\n5 5\n5 4\n5 3\n5 2\n5 1\n4 4\n4 3\n4 2\n4 1\n3 3\n3 2\n3 1\n2 2\n2 1\n1 1\n",
"2\n392222 322\n3\n2 2\n2 1\n1 1\n"
],
"output": [
"10\n1\n10\n",
"3922\n3922\n3922\n",
"1000000000\n",
"1\n1\n1\n",
"2\n1\n4\n1\n3\n2\n4\n1\n3\n2\n4\n3\n4\n3\n4\n",
"322\n392222\n392222\n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
void func() {
int n;
cin >> n;
vector<int> arr(n), acop(n);
for (int i = 0; i < n; ++i) {
cin >> arr[i];
acop[i] = arr[i];
}
sort(acop.begin(), acop.end(), greater<int>());
int q;
cin >> q;
for (int i = 0; i < q; ++i) {
int k, pos, cnt = 0;
cin >> k >> pos;
for (int i = 0; i < n; ++i) {
if (arr[i] > acop[k - 1]) {
++cnt;
}
}
int cnt1 = k - cnt;
cnt = 0;
for (int i = 0; i < n; ++i) {
if (arr[i] >= acop[k - 1]) {
if (arr[i] > acop[k - 1]) {
++cnt;
} else if (cnt1 > 0 && arr[i] == acop[k - 1]) {
++cnt;
--cnt1;
}
if (cnt == pos) {
cout << arr[i] << "\n";
break;
}
}
}
}
}
int main() {
int t = 1;
int cnt = 0;
while (t--) {
func();
}
return 0;
}
|
1227_D1. Optimal Subsequences (Easy Version)
|
This is the easier version of the problem. In this version 1 β€ n, m β€ 100. You can hack this problem only if you solve and lock both problems.
You are given a sequence of integers a=[a_1,a_2,...,a_n] of length n. Its subsequence is obtained by removing zero or more elements from the sequence a (they do not necessarily go consecutively). For example, for the sequence a=[11,20,11,33,11,20,11]:
* [11,20,11,33,11,20,11], [11,20,11,33,11,20], [11,11,11,11], [20], [33,20] are subsequences (these are just some of the long list);
* [40], [33,33], [33,20,20], [20,20,11,11] are not subsequences.
Suppose that an additional non-negative integer k (1 β€ k β€ n) is given, then the subsequence is called optimal if:
* it has a length of k and the sum of its elements is the maximum possible among all subsequences of length k;
* and among all subsequences of length k that satisfy the previous item, it is lexicographically minimal.
Recall that the sequence b=[b_1, b_2, ..., b_k] is lexicographically smaller than the sequence c=[c_1, c_2, ..., c_k] if the first element (from the left) in which they differ less in the sequence b than in c. Formally: there exists t (1 β€ t β€ k) such that b_1=c_1, b_2=c_2, ..., b_{t-1}=c_{t-1} and at the same time b_t<c_t. For example:
* [10, 20, 20] lexicographically less than [10, 21, 1],
* [7, 99, 99] is lexicographically less than [10, 21, 1],
* [10, 21, 0] is lexicographically less than [10, 21, 1].
You are given a sequence of a=[a_1,a_2,...,a_n] and m requests, each consisting of two numbers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j). For each query, print the value that is in the index pos_j of the optimal subsequence of the given sequence a for k=k_j.
For example, if n=4, a=[10,20,30,20], k_j=2, then the optimal subsequence is [20,30] β it is the minimum lexicographically among all subsequences of length 2 with the maximum total sum of items. Thus, the answer to the request k_j=2, pos_j=1 is the number 20, and the answer to the request k_j=2, pos_j=2 is the number 30.
Input
The first line contains an integer n (1 β€ n β€ 100) β the length of the sequence a.
The second line contains elements of the sequence a: integer numbers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9).
The third line contains an integer m (1 β€ m β€ 100) β the number of requests.
The following m lines contain pairs of integers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j) β the requests.
Output
Print m integers r_1, r_2, ..., r_m (1 β€ r_j β€ 10^9) one per line: answers to the requests in the order they appear in the input. The value of r_j should be equal to the value contained in the position pos_j of the optimal subsequence for k=k_j.
Examples
Input
3
10 20 10
6
1 1
2 1
2 2
3 1
3 2
3 3
Output
20
10
20
10
20
10
Input
7
1 2 1 3 1 2 1
9
2 1
2 2
3 1
3 2
3 3
1 1
7 1
7 7
7 4
Output
2
3
2
3
2
3
1
1
3
Note
In the first example, for a=[10,20,10] the optimal subsequences are:
* for k=1: [20],
* for k=2: [10,20],
* for k=3: [10,20,10].
|
{
"input": [
"3\n10 20 10\n6\n1 1\n2 1\n2 2\n3 1\n3 2\n3 3\n",
"7\n1 2 1 3 1 2 1\n9\n2 1\n2 2\n3 1\n3 2\n3 3\n1 1\n7 1\n7 7\n7 4\n"
],
"output": [
"20\n10\n20\n10\n20\n10\n",
"2\n3\n2\n3\n2\n3\n1\n1\n3\n"
]
}
|
{
"input": [
"2\n1 10\n3\n2 2\n2 1\n1 1\n",
"2\n3922 3922\n3\n2 2\n2 1\n1 1\n",
"1\n1000000000\n1\n1 1\n",
"1\n1\n3\n1 1\n1 1\n1 1\n",
"5\n3 1 4 1 2\n15\n5 5\n5 4\n5 3\n5 2\n5 1\n4 4\n4 3\n4 2\n4 1\n3 3\n3 2\n3 1\n2 2\n2 1\n1 1\n",
"2\n392222 322\n3\n2 2\n2 1\n1 1\n"
],
"output": [
"10\n1\n10\n",
"3922\n3922\n3922\n",
"1000000000\n",
"1\n1\n1\n",
"2\n1\n4\n1\n3\n2\n4\n1\n3\n2\n4\n3\n4\n3\n4\n",
"322\n392222\n392222\n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1000000007;
const long long INF = 1e9 + 5;
const double eps = 1e-7;
const double PI = acos(-1.0);
inline void debug_vi(vector<int> a) {
for (long long i = (long long)(0); i < (long long)(a.size()); i++)
cout << a[i] << " ";
}
inline void debug_vll(vector<long long> a) {
for (long long i = (long long)(0); i < (long long)(a.size()); i++)
cout << a[i] << " ";
}
inline void print_case(int tn) { cout << "Case #" << tn << ": "; }
template <typename T>
using minpq = priority_queue<T, vector<T>, greater<T>>;
template <typename T>
using maxpq = priority_queue<T>;
const int N = 2e5 + 5;
int a[N], arr[N];
vector<pair<int, int>> vals;
int n;
struct SegmentTreeNode {
int sm;
void assignLeaf(int value) { sm = value; }
void merge(SegmentTreeNode& left, SegmentTreeNode& right) {
sm = left.sm + right.sm;
}
int getValue() { return sm; }
};
template <class T, class V>
class SegmentTree {
SegmentTreeNode* nodes;
int N;
public:
SegmentTree(T arr[], int N) {
this->N = N;
nodes = new SegmentTreeNode[getSegmentTreeSize(N)];
buildTree(arr, 1, 0, N - 1);
}
~SegmentTree() { delete[] nodes; }
V getValue(int pos) { return getValue(1, 0, N - 1, pos); }
void update(int index, T value) { update(1, 0, N - 1, index, value); }
private:
void buildTree(T arr[], int stIndex, int lo, int hi) {
if (lo == hi) {
nodes[stIndex].assignLeaf(arr[lo]);
return;
}
int left = 2 * stIndex, right = left + 1, mid = lo + (hi - lo) / 2;
buildTree(arr, left, lo, mid);
buildTree(arr, right, mid + 1, hi);
nodes[stIndex].merge(nodes[left], nodes[right]);
}
int getValue(int stIndex, int left, int right, int pos) {
if (left == right) {
return left;
}
int mid = (left + right) / 2;
if (nodes[2 * stIndex].getValue() >= pos)
return getValue(2 * stIndex, left, mid, pos);
return getValue(2 * stIndex + 1, mid + 1, right,
pos - nodes[2 * stIndex].getValue());
}
int getSegmentTreeSize(int N) { return 4 * N; }
void update(int stIndex, int lo, int hi, int index, T value) {
if (lo == hi) {
nodes[stIndex].assignLeaf(value);
return;
}
int left = 2 * stIndex, right = left + 1, mid = lo + (hi - lo) / 2;
if (index <= mid)
update(left, lo, mid, index, value);
else
update(right, mid + 1, hi, index, value);
nodes[stIndex].merge(nodes[left], nodes[right]);
}
};
bool cmp(pair<int, int> a, pair<int, int> b) {
if (a.first != b.first) return a.first > b.first;
return a.second < b.second;
}
int main() {
std::ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
clock_t clk = clock();
cin >> n;
vals.resize(n);
for (long long i = (long long)(0); i < (long long)(n); i++) {
cin >> a[i];
vals[i] = {a[i], i};
arr[i] = 1;
}
sort((vals).begin(), (vals).end(), cmp);
SegmentTree<int, int> st(arr, n);
int m;
cin >> m;
vector<tuple<int, int, int>> reqs(m);
int k, pos, idx;
for (long long i = (long long)(0); i < (long long)(m); i++) {
cin >> k >> pos;
reqs[i] = make_tuple(k, pos, i);
}
sort((reqs).begin(), (reqs).end());
vector<int> res(m);
int curr = n;
for (long long i = (long long)(m - 1); i >= (long long)(0); i--) {
tie(k, pos, idx) = reqs[i];
while (curr > k) {
st.update(vals[curr - 1].second, 0);
curr--;
}
res[idx] = a[st.getValue(pos)];
}
for (int a : res) {
cout << a << "\n";
}
cerr << "\n"
<< "Time (in ms): " << double(clock() - clk) * 1000.0 / CLOCKS_PER_SEC
<< "\n";
return 0;
}
|
1227_D1. Optimal Subsequences (Easy Version)
|
This is the easier version of the problem. In this version 1 β€ n, m β€ 100. You can hack this problem only if you solve and lock both problems.
You are given a sequence of integers a=[a_1,a_2,...,a_n] of length n. Its subsequence is obtained by removing zero or more elements from the sequence a (they do not necessarily go consecutively). For example, for the sequence a=[11,20,11,33,11,20,11]:
* [11,20,11,33,11,20,11], [11,20,11,33,11,20], [11,11,11,11], [20], [33,20] are subsequences (these are just some of the long list);
* [40], [33,33], [33,20,20], [20,20,11,11] are not subsequences.
Suppose that an additional non-negative integer k (1 β€ k β€ n) is given, then the subsequence is called optimal if:
* it has a length of k and the sum of its elements is the maximum possible among all subsequences of length k;
* and among all subsequences of length k that satisfy the previous item, it is lexicographically minimal.
Recall that the sequence b=[b_1, b_2, ..., b_k] is lexicographically smaller than the sequence c=[c_1, c_2, ..., c_k] if the first element (from the left) in which they differ less in the sequence b than in c. Formally: there exists t (1 β€ t β€ k) such that b_1=c_1, b_2=c_2, ..., b_{t-1}=c_{t-1} and at the same time b_t<c_t. For example:
* [10, 20, 20] lexicographically less than [10, 21, 1],
* [7, 99, 99] is lexicographically less than [10, 21, 1],
* [10, 21, 0] is lexicographically less than [10, 21, 1].
You are given a sequence of a=[a_1,a_2,...,a_n] and m requests, each consisting of two numbers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j). For each query, print the value that is in the index pos_j of the optimal subsequence of the given sequence a for k=k_j.
For example, if n=4, a=[10,20,30,20], k_j=2, then the optimal subsequence is [20,30] β it is the minimum lexicographically among all subsequences of length 2 with the maximum total sum of items. Thus, the answer to the request k_j=2, pos_j=1 is the number 20, and the answer to the request k_j=2, pos_j=2 is the number 30.
Input
The first line contains an integer n (1 β€ n β€ 100) β the length of the sequence a.
The second line contains elements of the sequence a: integer numbers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9).
The third line contains an integer m (1 β€ m β€ 100) β the number of requests.
The following m lines contain pairs of integers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j) β the requests.
Output
Print m integers r_1, r_2, ..., r_m (1 β€ r_j β€ 10^9) one per line: answers to the requests in the order they appear in the input. The value of r_j should be equal to the value contained in the position pos_j of the optimal subsequence for k=k_j.
Examples
Input
3
10 20 10
6
1 1
2 1
2 2
3 1
3 2
3 3
Output
20
10
20
10
20
10
Input
7
1 2 1 3 1 2 1
9
2 1
2 2
3 1
3 2
3 3
1 1
7 1
7 7
7 4
Output
2
3
2
3
2
3
1
1
3
Note
In the first example, for a=[10,20,10] the optimal subsequences are:
* for k=1: [20],
* for k=2: [10,20],
* for k=3: [10,20,10].
|
{
"input": [
"3\n10 20 10\n6\n1 1\n2 1\n2 2\n3 1\n3 2\n3 3\n",
"7\n1 2 1 3 1 2 1\n9\n2 1\n2 2\n3 1\n3 2\n3 3\n1 1\n7 1\n7 7\n7 4\n"
],
"output": [
"20\n10\n20\n10\n20\n10\n",
"2\n3\n2\n3\n2\n3\n1\n1\n3\n"
]
}
|
{
"input": [
"2\n1 10\n3\n2 2\n2 1\n1 1\n",
"2\n3922 3922\n3\n2 2\n2 1\n1 1\n",
"1\n1000000000\n1\n1 1\n",
"1\n1\n3\n1 1\n1 1\n1 1\n",
"5\n3 1 4 1 2\n15\n5 5\n5 4\n5 3\n5 2\n5 1\n4 4\n4 3\n4 2\n4 1\n3 3\n3 2\n3 1\n2 2\n2 1\n1 1\n",
"2\n392222 322\n3\n2 2\n2 1\n1 1\n"
],
"output": [
"10\n1\n10\n",
"3922\n3922\n3922\n",
"1000000000\n",
"1\n1\n1\n",
"2\n1\n4\n1\n3\n2\n4\n1\n3\n2\n4\n3\n4\n3\n4\n",
"322\n392222\n392222\n"
]
}
|
CORRECT
|
java
|
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.*;
public class OptimalSubsequences {
InputStream is;
PrintWriter pw;
String INPUT = "";
long L_INF = (1L << 60L);
void solve() {
int k,pos,n=ni(), m;
int a[] = na(n);
m = ni();
List<Integer> cache[] = new ArrayList[n];
while(m-->0){
k=ni();
pos = ni();
if(cache[k-1]==null){
cache[k-1] = calc(a,k);
}
pw.println(cache[k-1].get(pos-1));
}
// for (int i = 0; i < n; i++) {
// System.out.println(cache[i]);
// }
}
private List<Integer> calc(int[] a, int k) {
PriorityQueue<int[]> pq = new PriorityQueue<>(new Comparator<int[]>() {
@Override
public int compare(int[] o1, int[] o2) {
int x = Integer.compare(o1[0],o2[0]);
if(x!=0)
return x;
return -Integer.compare(o1[1], o2[1]);
}
});
for (int i = 0; i < a.length; i++) {
if(pq.size()<k){
pq.add(new int[]{a[i],i});
}
else if(pq.size()==k && pq.peek()[0]<a[i]) {
pq.poll();
pq.add(new int[]{a[i], i});
}
}
TreeMap<Integer,Integer> mp = new TreeMap<>();
while(!pq.isEmpty()){
int t[] = pq.poll();
mp.put(t[1],t[0]);
}
// System.out.println(mp);
List<Integer> ans = new ArrayList<>();
for (Integer value : mp.values()) {
ans.add(value);
}
return ans;
}
void run() throws Exception {
// is = oj ? System.in : new ByteArrayInputStream(INPUT.getBytes());
is = System.in;
pw = new PrintWriter(System.out);
long s = System.currentTimeMillis();
// int t = ni();
// while (t-- > 0)
solve();
pw.flush();
tr(System.currentTimeMillis() - s + "ms");
}
public static void main(String[] args) throws Exception {
new OptimalSubsequences().run();
}
private byte[] inbuf = new byte[1024];
private int lenbuf = 0, ptrbuf = 0;
private int readByte() {
if (lenbuf == -1) throw new InputMismatchException();
if (ptrbuf >= lenbuf) {
ptrbuf = 0;
try {
lenbuf = is.read(inbuf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (lenbuf <= 0) return -1;
}
return inbuf[ptrbuf++];
}
private boolean isSpaceChar(int c) {
return !(c >= 33 && c <= 126);
}
private int skip() {
int b;
while ((b = readByte()) != -1 && isSpaceChar(b)) ;
return b;
}
private double nd() {
return Double.parseDouble(ns());
}
private char nc() {
return (char) skip();
}
private String ns() {
int b = skip();
StringBuilder sb = new StringBuilder();
while (!(isSpaceChar(b))) { // when nextLine, (isSpaceChar(b) && b != ' ')
sb.appendCodePoint(b);
b = readByte();
}
return sb.toString();
}
private char[] ns(int n) {
char[] buf = new char[n];
int b = skip(), p = 0;
while (p < n && !(isSpaceChar(b))) {
buf[p++] = (char) b;
b = readByte();
}
return n == p ? buf : Arrays.copyOf(buf, p);
}
private char[][] nm(int n, int m) {
char[][] map = new char[n][];
for (int i = 0; i < n; i++) map[i] = ns(m);
return map;
}
private int[] na(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++) a[i] = ni();
return a;
}
private int ni() {
int num = 0, b;
boolean minus = false;
while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ;
if (b == '-') {
minus = true;
b = readByte();
}
while (true) {
if (b >= '0' && b <= '9') {
num = num * 10 + (b - '0');
} else {
return minus ? -num : num;
}
b = readByte();
}
}
private long nl() {
long num = 0;
int b;
boolean minus = false;
while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ;
if (b == '-') {
minus = true;
b = readByte();
}
while (true) {
if (b >= '0' && b <= '9') {
num = num * 10 + (b - '0');
} else {
return minus ? -num : num;
}
b = readByte();
}
}
private boolean oj = System.getProperty("ONLINE_JUDGE") != null;
private void tr(Object... o) {
if (!oj) System.out.println(Arrays.deepToString(o));
}
}
|
1227_D1. Optimal Subsequences (Easy Version)
|
This is the easier version of the problem. In this version 1 β€ n, m β€ 100. You can hack this problem only if you solve and lock both problems.
You are given a sequence of integers a=[a_1,a_2,...,a_n] of length n. Its subsequence is obtained by removing zero or more elements from the sequence a (they do not necessarily go consecutively). For example, for the sequence a=[11,20,11,33,11,20,11]:
* [11,20,11,33,11,20,11], [11,20,11,33,11,20], [11,11,11,11], [20], [33,20] are subsequences (these are just some of the long list);
* [40], [33,33], [33,20,20], [20,20,11,11] are not subsequences.
Suppose that an additional non-negative integer k (1 β€ k β€ n) is given, then the subsequence is called optimal if:
* it has a length of k and the sum of its elements is the maximum possible among all subsequences of length k;
* and among all subsequences of length k that satisfy the previous item, it is lexicographically minimal.
Recall that the sequence b=[b_1, b_2, ..., b_k] is lexicographically smaller than the sequence c=[c_1, c_2, ..., c_k] if the first element (from the left) in which they differ less in the sequence b than in c. Formally: there exists t (1 β€ t β€ k) such that b_1=c_1, b_2=c_2, ..., b_{t-1}=c_{t-1} and at the same time b_t<c_t. For example:
* [10, 20, 20] lexicographically less than [10, 21, 1],
* [7, 99, 99] is lexicographically less than [10, 21, 1],
* [10, 21, 0] is lexicographically less than [10, 21, 1].
You are given a sequence of a=[a_1,a_2,...,a_n] and m requests, each consisting of two numbers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j). For each query, print the value that is in the index pos_j of the optimal subsequence of the given sequence a for k=k_j.
For example, if n=4, a=[10,20,30,20], k_j=2, then the optimal subsequence is [20,30] β it is the minimum lexicographically among all subsequences of length 2 with the maximum total sum of items. Thus, the answer to the request k_j=2, pos_j=1 is the number 20, and the answer to the request k_j=2, pos_j=2 is the number 30.
Input
The first line contains an integer n (1 β€ n β€ 100) β the length of the sequence a.
The second line contains elements of the sequence a: integer numbers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9).
The third line contains an integer m (1 β€ m β€ 100) β the number of requests.
The following m lines contain pairs of integers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j) β the requests.
Output
Print m integers r_1, r_2, ..., r_m (1 β€ r_j β€ 10^9) one per line: answers to the requests in the order they appear in the input. The value of r_j should be equal to the value contained in the position pos_j of the optimal subsequence for k=k_j.
Examples
Input
3
10 20 10
6
1 1
2 1
2 2
3 1
3 2
3 3
Output
20
10
20
10
20
10
Input
7
1 2 1 3 1 2 1
9
2 1
2 2
3 1
3 2
3 3
1 1
7 1
7 7
7 4
Output
2
3
2
3
2
3
1
1
3
Note
In the first example, for a=[10,20,10] the optimal subsequences are:
* for k=1: [20],
* for k=2: [10,20],
* for k=3: [10,20,10].
|
{
"input": [
"3\n10 20 10\n6\n1 1\n2 1\n2 2\n3 1\n3 2\n3 3\n",
"7\n1 2 1 3 1 2 1\n9\n2 1\n2 2\n3 1\n3 2\n3 3\n1 1\n7 1\n7 7\n7 4\n"
],
"output": [
"20\n10\n20\n10\n20\n10\n",
"2\n3\n2\n3\n2\n3\n1\n1\n3\n"
]
}
|
{
"input": [
"2\n1 10\n3\n2 2\n2 1\n1 1\n",
"2\n3922 3922\n3\n2 2\n2 1\n1 1\n",
"1\n1000000000\n1\n1 1\n",
"1\n1\n3\n1 1\n1 1\n1 1\n",
"5\n3 1 4 1 2\n15\n5 5\n5 4\n5 3\n5 2\n5 1\n4 4\n4 3\n4 2\n4 1\n3 3\n3 2\n3 1\n2 2\n2 1\n1 1\n",
"2\n392222 322\n3\n2 2\n2 1\n1 1\n"
],
"output": [
"10\n1\n10\n",
"3922\n3922\n3922\n",
"1000000000\n",
"1\n1\n1\n",
"2\n1\n4\n1\n3\n2\n4\n1\n3\n2\n4\n3\n4\n3\n4\n",
"322\n392222\n392222\n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
template <typename T1, typename T2, typename T3>
struct pair3 {
T1 first;
T2 second;
T3 third;
};
template <typename T1, typename T2, typename T3, typename T4>
struct pair4 {
T1 first;
T2 second;
T3 third;
T4 fourth;
};
const long long MOD = 1000000007;
const long double PI = acos(-1.0);
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long n, m;
cin >> n;
vector<long long> ar(n);
for (long long i = 0; i < n; ++i) {
cin >> ar[i];
}
vector<vector<long long>> ans(n + 1);
for (long long len = 1; len <= n; ++len) {
vector<long long> temp;
long long mx = -1, mn = INT_MAX;
multiset<long long> st;
auto del = [&](long long val) {
auto it = temp.end();
--it;
while (1) {
if (*it == val) {
temp.erase(it);
break;
}
it -= 1;
}
};
for (long long start = 0; start < n; ++start) {
if (temp.size() < len) {
temp.push_back(ar[start]);
st.insert(ar[start]);
} else {
if (ar[start] > *st.begin()) {
del(*st.begin());
st.erase(st.begin());
st.insert(ar[start]);
temp.push_back(ar[start]);
}
}
ans[len] = temp;
}
}
cin >> m;
long long l, idx;
while (m--) {
cin >> l >> idx;
cout << ans[l][idx - 1] << endl;
}
return 0;
}
|
1227_D1. Optimal Subsequences (Easy Version)
|
This is the easier version of the problem. In this version 1 β€ n, m β€ 100. You can hack this problem only if you solve and lock both problems.
You are given a sequence of integers a=[a_1,a_2,...,a_n] of length n. Its subsequence is obtained by removing zero or more elements from the sequence a (they do not necessarily go consecutively). For example, for the sequence a=[11,20,11,33,11,20,11]:
* [11,20,11,33,11,20,11], [11,20,11,33,11,20], [11,11,11,11], [20], [33,20] are subsequences (these are just some of the long list);
* [40], [33,33], [33,20,20], [20,20,11,11] are not subsequences.
Suppose that an additional non-negative integer k (1 β€ k β€ n) is given, then the subsequence is called optimal if:
* it has a length of k and the sum of its elements is the maximum possible among all subsequences of length k;
* and among all subsequences of length k that satisfy the previous item, it is lexicographically minimal.
Recall that the sequence b=[b_1, b_2, ..., b_k] is lexicographically smaller than the sequence c=[c_1, c_2, ..., c_k] if the first element (from the left) in which they differ less in the sequence b than in c. Formally: there exists t (1 β€ t β€ k) such that b_1=c_1, b_2=c_2, ..., b_{t-1}=c_{t-1} and at the same time b_t<c_t. For example:
* [10, 20, 20] lexicographically less than [10, 21, 1],
* [7, 99, 99] is lexicographically less than [10, 21, 1],
* [10, 21, 0] is lexicographically less than [10, 21, 1].
You are given a sequence of a=[a_1,a_2,...,a_n] and m requests, each consisting of two numbers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j). For each query, print the value that is in the index pos_j of the optimal subsequence of the given sequence a for k=k_j.
For example, if n=4, a=[10,20,30,20], k_j=2, then the optimal subsequence is [20,30] β it is the minimum lexicographically among all subsequences of length 2 with the maximum total sum of items. Thus, the answer to the request k_j=2, pos_j=1 is the number 20, and the answer to the request k_j=2, pos_j=2 is the number 30.
Input
The first line contains an integer n (1 β€ n β€ 100) β the length of the sequence a.
The second line contains elements of the sequence a: integer numbers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9).
The third line contains an integer m (1 β€ m β€ 100) β the number of requests.
The following m lines contain pairs of integers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j) β the requests.
Output
Print m integers r_1, r_2, ..., r_m (1 β€ r_j β€ 10^9) one per line: answers to the requests in the order they appear in the input. The value of r_j should be equal to the value contained in the position pos_j of the optimal subsequence for k=k_j.
Examples
Input
3
10 20 10
6
1 1
2 1
2 2
3 1
3 2
3 3
Output
20
10
20
10
20
10
Input
7
1 2 1 3 1 2 1
9
2 1
2 2
3 1
3 2
3 3
1 1
7 1
7 7
7 4
Output
2
3
2
3
2
3
1
1
3
Note
In the first example, for a=[10,20,10] the optimal subsequences are:
* for k=1: [20],
* for k=2: [10,20],
* for k=3: [10,20,10].
|
{
"input": [
"3\n10 20 10\n6\n1 1\n2 1\n2 2\n3 1\n3 2\n3 3\n",
"7\n1 2 1 3 1 2 1\n9\n2 1\n2 2\n3 1\n3 2\n3 3\n1 1\n7 1\n7 7\n7 4\n"
],
"output": [
"20\n10\n20\n10\n20\n10\n",
"2\n3\n2\n3\n2\n3\n1\n1\n3\n"
]
}
|
{
"input": [
"2\n1 10\n3\n2 2\n2 1\n1 1\n",
"2\n3922 3922\n3\n2 2\n2 1\n1 1\n",
"1\n1000000000\n1\n1 1\n",
"1\n1\n3\n1 1\n1 1\n1 1\n",
"5\n3 1 4 1 2\n15\n5 5\n5 4\n5 3\n5 2\n5 1\n4 4\n4 3\n4 2\n4 1\n3 3\n3 2\n3 1\n2 2\n2 1\n1 1\n",
"2\n392222 322\n3\n2 2\n2 1\n1 1\n"
],
"output": [
"10\n1\n10\n",
"3922\n3922\n3922\n",
"1000000000\n",
"1\n1\n1\n",
"2\n1\n4\n1\n3\n2\n4\n1\n3\n2\n4\n3\n4\n3\n4\n",
"322\n392222\n392222\n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
struct eq {
long long si, f, s, r;
};
bool compare(eq a, eq b) {
if (a.f == b.f) return a.s < b.s;
return a.f < b.f;
}
bool comp(eq a, eq b) { return a.si < b.si; }
void solve() {
long long n;
cin >> n;
vector<pair<long long, long long> > v;
for (long long i = 0; i < n; i++) {
long long t;
cin >> t;
v.push_back({t, -(i + 1)});
}
sort(v.begin(), v.end());
long long m;
cin >> m;
while (m--) {
long long k, p;
cin >> k >> p;
vector<pair<long long, long long> > temp;
for (long long i = n - 1; i > n - 1 - k; i--) {
temp.push_back({-(v[i].second), v[i].first});
}
sort(temp.begin(), temp.end());
cout << temp[p - 1].second << endl;
}
return;
}
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
long long t = 1;
while (t--) {
solve();
}
}
|
1227_D1. Optimal Subsequences (Easy Version)
|
This is the easier version of the problem. In this version 1 β€ n, m β€ 100. You can hack this problem only if you solve and lock both problems.
You are given a sequence of integers a=[a_1,a_2,...,a_n] of length n. Its subsequence is obtained by removing zero or more elements from the sequence a (they do not necessarily go consecutively). For example, for the sequence a=[11,20,11,33,11,20,11]:
* [11,20,11,33,11,20,11], [11,20,11,33,11,20], [11,11,11,11], [20], [33,20] are subsequences (these are just some of the long list);
* [40], [33,33], [33,20,20], [20,20,11,11] are not subsequences.
Suppose that an additional non-negative integer k (1 β€ k β€ n) is given, then the subsequence is called optimal if:
* it has a length of k and the sum of its elements is the maximum possible among all subsequences of length k;
* and among all subsequences of length k that satisfy the previous item, it is lexicographically minimal.
Recall that the sequence b=[b_1, b_2, ..., b_k] is lexicographically smaller than the sequence c=[c_1, c_2, ..., c_k] if the first element (from the left) in which they differ less in the sequence b than in c. Formally: there exists t (1 β€ t β€ k) such that b_1=c_1, b_2=c_2, ..., b_{t-1}=c_{t-1} and at the same time b_t<c_t. For example:
* [10, 20, 20] lexicographically less than [10, 21, 1],
* [7, 99, 99] is lexicographically less than [10, 21, 1],
* [10, 21, 0] is lexicographically less than [10, 21, 1].
You are given a sequence of a=[a_1,a_2,...,a_n] and m requests, each consisting of two numbers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j). For each query, print the value that is in the index pos_j of the optimal subsequence of the given sequence a for k=k_j.
For example, if n=4, a=[10,20,30,20], k_j=2, then the optimal subsequence is [20,30] β it is the minimum lexicographically among all subsequences of length 2 with the maximum total sum of items. Thus, the answer to the request k_j=2, pos_j=1 is the number 20, and the answer to the request k_j=2, pos_j=2 is the number 30.
Input
The first line contains an integer n (1 β€ n β€ 100) β the length of the sequence a.
The second line contains elements of the sequence a: integer numbers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9).
The third line contains an integer m (1 β€ m β€ 100) β the number of requests.
The following m lines contain pairs of integers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j) β the requests.
Output
Print m integers r_1, r_2, ..., r_m (1 β€ r_j β€ 10^9) one per line: answers to the requests in the order they appear in the input. The value of r_j should be equal to the value contained in the position pos_j of the optimal subsequence for k=k_j.
Examples
Input
3
10 20 10
6
1 1
2 1
2 2
3 1
3 2
3 3
Output
20
10
20
10
20
10
Input
7
1 2 1 3 1 2 1
9
2 1
2 2
3 1
3 2
3 3
1 1
7 1
7 7
7 4
Output
2
3
2
3
2
3
1
1
3
Note
In the first example, for a=[10,20,10] the optimal subsequences are:
* for k=1: [20],
* for k=2: [10,20],
* for k=3: [10,20,10].
|
{
"input": [
"3\n10 20 10\n6\n1 1\n2 1\n2 2\n3 1\n3 2\n3 3\n",
"7\n1 2 1 3 1 2 1\n9\n2 1\n2 2\n3 1\n3 2\n3 3\n1 1\n7 1\n7 7\n7 4\n"
],
"output": [
"20\n10\n20\n10\n20\n10\n",
"2\n3\n2\n3\n2\n3\n1\n1\n3\n"
]
}
|
{
"input": [
"2\n1 10\n3\n2 2\n2 1\n1 1\n",
"2\n3922 3922\n3\n2 2\n2 1\n1 1\n",
"1\n1000000000\n1\n1 1\n",
"1\n1\n3\n1 1\n1 1\n1 1\n",
"5\n3 1 4 1 2\n15\n5 5\n5 4\n5 3\n5 2\n5 1\n4 4\n4 3\n4 2\n4 1\n3 3\n3 2\n3 1\n2 2\n2 1\n1 1\n",
"2\n392222 322\n3\n2 2\n2 1\n1 1\n"
],
"output": [
"10\n1\n10\n",
"3922\n3922\n3922\n",
"1000000000\n",
"1\n1\n1\n",
"2\n1\n4\n1\n3\n2\n4\n1\n3\n2\n4\n3\n4\n3\n4\n",
"322\n392222\n392222\n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
bool comp(const pair<long long int, long long int> &a,
const pair<long long int, long long int> &b) {
if (a.first == b.first) {
return a.second < b.second;
}
return a.first > b.first;
}
int main() {
int n;
cin >> n;
vector<long long int> arr(n);
vector<pair<long long int, long long int>> temp(n);
for (int i = 0; i < n; i++) {
cin >> arr[i];
temp[i] = {arr[i], i};
}
sort(temp.begin(), temp.end(), comp);
int m, k, pos;
cin >> m;
while (m--) {
cin >> k >> pos;
vector<pair<long long int, long long int>> curr;
for (int i = 0; i < k; i++) {
curr.push_back({temp[i].second, temp[i].first});
}
sort(curr.begin(), curr.end());
cout << curr[pos - 1].second << endl;
}
}
|
1227_D1. Optimal Subsequences (Easy Version)
|
This is the easier version of the problem. In this version 1 β€ n, m β€ 100. You can hack this problem only if you solve and lock both problems.
You are given a sequence of integers a=[a_1,a_2,...,a_n] of length n. Its subsequence is obtained by removing zero or more elements from the sequence a (they do not necessarily go consecutively). For example, for the sequence a=[11,20,11,33,11,20,11]:
* [11,20,11,33,11,20,11], [11,20,11,33,11,20], [11,11,11,11], [20], [33,20] are subsequences (these are just some of the long list);
* [40], [33,33], [33,20,20], [20,20,11,11] are not subsequences.
Suppose that an additional non-negative integer k (1 β€ k β€ n) is given, then the subsequence is called optimal if:
* it has a length of k and the sum of its elements is the maximum possible among all subsequences of length k;
* and among all subsequences of length k that satisfy the previous item, it is lexicographically minimal.
Recall that the sequence b=[b_1, b_2, ..., b_k] is lexicographically smaller than the sequence c=[c_1, c_2, ..., c_k] if the first element (from the left) in which they differ less in the sequence b than in c. Formally: there exists t (1 β€ t β€ k) such that b_1=c_1, b_2=c_2, ..., b_{t-1}=c_{t-1} and at the same time b_t<c_t. For example:
* [10, 20, 20] lexicographically less than [10, 21, 1],
* [7, 99, 99] is lexicographically less than [10, 21, 1],
* [10, 21, 0] is lexicographically less than [10, 21, 1].
You are given a sequence of a=[a_1,a_2,...,a_n] and m requests, each consisting of two numbers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j). For each query, print the value that is in the index pos_j of the optimal subsequence of the given sequence a for k=k_j.
For example, if n=4, a=[10,20,30,20], k_j=2, then the optimal subsequence is [20,30] β it is the minimum lexicographically among all subsequences of length 2 with the maximum total sum of items. Thus, the answer to the request k_j=2, pos_j=1 is the number 20, and the answer to the request k_j=2, pos_j=2 is the number 30.
Input
The first line contains an integer n (1 β€ n β€ 100) β the length of the sequence a.
The second line contains elements of the sequence a: integer numbers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9).
The third line contains an integer m (1 β€ m β€ 100) β the number of requests.
The following m lines contain pairs of integers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j) β the requests.
Output
Print m integers r_1, r_2, ..., r_m (1 β€ r_j β€ 10^9) one per line: answers to the requests in the order they appear in the input. The value of r_j should be equal to the value contained in the position pos_j of the optimal subsequence for k=k_j.
Examples
Input
3
10 20 10
6
1 1
2 1
2 2
3 1
3 2
3 3
Output
20
10
20
10
20
10
Input
7
1 2 1 3 1 2 1
9
2 1
2 2
3 1
3 2
3 3
1 1
7 1
7 7
7 4
Output
2
3
2
3
2
3
1
1
3
Note
In the first example, for a=[10,20,10] the optimal subsequences are:
* for k=1: [20],
* for k=2: [10,20],
* for k=3: [10,20,10].
|
{
"input": [
"3\n10 20 10\n6\n1 1\n2 1\n2 2\n3 1\n3 2\n3 3\n",
"7\n1 2 1 3 1 2 1\n9\n2 1\n2 2\n3 1\n3 2\n3 3\n1 1\n7 1\n7 7\n7 4\n"
],
"output": [
"20\n10\n20\n10\n20\n10\n",
"2\n3\n2\n3\n2\n3\n1\n1\n3\n"
]
}
|
{
"input": [
"2\n1 10\n3\n2 2\n2 1\n1 1\n",
"2\n3922 3922\n3\n2 2\n2 1\n1 1\n",
"1\n1000000000\n1\n1 1\n",
"1\n1\n3\n1 1\n1 1\n1 1\n",
"5\n3 1 4 1 2\n15\n5 5\n5 4\n5 3\n5 2\n5 1\n4 4\n4 3\n4 2\n4 1\n3 3\n3 2\n3 1\n2 2\n2 1\n1 1\n",
"2\n392222 322\n3\n2 2\n2 1\n1 1\n"
],
"output": [
"10\n1\n10\n",
"3922\n3922\n3922\n",
"1000000000\n",
"1\n1\n1\n",
"2\n1\n4\n1\n3\n2\n4\n1\n3\n2\n4\n3\n4\n3\n4\n",
"322\n392222\n392222\n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
int main() {
long long n;
cin >> n;
vector<long long> a(n), b(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
for (int i = 0; i < n; i++) {
b[i] = a[i];
}
sort(a.begin(), a.end(), greater<long long>());
long long m;
cin >> m;
vector<long long> result;
while (m--) {
long long k, pos;
cin >> k >> pos;
map<long long, long long> adj;
for (int i = 0; i < k; i++) {
adj[a[i]]++;
}
int ind = 1;
long long ans = -1;
for (int i = 0; i < n; i++) {
auto x = adj.find(b[i]);
if (x == adj.end() || (adj[b[i]] == 0)) {
continue;
}
if (ind == pos) {
ans = b[i];
break;
}
adj[b[i]]--;
ind++;
}
cout << ans << endl;
}
}
|
1227_D1. Optimal Subsequences (Easy Version)
|
This is the easier version of the problem. In this version 1 β€ n, m β€ 100. You can hack this problem only if you solve and lock both problems.
You are given a sequence of integers a=[a_1,a_2,...,a_n] of length n. Its subsequence is obtained by removing zero or more elements from the sequence a (they do not necessarily go consecutively). For example, for the sequence a=[11,20,11,33,11,20,11]:
* [11,20,11,33,11,20,11], [11,20,11,33,11,20], [11,11,11,11], [20], [33,20] are subsequences (these are just some of the long list);
* [40], [33,33], [33,20,20], [20,20,11,11] are not subsequences.
Suppose that an additional non-negative integer k (1 β€ k β€ n) is given, then the subsequence is called optimal if:
* it has a length of k and the sum of its elements is the maximum possible among all subsequences of length k;
* and among all subsequences of length k that satisfy the previous item, it is lexicographically minimal.
Recall that the sequence b=[b_1, b_2, ..., b_k] is lexicographically smaller than the sequence c=[c_1, c_2, ..., c_k] if the first element (from the left) in which they differ less in the sequence b than in c. Formally: there exists t (1 β€ t β€ k) such that b_1=c_1, b_2=c_2, ..., b_{t-1}=c_{t-1} and at the same time b_t<c_t. For example:
* [10, 20, 20] lexicographically less than [10, 21, 1],
* [7, 99, 99] is lexicographically less than [10, 21, 1],
* [10, 21, 0] is lexicographically less than [10, 21, 1].
You are given a sequence of a=[a_1,a_2,...,a_n] and m requests, each consisting of two numbers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j). For each query, print the value that is in the index pos_j of the optimal subsequence of the given sequence a for k=k_j.
For example, if n=4, a=[10,20,30,20], k_j=2, then the optimal subsequence is [20,30] β it is the minimum lexicographically among all subsequences of length 2 with the maximum total sum of items. Thus, the answer to the request k_j=2, pos_j=1 is the number 20, and the answer to the request k_j=2, pos_j=2 is the number 30.
Input
The first line contains an integer n (1 β€ n β€ 100) β the length of the sequence a.
The second line contains elements of the sequence a: integer numbers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9).
The third line contains an integer m (1 β€ m β€ 100) β the number of requests.
The following m lines contain pairs of integers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j) β the requests.
Output
Print m integers r_1, r_2, ..., r_m (1 β€ r_j β€ 10^9) one per line: answers to the requests in the order they appear in the input. The value of r_j should be equal to the value contained in the position pos_j of the optimal subsequence for k=k_j.
Examples
Input
3
10 20 10
6
1 1
2 1
2 2
3 1
3 2
3 3
Output
20
10
20
10
20
10
Input
7
1 2 1 3 1 2 1
9
2 1
2 2
3 1
3 2
3 3
1 1
7 1
7 7
7 4
Output
2
3
2
3
2
3
1
1
3
Note
In the first example, for a=[10,20,10] the optimal subsequences are:
* for k=1: [20],
* for k=2: [10,20],
* for k=3: [10,20,10].
|
{
"input": [
"3\n10 20 10\n6\n1 1\n2 1\n2 2\n3 1\n3 2\n3 3\n",
"7\n1 2 1 3 1 2 1\n9\n2 1\n2 2\n3 1\n3 2\n3 3\n1 1\n7 1\n7 7\n7 4\n"
],
"output": [
"20\n10\n20\n10\n20\n10\n",
"2\n3\n2\n3\n2\n3\n1\n1\n3\n"
]
}
|
{
"input": [
"2\n1 10\n3\n2 2\n2 1\n1 1\n",
"2\n3922 3922\n3\n2 2\n2 1\n1 1\n",
"1\n1000000000\n1\n1 1\n",
"1\n1\n3\n1 1\n1 1\n1 1\n",
"5\n3 1 4 1 2\n15\n5 5\n5 4\n5 3\n5 2\n5 1\n4 4\n4 3\n4 2\n4 1\n3 3\n3 2\n3 1\n2 2\n2 1\n1 1\n",
"2\n392222 322\n3\n2 2\n2 1\n1 1\n"
],
"output": [
"10\n1\n10\n",
"3922\n3922\n3922\n",
"1000000000\n",
"1\n1\n1\n",
"2\n1\n4\n1\n3\n2\n4\n1\n3\n2\n4\n3\n4\n3\n4\n",
"322\n392222\n392222\n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
int main() {
long long n;
cin >> n;
long long ar[n];
long long br[n];
for (auto x = 0; x < n; x++) {
cin >> ar[x];
br[x] = ar[x];
}
sort(br, br + n);
long long m;
cin >> m;
while (m--) {
long long pos, k;
set<long long> s;
cin >> k >> pos;
long long bound = br[n - k];
vector<long long> q;
for (int x = 0; x < n; x++) {
if (bound <= ar[x]) {
q.push_back(ar[x]);
}
}
long long sub = q.size() - k;
for (int x = q.size() - 1; x >= 0 && sub != 0; x--) {
if (bound == q[x]) {
q[x] = -1;
sub--;
}
if (sub == 0) {
break;
}
}
for (int x = 0; x < q.size(); x++) {
if (q[x] == -1) {
continue;
}
pos--;
if (pos == 0) cout << q[x] << endl;
}
}
}
|
1227_D1. Optimal Subsequences (Easy Version)
|
This is the easier version of the problem. In this version 1 β€ n, m β€ 100. You can hack this problem only if you solve and lock both problems.
You are given a sequence of integers a=[a_1,a_2,...,a_n] of length n. Its subsequence is obtained by removing zero or more elements from the sequence a (they do not necessarily go consecutively). For example, for the sequence a=[11,20,11,33,11,20,11]:
* [11,20,11,33,11,20,11], [11,20,11,33,11,20], [11,11,11,11], [20], [33,20] are subsequences (these are just some of the long list);
* [40], [33,33], [33,20,20], [20,20,11,11] are not subsequences.
Suppose that an additional non-negative integer k (1 β€ k β€ n) is given, then the subsequence is called optimal if:
* it has a length of k and the sum of its elements is the maximum possible among all subsequences of length k;
* and among all subsequences of length k that satisfy the previous item, it is lexicographically minimal.
Recall that the sequence b=[b_1, b_2, ..., b_k] is lexicographically smaller than the sequence c=[c_1, c_2, ..., c_k] if the first element (from the left) in which they differ less in the sequence b than in c. Formally: there exists t (1 β€ t β€ k) such that b_1=c_1, b_2=c_2, ..., b_{t-1}=c_{t-1} and at the same time b_t<c_t. For example:
* [10, 20, 20] lexicographically less than [10, 21, 1],
* [7, 99, 99] is lexicographically less than [10, 21, 1],
* [10, 21, 0] is lexicographically less than [10, 21, 1].
You are given a sequence of a=[a_1,a_2,...,a_n] and m requests, each consisting of two numbers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j). For each query, print the value that is in the index pos_j of the optimal subsequence of the given sequence a for k=k_j.
For example, if n=4, a=[10,20,30,20], k_j=2, then the optimal subsequence is [20,30] β it is the minimum lexicographically among all subsequences of length 2 with the maximum total sum of items. Thus, the answer to the request k_j=2, pos_j=1 is the number 20, and the answer to the request k_j=2, pos_j=2 is the number 30.
Input
The first line contains an integer n (1 β€ n β€ 100) β the length of the sequence a.
The second line contains elements of the sequence a: integer numbers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9).
The third line contains an integer m (1 β€ m β€ 100) β the number of requests.
The following m lines contain pairs of integers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j) β the requests.
Output
Print m integers r_1, r_2, ..., r_m (1 β€ r_j β€ 10^9) one per line: answers to the requests in the order they appear in the input. The value of r_j should be equal to the value contained in the position pos_j of the optimal subsequence for k=k_j.
Examples
Input
3
10 20 10
6
1 1
2 1
2 2
3 1
3 2
3 3
Output
20
10
20
10
20
10
Input
7
1 2 1 3 1 2 1
9
2 1
2 2
3 1
3 2
3 3
1 1
7 1
7 7
7 4
Output
2
3
2
3
2
3
1
1
3
Note
In the first example, for a=[10,20,10] the optimal subsequences are:
* for k=1: [20],
* for k=2: [10,20],
* for k=3: [10,20,10].
|
{
"input": [
"3\n10 20 10\n6\n1 1\n2 1\n2 2\n3 1\n3 2\n3 3\n",
"7\n1 2 1 3 1 2 1\n9\n2 1\n2 2\n3 1\n3 2\n3 3\n1 1\n7 1\n7 7\n7 4\n"
],
"output": [
"20\n10\n20\n10\n20\n10\n",
"2\n3\n2\n3\n2\n3\n1\n1\n3\n"
]
}
|
{
"input": [
"2\n1 10\n3\n2 2\n2 1\n1 1\n",
"2\n3922 3922\n3\n2 2\n2 1\n1 1\n",
"1\n1000000000\n1\n1 1\n",
"1\n1\n3\n1 1\n1 1\n1 1\n",
"5\n3 1 4 1 2\n15\n5 5\n5 4\n5 3\n5 2\n5 1\n4 4\n4 3\n4 2\n4 1\n3 3\n3 2\n3 1\n2 2\n2 1\n1 1\n",
"2\n392222 322\n3\n2 2\n2 1\n1 1\n"
],
"output": [
"10\n1\n10\n",
"3922\n3922\n3922\n",
"1000000000\n",
"1\n1\n1\n",
"2\n1\n4\n1\n3\n2\n4\n1\n3\n2\n4\n3\n4\n3\n4\n",
"322\n392222\n392222\n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
signed main() {
long long n;
cin >> n;
vector<long long> v(n);
map<long long, vector<long long>> def;
for (long long i = 0; i < n; i++) {
cin >> v[i];
def[v[i]].push_back(i);
}
vector<long long> g;
for (auto p : def) {
long long a = p.first;
vector<long long> b = p.second;
for (long long i = 0; i < b.size(); i++) {
g.push_back(b[b.size() * 1ll - i - 1]);
}
}
reverse(g.begin(), g.end());
long long m;
cin >> m;
for (long long i = 0; i < m; i++) {
long long k, pos;
cin >> k >> pos;
vector<long long> f;
for (long long j = 0; j < k; j++) {
f.push_back(g[j]);
}
sort(f.begin(), f.end());
cout << v[f[pos - 1]] << "\n";
}
return 0;
}
|
1227_D1. Optimal Subsequences (Easy Version)
|
This is the easier version of the problem. In this version 1 β€ n, m β€ 100. You can hack this problem only if you solve and lock both problems.
You are given a sequence of integers a=[a_1,a_2,...,a_n] of length n. Its subsequence is obtained by removing zero or more elements from the sequence a (they do not necessarily go consecutively). For example, for the sequence a=[11,20,11,33,11,20,11]:
* [11,20,11,33,11,20,11], [11,20,11,33,11,20], [11,11,11,11], [20], [33,20] are subsequences (these are just some of the long list);
* [40], [33,33], [33,20,20], [20,20,11,11] are not subsequences.
Suppose that an additional non-negative integer k (1 β€ k β€ n) is given, then the subsequence is called optimal if:
* it has a length of k and the sum of its elements is the maximum possible among all subsequences of length k;
* and among all subsequences of length k that satisfy the previous item, it is lexicographically minimal.
Recall that the sequence b=[b_1, b_2, ..., b_k] is lexicographically smaller than the sequence c=[c_1, c_2, ..., c_k] if the first element (from the left) in which they differ less in the sequence b than in c. Formally: there exists t (1 β€ t β€ k) such that b_1=c_1, b_2=c_2, ..., b_{t-1}=c_{t-1} and at the same time b_t<c_t. For example:
* [10, 20, 20] lexicographically less than [10, 21, 1],
* [7, 99, 99] is lexicographically less than [10, 21, 1],
* [10, 21, 0] is lexicographically less than [10, 21, 1].
You are given a sequence of a=[a_1,a_2,...,a_n] and m requests, each consisting of two numbers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j). For each query, print the value that is in the index pos_j of the optimal subsequence of the given sequence a for k=k_j.
For example, if n=4, a=[10,20,30,20], k_j=2, then the optimal subsequence is [20,30] β it is the minimum lexicographically among all subsequences of length 2 with the maximum total sum of items. Thus, the answer to the request k_j=2, pos_j=1 is the number 20, and the answer to the request k_j=2, pos_j=2 is the number 30.
Input
The first line contains an integer n (1 β€ n β€ 100) β the length of the sequence a.
The second line contains elements of the sequence a: integer numbers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9).
The third line contains an integer m (1 β€ m β€ 100) β the number of requests.
The following m lines contain pairs of integers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j) β the requests.
Output
Print m integers r_1, r_2, ..., r_m (1 β€ r_j β€ 10^9) one per line: answers to the requests in the order they appear in the input. The value of r_j should be equal to the value contained in the position pos_j of the optimal subsequence for k=k_j.
Examples
Input
3
10 20 10
6
1 1
2 1
2 2
3 1
3 2
3 3
Output
20
10
20
10
20
10
Input
7
1 2 1 3 1 2 1
9
2 1
2 2
3 1
3 2
3 3
1 1
7 1
7 7
7 4
Output
2
3
2
3
2
3
1
1
3
Note
In the first example, for a=[10,20,10] the optimal subsequences are:
* for k=1: [20],
* for k=2: [10,20],
* for k=3: [10,20,10].
|
{
"input": [
"3\n10 20 10\n6\n1 1\n2 1\n2 2\n3 1\n3 2\n3 3\n",
"7\n1 2 1 3 1 2 1\n9\n2 1\n2 2\n3 1\n3 2\n3 3\n1 1\n7 1\n7 7\n7 4\n"
],
"output": [
"20\n10\n20\n10\n20\n10\n",
"2\n3\n2\n3\n2\n3\n1\n1\n3\n"
]
}
|
{
"input": [
"2\n1 10\n3\n2 2\n2 1\n1 1\n",
"2\n3922 3922\n3\n2 2\n2 1\n1 1\n",
"1\n1000000000\n1\n1 1\n",
"1\n1\n3\n1 1\n1 1\n1 1\n",
"5\n3 1 4 1 2\n15\n5 5\n5 4\n5 3\n5 2\n5 1\n4 4\n4 3\n4 2\n4 1\n3 3\n3 2\n3 1\n2 2\n2 1\n1 1\n",
"2\n392222 322\n3\n2 2\n2 1\n1 1\n"
],
"output": [
"10\n1\n10\n",
"3922\n3922\n3922\n",
"1000000000\n",
"1\n1\n1\n",
"2\n1\n4\n1\n3\n2\n4\n1\n3\n2\n4\n3\n4\n3\n4\n",
"322\n392222\n392222\n"
]
}
|
CORRECT
|
python3
|
n=int(input())
a=[int(x) for x in input().split()]
z=a[:]
z.sort(reverse=True)
an=[[] for i in range(n+1)]
an[0]=a[:]
for i in range(n):
for j in range(len(a)-1,-1,-1) :
# print(j,z)
if a[j]==z[-1]:
del a[j]
z.pop()
break
an[i+1]=a[:]
an=an[::-1]
for j in range(int(input())):
x,y=map(int,input().split())
print(an[x][y-1])
|
1227_D1. Optimal Subsequences (Easy Version)
|
This is the easier version of the problem. In this version 1 β€ n, m β€ 100. You can hack this problem only if you solve and lock both problems.
You are given a sequence of integers a=[a_1,a_2,...,a_n] of length n. Its subsequence is obtained by removing zero or more elements from the sequence a (they do not necessarily go consecutively). For example, for the sequence a=[11,20,11,33,11,20,11]:
* [11,20,11,33,11,20,11], [11,20,11,33,11,20], [11,11,11,11], [20], [33,20] are subsequences (these are just some of the long list);
* [40], [33,33], [33,20,20], [20,20,11,11] are not subsequences.
Suppose that an additional non-negative integer k (1 β€ k β€ n) is given, then the subsequence is called optimal if:
* it has a length of k and the sum of its elements is the maximum possible among all subsequences of length k;
* and among all subsequences of length k that satisfy the previous item, it is lexicographically minimal.
Recall that the sequence b=[b_1, b_2, ..., b_k] is lexicographically smaller than the sequence c=[c_1, c_2, ..., c_k] if the first element (from the left) in which they differ less in the sequence b than in c. Formally: there exists t (1 β€ t β€ k) such that b_1=c_1, b_2=c_2, ..., b_{t-1}=c_{t-1} and at the same time b_t<c_t. For example:
* [10, 20, 20] lexicographically less than [10, 21, 1],
* [7, 99, 99] is lexicographically less than [10, 21, 1],
* [10, 21, 0] is lexicographically less than [10, 21, 1].
You are given a sequence of a=[a_1,a_2,...,a_n] and m requests, each consisting of two numbers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j). For each query, print the value that is in the index pos_j of the optimal subsequence of the given sequence a for k=k_j.
For example, if n=4, a=[10,20,30,20], k_j=2, then the optimal subsequence is [20,30] β it is the minimum lexicographically among all subsequences of length 2 with the maximum total sum of items. Thus, the answer to the request k_j=2, pos_j=1 is the number 20, and the answer to the request k_j=2, pos_j=2 is the number 30.
Input
The first line contains an integer n (1 β€ n β€ 100) β the length of the sequence a.
The second line contains elements of the sequence a: integer numbers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9).
The third line contains an integer m (1 β€ m β€ 100) β the number of requests.
The following m lines contain pairs of integers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j) β the requests.
Output
Print m integers r_1, r_2, ..., r_m (1 β€ r_j β€ 10^9) one per line: answers to the requests in the order they appear in the input. The value of r_j should be equal to the value contained in the position pos_j of the optimal subsequence for k=k_j.
Examples
Input
3
10 20 10
6
1 1
2 1
2 2
3 1
3 2
3 3
Output
20
10
20
10
20
10
Input
7
1 2 1 3 1 2 1
9
2 1
2 2
3 1
3 2
3 3
1 1
7 1
7 7
7 4
Output
2
3
2
3
2
3
1
1
3
Note
In the first example, for a=[10,20,10] the optimal subsequences are:
* for k=1: [20],
* for k=2: [10,20],
* for k=3: [10,20,10].
|
{
"input": [
"3\n10 20 10\n6\n1 1\n2 1\n2 2\n3 1\n3 2\n3 3\n",
"7\n1 2 1 3 1 2 1\n9\n2 1\n2 2\n3 1\n3 2\n3 3\n1 1\n7 1\n7 7\n7 4\n"
],
"output": [
"20\n10\n20\n10\n20\n10\n",
"2\n3\n2\n3\n2\n3\n1\n1\n3\n"
]
}
|
{
"input": [
"2\n1 10\n3\n2 2\n2 1\n1 1\n",
"2\n3922 3922\n3\n2 2\n2 1\n1 1\n",
"1\n1000000000\n1\n1 1\n",
"1\n1\n3\n1 1\n1 1\n1 1\n",
"5\n3 1 4 1 2\n15\n5 5\n5 4\n5 3\n5 2\n5 1\n4 4\n4 3\n4 2\n4 1\n3 3\n3 2\n3 1\n2 2\n2 1\n1 1\n",
"2\n392222 322\n3\n2 2\n2 1\n1 1\n"
],
"output": [
"10\n1\n10\n",
"3922\n3922\n3922\n",
"1000000000\n",
"1\n1\n1\n",
"2\n1\n4\n1\n3\n2\n4\n1\n3\n2\n4\n3\n4\n3\n4\n",
"322\n392222\n392222\n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
long long bin_pow(long long a, long long b) {
if (b == 0) return 1;
if (b % 2 == 0) {
long long t = bin_pow(a, b / 2);
return t * t % 1000000007;
} else
return a * bin_pow(a, b - 1) % 1000000007;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
long long t = 1, n, m, k = 0, x = 0, y = 0, z = 0, sum = 0, l = 0, r = 0,
ans = 0, mn = LLONG_MAX, mx = LLONG_MIN;
cin >> n;
vector<long long> a(n), b(n);
for (int i = 0; i < n; i++) cin >> a[i], b[i] = a[i];
sort(b.begin(), b.end());
reverse(b.begin(), b.end());
cin >> m;
while (m--) {
map<long long, long long> mp, mp1, mp2;
cin >> k >> x;
vector<long long> c, d;
for (int i = 0; i < k; i++) c.push_back(b[i]), mp[b[i]]++;
for (int i = 0; i < n; i++) mp1[a[i]]++;
reverse(c.begin(), c.end());
c.erase(unique(c.begin(), c.end()), c.end());
l = 0;
for (int i = 0; i < n; i++) {
if (mp[a[i]] == 0) continue;
mp2[a[i]]++;
if (a[i] == c[l]) {
d.push_back(a[i]);
mp[a[i]]--;
if (mp[a[i]] == 0) l++;
continue;
}
if (mp[a[i]] > mp1[a[i]] - mp2[a[i]]) {
d.push_back(a[i]);
mp[a[i]]--;
}
}
ans = d[x - 1];
cout << ans;
cout << "\n";
}
return 0;
}
|
1227_D1. Optimal Subsequences (Easy Version)
|
This is the easier version of the problem. In this version 1 β€ n, m β€ 100. You can hack this problem only if you solve and lock both problems.
You are given a sequence of integers a=[a_1,a_2,...,a_n] of length n. Its subsequence is obtained by removing zero or more elements from the sequence a (they do not necessarily go consecutively). For example, for the sequence a=[11,20,11,33,11,20,11]:
* [11,20,11,33,11,20,11], [11,20,11,33,11,20], [11,11,11,11], [20], [33,20] are subsequences (these are just some of the long list);
* [40], [33,33], [33,20,20], [20,20,11,11] are not subsequences.
Suppose that an additional non-negative integer k (1 β€ k β€ n) is given, then the subsequence is called optimal if:
* it has a length of k and the sum of its elements is the maximum possible among all subsequences of length k;
* and among all subsequences of length k that satisfy the previous item, it is lexicographically minimal.
Recall that the sequence b=[b_1, b_2, ..., b_k] is lexicographically smaller than the sequence c=[c_1, c_2, ..., c_k] if the first element (from the left) in which they differ less in the sequence b than in c. Formally: there exists t (1 β€ t β€ k) such that b_1=c_1, b_2=c_2, ..., b_{t-1}=c_{t-1} and at the same time b_t<c_t. For example:
* [10, 20, 20] lexicographically less than [10, 21, 1],
* [7, 99, 99] is lexicographically less than [10, 21, 1],
* [10, 21, 0] is lexicographically less than [10, 21, 1].
You are given a sequence of a=[a_1,a_2,...,a_n] and m requests, each consisting of two numbers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j). For each query, print the value that is in the index pos_j of the optimal subsequence of the given sequence a for k=k_j.
For example, if n=4, a=[10,20,30,20], k_j=2, then the optimal subsequence is [20,30] β it is the minimum lexicographically among all subsequences of length 2 with the maximum total sum of items. Thus, the answer to the request k_j=2, pos_j=1 is the number 20, and the answer to the request k_j=2, pos_j=2 is the number 30.
Input
The first line contains an integer n (1 β€ n β€ 100) β the length of the sequence a.
The second line contains elements of the sequence a: integer numbers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9).
The third line contains an integer m (1 β€ m β€ 100) β the number of requests.
The following m lines contain pairs of integers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j) β the requests.
Output
Print m integers r_1, r_2, ..., r_m (1 β€ r_j β€ 10^9) one per line: answers to the requests in the order they appear in the input. The value of r_j should be equal to the value contained in the position pos_j of the optimal subsequence for k=k_j.
Examples
Input
3
10 20 10
6
1 1
2 1
2 2
3 1
3 2
3 3
Output
20
10
20
10
20
10
Input
7
1 2 1 3 1 2 1
9
2 1
2 2
3 1
3 2
3 3
1 1
7 1
7 7
7 4
Output
2
3
2
3
2
3
1
1
3
Note
In the first example, for a=[10,20,10] the optimal subsequences are:
* for k=1: [20],
* for k=2: [10,20],
* for k=3: [10,20,10].
|
{
"input": [
"3\n10 20 10\n6\n1 1\n2 1\n2 2\n3 1\n3 2\n3 3\n",
"7\n1 2 1 3 1 2 1\n9\n2 1\n2 2\n3 1\n3 2\n3 3\n1 1\n7 1\n7 7\n7 4\n"
],
"output": [
"20\n10\n20\n10\n20\n10\n",
"2\n3\n2\n3\n2\n3\n1\n1\n3\n"
]
}
|
{
"input": [
"2\n1 10\n3\n2 2\n2 1\n1 1\n",
"2\n3922 3922\n3\n2 2\n2 1\n1 1\n",
"1\n1000000000\n1\n1 1\n",
"1\n1\n3\n1 1\n1 1\n1 1\n",
"5\n3 1 4 1 2\n15\n5 5\n5 4\n5 3\n5 2\n5 1\n4 4\n4 3\n4 2\n4 1\n3 3\n3 2\n3 1\n2 2\n2 1\n1 1\n",
"2\n392222 322\n3\n2 2\n2 1\n1 1\n"
],
"output": [
"10\n1\n10\n",
"3922\n3922\n3922\n",
"1000000000\n",
"1\n1\n1\n",
"2\n1\n4\n1\n3\n2\n4\n1\n3\n2\n4\n3\n4\n3\n4\n",
"322\n392222\n392222\n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
vector<int> srr;
map<int, int> occ;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int arr[200], n, m, pj, k, fast = 1, last;
cin >> n;
for (int i = 1; i <= n; ++i) {
cin >> arr[i];
srr.push_back(arr[i]);
}
sort(srr.begin(), srr.end(), greater<int>());
cin >> m;
last = n;
while (m--) {
vector<int> vec;
cin >> k >> pj;
for (int i = 0; i < k; ++i) vec.push_back(srr[i]);
vector<int> ans;
sort(vec.begin(), vec.end());
fast = 1;
for (int s = 1; s <= k; ++s) {
for (int i = 0; i < k; ++i) {
if (vec[i] != -1) {
for (int j = fast; j <= last; ++j) {
if (arr[j] == vec[i]) {
occ.clear();
for (int p = 0; p < k; ++p) {
if (vec[p] != -1) occ[vec[p]]++;
}
for (int p = j; p <= last; ++p) {
if (occ[arr[p]]) occ[arr[p]]--;
}
bool pos = true;
for (int p = 0; p < k && pos; ++p)
if (vec[p] > 0 && occ[vec[p]] > 0) pos = false;
if (pos) fast = j + 1;
if (pos) {
ans.push_back(vec[i]);
vec[i] = -1;
i = k;
}
break;
}
}
}
}
}
cout << ans[pj - 1] << endl;
}
return 0;
}
|
1227_D1. Optimal Subsequences (Easy Version)
|
This is the easier version of the problem. In this version 1 β€ n, m β€ 100. You can hack this problem only if you solve and lock both problems.
You are given a sequence of integers a=[a_1,a_2,...,a_n] of length n. Its subsequence is obtained by removing zero or more elements from the sequence a (they do not necessarily go consecutively). For example, for the sequence a=[11,20,11,33,11,20,11]:
* [11,20,11,33,11,20,11], [11,20,11,33,11,20], [11,11,11,11], [20], [33,20] are subsequences (these are just some of the long list);
* [40], [33,33], [33,20,20], [20,20,11,11] are not subsequences.
Suppose that an additional non-negative integer k (1 β€ k β€ n) is given, then the subsequence is called optimal if:
* it has a length of k and the sum of its elements is the maximum possible among all subsequences of length k;
* and among all subsequences of length k that satisfy the previous item, it is lexicographically minimal.
Recall that the sequence b=[b_1, b_2, ..., b_k] is lexicographically smaller than the sequence c=[c_1, c_2, ..., c_k] if the first element (from the left) in which they differ less in the sequence b than in c. Formally: there exists t (1 β€ t β€ k) such that b_1=c_1, b_2=c_2, ..., b_{t-1}=c_{t-1} and at the same time b_t<c_t. For example:
* [10, 20, 20] lexicographically less than [10, 21, 1],
* [7, 99, 99] is lexicographically less than [10, 21, 1],
* [10, 21, 0] is lexicographically less than [10, 21, 1].
You are given a sequence of a=[a_1,a_2,...,a_n] and m requests, each consisting of two numbers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j). For each query, print the value that is in the index pos_j of the optimal subsequence of the given sequence a for k=k_j.
For example, if n=4, a=[10,20,30,20], k_j=2, then the optimal subsequence is [20,30] β it is the minimum lexicographically among all subsequences of length 2 with the maximum total sum of items. Thus, the answer to the request k_j=2, pos_j=1 is the number 20, and the answer to the request k_j=2, pos_j=2 is the number 30.
Input
The first line contains an integer n (1 β€ n β€ 100) β the length of the sequence a.
The second line contains elements of the sequence a: integer numbers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9).
The third line contains an integer m (1 β€ m β€ 100) β the number of requests.
The following m lines contain pairs of integers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j) β the requests.
Output
Print m integers r_1, r_2, ..., r_m (1 β€ r_j β€ 10^9) one per line: answers to the requests in the order they appear in the input. The value of r_j should be equal to the value contained in the position pos_j of the optimal subsequence for k=k_j.
Examples
Input
3
10 20 10
6
1 1
2 1
2 2
3 1
3 2
3 3
Output
20
10
20
10
20
10
Input
7
1 2 1 3 1 2 1
9
2 1
2 2
3 1
3 2
3 3
1 1
7 1
7 7
7 4
Output
2
3
2
3
2
3
1
1
3
Note
In the first example, for a=[10,20,10] the optimal subsequences are:
* for k=1: [20],
* for k=2: [10,20],
* for k=3: [10,20,10].
|
{
"input": [
"3\n10 20 10\n6\n1 1\n2 1\n2 2\n3 1\n3 2\n3 3\n",
"7\n1 2 1 3 1 2 1\n9\n2 1\n2 2\n3 1\n3 2\n3 3\n1 1\n7 1\n7 7\n7 4\n"
],
"output": [
"20\n10\n20\n10\n20\n10\n",
"2\n3\n2\n3\n2\n3\n1\n1\n3\n"
]
}
|
{
"input": [
"2\n1 10\n3\n2 2\n2 1\n1 1\n",
"2\n3922 3922\n3\n2 2\n2 1\n1 1\n",
"1\n1000000000\n1\n1 1\n",
"1\n1\n3\n1 1\n1 1\n1 1\n",
"5\n3 1 4 1 2\n15\n5 5\n5 4\n5 3\n5 2\n5 1\n4 4\n4 3\n4 2\n4 1\n3 3\n3 2\n3 1\n2 2\n2 1\n1 1\n",
"2\n392222 322\n3\n2 2\n2 1\n1 1\n"
],
"output": [
"10\n1\n10\n",
"3922\n3922\n3922\n",
"1000000000\n",
"1\n1\n1\n",
"2\n1\n4\n1\n3\n2\n4\n1\n3\n2\n4\n3\n4\n3\n4\n",
"322\n392222\n392222\n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
int main() {
long long int t, n, m, k;
cin >> n;
vector<int> v[n + 1];
vector<long long int> s;
for (int i = 0; i < n; i++) {
cin >> t;
v[n].push_back(t);
s.push_back(t);
}
sort(s.begin(), s.end());
int l = n - 1, x = 0, y;
while (l > 0) {
for (int i = v[l + 1].size() - 1; i >= 0; i--) {
if (v[l + 1][i] == s[x]) {
y = i;
break;
}
}
for (int i = 0; i < v[l + 1].size(); i++) {
if (i == y) continue;
v[l].push_back(v[l + 1][i]);
}
x++;
l--;
}
cin >> m;
for (int i = 0; i < m; i++) {
cin >> x >> y;
cout << v[x][y - 1] << endl;
}
}
|
1227_D1. Optimal Subsequences (Easy Version)
|
This is the easier version of the problem. In this version 1 β€ n, m β€ 100. You can hack this problem only if you solve and lock both problems.
You are given a sequence of integers a=[a_1,a_2,...,a_n] of length n. Its subsequence is obtained by removing zero or more elements from the sequence a (they do not necessarily go consecutively). For example, for the sequence a=[11,20,11,33,11,20,11]:
* [11,20,11,33,11,20,11], [11,20,11,33,11,20], [11,11,11,11], [20], [33,20] are subsequences (these are just some of the long list);
* [40], [33,33], [33,20,20], [20,20,11,11] are not subsequences.
Suppose that an additional non-negative integer k (1 β€ k β€ n) is given, then the subsequence is called optimal if:
* it has a length of k and the sum of its elements is the maximum possible among all subsequences of length k;
* and among all subsequences of length k that satisfy the previous item, it is lexicographically minimal.
Recall that the sequence b=[b_1, b_2, ..., b_k] is lexicographically smaller than the sequence c=[c_1, c_2, ..., c_k] if the first element (from the left) in which they differ less in the sequence b than in c. Formally: there exists t (1 β€ t β€ k) such that b_1=c_1, b_2=c_2, ..., b_{t-1}=c_{t-1} and at the same time b_t<c_t. For example:
* [10, 20, 20] lexicographically less than [10, 21, 1],
* [7, 99, 99] is lexicographically less than [10, 21, 1],
* [10, 21, 0] is lexicographically less than [10, 21, 1].
You are given a sequence of a=[a_1,a_2,...,a_n] and m requests, each consisting of two numbers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j). For each query, print the value that is in the index pos_j of the optimal subsequence of the given sequence a for k=k_j.
For example, if n=4, a=[10,20,30,20], k_j=2, then the optimal subsequence is [20,30] β it is the minimum lexicographically among all subsequences of length 2 with the maximum total sum of items. Thus, the answer to the request k_j=2, pos_j=1 is the number 20, and the answer to the request k_j=2, pos_j=2 is the number 30.
Input
The first line contains an integer n (1 β€ n β€ 100) β the length of the sequence a.
The second line contains elements of the sequence a: integer numbers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9).
The third line contains an integer m (1 β€ m β€ 100) β the number of requests.
The following m lines contain pairs of integers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j) β the requests.
Output
Print m integers r_1, r_2, ..., r_m (1 β€ r_j β€ 10^9) one per line: answers to the requests in the order they appear in the input. The value of r_j should be equal to the value contained in the position pos_j of the optimal subsequence for k=k_j.
Examples
Input
3
10 20 10
6
1 1
2 1
2 2
3 1
3 2
3 3
Output
20
10
20
10
20
10
Input
7
1 2 1 3 1 2 1
9
2 1
2 2
3 1
3 2
3 3
1 1
7 1
7 7
7 4
Output
2
3
2
3
2
3
1
1
3
Note
In the first example, for a=[10,20,10] the optimal subsequences are:
* for k=1: [20],
* for k=2: [10,20],
* for k=3: [10,20,10].
|
{
"input": [
"3\n10 20 10\n6\n1 1\n2 1\n2 2\n3 1\n3 2\n3 3\n",
"7\n1 2 1 3 1 2 1\n9\n2 1\n2 2\n3 1\n3 2\n3 3\n1 1\n7 1\n7 7\n7 4\n"
],
"output": [
"20\n10\n20\n10\n20\n10\n",
"2\n3\n2\n3\n2\n3\n1\n1\n3\n"
]
}
|
{
"input": [
"2\n1 10\n3\n2 2\n2 1\n1 1\n",
"2\n3922 3922\n3\n2 2\n2 1\n1 1\n",
"1\n1000000000\n1\n1 1\n",
"1\n1\n3\n1 1\n1 1\n1 1\n",
"5\n3 1 4 1 2\n15\n5 5\n5 4\n5 3\n5 2\n5 1\n4 4\n4 3\n4 2\n4 1\n3 3\n3 2\n3 1\n2 2\n2 1\n1 1\n",
"2\n392222 322\n3\n2 2\n2 1\n1 1\n"
],
"output": [
"10\n1\n10\n",
"3922\n3922\n3922\n",
"1000000000\n",
"1\n1\n1\n",
"2\n1\n4\n1\n3\n2\n4\n1\n3\n2\n4\n3\n4\n3\n4\n",
"322\n392222\n392222\n"
]
}
|
CORRECT
|
java
|
import java.io.*;
import java.util.ArrayList;
import java.util.Collections;
public class competetive {
static class pair implements Comparable<pair>{
long f;
int s;
pair(long a,int b){
this.f=a;
this.s=b;
}
long getF(){
return f;
}
int getS(){
return s;
}
public int compareTo(pair p){
if(this.f==p.f){
return p.s-this.s;
}
return (int)(this.f-p.f);
}
}
static class pair2 implements Comparable<pair2>{
long f;
int s;
pair2(long a,int b){
this.f=a;
this.s=b;
}
long getF2(){
return f;
}
int getS2(){
return s;
}
public int compareTo(pair2 p){
return this.s-p.s;
}
}
public static void main(String[] args) throws java.lang.Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(java.io.FileDescriptor.out), "ASCII"), 512);
int n=Integer.parseInt(br.readLine());
String line=br.readLine();
String []s=line.trim().split(" ");
ArrayList<pair>pl=new ArrayList<>();
for(int i=0;i<n;i++){
pl.add(new pair(Long.parseLong(s[i]),i));
}
int m=Integer.parseInt(br.readLine());
Collections.sort(pl);
ArrayList<ArrayList<pair2>>l=new ArrayList<>();
for(int i=0;i<101;i++){
l.add(i,new ArrayList<>());
}
for(int i=1;i<101;i++){
if(n-i==-1)break;
for(int j=n-i;j<n;j++){
l.get(i).add(new pair2(pl.get(j).getF(),pl.get(j).getS()));
}
Collections.sort(l.get(i));
}
for(int i=0;i<m;i++){
String query=br.readLine();
String[]q=query.trim().split(" ");
int k=Integer.parseInt(q[0]);
int pos=Integer.parseInt(q[1]);
out.write(String.valueOf(l.get(k).get(pos-1).getF2()));
out.newLine();
}
out.flush();
}
}
|
1227_D1. Optimal Subsequences (Easy Version)
|
This is the easier version of the problem. In this version 1 β€ n, m β€ 100. You can hack this problem only if you solve and lock both problems.
You are given a sequence of integers a=[a_1,a_2,...,a_n] of length n. Its subsequence is obtained by removing zero or more elements from the sequence a (they do not necessarily go consecutively). For example, for the sequence a=[11,20,11,33,11,20,11]:
* [11,20,11,33,11,20,11], [11,20,11,33,11,20], [11,11,11,11], [20], [33,20] are subsequences (these are just some of the long list);
* [40], [33,33], [33,20,20], [20,20,11,11] are not subsequences.
Suppose that an additional non-negative integer k (1 β€ k β€ n) is given, then the subsequence is called optimal if:
* it has a length of k and the sum of its elements is the maximum possible among all subsequences of length k;
* and among all subsequences of length k that satisfy the previous item, it is lexicographically minimal.
Recall that the sequence b=[b_1, b_2, ..., b_k] is lexicographically smaller than the sequence c=[c_1, c_2, ..., c_k] if the first element (from the left) in which they differ less in the sequence b than in c. Formally: there exists t (1 β€ t β€ k) such that b_1=c_1, b_2=c_2, ..., b_{t-1}=c_{t-1} and at the same time b_t<c_t. For example:
* [10, 20, 20] lexicographically less than [10, 21, 1],
* [7, 99, 99] is lexicographically less than [10, 21, 1],
* [10, 21, 0] is lexicographically less than [10, 21, 1].
You are given a sequence of a=[a_1,a_2,...,a_n] and m requests, each consisting of two numbers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j). For each query, print the value that is in the index pos_j of the optimal subsequence of the given sequence a for k=k_j.
For example, if n=4, a=[10,20,30,20], k_j=2, then the optimal subsequence is [20,30] β it is the minimum lexicographically among all subsequences of length 2 with the maximum total sum of items. Thus, the answer to the request k_j=2, pos_j=1 is the number 20, and the answer to the request k_j=2, pos_j=2 is the number 30.
Input
The first line contains an integer n (1 β€ n β€ 100) β the length of the sequence a.
The second line contains elements of the sequence a: integer numbers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9).
The third line contains an integer m (1 β€ m β€ 100) β the number of requests.
The following m lines contain pairs of integers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j) β the requests.
Output
Print m integers r_1, r_2, ..., r_m (1 β€ r_j β€ 10^9) one per line: answers to the requests in the order they appear in the input. The value of r_j should be equal to the value contained in the position pos_j of the optimal subsequence for k=k_j.
Examples
Input
3
10 20 10
6
1 1
2 1
2 2
3 1
3 2
3 3
Output
20
10
20
10
20
10
Input
7
1 2 1 3 1 2 1
9
2 1
2 2
3 1
3 2
3 3
1 1
7 1
7 7
7 4
Output
2
3
2
3
2
3
1
1
3
Note
In the first example, for a=[10,20,10] the optimal subsequences are:
* for k=1: [20],
* for k=2: [10,20],
* for k=3: [10,20,10].
|
{
"input": [
"3\n10 20 10\n6\n1 1\n2 1\n2 2\n3 1\n3 2\n3 3\n",
"7\n1 2 1 3 1 2 1\n9\n2 1\n2 2\n3 1\n3 2\n3 3\n1 1\n7 1\n7 7\n7 4\n"
],
"output": [
"20\n10\n20\n10\n20\n10\n",
"2\n3\n2\n3\n2\n3\n1\n1\n3\n"
]
}
|
{
"input": [
"2\n1 10\n3\n2 2\n2 1\n1 1\n",
"2\n3922 3922\n3\n2 2\n2 1\n1 1\n",
"1\n1000000000\n1\n1 1\n",
"1\n1\n3\n1 1\n1 1\n1 1\n",
"5\n3 1 4 1 2\n15\n5 5\n5 4\n5 3\n5 2\n5 1\n4 4\n4 3\n4 2\n4 1\n3 3\n3 2\n3 1\n2 2\n2 1\n1 1\n",
"2\n392222 322\n3\n2 2\n2 1\n1 1\n"
],
"output": [
"10\n1\n10\n",
"3922\n3922\n3922\n",
"1000000000\n",
"1\n1\n1\n",
"2\n1\n4\n1\n3\n2\n4\n1\n3\n2\n4\n3\n4\n3\n4\n",
"322\n392222\n392222\n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> arr(n);
set<pair<int, int>> left;
for (int i = 0; i < arr.size(); i++) {
cin >> arr[i];
left.insert(make_pair(arr[i], i * -1));
}
int m;
cin >> m;
while (m--) {
int k, j;
cin >> k >> j;
j--;
set<pair<int, int>> test = left;
while (test.size() > k) {
auto t = test.begin();
test.erase(t);
}
vector<pair<int, int>> fil;
for (auto a : test) {
fil.push_back(make_pair(a.second * -1, a.first));
}
sort(fil.begin(), fil.end());
cout << fil[j].second << "\n";
}
return 0;
}
|
1227_D1. Optimal Subsequences (Easy Version)
|
This is the easier version of the problem. In this version 1 β€ n, m β€ 100. You can hack this problem only if you solve and lock both problems.
You are given a sequence of integers a=[a_1,a_2,...,a_n] of length n. Its subsequence is obtained by removing zero or more elements from the sequence a (they do not necessarily go consecutively). For example, for the sequence a=[11,20,11,33,11,20,11]:
* [11,20,11,33,11,20,11], [11,20,11,33,11,20], [11,11,11,11], [20], [33,20] are subsequences (these are just some of the long list);
* [40], [33,33], [33,20,20], [20,20,11,11] are not subsequences.
Suppose that an additional non-negative integer k (1 β€ k β€ n) is given, then the subsequence is called optimal if:
* it has a length of k and the sum of its elements is the maximum possible among all subsequences of length k;
* and among all subsequences of length k that satisfy the previous item, it is lexicographically minimal.
Recall that the sequence b=[b_1, b_2, ..., b_k] is lexicographically smaller than the sequence c=[c_1, c_2, ..., c_k] if the first element (from the left) in which they differ less in the sequence b than in c. Formally: there exists t (1 β€ t β€ k) such that b_1=c_1, b_2=c_2, ..., b_{t-1}=c_{t-1} and at the same time b_t<c_t. For example:
* [10, 20, 20] lexicographically less than [10, 21, 1],
* [7, 99, 99] is lexicographically less than [10, 21, 1],
* [10, 21, 0] is lexicographically less than [10, 21, 1].
You are given a sequence of a=[a_1,a_2,...,a_n] and m requests, each consisting of two numbers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j). For each query, print the value that is in the index pos_j of the optimal subsequence of the given sequence a for k=k_j.
For example, if n=4, a=[10,20,30,20], k_j=2, then the optimal subsequence is [20,30] β it is the minimum lexicographically among all subsequences of length 2 with the maximum total sum of items. Thus, the answer to the request k_j=2, pos_j=1 is the number 20, and the answer to the request k_j=2, pos_j=2 is the number 30.
Input
The first line contains an integer n (1 β€ n β€ 100) β the length of the sequence a.
The second line contains elements of the sequence a: integer numbers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9).
The third line contains an integer m (1 β€ m β€ 100) β the number of requests.
The following m lines contain pairs of integers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j) β the requests.
Output
Print m integers r_1, r_2, ..., r_m (1 β€ r_j β€ 10^9) one per line: answers to the requests in the order they appear in the input. The value of r_j should be equal to the value contained in the position pos_j of the optimal subsequence for k=k_j.
Examples
Input
3
10 20 10
6
1 1
2 1
2 2
3 1
3 2
3 3
Output
20
10
20
10
20
10
Input
7
1 2 1 3 1 2 1
9
2 1
2 2
3 1
3 2
3 3
1 1
7 1
7 7
7 4
Output
2
3
2
3
2
3
1
1
3
Note
In the first example, for a=[10,20,10] the optimal subsequences are:
* for k=1: [20],
* for k=2: [10,20],
* for k=3: [10,20,10].
|
{
"input": [
"3\n10 20 10\n6\n1 1\n2 1\n2 2\n3 1\n3 2\n3 3\n",
"7\n1 2 1 3 1 2 1\n9\n2 1\n2 2\n3 1\n3 2\n3 3\n1 1\n7 1\n7 7\n7 4\n"
],
"output": [
"20\n10\n20\n10\n20\n10\n",
"2\n3\n2\n3\n2\n3\n1\n1\n3\n"
]
}
|
{
"input": [
"2\n1 10\n3\n2 2\n2 1\n1 1\n",
"2\n3922 3922\n3\n2 2\n2 1\n1 1\n",
"1\n1000000000\n1\n1 1\n",
"1\n1\n3\n1 1\n1 1\n1 1\n",
"5\n3 1 4 1 2\n15\n5 5\n5 4\n5 3\n5 2\n5 1\n4 4\n4 3\n4 2\n4 1\n3 3\n3 2\n3 1\n2 2\n2 1\n1 1\n",
"2\n392222 322\n3\n2 2\n2 1\n1 1\n"
],
"output": [
"10\n1\n10\n",
"3922\n3922\n3922\n",
"1000000000\n",
"1\n1\n1\n",
"2\n1\n4\n1\n3\n2\n4\n1\n3\n2\n4\n3\n4\n3\n4\n",
"322\n392222\n392222\n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
using namespace std;
long long N = 1234567891, M = 31;
long long n, m, t, k, f, mi = 2e9, x, y, z, ma, x3, x2, y2, l, r, i, j, ans, vv,
a2[410000], d[410000], dp[810000];
string s, s2, s3;
vector<long long> v, v2, v3;
struct pos {
long long x, y, z;
};
pos a[410000], b[410000];
bool cmp(pos l, pos r) {
if (l.x == r.x) return l.y > r.y;
return l.x < r.x;
}
void upd(int l, int r, int x, int ve) {
if (l == r) {
dp[ve] = 1;
return;
}
int t = (l + r) / 2;
if (t >= x)
upd(l, t, x, ve * 2);
else
upd(t + 1, r, x, ve * 2 + 1);
dp[ve] = dp[ve * 2] + dp[ve * 2 + 1];
}
int sum(int l, int r, int x, int ve) {
if (l == r) return l;
int t = (l + r) / 2;
if (dp[ve * 2] >= x) return sum(l, t, x, ve * 2);
return sum(t + 1, r, x - dp[ve * 2], ve * 2 + 1);
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);
cin >> n;
for (i = 1; i <= n; i++) {
cin >> a[i].x;
a2[i] = a[i].x;
a[i].y = i;
}
sort(a + 1, a + n + 1, cmp);
cin >> m;
for (i = 1; i <= m; i++) {
cin >> b[i].x >> b[i].y;
b[i].z = i;
}
sort(b + 1, b + m + 1, cmp);
for (i = 1; i <= m; i++) {
for (j = b[i - 1].x + 1; j <= b[i].x; j++) {
l = a[n - j + 1].y;
upd(1, n, l, 1);
}
d[b[i].z] = a2[sum(1, n, b[i].y, 1)];
}
for (i = 1; i <= m; i++) {
cout << d[i] << endl;
}
}
|
1227_D1. Optimal Subsequences (Easy Version)
|
This is the easier version of the problem. In this version 1 β€ n, m β€ 100. You can hack this problem only if you solve and lock both problems.
You are given a sequence of integers a=[a_1,a_2,...,a_n] of length n. Its subsequence is obtained by removing zero or more elements from the sequence a (they do not necessarily go consecutively). For example, for the sequence a=[11,20,11,33,11,20,11]:
* [11,20,11,33,11,20,11], [11,20,11,33,11,20], [11,11,11,11], [20], [33,20] are subsequences (these are just some of the long list);
* [40], [33,33], [33,20,20], [20,20,11,11] are not subsequences.
Suppose that an additional non-negative integer k (1 β€ k β€ n) is given, then the subsequence is called optimal if:
* it has a length of k and the sum of its elements is the maximum possible among all subsequences of length k;
* and among all subsequences of length k that satisfy the previous item, it is lexicographically minimal.
Recall that the sequence b=[b_1, b_2, ..., b_k] is lexicographically smaller than the sequence c=[c_1, c_2, ..., c_k] if the first element (from the left) in which they differ less in the sequence b than in c. Formally: there exists t (1 β€ t β€ k) such that b_1=c_1, b_2=c_2, ..., b_{t-1}=c_{t-1} and at the same time b_t<c_t. For example:
* [10, 20, 20] lexicographically less than [10, 21, 1],
* [7, 99, 99] is lexicographically less than [10, 21, 1],
* [10, 21, 0] is lexicographically less than [10, 21, 1].
You are given a sequence of a=[a_1,a_2,...,a_n] and m requests, each consisting of two numbers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j). For each query, print the value that is in the index pos_j of the optimal subsequence of the given sequence a for k=k_j.
For example, if n=4, a=[10,20,30,20], k_j=2, then the optimal subsequence is [20,30] β it is the minimum lexicographically among all subsequences of length 2 with the maximum total sum of items. Thus, the answer to the request k_j=2, pos_j=1 is the number 20, and the answer to the request k_j=2, pos_j=2 is the number 30.
Input
The first line contains an integer n (1 β€ n β€ 100) β the length of the sequence a.
The second line contains elements of the sequence a: integer numbers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9).
The third line contains an integer m (1 β€ m β€ 100) β the number of requests.
The following m lines contain pairs of integers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j) β the requests.
Output
Print m integers r_1, r_2, ..., r_m (1 β€ r_j β€ 10^9) one per line: answers to the requests in the order they appear in the input. The value of r_j should be equal to the value contained in the position pos_j of the optimal subsequence for k=k_j.
Examples
Input
3
10 20 10
6
1 1
2 1
2 2
3 1
3 2
3 3
Output
20
10
20
10
20
10
Input
7
1 2 1 3 1 2 1
9
2 1
2 2
3 1
3 2
3 3
1 1
7 1
7 7
7 4
Output
2
3
2
3
2
3
1
1
3
Note
In the first example, for a=[10,20,10] the optimal subsequences are:
* for k=1: [20],
* for k=2: [10,20],
* for k=3: [10,20,10].
|
{
"input": [
"3\n10 20 10\n6\n1 1\n2 1\n2 2\n3 1\n3 2\n3 3\n",
"7\n1 2 1 3 1 2 1\n9\n2 1\n2 2\n3 1\n3 2\n3 3\n1 1\n7 1\n7 7\n7 4\n"
],
"output": [
"20\n10\n20\n10\n20\n10\n",
"2\n3\n2\n3\n2\n3\n1\n1\n3\n"
]
}
|
{
"input": [
"2\n1 10\n3\n2 2\n2 1\n1 1\n",
"2\n3922 3922\n3\n2 2\n2 1\n1 1\n",
"1\n1000000000\n1\n1 1\n",
"1\n1\n3\n1 1\n1 1\n1 1\n",
"5\n3 1 4 1 2\n15\n5 5\n5 4\n5 3\n5 2\n5 1\n4 4\n4 3\n4 2\n4 1\n3 3\n3 2\n3 1\n2 2\n2 1\n1 1\n",
"2\n392222 322\n3\n2 2\n2 1\n1 1\n"
],
"output": [
"10\n1\n10\n",
"3922\n3922\n3922\n",
"1000000000\n",
"1\n1\n1\n",
"2\n1\n4\n1\n3\n2\n4\n1\n3\n2\n4\n3\n4\n3\n4\n",
"322\n392222\n392222\n"
]
}
|
CORRECT
|
python3
|
n=int(input())
a=[int(i) for i in input().split()]
copy1=a[:]
a.sort()
m=int(input())
for i in range(m):
k,pos=map(int,input().split())
ans=[-1]
copy=a[-k:]
for i in copy1:
if i in copy:
copy.pop(copy.index(i))
ans.append(i)
print(ans[pos])
|
1227_D1. Optimal Subsequences (Easy Version)
|
This is the easier version of the problem. In this version 1 β€ n, m β€ 100. You can hack this problem only if you solve and lock both problems.
You are given a sequence of integers a=[a_1,a_2,...,a_n] of length n. Its subsequence is obtained by removing zero or more elements from the sequence a (they do not necessarily go consecutively). For example, for the sequence a=[11,20,11,33,11,20,11]:
* [11,20,11,33,11,20,11], [11,20,11,33,11,20], [11,11,11,11], [20], [33,20] are subsequences (these are just some of the long list);
* [40], [33,33], [33,20,20], [20,20,11,11] are not subsequences.
Suppose that an additional non-negative integer k (1 β€ k β€ n) is given, then the subsequence is called optimal if:
* it has a length of k and the sum of its elements is the maximum possible among all subsequences of length k;
* and among all subsequences of length k that satisfy the previous item, it is lexicographically minimal.
Recall that the sequence b=[b_1, b_2, ..., b_k] is lexicographically smaller than the sequence c=[c_1, c_2, ..., c_k] if the first element (from the left) in which they differ less in the sequence b than in c. Formally: there exists t (1 β€ t β€ k) such that b_1=c_1, b_2=c_2, ..., b_{t-1}=c_{t-1} and at the same time b_t<c_t. For example:
* [10, 20, 20] lexicographically less than [10, 21, 1],
* [7, 99, 99] is lexicographically less than [10, 21, 1],
* [10, 21, 0] is lexicographically less than [10, 21, 1].
You are given a sequence of a=[a_1,a_2,...,a_n] and m requests, each consisting of two numbers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j). For each query, print the value that is in the index pos_j of the optimal subsequence of the given sequence a for k=k_j.
For example, if n=4, a=[10,20,30,20], k_j=2, then the optimal subsequence is [20,30] β it is the minimum lexicographically among all subsequences of length 2 with the maximum total sum of items. Thus, the answer to the request k_j=2, pos_j=1 is the number 20, and the answer to the request k_j=2, pos_j=2 is the number 30.
Input
The first line contains an integer n (1 β€ n β€ 100) β the length of the sequence a.
The second line contains elements of the sequence a: integer numbers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9).
The third line contains an integer m (1 β€ m β€ 100) β the number of requests.
The following m lines contain pairs of integers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j) β the requests.
Output
Print m integers r_1, r_2, ..., r_m (1 β€ r_j β€ 10^9) one per line: answers to the requests in the order they appear in the input. The value of r_j should be equal to the value contained in the position pos_j of the optimal subsequence for k=k_j.
Examples
Input
3
10 20 10
6
1 1
2 1
2 2
3 1
3 2
3 3
Output
20
10
20
10
20
10
Input
7
1 2 1 3 1 2 1
9
2 1
2 2
3 1
3 2
3 3
1 1
7 1
7 7
7 4
Output
2
3
2
3
2
3
1
1
3
Note
In the first example, for a=[10,20,10] the optimal subsequences are:
* for k=1: [20],
* for k=2: [10,20],
* for k=3: [10,20,10].
|
{
"input": [
"3\n10 20 10\n6\n1 1\n2 1\n2 2\n3 1\n3 2\n3 3\n",
"7\n1 2 1 3 1 2 1\n9\n2 1\n2 2\n3 1\n3 2\n3 3\n1 1\n7 1\n7 7\n7 4\n"
],
"output": [
"20\n10\n20\n10\n20\n10\n",
"2\n3\n2\n3\n2\n3\n1\n1\n3\n"
]
}
|
{
"input": [
"2\n1 10\n3\n2 2\n2 1\n1 1\n",
"2\n3922 3922\n3\n2 2\n2 1\n1 1\n",
"1\n1000000000\n1\n1 1\n",
"1\n1\n3\n1 1\n1 1\n1 1\n",
"5\n3 1 4 1 2\n15\n5 5\n5 4\n5 3\n5 2\n5 1\n4 4\n4 3\n4 2\n4 1\n3 3\n3 2\n3 1\n2 2\n2 1\n1 1\n",
"2\n392222 322\n3\n2 2\n2 1\n1 1\n"
],
"output": [
"10\n1\n10\n",
"3922\n3922\n3922\n",
"1000000000\n",
"1\n1\n1\n",
"2\n1\n4\n1\n3\n2\n4\n1\n3\n2\n4\n3\n4\n3\n4\n",
"322\n392222\n392222\n"
]
}
|
CORRECT
|
python3
|
n = int(input())
a = [int(i) for i in input().split()]
b = sorted(a)
b.reverse()
m = int(input())
for _ in range(m):
k, pos = map(int, input().split())
x = 0
j = 0
d = dict()
ans = 0
for i in b[:k]:
d[i] = d.get(i, 0) + 1
while x != pos:
u = a[j]
if u in d:
d[u] -= 1
x += 1
if d[u] == 0:
del d[u]
j += 1
ans = u
print(ans)
|
1227_D1. Optimal Subsequences (Easy Version)
|
This is the easier version of the problem. In this version 1 β€ n, m β€ 100. You can hack this problem only if you solve and lock both problems.
You are given a sequence of integers a=[a_1,a_2,...,a_n] of length n. Its subsequence is obtained by removing zero or more elements from the sequence a (they do not necessarily go consecutively). For example, for the sequence a=[11,20,11,33,11,20,11]:
* [11,20,11,33,11,20,11], [11,20,11,33,11,20], [11,11,11,11], [20], [33,20] are subsequences (these are just some of the long list);
* [40], [33,33], [33,20,20], [20,20,11,11] are not subsequences.
Suppose that an additional non-negative integer k (1 β€ k β€ n) is given, then the subsequence is called optimal if:
* it has a length of k and the sum of its elements is the maximum possible among all subsequences of length k;
* and among all subsequences of length k that satisfy the previous item, it is lexicographically minimal.
Recall that the sequence b=[b_1, b_2, ..., b_k] is lexicographically smaller than the sequence c=[c_1, c_2, ..., c_k] if the first element (from the left) in which they differ less in the sequence b than in c. Formally: there exists t (1 β€ t β€ k) such that b_1=c_1, b_2=c_2, ..., b_{t-1}=c_{t-1} and at the same time b_t<c_t. For example:
* [10, 20, 20] lexicographically less than [10, 21, 1],
* [7, 99, 99] is lexicographically less than [10, 21, 1],
* [10, 21, 0] is lexicographically less than [10, 21, 1].
You are given a sequence of a=[a_1,a_2,...,a_n] and m requests, each consisting of two numbers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j). For each query, print the value that is in the index pos_j of the optimal subsequence of the given sequence a for k=k_j.
For example, if n=4, a=[10,20,30,20], k_j=2, then the optimal subsequence is [20,30] β it is the minimum lexicographically among all subsequences of length 2 with the maximum total sum of items. Thus, the answer to the request k_j=2, pos_j=1 is the number 20, and the answer to the request k_j=2, pos_j=2 is the number 30.
Input
The first line contains an integer n (1 β€ n β€ 100) β the length of the sequence a.
The second line contains elements of the sequence a: integer numbers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9).
The third line contains an integer m (1 β€ m β€ 100) β the number of requests.
The following m lines contain pairs of integers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j) β the requests.
Output
Print m integers r_1, r_2, ..., r_m (1 β€ r_j β€ 10^9) one per line: answers to the requests in the order they appear in the input. The value of r_j should be equal to the value contained in the position pos_j of the optimal subsequence for k=k_j.
Examples
Input
3
10 20 10
6
1 1
2 1
2 2
3 1
3 2
3 3
Output
20
10
20
10
20
10
Input
7
1 2 1 3 1 2 1
9
2 1
2 2
3 1
3 2
3 3
1 1
7 1
7 7
7 4
Output
2
3
2
3
2
3
1
1
3
Note
In the first example, for a=[10,20,10] the optimal subsequences are:
* for k=1: [20],
* for k=2: [10,20],
* for k=3: [10,20,10].
|
{
"input": [
"3\n10 20 10\n6\n1 1\n2 1\n2 2\n3 1\n3 2\n3 3\n",
"7\n1 2 1 3 1 2 1\n9\n2 1\n2 2\n3 1\n3 2\n3 3\n1 1\n7 1\n7 7\n7 4\n"
],
"output": [
"20\n10\n20\n10\n20\n10\n",
"2\n3\n2\n3\n2\n3\n1\n1\n3\n"
]
}
|
{
"input": [
"2\n1 10\n3\n2 2\n2 1\n1 1\n",
"2\n3922 3922\n3\n2 2\n2 1\n1 1\n",
"1\n1000000000\n1\n1 1\n",
"1\n1\n3\n1 1\n1 1\n1 1\n",
"5\n3 1 4 1 2\n15\n5 5\n5 4\n5 3\n5 2\n5 1\n4 4\n4 3\n4 2\n4 1\n3 3\n3 2\n3 1\n2 2\n2 1\n1 1\n",
"2\n392222 322\n3\n2 2\n2 1\n1 1\n"
],
"output": [
"10\n1\n10\n",
"3922\n3922\n3922\n",
"1000000000\n",
"1\n1\n1\n",
"2\n1\n4\n1\n3\n2\n4\n1\n3\n2\n4\n3\n4\n3\n4\n",
"322\n392222\n392222\n"
]
}
|
CORRECT
|
python3
|
n=int(input())
a=list(map(int,input().split()))
d=[]
for i in range(n):
d.append([a[i],-i])
d.sort(reverse=True)
e=[]
for i in range(n):
e.append([-d[i][1],d[i][0]])
m=int(input())
f=[]
for i in range(m):
b,c=map(int,input().split())
f=e[:b]
f.sort()
print(f[c-1][1])
|
1227_D1. Optimal Subsequences (Easy Version)
|
This is the easier version of the problem. In this version 1 β€ n, m β€ 100. You can hack this problem only if you solve and lock both problems.
You are given a sequence of integers a=[a_1,a_2,...,a_n] of length n. Its subsequence is obtained by removing zero or more elements from the sequence a (they do not necessarily go consecutively). For example, for the sequence a=[11,20,11,33,11,20,11]:
* [11,20,11,33,11,20,11], [11,20,11,33,11,20], [11,11,11,11], [20], [33,20] are subsequences (these are just some of the long list);
* [40], [33,33], [33,20,20], [20,20,11,11] are not subsequences.
Suppose that an additional non-negative integer k (1 β€ k β€ n) is given, then the subsequence is called optimal if:
* it has a length of k and the sum of its elements is the maximum possible among all subsequences of length k;
* and among all subsequences of length k that satisfy the previous item, it is lexicographically minimal.
Recall that the sequence b=[b_1, b_2, ..., b_k] is lexicographically smaller than the sequence c=[c_1, c_2, ..., c_k] if the first element (from the left) in which they differ less in the sequence b than in c. Formally: there exists t (1 β€ t β€ k) such that b_1=c_1, b_2=c_2, ..., b_{t-1}=c_{t-1} and at the same time b_t<c_t. For example:
* [10, 20, 20] lexicographically less than [10, 21, 1],
* [7, 99, 99] is lexicographically less than [10, 21, 1],
* [10, 21, 0] is lexicographically less than [10, 21, 1].
You are given a sequence of a=[a_1,a_2,...,a_n] and m requests, each consisting of two numbers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j). For each query, print the value that is in the index pos_j of the optimal subsequence of the given sequence a for k=k_j.
For example, if n=4, a=[10,20,30,20], k_j=2, then the optimal subsequence is [20,30] β it is the minimum lexicographically among all subsequences of length 2 with the maximum total sum of items. Thus, the answer to the request k_j=2, pos_j=1 is the number 20, and the answer to the request k_j=2, pos_j=2 is the number 30.
Input
The first line contains an integer n (1 β€ n β€ 100) β the length of the sequence a.
The second line contains elements of the sequence a: integer numbers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9).
The third line contains an integer m (1 β€ m β€ 100) β the number of requests.
The following m lines contain pairs of integers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j) β the requests.
Output
Print m integers r_1, r_2, ..., r_m (1 β€ r_j β€ 10^9) one per line: answers to the requests in the order they appear in the input. The value of r_j should be equal to the value contained in the position pos_j of the optimal subsequence for k=k_j.
Examples
Input
3
10 20 10
6
1 1
2 1
2 2
3 1
3 2
3 3
Output
20
10
20
10
20
10
Input
7
1 2 1 3 1 2 1
9
2 1
2 2
3 1
3 2
3 3
1 1
7 1
7 7
7 4
Output
2
3
2
3
2
3
1
1
3
Note
In the first example, for a=[10,20,10] the optimal subsequences are:
* for k=1: [20],
* for k=2: [10,20],
* for k=3: [10,20,10].
|
{
"input": [
"3\n10 20 10\n6\n1 1\n2 1\n2 2\n3 1\n3 2\n3 3\n",
"7\n1 2 1 3 1 2 1\n9\n2 1\n2 2\n3 1\n3 2\n3 3\n1 1\n7 1\n7 7\n7 4\n"
],
"output": [
"20\n10\n20\n10\n20\n10\n",
"2\n3\n2\n3\n2\n3\n1\n1\n3\n"
]
}
|
{
"input": [
"2\n1 10\n3\n2 2\n2 1\n1 1\n",
"2\n3922 3922\n3\n2 2\n2 1\n1 1\n",
"1\n1000000000\n1\n1 1\n",
"1\n1\n3\n1 1\n1 1\n1 1\n",
"5\n3 1 4 1 2\n15\n5 5\n5 4\n5 3\n5 2\n5 1\n4 4\n4 3\n4 2\n4 1\n3 3\n3 2\n3 1\n2 2\n2 1\n1 1\n",
"2\n392222 322\n3\n2 2\n2 1\n1 1\n"
],
"output": [
"10\n1\n10\n",
"3922\n3922\n3922\n",
"1000000000\n",
"1\n1\n1\n",
"2\n1\n4\n1\n3\n2\n4\n1\n3\n2\n4\n3\n4\n3\n4\n",
"322\n392222\n392222\n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
int gcd(int a, int b) {
while (b) {
a %= b;
swap(a, b);
}
return a;
}
void ct(vector<int> &a) {
for (auto &i : a) cout << i << ' ';
cout << '\n';
}
void ct(vector<pair<int, int>> &a) {
for (auto &i : a) cout << i.first << ":" << i.second << ' ';
cout << '\n';
}
void ct(vector<vector<pair<int, int>>> &a) {
for (auto &i : a) ct(i);
}
void ct(vector<vector<int>> &a) {
for (auto &i : a) ct(i);
}
void ct(vector<set<int>> &a) {
for (auto &i : a) {
for (auto &j : i) cout << j << ' ';
cout << '\n';
}
}
void ct(set<pair<int, int>> &a) {
for (auto &i : a) cout << i.first << ':' << i.second << ' ';
cout << '\n';
}
void ct(pair<int, int> &a) { cout << a.first << ':' << a.second << '\n'; }
void ci(vector<int> &a) {
for (int i = 0; i < a.size(); ++i) cin >> a[i];
}
void ci(vector<vector<int>> &a) {
for (int i = 0; i < a.size(); ++i) ci(a[i]);
}
void think() {}
signed main() {
int n, m;
cin >> n;
vector<pair<int, int>> a;
for (int i = 0; i < n; ++i) {
int c;
cin >> c;
a.push_back(make_pair(c, -i));
}
sort(a.begin(), a.end());
cin >> m;
for (int i = 0; i < m; ++i) {
int k, pos;
vector<pair<int, int>> ans;
cin >> k >> pos;
for (int i = n - k; i < n; ++i) {
ans.push_back(make_pair(-a[i].second, a[i].first));
}
sort(ans.begin(), ans.end());
cout << ans[pos - 1].second << '\n';
ans.clear();
}
}
|
1227_D1. Optimal Subsequences (Easy Version)
|
This is the easier version of the problem. In this version 1 β€ n, m β€ 100. You can hack this problem only if you solve and lock both problems.
You are given a sequence of integers a=[a_1,a_2,...,a_n] of length n. Its subsequence is obtained by removing zero or more elements from the sequence a (they do not necessarily go consecutively). For example, for the sequence a=[11,20,11,33,11,20,11]:
* [11,20,11,33,11,20,11], [11,20,11,33,11,20], [11,11,11,11], [20], [33,20] are subsequences (these are just some of the long list);
* [40], [33,33], [33,20,20], [20,20,11,11] are not subsequences.
Suppose that an additional non-negative integer k (1 β€ k β€ n) is given, then the subsequence is called optimal if:
* it has a length of k and the sum of its elements is the maximum possible among all subsequences of length k;
* and among all subsequences of length k that satisfy the previous item, it is lexicographically minimal.
Recall that the sequence b=[b_1, b_2, ..., b_k] is lexicographically smaller than the sequence c=[c_1, c_2, ..., c_k] if the first element (from the left) in which they differ less in the sequence b than in c. Formally: there exists t (1 β€ t β€ k) such that b_1=c_1, b_2=c_2, ..., b_{t-1}=c_{t-1} and at the same time b_t<c_t. For example:
* [10, 20, 20] lexicographically less than [10, 21, 1],
* [7, 99, 99] is lexicographically less than [10, 21, 1],
* [10, 21, 0] is lexicographically less than [10, 21, 1].
You are given a sequence of a=[a_1,a_2,...,a_n] and m requests, each consisting of two numbers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j). For each query, print the value that is in the index pos_j of the optimal subsequence of the given sequence a for k=k_j.
For example, if n=4, a=[10,20,30,20], k_j=2, then the optimal subsequence is [20,30] β it is the minimum lexicographically among all subsequences of length 2 with the maximum total sum of items. Thus, the answer to the request k_j=2, pos_j=1 is the number 20, and the answer to the request k_j=2, pos_j=2 is the number 30.
Input
The first line contains an integer n (1 β€ n β€ 100) β the length of the sequence a.
The second line contains elements of the sequence a: integer numbers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9).
The third line contains an integer m (1 β€ m β€ 100) β the number of requests.
The following m lines contain pairs of integers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j) β the requests.
Output
Print m integers r_1, r_2, ..., r_m (1 β€ r_j β€ 10^9) one per line: answers to the requests in the order they appear in the input. The value of r_j should be equal to the value contained in the position pos_j of the optimal subsequence for k=k_j.
Examples
Input
3
10 20 10
6
1 1
2 1
2 2
3 1
3 2
3 3
Output
20
10
20
10
20
10
Input
7
1 2 1 3 1 2 1
9
2 1
2 2
3 1
3 2
3 3
1 1
7 1
7 7
7 4
Output
2
3
2
3
2
3
1
1
3
Note
In the first example, for a=[10,20,10] the optimal subsequences are:
* for k=1: [20],
* for k=2: [10,20],
* for k=3: [10,20,10].
|
{
"input": [
"3\n10 20 10\n6\n1 1\n2 1\n2 2\n3 1\n3 2\n3 3\n",
"7\n1 2 1 3 1 2 1\n9\n2 1\n2 2\n3 1\n3 2\n3 3\n1 1\n7 1\n7 7\n7 4\n"
],
"output": [
"20\n10\n20\n10\n20\n10\n",
"2\n3\n2\n3\n2\n3\n1\n1\n3\n"
]
}
|
{
"input": [
"2\n1 10\n3\n2 2\n2 1\n1 1\n",
"2\n3922 3922\n3\n2 2\n2 1\n1 1\n",
"1\n1000000000\n1\n1 1\n",
"1\n1\n3\n1 1\n1 1\n1 1\n",
"5\n3 1 4 1 2\n15\n5 5\n5 4\n5 3\n5 2\n5 1\n4 4\n4 3\n4 2\n4 1\n3 3\n3 2\n3 1\n2 2\n2 1\n1 1\n",
"2\n392222 322\n3\n2 2\n2 1\n1 1\n"
],
"output": [
"10\n1\n10\n",
"3922\n3922\n3922\n",
"1000000000\n",
"1\n1\n1\n",
"2\n1\n4\n1\n3\n2\n4\n1\n3\n2\n4\n3\n4\n3\n4\n",
"322\n392222\n392222\n"
]
}
|
CORRECT
|
java
|
import java.io.*;
import java.util.*;
public class OptimalSubsequences {
public static void main(String[] args) {
FastScanner in = new FastScanner();
PrintWriter out = new PrintWriter(System.out);
HashMap<Integer,ArrayList<Integer>> map = new HashMap<>();
int n = in.nextInt();
int a[] = in.readArray(n);
Integer b[] = new Integer[n];
for(int i=0;i<n;i++) b[i] = a[i];
Arrays.parallelSort(b,Collections.reverseOrder());
int m = in.nextInt();
while(m-->0) {
int k = in.nextInt(), pos = in.nextInt()-1;
if(!map.containsKey(k)) {
int max[] = new int[k];
for(int i=0;i<k;i++) max[i] = b[i];
ArrayList<Integer> order = new ArrayList<>();
int vis[] = new int[n];
for(int i : max) {
for(int j=0;j<n;j++) {
if(a[j]==i&&vis[j]!=1) {
vis[j] = 1;
order.add(j);break;
}
}
}
Collections.sort(order);
map.put(k, order);
}
out.println(a[map.get(k).get(pos)]);
}
out.flush();
}
static class FastScanner {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer("");
String next() {
while(!st.hasMoreTokens())
try {
st = new StringTokenizer(br.readLine());
}catch(IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
int[] readArray(int n) {
int a[] = new int[n];
for(int i=0;i<n;i++) a[i] = nextInt();
return a;
}
long[] readArray(int n,long x) {
long a[] = new long[n];
for(int i=0;i<n;i++) a[i] = nextLong();
return a;
}
}
static boolean arrayEquals(char a[], char b[]) {
int n = a.length;
boolean verdict = true;
for(int i=0;i<n;i++) {
if(a[i]!=b[i]) {
verdict = false;break;
}
}
return verdict;
}
static long lcm(long a, long b) {
return (a*b)/gcd(a,b);
}
static long gcd(long a, long b) {
if(b==0) return a;
else return gcd(b,a%b);
}
}
|
1227_D1. Optimal Subsequences (Easy Version)
|
This is the easier version of the problem. In this version 1 β€ n, m β€ 100. You can hack this problem only if you solve and lock both problems.
You are given a sequence of integers a=[a_1,a_2,...,a_n] of length n. Its subsequence is obtained by removing zero or more elements from the sequence a (they do not necessarily go consecutively). For example, for the sequence a=[11,20,11,33,11,20,11]:
* [11,20,11,33,11,20,11], [11,20,11,33,11,20], [11,11,11,11], [20], [33,20] are subsequences (these are just some of the long list);
* [40], [33,33], [33,20,20], [20,20,11,11] are not subsequences.
Suppose that an additional non-negative integer k (1 β€ k β€ n) is given, then the subsequence is called optimal if:
* it has a length of k and the sum of its elements is the maximum possible among all subsequences of length k;
* and among all subsequences of length k that satisfy the previous item, it is lexicographically minimal.
Recall that the sequence b=[b_1, b_2, ..., b_k] is lexicographically smaller than the sequence c=[c_1, c_2, ..., c_k] if the first element (from the left) in which they differ less in the sequence b than in c. Formally: there exists t (1 β€ t β€ k) such that b_1=c_1, b_2=c_2, ..., b_{t-1}=c_{t-1} and at the same time b_t<c_t. For example:
* [10, 20, 20] lexicographically less than [10, 21, 1],
* [7, 99, 99] is lexicographically less than [10, 21, 1],
* [10, 21, 0] is lexicographically less than [10, 21, 1].
You are given a sequence of a=[a_1,a_2,...,a_n] and m requests, each consisting of two numbers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j). For each query, print the value that is in the index pos_j of the optimal subsequence of the given sequence a for k=k_j.
For example, if n=4, a=[10,20,30,20], k_j=2, then the optimal subsequence is [20,30] β it is the minimum lexicographically among all subsequences of length 2 with the maximum total sum of items. Thus, the answer to the request k_j=2, pos_j=1 is the number 20, and the answer to the request k_j=2, pos_j=2 is the number 30.
Input
The first line contains an integer n (1 β€ n β€ 100) β the length of the sequence a.
The second line contains elements of the sequence a: integer numbers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9).
The third line contains an integer m (1 β€ m β€ 100) β the number of requests.
The following m lines contain pairs of integers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j) β the requests.
Output
Print m integers r_1, r_2, ..., r_m (1 β€ r_j β€ 10^9) one per line: answers to the requests in the order they appear in the input. The value of r_j should be equal to the value contained in the position pos_j of the optimal subsequence for k=k_j.
Examples
Input
3
10 20 10
6
1 1
2 1
2 2
3 1
3 2
3 3
Output
20
10
20
10
20
10
Input
7
1 2 1 3 1 2 1
9
2 1
2 2
3 1
3 2
3 3
1 1
7 1
7 7
7 4
Output
2
3
2
3
2
3
1
1
3
Note
In the first example, for a=[10,20,10] the optimal subsequences are:
* for k=1: [20],
* for k=2: [10,20],
* for k=3: [10,20,10].
|
{
"input": [
"3\n10 20 10\n6\n1 1\n2 1\n2 2\n3 1\n3 2\n3 3\n",
"7\n1 2 1 3 1 2 1\n9\n2 1\n2 2\n3 1\n3 2\n3 3\n1 1\n7 1\n7 7\n7 4\n"
],
"output": [
"20\n10\n20\n10\n20\n10\n",
"2\n3\n2\n3\n2\n3\n1\n1\n3\n"
]
}
|
{
"input": [
"2\n1 10\n3\n2 2\n2 1\n1 1\n",
"2\n3922 3922\n3\n2 2\n2 1\n1 1\n",
"1\n1000000000\n1\n1 1\n",
"1\n1\n3\n1 1\n1 1\n1 1\n",
"5\n3 1 4 1 2\n15\n5 5\n5 4\n5 3\n5 2\n5 1\n4 4\n4 3\n4 2\n4 1\n3 3\n3 2\n3 1\n2 2\n2 1\n1 1\n",
"2\n392222 322\n3\n2 2\n2 1\n1 1\n"
],
"output": [
"10\n1\n10\n",
"3922\n3922\n3922\n",
"1000000000\n",
"1\n1\n1\n",
"2\n1\n4\n1\n3\n2\n4\n1\n3\n2\n4\n3\n4\n3\n4\n",
"322\n392222\n392222\n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
int n, m;
pair<int, int> a[112];
int main() {
ios_base::sync_with_stdio(0);
cin >> n;
for (int i = 0; i < n; i++) {
int num;
cin >> num;
a[i] = {-num, i};
}
sort(a, a + n);
cin >> m;
for (int i = 1; i <= m; i++) {
int k, pos;
cin >> k >> pos;
vector<pair<int, int> > sub;
for (int j = 0; j < k; j++) {
sub.push_back({a[j].second, -a[j].first});
}
sort(sub.begin(), sub.end());
cout << sub[pos - 1].second << "\n";
}
}
|
1227_D1. Optimal Subsequences (Easy Version)
|
This is the easier version of the problem. In this version 1 β€ n, m β€ 100. You can hack this problem only if you solve and lock both problems.
You are given a sequence of integers a=[a_1,a_2,...,a_n] of length n. Its subsequence is obtained by removing zero or more elements from the sequence a (they do not necessarily go consecutively). For example, for the sequence a=[11,20,11,33,11,20,11]:
* [11,20,11,33,11,20,11], [11,20,11,33,11,20], [11,11,11,11], [20], [33,20] are subsequences (these are just some of the long list);
* [40], [33,33], [33,20,20], [20,20,11,11] are not subsequences.
Suppose that an additional non-negative integer k (1 β€ k β€ n) is given, then the subsequence is called optimal if:
* it has a length of k and the sum of its elements is the maximum possible among all subsequences of length k;
* and among all subsequences of length k that satisfy the previous item, it is lexicographically minimal.
Recall that the sequence b=[b_1, b_2, ..., b_k] is lexicographically smaller than the sequence c=[c_1, c_2, ..., c_k] if the first element (from the left) in which they differ less in the sequence b than in c. Formally: there exists t (1 β€ t β€ k) such that b_1=c_1, b_2=c_2, ..., b_{t-1}=c_{t-1} and at the same time b_t<c_t. For example:
* [10, 20, 20] lexicographically less than [10, 21, 1],
* [7, 99, 99] is lexicographically less than [10, 21, 1],
* [10, 21, 0] is lexicographically less than [10, 21, 1].
You are given a sequence of a=[a_1,a_2,...,a_n] and m requests, each consisting of two numbers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j). For each query, print the value that is in the index pos_j of the optimal subsequence of the given sequence a for k=k_j.
For example, if n=4, a=[10,20,30,20], k_j=2, then the optimal subsequence is [20,30] β it is the minimum lexicographically among all subsequences of length 2 with the maximum total sum of items. Thus, the answer to the request k_j=2, pos_j=1 is the number 20, and the answer to the request k_j=2, pos_j=2 is the number 30.
Input
The first line contains an integer n (1 β€ n β€ 100) β the length of the sequence a.
The second line contains elements of the sequence a: integer numbers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9).
The third line contains an integer m (1 β€ m β€ 100) β the number of requests.
The following m lines contain pairs of integers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j) β the requests.
Output
Print m integers r_1, r_2, ..., r_m (1 β€ r_j β€ 10^9) one per line: answers to the requests in the order they appear in the input. The value of r_j should be equal to the value contained in the position pos_j of the optimal subsequence for k=k_j.
Examples
Input
3
10 20 10
6
1 1
2 1
2 2
3 1
3 2
3 3
Output
20
10
20
10
20
10
Input
7
1 2 1 3 1 2 1
9
2 1
2 2
3 1
3 2
3 3
1 1
7 1
7 7
7 4
Output
2
3
2
3
2
3
1
1
3
Note
In the first example, for a=[10,20,10] the optimal subsequences are:
* for k=1: [20],
* for k=2: [10,20],
* for k=3: [10,20,10].
|
{
"input": [
"3\n10 20 10\n6\n1 1\n2 1\n2 2\n3 1\n3 2\n3 3\n",
"7\n1 2 1 3 1 2 1\n9\n2 1\n2 2\n3 1\n3 2\n3 3\n1 1\n7 1\n7 7\n7 4\n"
],
"output": [
"20\n10\n20\n10\n20\n10\n",
"2\n3\n2\n3\n2\n3\n1\n1\n3\n"
]
}
|
{
"input": [
"2\n1 10\n3\n2 2\n2 1\n1 1\n",
"2\n3922 3922\n3\n2 2\n2 1\n1 1\n",
"1\n1000000000\n1\n1 1\n",
"1\n1\n3\n1 1\n1 1\n1 1\n",
"5\n3 1 4 1 2\n15\n5 5\n5 4\n5 3\n5 2\n5 1\n4 4\n4 3\n4 2\n4 1\n3 3\n3 2\n3 1\n2 2\n2 1\n1 1\n",
"2\n392222 322\n3\n2 2\n2 1\n1 1\n"
],
"output": [
"10\n1\n10\n",
"3922\n3922\n3922\n",
"1000000000\n",
"1\n1\n1\n",
"2\n1\n4\n1\n3\n2\n4\n1\n3\n2\n4\n3\n4\n3\n4\n",
"322\n392222\n392222\n"
]
}
|
CORRECT
|
python3
|
n=int(input())
import copy
mins=[0]*n
a=list(map(int,input().split()))
m=int(input())
for i in range(m):
f=a.copy()
k,pos=map(int,input().split())
for l in range(n-k):
if mins[l]==0:
mins[l]=min(f)
for j in range(len(f)-1,-1,-1):
if f[j]==mins[l]:
f.pop(j)
break
print(f[pos-1])
|
1227_D1. Optimal Subsequences (Easy Version)
|
This is the easier version of the problem. In this version 1 β€ n, m β€ 100. You can hack this problem only if you solve and lock both problems.
You are given a sequence of integers a=[a_1,a_2,...,a_n] of length n. Its subsequence is obtained by removing zero or more elements from the sequence a (they do not necessarily go consecutively). For example, for the sequence a=[11,20,11,33,11,20,11]:
* [11,20,11,33,11,20,11], [11,20,11,33,11,20], [11,11,11,11], [20], [33,20] are subsequences (these are just some of the long list);
* [40], [33,33], [33,20,20], [20,20,11,11] are not subsequences.
Suppose that an additional non-negative integer k (1 β€ k β€ n) is given, then the subsequence is called optimal if:
* it has a length of k and the sum of its elements is the maximum possible among all subsequences of length k;
* and among all subsequences of length k that satisfy the previous item, it is lexicographically minimal.
Recall that the sequence b=[b_1, b_2, ..., b_k] is lexicographically smaller than the sequence c=[c_1, c_2, ..., c_k] if the first element (from the left) in which they differ less in the sequence b than in c. Formally: there exists t (1 β€ t β€ k) such that b_1=c_1, b_2=c_2, ..., b_{t-1}=c_{t-1} and at the same time b_t<c_t. For example:
* [10, 20, 20] lexicographically less than [10, 21, 1],
* [7, 99, 99] is lexicographically less than [10, 21, 1],
* [10, 21, 0] is lexicographically less than [10, 21, 1].
You are given a sequence of a=[a_1,a_2,...,a_n] and m requests, each consisting of two numbers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j). For each query, print the value that is in the index pos_j of the optimal subsequence of the given sequence a for k=k_j.
For example, if n=4, a=[10,20,30,20], k_j=2, then the optimal subsequence is [20,30] β it is the minimum lexicographically among all subsequences of length 2 with the maximum total sum of items. Thus, the answer to the request k_j=2, pos_j=1 is the number 20, and the answer to the request k_j=2, pos_j=2 is the number 30.
Input
The first line contains an integer n (1 β€ n β€ 100) β the length of the sequence a.
The second line contains elements of the sequence a: integer numbers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9).
The third line contains an integer m (1 β€ m β€ 100) β the number of requests.
The following m lines contain pairs of integers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j) β the requests.
Output
Print m integers r_1, r_2, ..., r_m (1 β€ r_j β€ 10^9) one per line: answers to the requests in the order they appear in the input. The value of r_j should be equal to the value contained in the position pos_j of the optimal subsequence for k=k_j.
Examples
Input
3
10 20 10
6
1 1
2 1
2 2
3 1
3 2
3 3
Output
20
10
20
10
20
10
Input
7
1 2 1 3 1 2 1
9
2 1
2 2
3 1
3 2
3 3
1 1
7 1
7 7
7 4
Output
2
3
2
3
2
3
1
1
3
Note
In the first example, for a=[10,20,10] the optimal subsequences are:
* for k=1: [20],
* for k=2: [10,20],
* for k=3: [10,20,10].
|
{
"input": [
"3\n10 20 10\n6\n1 1\n2 1\n2 2\n3 1\n3 2\n3 3\n",
"7\n1 2 1 3 1 2 1\n9\n2 1\n2 2\n3 1\n3 2\n3 3\n1 1\n7 1\n7 7\n7 4\n"
],
"output": [
"20\n10\n20\n10\n20\n10\n",
"2\n3\n2\n3\n2\n3\n1\n1\n3\n"
]
}
|
{
"input": [
"2\n1 10\n3\n2 2\n2 1\n1 1\n",
"2\n3922 3922\n3\n2 2\n2 1\n1 1\n",
"1\n1000000000\n1\n1 1\n",
"1\n1\n3\n1 1\n1 1\n1 1\n",
"5\n3 1 4 1 2\n15\n5 5\n5 4\n5 3\n5 2\n5 1\n4 4\n4 3\n4 2\n4 1\n3 3\n3 2\n3 1\n2 2\n2 1\n1 1\n",
"2\n392222 322\n3\n2 2\n2 1\n1 1\n"
],
"output": [
"10\n1\n10\n",
"3922\n3922\n3922\n",
"1000000000\n",
"1\n1\n1\n",
"2\n1\n4\n1\n3\n2\n4\n1\n3\n2\n4\n3\n4\n3\n4\n",
"322\n392222\n392222\n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e4 + 9;
struct node {
int x, y;
} a[maxn];
int b[maxn];
bool cmp(node a, node b) { return a.x == b.x ? a.y < b.y : a.x > b.x; }
map<int, int> A;
int main() {
int n;
scanf("%d", &n);
for (int i = 1; i <= n; ++i) {
scanf("%d", &a[i].x);
b[i] = a[i].x;
a[i].y = i;
}
sort(a + 1, a + 1 + n, cmp);
int m;
scanf("%d", &m);
while (m--) {
int k, pos;
scanf("%d%d", &k, &pos);
for (int i = 1; i <= k; ++i) {
A[a[i].y] = 1;
}
int r = 1;
for (int j = 1; j <= n; ++j) {
if (A[j]) {
if (r == pos) {
printf("%d\n", b[j]);
break;
}
r++;
}
}
A.clear();
}
return 0;
}
|
1227_D1. Optimal Subsequences (Easy Version)
|
This is the easier version of the problem. In this version 1 β€ n, m β€ 100. You can hack this problem only if you solve and lock both problems.
You are given a sequence of integers a=[a_1,a_2,...,a_n] of length n. Its subsequence is obtained by removing zero or more elements from the sequence a (they do not necessarily go consecutively). For example, for the sequence a=[11,20,11,33,11,20,11]:
* [11,20,11,33,11,20,11], [11,20,11,33,11,20], [11,11,11,11], [20], [33,20] are subsequences (these are just some of the long list);
* [40], [33,33], [33,20,20], [20,20,11,11] are not subsequences.
Suppose that an additional non-negative integer k (1 β€ k β€ n) is given, then the subsequence is called optimal if:
* it has a length of k and the sum of its elements is the maximum possible among all subsequences of length k;
* and among all subsequences of length k that satisfy the previous item, it is lexicographically minimal.
Recall that the sequence b=[b_1, b_2, ..., b_k] is lexicographically smaller than the sequence c=[c_1, c_2, ..., c_k] if the first element (from the left) in which they differ less in the sequence b than in c. Formally: there exists t (1 β€ t β€ k) such that b_1=c_1, b_2=c_2, ..., b_{t-1}=c_{t-1} and at the same time b_t<c_t. For example:
* [10, 20, 20] lexicographically less than [10, 21, 1],
* [7, 99, 99] is lexicographically less than [10, 21, 1],
* [10, 21, 0] is lexicographically less than [10, 21, 1].
You are given a sequence of a=[a_1,a_2,...,a_n] and m requests, each consisting of two numbers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j). For each query, print the value that is in the index pos_j of the optimal subsequence of the given sequence a for k=k_j.
For example, if n=4, a=[10,20,30,20], k_j=2, then the optimal subsequence is [20,30] β it is the minimum lexicographically among all subsequences of length 2 with the maximum total sum of items. Thus, the answer to the request k_j=2, pos_j=1 is the number 20, and the answer to the request k_j=2, pos_j=2 is the number 30.
Input
The first line contains an integer n (1 β€ n β€ 100) β the length of the sequence a.
The second line contains elements of the sequence a: integer numbers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9).
The third line contains an integer m (1 β€ m β€ 100) β the number of requests.
The following m lines contain pairs of integers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j) β the requests.
Output
Print m integers r_1, r_2, ..., r_m (1 β€ r_j β€ 10^9) one per line: answers to the requests in the order they appear in the input. The value of r_j should be equal to the value contained in the position pos_j of the optimal subsequence for k=k_j.
Examples
Input
3
10 20 10
6
1 1
2 1
2 2
3 1
3 2
3 3
Output
20
10
20
10
20
10
Input
7
1 2 1 3 1 2 1
9
2 1
2 2
3 1
3 2
3 3
1 1
7 1
7 7
7 4
Output
2
3
2
3
2
3
1
1
3
Note
In the first example, for a=[10,20,10] the optimal subsequences are:
* for k=1: [20],
* for k=2: [10,20],
* for k=3: [10,20,10].
|
{
"input": [
"3\n10 20 10\n6\n1 1\n2 1\n2 2\n3 1\n3 2\n3 3\n",
"7\n1 2 1 3 1 2 1\n9\n2 1\n2 2\n3 1\n3 2\n3 3\n1 1\n7 1\n7 7\n7 4\n"
],
"output": [
"20\n10\n20\n10\n20\n10\n",
"2\n3\n2\n3\n2\n3\n1\n1\n3\n"
]
}
|
{
"input": [
"2\n1 10\n3\n2 2\n2 1\n1 1\n",
"2\n3922 3922\n3\n2 2\n2 1\n1 1\n",
"1\n1000000000\n1\n1 1\n",
"1\n1\n3\n1 1\n1 1\n1 1\n",
"5\n3 1 4 1 2\n15\n5 5\n5 4\n5 3\n5 2\n5 1\n4 4\n4 3\n4 2\n4 1\n3 3\n3 2\n3 1\n2 2\n2 1\n1 1\n",
"2\n392222 322\n3\n2 2\n2 1\n1 1\n"
],
"output": [
"10\n1\n10\n",
"3922\n3922\n3922\n",
"1000000000\n",
"1\n1\n1\n",
"2\n1\n4\n1\n3\n2\n4\n1\n3\n2\n4\n3\n4\n3\n4\n",
"322\n392222\n392222\n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 100;
int arr[N];
multimap<int, int, greater<int>> l;
map<int, int> mp;
vector<int> ans[N];
int main() {
ios_base::sync_with_stdio();
cin.tie(0);
cout.tie(0);
int n;
scanf("%d", &n);
for (int i = 0; i < n; ++i) scanf("%d", arr + i);
int m;
scanf("%d", &m);
for (int i = 0; i < n; ++i) {
l.insert(make_pair(arr[i], i));
}
for (int i = 0; i < n; ++i) {
map<int, int> temp;
int j = 0;
for (auto t = l.begin(); j <= i; ++j, ++t) {
temp[t->second] = t->first;
}
for (auto t : temp) {
ans[i].emplace_back(t.second);
}
}
for (int i = 0; i < m; ++i) {
int k, pos;
scanf("%d%d", &k, &pos);
--k, --pos;
printf("%d\n", ans[k][pos]);
}
}
|
1227_D1. Optimal Subsequences (Easy Version)
|
This is the easier version of the problem. In this version 1 β€ n, m β€ 100. You can hack this problem only if you solve and lock both problems.
You are given a sequence of integers a=[a_1,a_2,...,a_n] of length n. Its subsequence is obtained by removing zero or more elements from the sequence a (they do not necessarily go consecutively). For example, for the sequence a=[11,20,11,33,11,20,11]:
* [11,20,11,33,11,20,11], [11,20,11,33,11,20], [11,11,11,11], [20], [33,20] are subsequences (these are just some of the long list);
* [40], [33,33], [33,20,20], [20,20,11,11] are not subsequences.
Suppose that an additional non-negative integer k (1 β€ k β€ n) is given, then the subsequence is called optimal if:
* it has a length of k and the sum of its elements is the maximum possible among all subsequences of length k;
* and among all subsequences of length k that satisfy the previous item, it is lexicographically minimal.
Recall that the sequence b=[b_1, b_2, ..., b_k] is lexicographically smaller than the sequence c=[c_1, c_2, ..., c_k] if the first element (from the left) in which they differ less in the sequence b than in c. Formally: there exists t (1 β€ t β€ k) such that b_1=c_1, b_2=c_2, ..., b_{t-1}=c_{t-1} and at the same time b_t<c_t. For example:
* [10, 20, 20] lexicographically less than [10, 21, 1],
* [7, 99, 99] is lexicographically less than [10, 21, 1],
* [10, 21, 0] is lexicographically less than [10, 21, 1].
You are given a sequence of a=[a_1,a_2,...,a_n] and m requests, each consisting of two numbers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j). For each query, print the value that is in the index pos_j of the optimal subsequence of the given sequence a for k=k_j.
For example, if n=4, a=[10,20,30,20], k_j=2, then the optimal subsequence is [20,30] β it is the minimum lexicographically among all subsequences of length 2 with the maximum total sum of items. Thus, the answer to the request k_j=2, pos_j=1 is the number 20, and the answer to the request k_j=2, pos_j=2 is the number 30.
Input
The first line contains an integer n (1 β€ n β€ 100) β the length of the sequence a.
The second line contains elements of the sequence a: integer numbers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9).
The third line contains an integer m (1 β€ m β€ 100) β the number of requests.
The following m lines contain pairs of integers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j) β the requests.
Output
Print m integers r_1, r_2, ..., r_m (1 β€ r_j β€ 10^9) one per line: answers to the requests in the order they appear in the input. The value of r_j should be equal to the value contained in the position pos_j of the optimal subsequence for k=k_j.
Examples
Input
3
10 20 10
6
1 1
2 1
2 2
3 1
3 2
3 3
Output
20
10
20
10
20
10
Input
7
1 2 1 3 1 2 1
9
2 1
2 2
3 1
3 2
3 3
1 1
7 1
7 7
7 4
Output
2
3
2
3
2
3
1
1
3
Note
In the first example, for a=[10,20,10] the optimal subsequences are:
* for k=1: [20],
* for k=2: [10,20],
* for k=3: [10,20,10].
|
{
"input": [
"3\n10 20 10\n6\n1 1\n2 1\n2 2\n3 1\n3 2\n3 3\n",
"7\n1 2 1 3 1 2 1\n9\n2 1\n2 2\n3 1\n3 2\n3 3\n1 1\n7 1\n7 7\n7 4\n"
],
"output": [
"20\n10\n20\n10\n20\n10\n",
"2\n3\n2\n3\n2\n3\n1\n1\n3\n"
]
}
|
{
"input": [
"2\n1 10\n3\n2 2\n2 1\n1 1\n",
"2\n3922 3922\n3\n2 2\n2 1\n1 1\n",
"1\n1000000000\n1\n1 1\n",
"1\n1\n3\n1 1\n1 1\n1 1\n",
"5\n3 1 4 1 2\n15\n5 5\n5 4\n5 3\n5 2\n5 1\n4 4\n4 3\n4 2\n4 1\n3 3\n3 2\n3 1\n2 2\n2 1\n1 1\n",
"2\n392222 322\n3\n2 2\n2 1\n1 1\n"
],
"output": [
"10\n1\n10\n",
"3922\n3922\n3922\n",
"1000000000\n",
"1\n1\n1\n",
"2\n1\n4\n1\n3\n2\n4\n1\n3\n2\n4\n3\n4\n3\n4\n",
"322\n392222\n392222\n"
]
}
|
CORRECT
|
python3
|
n = int(input())
b = list(map(int, input().split()))
a = []
for i in range(n):
a.append([b[i], n - i])
a.sort()
a.reverse()
p = []
t = []
for i in range(n):
t.append(a[i])
d = t.copy()
d.sort(key=lambda x: -x[1])
p.append(d)
m = int(input())
#print(p)
for i in range(m):
k, pos = map(int, input().split())
d = p[k - 1].copy()
#print(d)
print(d[pos - 1][0])
|
1227_D1. Optimal Subsequences (Easy Version)
|
This is the easier version of the problem. In this version 1 β€ n, m β€ 100. You can hack this problem only if you solve and lock both problems.
You are given a sequence of integers a=[a_1,a_2,...,a_n] of length n. Its subsequence is obtained by removing zero or more elements from the sequence a (they do not necessarily go consecutively). For example, for the sequence a=[11,20,11,33,11,20,11]:
* [11,20,11,33,11,20,11], [11,20,11,33,11,20], [11,11,11,11], [20], [33,20] are subsequences (these are just some of the long list);
* [40], [33,33], [33,20,20], [20,20,11,11] are not subsequences.
Suppose that an additional non-negative integer k (1 β€ k β€ n) is given, then the subsequence is called optimal if:
* it has a length of k and the sum of its elements is the maximum possible among all subsequences of length k;
* and among all subsequences of length k that satisfy the previous item, it is lexicographically minimal.
Recall that the sequence b=[b_1, b_2, ..., b_k] is lexicographically smaller than the sequence c=[c_1, c_2, ..., c_k] if the first element (from the left) in which they differ less in the sequence b than in c. Formally: there exists t (1 β€ t β€ k) such that b_1=c_1, b_2=c_2, ..., b_{t-1}=c_{t-1} and at the same time b_t<c_t. For example:
* [10, 20, 20] lexicographically less than [10, 21, 1],
* [7, 99, 99] is lexicographically less than [10, 21, 1],
* [10, 21, 0] is lexicographically less than [10, 21, 1].
You are given a sequence of a=[a_1,a_2,...,a_n] and m requests, each consisting of two numbers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j). For each query, print the value that is in the index pos_j of the optimal subsequence of the given sequence a for k=k_j.
For example, if n=4, a=[10,20,30,20], k_j=2, then the optimal subsequence is [20,30] β it is the minimum lexicographically among all subsequences of length 2 with the maximum total sum of items. Thus, the answer to the request k_j=2, pos_j=1 is the number 20, and the answer to the request k_j=2, pos_j=2 is the number 30.
Input
The first line contains an integer n (1 β€ n β€ 100) β the length of the sequence a.
The second line contains elements of the sequence a: integer numbers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9).
The third line contains an integer m (1 β€ m β€ 100) β the number of requests.
The following m lines contain pairs of integers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j) β the requests.
Output
Print m integers r_1, r_2, ..., r_m (1 β€ r_j β€ 10^9) one per line: answers to the requests in the order they appear in the input. The value of r_j should be equal to the value contained in the position pos_j of the optimal subsequence for k=k_j.
Examples
Input
3
10 20 10
6
1 1
2 1
2 2
3 1
3 2
3 3
Output
20
10
20
10
20
10
Input
7
1 2 1 3 1 2 1
9
2 1
2 2
3 1
3 2
3 3
1 1
7 1
7 7
7 4
Output
2
3
2
3
2
3
1
1
3
Note
In the first example, for a=[10,20,10] the optimal subsequences are:
* for k=1: [20],
* for k=2: [10,20],
* for k=3: [10,20,10].
|
{
"input": [
"3\n10 20 10\n6\n1 1\n2 1\n2 2\n3 1\n3 2\n3 3\n",
"7\n1 2 1 3 1 2 1\n9\n2 1\n2 2\n3 1\n3 2\n3 3\n1 1\n7 1\n7 7\n7 4\n"
],
"output": [
"20\n10\n20\n10\n20\n10\n",
"2\n3\n2\n3\n2\n3\n1\n1\n3\n"
]
}
|
{
"input": [
"2\n1 10\n3\n2 2\n2 1\n1 1\n",
"2\n3922 3922\n3\n2 2\n2 1\n1 1\n",
"1\n1000000000\n1\n1 1\n",
"1\n1\n3\n1 1\n1 1\n1 1\n",
"5\n3 1 4 1 2\n15\n5 5\n5 4\n5 3\n5 2\n5 1\n4 4\n4 3\n4 2\n4 1\n3 3\n3 2\n3 1\n2 2\n2 1\n1 1\n",
"2\n392222 322\n3\n2 2\n2 1\n1 1\n"
],
"output": [
"10\n1\n10\n",
"3922\n3922\n3922\n",
"1000000000\n",
"1\n1\n1\n",
"2\n1\n4\n1\n3\n2\n4\n1\n3\n2\n4\n3\n4\n3\n4\n",
"322\n392222\n392222\n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
int bsl(vector<pair<int, int>> &A, int val) {
int left = -1;
int right = (int)A.size();
while (right - left > 1) {
int mid = (left + right) / 2;
if (A[mid].first >= val) {
right = mid;
} else {
left = mid;
}
}
return right;
}
int bsr(vector<pair<int, int>> &A, int val) {
int left = 0;
int right = (int)A.size() + 1;
while (right - left > 1) {
int mid = (left + right) / 2;
if (A[mid].first <= val) {
left = mid;
} else {
right = mid;
}
}
return left;
}
int main() {
int n;
cin >> n;
vector<int> N(n);
vector<pair<int, int>> A(n);
for (int i = 0; i < n; ++i) {
cin >> N[i];
A[i] = {N[i], i};
}
sort(A.begin(), A.end());
int m;
cin >> m;
for (int i = 0; i < m; ++i) {
int k, pos;
cin >> k >> pos;
int val = A[n - k].first;
set<int> S;
int r = bsr(A, val);
int l = bsl(A, val);
int val1 = n - r - 1;
int val2 = k - val1;
for (int j = l; j < l + val2; ++j) {
S.insert(A[j].second);
}
for (int j = r + 1; j < n; ++j) {
S.insert(A[j].second);
}
auto itr = S.begin();
advance(itr, pos - 1);
cout << N[*itr] << endl;
}
}
|
1227_D1. Optimal Subsequences (Easy Version)
|
This is the easier version of the problem. In this version 1 β€ n, m β€ 100. You can hack this problem only if you solve and lock both problems.
You are given a sequence of integers a=[a_1,a_2,...,a_n] of length n. Its subsequence is obtained by removing zero or more elements from the sequence a (they do not necessarily go consecutively). For example, for the sequence a=[11,20,11,33,11,20,11]:
* [11,20,11,33,11,20,11], [11,20,11,33,11,20], [11,11,11,11], [20], [33,20] are subsequences (these are just some of the long list);
* [40], [33,33], [33,20,20], [20,20,11,11] are not subsequences.
Suppose that an additional non-negative integer k (1 β€ k β€ n) is given, then the subsequence is called optimal if:
* it has a length of k and the sum of its elements is the maximum possible among all subsequences of length k;
* and among all subsequences of length k that satisfy the previous item, it is lexicographically minimal.
Recall that the sequence b=[b_1, b_2, ..., b_k] is lexicographically smaller than the sequence c=[c_1, c_2, ..., c_k] if the first element (from the left) in which they differ less in the sequence b than in c. Formally: there exists t (1 β€ t β€ k) such that b_1=c_1, b_2=c_2, ..., b_{t-1}=c_{t-1} and at the same time b_t<c_t. For example:
* [10, 20, 20] lexicographically less than [10, 21, 1],
* [7, 99, 99] is lexicographically less than [10, 21, 1],
* [10, 21, 0] is lexicographically less than [10, 21, 1].
You are given a sequence of a=[a_1,a_2,...,a_n] and m requests, each consisting of two numbers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j). For each query, print the value that is in the index pos_j of the optimal subsequence of the given sequence a for k=k_j.
For example, if n=4, a=[10,20,30,20], k_j=2, then the optimal subsequence is [20,30] β it is the minimum lexicographically among all subsequences of length 2 with the maximum total sum of items. Thus, the answer to the request k_j=2, pos_j=1 is the number 20, and the answer to the request k_j=2, pos_j=2 is the number 30.
Input
The first line contains an integer n (1 β€ n β€ 100) β the length of the sequence a.
The second line contains elements of the sequence a: integer numbers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9).
The third line contains an integer m (1 β€ m β€ 100) β the number of requests.
The following m lines contain pairs of integers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j) β the requests.
Output
Print m integers r_1, r_2, ..., r_m (1 β€ r_j β€ 10^9) one per line: answers to the requests in the order they appear in the input. The value of r_j should be equal to the value contained in the position pos_j of the optimal subsequence for k=k_j.
Examples
Input
3
10 20 10
6
1 1
2 1
2 2
3 1
3 2
3 3
Output
20
10
20
10
20
10
Input
7
1 2 1 3 1 2 1
9
2 1
2 2
3 1
3 2
3 3
1 1
7 1
7 7
7 4
Output
2
3
2
3
2
3
1
1
3
Note
In the first example, for a=[10,20,10] the optimal subsequences are:
* for k=1: [20],
* for k=2: [10,20],
* for k=3: [10,20,10].
|
{
"input": [
"3\n10 20 10\n6\n1 1\n2 1\n2 2\n3 1\n3 2\n3 3\n",
"7\n1 2 1 3 1 2 1\n9\n2 1\n2 2\n3 1\n3 2\n3 3\n1 1\n7 1\n7 7\n7 4\n"
],
"output": [
"20\n10\n20\n10\n20\n10\n",
"2\n3\n2\n3\n2\n3\n1\n1\n3\n"
]
}
|
{
"input": [
"2\n1 10\n3\n2 2\n2 1\n1 1\n",
"2\n3922 3922\n3\n2 2\n2 1\n1 1\n",
"1\n1000000000\n1\n1 1\n",
"1\n1\n3\n1 1\n1 1\n1 1\n",
"5\n3 1 4 1 2\n15\n5 5\n5 4\n5 3\n5 2\n5 1\n4 4\n4 3\n4 2\n4 1\n3 3\n3 2\n3 1\n2 2\n2 1\n1 1\n",
"2\n392222 322\n3\n2 2\n2 1\n1 1\n"
],
"output": [
"10\n1\n10\n",
"3922\n3922\n3922\n",
"1000000000\n",
"1\n1\n1\n",
"2\n1\n4\n1\n3\n2\n4\n1\n3\n2\n4\n3\n4\n3\n4\n",
"322\n392222\n392222\n"
]
}
|
CORRECT
|
python3
|
n = int(input())
a = [int(x) for x in input().split()]
a_sorted = sorted(a, reverse=True)
m = int(input())
for i in range(m):
k, pos = [int(x) for x in input().split()]
vals = a_sorted[0:k]
result = []
for val in a:
if val in vals:
result.append(val)
vals.remove(val)
if len(vals) == 0:
break
print(result[pos - 1])
|
1227_D1. Optimal Subsequences (Easy Version)
|
This is the easier version of the problem. In this version 1 β€ n, m β€ 100. You can hack this problem only if you solve and lock both problems.
You are given a sequence of integers a=[a_1,a_2,...,a_n] of length n. Its subsequence is obtained by removing zero or more elements from the sequence a (they do not necessarily go consecutively). For example, for the sequence a=[11,20,11,33,11,20,11]:
* [11,20,11,33,11,20,11], [11,20,11,33,11,20], [11,11,11,11], [20], [33,20] are subsequences (these are just some of the long list);
* [40], [33,33], [33,20,20], [20,20,11,11] are not subsequences.
Suppose that an additional non-negative integer k (1 β€ k β€ n) is given, then the subsequence is called optimal if:
* it has a length of k and the sum of its elements is the maximum possible among all subsequences of length k;
* and among all subsequences of length k that satisfy the previous item, it is lexicographically minimal.
Recall that the sequence b=[b_1, b_2, ..., b_k] is lexicographically smaller than the sequence c=[c_1, c_2, ..., c_k] if the first element (from the left) in which they differ less in the sequence b than in c. Formally: there exists t (1 β€ t β€ k) such that b_1=c_1, b_2=c_2, ..., b_{t-1}=c_{t-1} and at the same time b_t<c_t. For example:
* [10, 20, 20] lexicographically less than [10, 21, 1],
* [7, 99, 99] is lexicographically less than [10, 21, 1],
* [10, 21, 0] is lexicographically less than [10, 21, 1].
You are given a sequence of a=[a_1,a_2,...,a_n] and m requests, each consisting of two numbers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j). For each query, print the value that is in the index pos_j of the optimal subsequence of the given sequence a for k=k_j.
For example, if n=4, a=[10,20,30,20], k_j=2, then the optimal subsequence is [20,30] β it is the minimum lexicographically among all subsequences of length 2 with the maximum total sum of items. Thus, the answer to the request k_j=2, pos_j=1 is the number 20, and the answer to the request k_j=2, pos_j=2 is the number 30.
Input
The first line contains an integer n (1 β€ n β€ 100) β the length of the sequence a.
The second line contains elements of the sequence a: integer numbers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9).
The third line contains an integer m (1 β€ m β€ 100) β the number of requests.
The following m lines contain pairs of integers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j) β the requests.
Output
Print m integers r_1, r_2, ..., r_m (1 β€ r_j β€ 10^9) one per line: answers to the requests in the order they appear in the input. The value of r_j should be equal to the value contained in the position pos_j of the optimal subsequence for k=k_j.
Examples
Input
3
10 20 10
6
1 1
2 1
2 2
3 1
3 2
3 3
Output
20
10
20
10
20
10
Input
7
1 2 1 3 1 2 1
9
2 1
2 2
3 1
3 2
3 3
1 1
7 1
7 7
7 4
Output
2
3
2
3
2
3
1
1
3
Note
In the first example, for a=[10,20,10] the optimal subsequences are:
* for k=1: [20],
* for k=2: [10,20],
* for k=3: [10,20,10].
|
{
"input": [
"3\n10 20 10\n6\n1 1\n2 1\n2 2\n3 1\n3 2\n3 3\n",
"7\n1 2 1 3 1 2 1\n9\n2 1\n2 2\n3 1\n3 2\n3 3\n1 1\n7 1\n7 7\n7 4\n"
],
"output": [
"20\n10\n20\n10\n20\n10\n",
"2\n3\n2\n3\n2\n3\n1\n1\n3\n"
]
}
|
{
"input": [
"2\n1 10\n3\n2 2\n2 1\n1 1\n",
"2\n3922 3922\n3\n2 2\n2 1\n1 1\n",
"1\n1000000000\n1\n1 1\n",
"1\n1\n3\n1 1\n1 1\n1 1\n",
"5\n3 1 4 1 2\n15\n5 5\n5 4\n5 3\n5 2\n5 1\n4 4\n4 3\n4 2\n4 1\n3 3\n3 2\n3 1\n2 2\n2 1\n1 1\n",
"2\n392222 322\n3\n2 2\n2 1\n1 1\n"
],
"output": [
"10\n1\n10\n",
"3922\n3922\n3922\n",
"1000000000\n",
"1\n1\n1\n",
"2\n1\n4\n1\n3\n2\n4\n1\n3\n2\n4\n3\n4\n3\n4\n",
"322\n392222\n392222\n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
int main() {
long long int n, m, x, i, ma = 0;
cin >> n;
vector<pair<long long int, long long int>> v1;
for (i = 1; i <= n; i++) cin >> x, v1.push_back(make_pair(-x, i));
sort(v1.begin(), v1.end());
cin >> m;
while (m--) {
long long int k, pos, j = 0;
cin >> k >> pos;
vector<pair<long long int, long long int>> v;
while (j < k) {
v.push_back(make_pair(v1[j].second, -v1[j].first));
j++;
}
sort(v.begin(), v.end());
cout << v[pos - 1].second << endl;
}
return 0;
}
|
1227_D1. Optimal Subsequences (Easy Version)
|
This is the easier version of the problem. In this version 1 β€ n, m β€ 100. You can hack this problem only if you solve and lock both problems.
You are given a sequence of integers a=[a_1,a_2,...,a_n] of length n. Its subsequence is obtained by removing zero or more elements from the sequence a (they do not necessarily go consecutively). For example, for the sequence a=[11,20,11,33,11,20,11]:
* [11,20,11,33,11,20,11], [11,20,11,33,11,20], [11,11,11,11], [20], [33,20] are subsequences (these are just some of the long list);
* [40], [33,33], [33,20,20], [20,20,11,11] are not subsequences.
Suppose that an additional non-negative integer k (1 β€ k β€ n) is given, then the subsequence is called optimal if:
* it has a length of k and the sum of its elements is the maximum possible among all subsequences of length k;
* and among all subsequences of length k that satisfy the previous item, it is lexicographically minimal.
Recall that the sequence b=[b_1, b_2, ..., b_k] is lexicographically smaller than the sequence c=[c_1, c_2, ..., c_k] if the first element (from the left) in which they differ less in the sequence b than in c. Formally: there exists t (1 β€ t β€ k) such that b_1=c_1, b_2=c_2, ..., b_{t-1}=c_{t-1} and at the same time b_t<c_t. For example:
* [10, 20, 20] lexicographically less than [10, 21, 1],
* [7, 99, 99] is lexicographically less than [10, 21, 1],
* [10, 21, 0] is lexicographically less than [10, 21, 1].
You are given a sequence of a=[a_1,a_2,...,a_n] and m requests, each consisting of two numbers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j). For each query, print the value that is in the index pos_j of the optimal subsequence of the given sequence a for k=k_j.
For example, if n=4, a=[10,20,30,20], k_j=2, then the optimal subsequence is [20,30] β it is the minimum lexicographically among all subsequences of length 2 with the maximum total sum of items. Thus, the answer to the request k_j=2, pos_j=1 is the number 20, and the answer to the request k_j=2, pos_j=2 is the number 30.
Input
The first line contains an integer n (1 β€ n β€ 100) β the length of the sequence a.
The second line contains elements of the sequence a: integer numbers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9).
The third line contains an integer m (1 β€ m β€ 100) β the number of requests.
The following m lines contain pairs of integers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j) β the requests.
Output
Print m integers r_1, r_2, ..., r_m (1 β€ r_j β€ 10^9) one per line: answers to the requests in the order they appear in the input. The value of r_j should be equal to the value contained in the position pos_j of the optimal subsequence for k=k_j.
Examples
Input
3
10 20 10
6
1 1
2 1
2 2
3 1
3 2
3 3
Output
20
10
20
10
20
10
Input
7
1 2 1 3 1 2 1
9
2 1
2 2
3 1
3 2
3 3
1 1
7 1
7 7
7 4
Output
2
3
2
3
2
3
1
1
3
Note
In the first example, for a=[10,20,10] the optimal subsequences are:
* for k=1: [20],
* for k=2: [10,20],
* for k=3: [10,20,10].
|
{
"input": [
"3\n10 20 10\n6\n1 1\n2 1\n2 2\n3 1\n3 2\n3 3\n",
"7\n1 2 1 3 1 2 1\n9\n2 1\n2 2\n3 1\n3 2\n3 3\n1 1\n7 1\n7 7\n7 4\n"
],
"output": [
"20\n10\n20\n10\n20\n10\n",
"2\n3\n2\n3\n2\n3\n1\n1\n3\n"
]
}
|
{
"input": [
"2\n1 10\n3\n2 2\n2 1\n1 1\n",
"2\n3922 3922\n3\n2 2\n2 1\n1 1\n",
"1\n1000000000\n1\n1 1\n",
"1\n1\n3\n1 1\n1 1\n1 1\n",
"5\n3 1 4 1 2\n15\n5 5\n5 4\n5 3\n5 2\n5 1\n4 4\n4 3\n4 2\n4 1\n3 3\n3 2\n3 1\n2 2\n2 1\n1 1\n",
"2\n392222 322\n3\n2 2\n2 1\n1 1\n"
],
"output": [
"10\n1\n10\n",
"3922\n3922\n3922\n",
"1000000000\n",
"1\n1\n1\n",
"2\n1\n4\n1\n3\n2\n4\n1\n3\n2\n4\n3\n4\n3\n4\n",
"322\n392222\n392222\n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
const int inf = 1e9 + 7;
bool compA(pair<int, int> a, pair<int, int> b) {
if (a.first == b.first) return a.second < b.second;
return a.first > b.first;
}
bool comp(pair<int, int> a, pair<int, int> b) { return a.second < b.second; }
void run() {
int n, m, k, pos;
cin >> n;
vector<pair<int, int> > A(n);
for (int i = 0; i < n; ++i) {
cin >> A[i].first;
A[i].second = i;
}
cin >> m;
sort(A.begin(), A.end(), compA);
vector<vector<pair<int, int> > > B(n);
for (int i = 0; i < n; ++i) {
for (int j = 0; j < i + 1; ++j) B[i].push_back(A[j]);
sort(B[i].begin(), B[i].end(), comp);
}
for (int i = 0; i < m; ++i) {
cin >> k >> pos;
cout << B[--k][--pos].first << '\n';
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t = 1;
for (int i = 0; i < t; ++i) {
run();
}
return 0;
}
|
1227_D1. Optimal Subsequences (Easy Version)
|
This is the easier version of the problem. In this version 1 β€ n, m β€ 100. You can hack this problem only if you solve and lock both problems.
You are given a sequence of integers a=[a_1,a_2,...,a_n] of length n. Its subsequence is obtained by removing zero or more elements from the sequence a (they do not necessarily go consecutively). For example, for the sequence a=[11,20,11,33,11,20,11]:
* [11,20,11,33,11,20,11], [11,20,11,33,11,20], [11,11,11,11], [20], [33,20] are subsequences (these are just some of the long list);
* [40], [33,33], [33,20,20], [20,20,11,11] are not subsequences.
Suppose that an additional non-negative integer k (1 β€ k β€ n) is given, then the subsequence is called optimal if:
* it has a length of k and the sum of its elements is the maximum possible among all subsequences of length k;
* and among all subsequences of length k that satisfy the previous item, it is lexicographically minimal.
Recall that the sequence b=[b_1, b_2, ..., b_k] is lexicographically smaller than the sequence c=[c_1, c_2, ..., c_k] if the first element (from the left) in which they differ less in the sequence b than in c. Formally: there exists t (1 β€ t β€ k) such that b_1=c_1, b_2=c_2, ..., b_{t-1}=c_{t-1} and at the same time b_t<c_t. For example:
* [10, 20, 20] lexicographically less than [10, 21, 1],
* [7, 99, 99] is lexicographically less than [10, 21, 1],
* [10, 21, 0] is lexicographically less than [10, 21, 1].
You are given a sequence of a=[a_1,a_2,...,a_n] and m requests, each consisting of two numbers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j). For each query, print the value that is in the index pos_j of the optimal subsequence of the given sequence a for k=k_j.
For example, if n=4, a=[10,20,30,20], k_j=2, then the optimal subsequence is [20,30] β it is the minimum lexicographically among all subsequences of length 2 with the maximum total sum of items. Thus, the answer to the request k_j=2, pos_j=1 is the number 20, and the answer to the request k_j=2, pos_j=2 is the number 30.
Input
The first line contains an integer n (1 β€ n β€ 100) β the length of the sequence a.
The second line contains elements of the sequence a: integer numbers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9).
The third line contains an integer m (1 β€ m β€ 100) β the number of requests.
The following m lines contain pairs of integers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j) β the requests.
Output
Print m integers r_1, r_2, ..., r_m (1 β€ r_j β€ 10^9) one per line: answers to the requests in the order they appear in the input. The value of r_j should be equal to the value contained in the position pos_j of the optimal subsequence for k=k_j.
Examples
Input
3
10 20 10
6
1 1
2 1
2 2
3 1
3 2
3 3
Output
20
10
20
10
20
10
Input
7
1 2 1 3 1 2 1
9
2 1
2 2
3 1
3 2
3 3
1 1
7 1
7 7
7 4
Output
2
3
2
3
2
3
1
1
3
Note
In the first example, for a=[10,20,10] the optimal subsequences are:
* for k=1: [20],
* for k=2: [10,20],
* for k=3: [10,20,10].
|
{
"input": [
"3\n10 20 10\n6\n1 1\n2 1\n2 2\n3 1\n3 2\n3 3\n",
"7\n1 2 1 3 1 2 1\n9\n2 1\n2 2\n3 1\n3 2\n3 3\n1 1\n7 1\n7 7\n7 4\n"
],
"output": [
"20\n10\n20\n10\n20\n10\n",
"2\n3\n2\n3\n2\n3\n1\n1\n3\n"
]
}
|
{
"input": [
"2\n1 10\n3\n2 2\n2 1\n1 1\n",
"2\n3922 3922\n3\n2 2\n2 1\n1 1\n",
"1\n1000000000\n1\n1 1\n",
"1\n1\n3\n1 1\n1 1\n1 1\n",
"5\n3 1 4 1 2\n15\n5 5\n5 4\n5 3\n5 2\n5 1\n4 4\n4 3\n4 2\n4 1\n3 3\n3 2\n3 1\n2 2\n2 1\n1 1\n",
"2\n392222 322\n3\n2 2\n2 1\n1 1\n"
],
"output": [
"10\n1\n10\n",
"3922\n3922\n3922\n",
"1000000000\n",
"1\n1\n1\n",
"2\n1\n4\n1\n3\n2\n4\n1\n3\n2\n4\n3\n4\n3\n4\n",
"322\n392222\n392222\n"
]
}
|
CORRECT
|
java
|
import java.io.*;
import java.util.*;
public class D {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
PrintWriter pw = new PrintWriter(System.out);
int n = sc.nextInt();
int[] arr = new int[n];
PriorityQueue<Pair> p = new PriorityQueue<>();
HashMap<Integer,Pair> pp = new HashMap<>();
for(int i=0;i<n;i++){
Pair tmp = new Pair(sc.nextInt(),i+1);
p.add(tmp);
pp.put(i+1,tmp);
}
PriorityQueue<Pair2> queries = new PriorityQueue<>();
int m = sc.nextInt();
Pair2[] q = new Pair2[m];
HashMap<Pair2,Integer> reshash = new HashMap<>();
for(int i=0;i<m;i++){
int k = sc.nextInt();
int pos = sc.nextInt();
Pair2 tmp = new Pair2(pos,k);
queries.add(tmp);
q[i] = tmp;
}
FenwickTree ft = new FenwickTree(200002);
int c = 0;
while (!queries.isEmpty()){
Pair2 t = queries.poll();
while (c<t.y){
c++;
Pair tmp = p.poll();
ft.point_update(tmp.y,1);
}
reshash.put(t,pp.get(ft.findKthmin(t.x)).x);
}
for(int i=0;i<q.length;i++){
pw.println(reshash.get(q[i]) + " ");
}
pw.println();
pw.flush();
pw.close();
}
static class FenwickTree { // one-based DS
int n;
int[] ft;
FenwickTree(int size) { n = size; ft = new int[n+1]; }
int rsq(int b) //O(log n)
{
int sum = 0;
while(b > 0) { sum += ft[b]; b -= b & -b;} //min?
return sum;
}
int rsq(int a, int b) { return rsq(b) - rsq(a-1); }
void point_update(int k, int val) //O(log n), update = increment
{
while(k <= n) { ft[k] += val; k += k & -k; } //min?
}
int get(int v){
int sum =0;
int id=0;
for (int i =22;i>=0;i--){
if (id+(1<<i)>n)
continue;
if (sum+ft[id+(1<<i)]<v){
id+=(1<<i);
sum+=ft[id];
}
}
return id+1;
}
public int findKthmin(int k){
int a = 1;
int b = n;
int mid = 0;
int res= 1;
while (a<=b){
mid = (a+b)/2;
if(this.rsq(mid)>=k){
res =mid;
b = mid-1;
}else{
a = mid+1;
}
}
return res;
}
}
static class Pair implements Comparable<Pair>{
int x;
int y;
public Pair(int x,int y){
this.x= x;
this.y = y;
}
public int compareTo(Pair p){
if(this.x==p.x)
return Long.compare(this.y,p.y);
return -Long.compare(this.x,p.x);
}
public String toString(){
return x+ " " + y;
}
}
static class Pair2 implements Comparable<Pair2>{
int x;
int y;
int index;
public Pair2(int x,int y){
this.x= x;
this.y = y;
this.index = index;
}
public int compareTo(Pair2 p){
if(this.y==p.y)
return Long.compare(p.x,this.x);
return Long.compare(this.y,p.y);
}
}
static class Scanner {
StringTokenizer st;
BufferedReader br;
public Scanner(FileReader r) {
br = new BufferedReader(r);
}
public Scanner(InputStream s) {
br = new BufferedReader(new InputStreamReader(s));
}
public String next() throws IOException {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public long nextLong() throws IOException {
return Long.parseLong(next());
}
public String nextLine() throws IOException {
return br.readLine();
}
public double nextDouble() throws IOException {
String x = next();
StringBuilder sb = new StringBuilder("0");
double res = 0, f = 1;
boolean dec = false, neg = false;
int start = 0;
if (x.charAt(0) == '-') {
neg = true;
start++;
}
for (int i = start; i < x.length(); i++)
if (x.charAt(i) == '.') {
res = Long.parseLong(sb.toString());
sb = new StringBuilder("0");
dec = true;
} else {
sb.append(x.charAt(i));
if (dec)
f *= 10;
}
res += Long.parseLong(sb.toString()) / f;
return res * (neg ? -1 : 1);
}
public boolean ready() throws IOException {
return br.ready();
}
}
}
|
1227_D1. Optimal Subsequences (Easy Version)
|
This is the easier version of the problem. In this version 1 β€ n, m β€ 100. You can hack this problem only if you solve and lock both problems.
You are given a sequence of integers a=[a_1,a_2,...,a_n] of length n. Its subsequence is obtained by removing zero or more elements from the sequence a (they do not necessarily go consecutively). For example, for the sequence a=[11,20,11,33,11,20,11]:
* [11,20,11,33,11,20,11], [11,20,11,33,11,20], [11,11,11,11], [20], [33,20] are subsequences (these are just some of the long list);
* [40], [33,33], [33,20,20], [20,20,11,11] are not subsequences.
Suppose that an additional non-negative integer k (1 β€ k β€ n) is given, then the subsequence is called optimal if:
* it has a length of k and the sum of its elements is the maximum possible among all subsequences of length k;
* and among all subsequences of length k that satisfy the previous item, it is lexicographically minimal.
Recall that the sequence b=[b_1, b_2, ..., b_k] is lexicographically smaller than the sequence c=[c_1, c_2, ..., c_k] if the first element (from the left) in which they differ less in the sequence b than in c. Formally: there exists t (1 β€ t β€ k) such that b_1=c_1, b_2=c_2, ..., b_{t-1}=c_{t-1} and at the same time b_t<c_t. For example:
* [10, 20, 20] lexicographically less than [10, 21, 1],
* [7, 99, 99] is lexicographically less than [10, 21, 1],
* [10, 21, 0] is lexicographically less than [10, 21, 1].
You are given a sequence of a=[a_1,a_2,...,a_n] and m requests, each consisting of two numbers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j). For each query, print the value that is in the index pos_j of the optimal subsequence of the given sequence a for k=k_j.
For example, if n=4, a=[10,20,30,20], k_j=2, then the optimal subsequence is [20,30] β it is the minimum lexicographically among all subsequences of length 2 with the maximum total sum of items. Thus, the answer to the request k_j=2, pos_j=1 is the number 20, and the answer to the request k_j=2, pos_j=2 is the number 30.
Input
The first line contains an integer n (1 β€ n β€ 100) β the length of the sequence a.
The second line contains elements of the sequence a: integer numbers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9).
The third line contains an integer m (1 β€ m β€ 100) β the number of requests.
The following m lines contain pairs of integers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j) β the requests.
Output
Print m integers r_1, r_2, ..., r_m (1 β€ r_j β€ 10^9) one per line: answers to the requests in the order they appear in the input. The value of r_j should be equal to the value contained in the position pos_j of the optimal subsequence for k=k_j.
Examples
Input
3
10 20 10
6
1 1
2 1
2 2
3 1
3 2
3 3
Output
20
10
20
10
20
10
Input
7
1 2 1 3 1 2 1
9
2 1
2 2
3 1
3 2
3 3
1 1
7 1
7 7
7 4
Output
2
3
2
3
2
3
1
1
3
Note
In the first example, for a=[10,20,10] the optimal subsequences are:
* for k=1: [20],
* for k=2: [10,20],
* for k=3: [10,20,10].
|
{
"input": [
"3\n10 20 10\n6\n1 1\n2 1\n2 2\n3 1\n3 2\n3 3\n",
"7\n1 2 1 3 1 2 1\n9\n2 1\n2 2\n3 1\n3 2\n3 3\n1 1\n7 1\n7 7\n7 4\n"
],
"output": [
"20\n10\n20\n10\n20\n10\n",
"2\n3\n2\n3\n2\n3\n1\n1\n3\n"
]
}
|
{
"input": [
"2\n1 10\n3\n2 2\n2 1\n1 1\n",
"2\n3922 3922\n3\n2 2\n2 1\n1 1\n",
"1\n1000000000\n1\n1 1\n",
"1\n1\n3\n1 1\n1 1\n1 1\n",
"5\n3 1 4 1 2\n15\n5 5\n5 4\n5 3\n5 2\n5 1\n4 4\n4 3\n4 2\n4 1\n3 3\n3 2\n3 1\n2 2\n2 1\n1 1\n",
"2\n392222 322\n3\n2 2\n2 1\n1 1\n"
],
"output": [
"10\n1\n10\n",
"3922\n3922\n3922\n",
"1000000000\n",
"1\n1\n1\n",
"2\n1\n4\n1\n3\n2\n4\n1\n3\n2\n4\n3\n4\n3\n4\n",
"322\n392222\n392222\n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
using namespace std;
const long long MOD = 1e9 + 7;
const long long mxN = 2e5 + 3;
bool cmp(pair<long long, long long> p1, pair<long long, long long> p2) {
if (p1.first > p2.first)
return true;
else if (p1.first == p2.first) {
if (p1.second < p2.second) return true;
return false;
}
return false;
}
void solve() {
long long n;
cin >> n;
long long a[n + 1];
pair<long long, long long> b[n];
for (long long i = 0; i < n; ++i) {
cin >> a[i + 1];
b[i].first = a[i + 1];
b[i].second = (i + 1);
}
sort(b, b + n, cmp);
long long m;
cin >> m;
long long kj, pos_j;
vector<vector<long long> > ve(n);
for (long long i = 0; i < n; ++i) {
for (long long j = i; j < n; ++j) {
ve[j].push_back(b[i].second);
}
}
for (long long i = 0; i < n; ++i) sort((ve[i]).begin(), (ve[i]).end());
while (m--) {
cin >> kj >> pos_j;
kj--;
(pos_j)--;
cout << a[ve[kj][pos_j]] << "\n";
}
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long nT = 1;
for (long long i = (long long)1; i <= (long long)nT; ++i) solve();
return 0;
}
|
1227_D1. Optimal Subsequences (Easy Version)
|
This is the easier version of the problem. In this version 1 β€ n, m β€ 100. You can hack this problem only if you solve and lock both problems.
You are given a sequence of integers a=[a_1,a_2,...,a_n] of length n. Its subsequence is obtained by removing zero or more elements from the sequence a (they do not necessarily go consecutively). For example, for the sequence a=[11,20,11,33,11,20,11]:
* [11,20,11,33,11,20,11], [11,20,11,33,11,20], [11,11,11,11], [20], [33,20] are subsequences (these are just some of the long list);
* [40], [33,33], [33,20,20], [20,20,11,11] are not subsequences.
Suppose that an additional non-negative integer k (1 β€ k β€ n) is given, then the subsequence is called optimal if:
* it has a length of k and the sum of its elements is the maximum possible among all subsequences of length k;
* and among all subsequences of length k that satisfy the previous item, it is lexicographically minimal.
Recall that the sequence b=[b_1, b_2, ..., b_k] is lexicographically smaller than the sequence c=[c_1, c_2, ..., c_k] if the first element (from the left) in which they differ less in the sequence b than in c. Formally: there exists t (1 β€ t β€ k) such that b_1=c_1, b_2=c_2, ..., b_{t-1}=c_{t-1} and at the same time b_t<c_t. For example:
* [10, 20, 20] lexicographically less than [10, 21, 1],
* [7, 99, 99] is lexicographically less than [10, 21, 1],
* [10, 21, 0] is lexicographically less than [10, 21, 1].
You are given a sequence of a=[a_1,a_2,...,a_n] and m requests, each consisting of two numbers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j). For each query, print the value that is in the index pos_j of the optimal subsequence of the given sequence a for k=k_j.
For example, if n=4, a=[10,20,30,20], k_j=2, then the optimal subsequence is [20,30] β it is the minimum lexicographically among all subsequences of length 2 with the maximum total sum of items. Thus, the answer to the request k_j=2, pos_j=1 is the number 20, and the answer to the request k_j=2, pos_j=2 is the number 30.
Input
The first line contains an integer n (1 β€ n β€ 100) β the length of the sequence a.
The second line contains elements of the sequence a: integer numbers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9).
The third line contains an integer m (1 β€ m β€ 100) β the number of requests.
The following m lines contain pairs of integers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j) β the requests.
Output
Print m integers r_1, r_2, ..., r_m (1 β€ r_j β€ 10^9) one per line: answers to the requests in the order they appear in the input. The value of r_j should be equal to the value contained in the position pos_j of the optimal subsequence for k=k_j.
Examples
Input
3
10 20 10
6
1 1
2 1
2 2
3 1
3 2
3 3
Output
20
10
20
10
20
10
Input
7
1 2 1 3 1 2 1
9
2 1
2 2
3 1
3 2
3 3
1 1
7 1
7 7
7 4
Output
2
3
2
3
2
3
1
1
3
Note
In the first example, for a=[10,20,10] the optimal subsequences are:
* for k=1: [20],
* for k=2: [10,20],
* for k=3: [10,20,10].
|
{
"input": [
"3\n10 20 10\n6\n1 1\n2 1\n2 2\n3 1\n3 2\n3 3\n",
"7\n1 2 1 3 1 2 1\n9\n2 1\n2 2\n3 1\n3 2\n3 3\n1 1\n7 1\n7 7\n7 4\n"
],
"output": [
"20\n10\n20\n10\n20\n10\n",
"2\n3\n2\n3\n2\n3\n1\n1\n3\n"
]
}
|
{
"input": [
"2\n1 10\n3\n2 2\n2 1\n1 1\n",
"2\n3922 3922\n3\n2 2\n2 1\n1 1\n",
"1\n1000000000\n1\n1 1\n",
"1\n1\n3\n1 1\n1 1\n1 1\n",
"5\n3 1 4 1 2\n15\n5 5\n5 4\n5 3\n5 2\n5 1\n4 4\n4 3\n4 2\n4 1\n3 3\n3 2\n3 1\n2 2\n2 1\n1 1\n",
"2\n392222 322\n3\n2 2\n2 1\n1 1\n"
],
"output": [
"10\n1\n10\n",
"3922\n3922\n3922\n",
"1000000000\n",
"1\n1\n1\n",
"2\n1\n4\n1\n3\n2\n4\n1\n3\n2\n4\n3\n4\n3\n4\n",
"322\n392222\n392222\n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 100;
struct node {
int id;
int num;
};
node s[maxn];
int b[maxn];
int num[maxn];
bool cmp(node a, node b) {
if (a.num != b.num)
return a.num > b.num;
else
return a.id < b.id;
}
struct ask {
int id;
int L = 0;
int pos;
};
ask A[maxn];
bool cmp2(ask a, ask b) { return a.L < b.L; }
int ans[maxn];
int main() {
ios::sync_with_stdio(false);
int N;
cin >> N;
for (int i = 1; i <= N; i++) cin >> num[i];
for (int i = 1; i <= N; i++) s[i].num = num[i], s[i].id = i;
sort(s + 1, s + 1 + N, cmp);
for (int i = 1; i <= N; i++) b[i] = s[i].id;
int Q;
cin >> Q;
for (int i = 1; i <= Q; i++) {
cin >> A[i].L;
A[i].id = i;
cin >> A[i].pos;
}
sort(A + 1, A + 1 + Q, cmp2);
for (int i = 1; i <= Q; i++) {
if (A[i].L != A[i - 1].L) sort(b + 1, b + 1 + A[i].L);
ans[A[i].id] = b[A[i].pos];
}
for (int i = 1; i <= Q; i++) cout << num[ans[i]] << endl;
return 0;
}
|
1227_D1. Optimal Subsequences (Easy Version)
|
This is the easier version of the problem. In this version 1 β€ n, m β€ 100. You can hack this problem only if you solve and lock both problems.
You are given a sequence of integers a=[a_1,a_2,...,a_n] of length n. Its subsequence is obtained by removing zero or more elements from the sequence a (they do not necessarily go consecutively). For example, for the sequence a=[11,20,11,33,11,20,11]:
* [11,20,11,33,11,20,11], [11,20,11,33,11,20], [11,11,11,11], [20], [33,20] are subsequences (these are just some of the long list);
* [40], [33,33], [33,20,20], [20,20,11,11] are not subsequences.
Suppose that an additional non-negative integer k (1 β€ k β€ n) is given, then the subsequence is called optimal if:
* it has a length of k and the sum of its elements is the maximum possible among all subsequences of length k;
* and among all subsequences of length k that satisfy the previous item, it is lexicographically minimal.
Recall that the sequence b=[b_1, b_2, ..., b_k] is lexicographically smaller than the sequence c=[c_1, c_2, ..., c_k] if the first element (from the left) in which they differ less in the sequence b than in c. Formally: there exists t (1 β€ t β€ k) such that b_1=c_1, b_2=c_2, ..., b_{t-1}=c_{t-1} and at the same time b_t<c_t. For example:
* [10, 20, 20] lexicographically less than [10, 21, 1],
* [7, 99, 99] is lexicographically less than [10, 21, 1],
* [10, 21, 0] is lexicographically less than [10, 21, 1].
You are given a sequence of a=[a_1,a_2,...,a_n] and m requests, each consisting of two numbers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j). For each query, print the value that is in the index pos_j of the optimal subsequence of the given sequence a for k=k_j.
For example, if n=4, a=[10,20,30,20], k_j=2, then the optimal subsequence is [20,30] β it is the minimum lexicographically among all subsequences of length 2 with the maximum total sum of items. Thus, the answer to the request k_j=2, pos_j=1 is the number 20, and the answer to the request k_j=2, pos_j=2 is the number 30.
Input
The first line contains an integer n (1 β€ n β€ 100) β the length of the sequence a.
The second line contains elements of the sequence a: integer numbers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9).
The third line contains an integer m (1 β€ m β€ 100) β the number of requests.
The following m lines contain pairs of integers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j) β the requests.
Output
Print m integers r_1, r_2, ..., r_m (1 β€ r_j β€ 10^9) one per line: answers to the requests in the order they appear in the input. The value of r_j should be equal to the value contained in the position pos_j of the optimal subsequence for k=k_j.
Examples
Input
3
10 20 10
6
1 1
2 1
2 2
3 1
3 2
3 3
Output
20
10
20
10
20
10
Input
7
1 2 1 3 1 2 1
9
2 1
2 2
3 1
3 2
3 3
1 1
7 1
7 7
7 4
Output
2
3
2
3
2
3
1
1
3
Note
In the first example, for a=[10,20,10] the optimal subsequences are:
* for k=1: [20],
* for k=2: [10,20],
* for k=3: [10,20,10].
|
{
"input": [
"3\n10 20 10\n6\n1 1\n2 1\n2 2\n3 1\n3 2\n3 3\n",
"7\n1 2 1 3 1 2 1\n9\n2 1\n2 2\n3 1\n3 2\n3 3\n1 1\n7 1\n7 7\n7 4\n"
],
"output": [
"20\n10\n20\n10\n20\n10\n",
"2\n3\n2\n3\n2\n3\n1\n1\n3\n"
]
}
|
{
"input": [
"2\n1 10\n3\n2 2\n2 1\n1 1\n",
"2\n3922 3922\n3\n2 2\n2 1\n1 1\n",
"1\n1000000000\n1\n1 1\n",
"1\n1\n3\n1 1\n1 1\n1 1\n",
"5\n3 1 4 1 2\n15\n5 5\n5 4\n5 3\n5 2\n5 1\n4 4\n4 3\n4 2\n4 1\n3 3\n3 2\n3 1\n2 2\n2 1\n1 1\n",
"2\n392222 322\n3\n2 2\n2 1\n1 1\n"
],
"output": [
"10\n1\n10\n",
"3922\n3922\n3922\n",
"1000000000\n",
"1\n1\n1\n",
"2\n1\n4\n1\n3\n2\n4\n1\n3\n2\n4\n3\n4\n3\n4\n",
"322\n392222\n392222\n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
mt19937 rnd(time(0));
const long long INF = 1e9;
struct Point {
Point() {
cin >> x;
cin >> y;
}
Point(long double x, long double y) : x(x), y(y) {}
long double x, y;
};
void solve() {
int n, m, k, pos;
cin >> n;
vector<int> v(n), b(n);
for (int i = 0; i < n; i++) {
cin >> v[i];
b[i] = v[i];
}
sort(b.begin(), b.end());
cin >> m;
while (m-- > 0) {
cin >> k >> pos;
int needDEL = n - k;
map<int, int> mp;
for (int i = 0; i < needDEL; i++) {
mp[b[i]]++;
}
vector<bool> used(n, 0);
for (int i = n - 1; i >= 0 && needDEL != 0; i--) {
if (mp[v[i]] != 0) {
mp[v[i]]--;
used[i] = 1;
needDEL--;
}
}
vector<int> newV;
for (int i = 0; i < n; i++) {
if (!used[i]) {
newV.push_back(v[i]);
}
}
cout << newV[pos - 1] << '\n';
}
}
void jafdj(int aijf, int akfka) {
cout << "0" << '\n';
long double o = 1 * 10;
long long p = (int)o << 2;
return;
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t = 1;
while (t--) {
solve();
}
return 0;
}
|
1227_D1. Optimal Subsequences (Easy Version)
|
This is the easier version of the problem. In this version 1 β€ n, m β€ 100. You can hack this problem only if you solve and lock both problems.
You are given a sequence of integers a=[a_1,a_2,...,a_n] of length n. Its subsequence is obtained by removing zero or more elements from the sequence a (they do not necessarily go consecutively). For example, for the sequence a=[11,20,11,33,11,20,11]:
* [11,20,11,33,11,20,11], [11,20,11,33,11,20], [11,11,11,11], [20], [33,20] are subsequences (these are just some of the long list);
* [40], [33,33], [33,20,20], [20,20,11,11] are not subsequences.
Suppose that an additional non-negative integer k (1 β€ k β€ n) is given, then the subsequence is called optimal if:
* it has a length of k and the sum of its elements is the maximum possible among all subsequences of length k;
* and among all subsequences of length k that satisfy the previous item, it is lexicographically minimal.
Recall that the sequence b=[b_1, b_2, ..., b_k] is lexicographically smaller than the sequence c=[c_1, c_2, ..., c_k] if the first element (from the left) in which they differ less in the sequence b than in c. Formally: there exists t (1 β€ t β€ k) such that b_1=c_1, b_2=c_2, ..., b_{t-1}=c_{t-1} and at the same time b_t<c_t. For example:
* [10, 20, 20] lexicographically less than [10, 21, 1],
* [7, 99, 99] is lexicographically less than [10, 21, 1],
* [10, 21, 0] is lexicographically less than [10, 21, 1].
You are given a sequence of a=[a_1,a_2,...,a_n] and m requests, each consisting of two numbers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j). For each query, print the value that is in the index pos_j of the optimal subsequence of the given sequence a for k=k_j.
For example, if n=4, a=[10,20,30,20], k_j=2, then the optimal subsequence is [20,30] β it is the minimum lexicographically among all subsequences of length 2 with the maximum total sum of items. Thus, the answer to the request k_j=2, pos_j=1 is the number 20, and the answer to the request k_j=2, pos_j=2 is the number 30.
Input
The first line contains an integer n (1 β€ n β€ 100) β the length of the sequence a.
The second line contains elements of the sequence a: integer numbers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9).
The third line contains an integer m (1 β€ m β€ 100) β the number of requests.
The following m lines contain pairs of integers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j) β the requests.
Output
Print m integers r_1, r_2, ..., r_m (1 β€ r_j β€ 10^9) one per line: answers to the requests in the order they appear in the input. The value of r_j should be equal to the value contained in the position pos_j of the optimal subsequence for k=k_j.
Examples
Input
3
10 20 10
6
1 1
2 1
2 2
3 1
3 2
3 3
Output
20
10
20
10
20
10
Input
7
1 2 1 3 1 2 1
9
2 1
2 2
3 1
3 2
3 3
1 1
7 1
7 7
7 4
Output
2
3
2
3
2
3
1
1
3
Note
In the first example, for a=[10,20,10] the optimal subsequences are:
* for k=1: [20],
* for k=2: [10,20],
* for k=3: [10,20,10].
|
{
"input": [
"3\n10 20 10\n6\n1 1\n2 1\n2 2\n3 1\n3 2\n3 3\n",
"7\n1 2 1 3 1 2 1\n9\n2 1\n2 2\n3 1\n3 2\n3 3\n1 1\n7 1\n7 7\n7 4\n"
],
"output": [
"20\n10\n20\n10\n20\n10\n",
"2\n3\n2\n3\n2\n3\n1\n1\n3\n"
]
}
|
{
"input": [
"2\n1 10\n3\n2 2\n2 1\n1 1\n",
"2\n3922 3922\n3\n2 2\n2 1\n1 1\n",
"1\n1000000000\n1\n1 1\n",
"1\n1\n3\n1 1\n1 1\n1 1\n",
"5\n3 1 4 1 2\n15\n5 5\n5 4\n5 3\n5 2\n5 1\n4 4\n4 3\n4 2\n4 1\n3 3\n3 2\n3 1\n2 2\n2 1\n1 1\n",
"2\n392222 322\n3\n2 2\n2 1\n1 1\n"
],
"output": [
"10\n1\n10\n",
"3922\n3922\n3922\n",
"1000000000\n",
"1\n1\n1\n",
"2\n1\n4\n1\n3\n2\n4\n1\n3\n2\n4\n3\n4\n3\n4\n",
"322\n392222\n392222\n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
long long mod = 1000000007;
const long long num = 1000000000;
bool check1(pair<long long, long long> a, pair<long long, long long> b) {
return a.first == b.first ? (a.second > b.second) : (a.first < b.first);
}
bool check2(pair<long long, long long> a, pair<long long, long long> b) {
return a.second < b.second;
}
int main() {
long long q = 1;
while (q--) {
long long n, m;
cin >> n;
vector<pair<long long, long long> > vec(n);
for (int i = 0; i < n; i++) {
long long a;
cin >> a;
vec[i].first = a;
vec[i].second = i;
}
cin >> m;
sort(vec.begin(), vec.end(), check1);
auto v1 = vec;
for (int i = 0; i < m; i++) {
vec = v1;
long long k, p;
cin >> k >> p;
sort(vec.begin() + n - k, vec.end(), check2);
cout << vec[n - k + p - 1].first << endl;
;
}
}
return 0;
}
|
1227_D1. Optimal Subsequences (Easy Version)
|
This is the easier version of the problem. In this version 1 β€ n, m β€ 100. You can hack this problem only if you solve and lock both problems.
You are given a sequence of integers a=[a_1,a_2,...,a_n] of length n. Its subsequence is obtained by removing zero or more elements from the sequence a (they do not necessarily go consecutively). For example, for the sequence a=[11,20,11,33,11,20,11]:
* [11,20,11,33,11,20,11], [11,20,11,33,11,20], [11,11,11,11], [20], [33,20] are subsequences (these are just some of the long list);
* [40], [33,33], [33,20,20], [20,20,11,11] are not subsequences.
Suppose that an additional non-negative integer k (1 β€ k β€ n) is given, then the subsequence is called optimal if:
* it has a length of k and the sum of its elements is the maximum possible among all subsequences of length k;
* and among all subsequences of length k that satisfy the previous item, it is lexicographically minimal.
Recall that the sequence b=[b_1, b_2, ..., b_k] is lexicographically smaller than the sequence c=[c_1, c_2, ..., c_k] if the first element (from the left) in which they differ less in the sequence b than in c. Formally: there exists t (1 β€ t β€ k) such that b_1=c_1, b_2=c_2, ..., b_{t-1}=c_{t-1} and at the same time b_t<c_t. For example:
* [10, 20, 20] lexicographically less than [10, 21, 1],
* [7, 99, 99] is lexicographically less than [10, 21, 1],
* [10, 21, 0] is lexicographically less than [10, 21, 1].
You are given a sequence of a=[a_1,a_2,...,a_n] and m requests, each consisting of two numbers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j). For each query, print the value that is in the index pos_j of the optimal subsequence of the given sequence a for k=k_j.
For example, if n=4, a=[10,20,30,20], k_j=2, then the optimal subsequence is [20,30] β it is the minimum lexicographically among all subsequences of length 2 with the maximum total sum of items. Thus, the answer to the request k_j=2, pos_j=1 is the number 20, and the answer to the request k_j=2, pos_j=2 is the number 30.
Input
The first line contains an integer n (1 β€ n β€ 100) β the length of the sequence a.
The second line contains elements of the sequence a: integer numbers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9).
The third line contains an integer m (1 β€ m β€ 100) β the number of requests.
The following m lines contain pairs of integers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j) β the requests.
Output
Print m integers r_1, r_2, ..., r_m (1 β€ r_j β€ 10^9) one per line: answers to the requests in the order they appear in the input. The value of r_j should be equal to the value contained in the position pos_j of the optimal subsequence for k=k_j.
Examples
Input
3
10 20 10
6
1 1
2 1
2 2
3 1
3 2
3 3
Output
20
10
20
10
20
10
Input
7
1 2 1 3 1 2 1
9
2 1
2 2
3 1
3 2
3 3
1 1
7 1
7 7
7 4
Output
2
3
2
3
2
3
1
1
3
Note
In the first example, for a=[10,20,10] the optimal subsequences are:
* for k=1: [20],
* for k=2: [10,20],
* for k=3: [10,20,10].
|
{
"input": [
"3\n10 20 10\n6\n1 1\n2 1\n2 2\n3 1\n3 2\n3 3\n",
"7\n1 2 1 3 1 2 1\n9\n2 1\n2 2\n3 1\n3 2\n3 3\n1 1\n7 1\n7 7\n7 4\n"
],
"output": [
"20\n10\n20\n10\n20\n10\n",
"2\n3\n2\n3\n2\n3\n1\n1\n3\n"
]
}
|
{
"input": [
"2\n1 10\n3\n2 2\n2 1\n1 1\n",
"2\n3922 3922\n3\n2 2\n2 1\n1 1\n",
"1\n1000000000\n1\n1 1\n",
"1\n1\n3\n1 1\n1 1\n1 1\n",
"5\n3 1 4 1 2\n15\n5 5\n5 4\n5 3\n5 2\n5 1\n4 4\n4 3\n4 2\n4 1\n3 3\n3 2\n3 1\n2 2\n2 1\n1 1\n",
"2\n392222 322\n3\n2 2\n2 1\n1 1\n"
],
"output": [
"10\n1\n10\n",
"3922\n3922\n3922\n",
"1000000000\n",
"1\n1\n1\n",
"2\n1\n4\n1\n3\n2\n4\n1\n3\n2\n4\n3\n4\n3\n4\n",
"322\n392222\n392222\n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
long long int powMod(long long int x, long long int y) {
long long int p = 1;
while (y) {
if (y % 2) {
p = (p * x) % 1000000007;
}
y /= 2;
x = (x * x) % 1000000007;
}
return p;
}
long long int invMod(long long int x) { return powMod(x, 1000000007 - 2); }
long long int gcd(long long int a, long long int b) {
return b == 0 ? a : gcd(b, a % b);
}
bool sortbysec(const pair<long long int, long long int> &a,
const pair<long long int, long long int> &b) {
if (a.first == b.first) {
return (a.second < b.second);
}
return (a.first > b.first);
}
void solve() {
long long int n;
cin >> n;
vector<pair<long long int, long long int> > v;
long long int k[n];
for (long long int i = 0; i < n; i++) {
cin >> k[i];
v.push_back(make_pair(k[i], i));
}
sort(v.begin(), v.end(), sortbysec);
long long int m;
cin >> m;
for (long long int i = 0; i < m; i++) {
long long int a, b;
cin >> a >> b;
long long int ans[a];
for (long long int j = 0; j < a; j++) {
ans[j] = v[j].second;
}
sort(ans, ans + a);
cout << k[ans[b - 1]] << '\n';
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
solve();
return 0;
}
|
1227_D1. Optimal Subsequences (Easy Version)
|
This is the easier version of the problem. In this version 1 β€ n, m β€ 100. You can hack this problem only if you solve and lock both problems.
You are given a sequence of integers a=[a_1,a_2,...,a_n] of length n. Its subsequence is obtained by removing zero or more elements from the sequence a (they do not necessarily go consecutively). For example, for the sequence a=[11,20,11,33,11,20,11]:
* [11,20,11,33,11,20,11], [11,20,11,33,11,20], [11,11,11,11], [20], [33,20] are subsequences (these are just some of the long list);
* [40], [33,33], [33,20,20], [20,20,11,11] are not subsequences.
Suppose that an additional non-negative integer k (1 β€ k β€ n) is given, then the subsequence is called optimal if:
* it has a length of k and the sum of its elements is the maximum possible among all subsequences of length k;
* and among all subsequences of length k that satisfy the previous item, it is lexicographically minimal.
Recall that the sequence b=[b_1, b_2, ..., b_k] is lexicographically smaller than the sequence c=[c_1, c_2, ..., c_k] if the first element (from the left) in which they differ less in the sequence b than in c. Formally: there exists t (1 β€ t β€ k) such that b_1=c_1, b_2=c_2, ..., b_{t-1}=c_{t-1} and at the same time b_t<c_t. For example:
* [10, 20, 20] lexicographically less than [10, 21, 1],
* [7, 99, 99] is lexicographically less than [10, 21, 1],
* [10, 21, 0] is lexicographically less than [10, 21, 1].
You are given a sequence of a=[a_1,a_2,...,a_n] and m requests, each consisting of two numbers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j). For each query, print the value that is in the index pos_j of the optimal subsequence of the given sequence a for k=k_j.
For example, if n=4, a=[10,20,30,20], k_j=2, then the optimal subsequence is [20,30] β it is the minimum lexicographically among all subsequences of length 2 with the maximum total sum of items. Thus, the answer to the request k_j=2, pos_j=1 is the number 20, and the answer to the request k_j=2, pos_j=2 is the number 30.
Input
The first line contains an integer n (1 β€ n β€ 100) β the length of the sequence a.
The second line contains elements of the sequence a: integer numbers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9).
The third line contains an integer m (1 β€ m β€ 100) β the number of requests.
The following m lines contain pairs of integers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j) β the requests.
Output
Print m integers r_1, r_2, ..., r_m (1 β€ r_j β€ 10^9) one per line: answers to the requests in the order they appear in the input. The value of r_j should be equal to the value contained in the position pos_j of the optimal subsequence for k=k_j.
Examples
Input
3
10 20 10
6
1 1
2 1
2 2
3 1
3 2
3 3
Output
20
10
20
10
20
10
Input
7
1 2 1 3 1 2 1
9
2 1
2 2
3 1
3 2
3 3
1 1
7 1
7 7
7 4
Output
2
3
2
3
2
3
1
1
3
Note
In the first example, for a=[10,20,10] the optimal subsequences are:
* for k=1: [20],
* for k=2: [10,20],
* for k=3: [10,20,10].
|
{
"input": [
"3\n10 20 10\n6\n1 1\n2 1\n2 2\n3 1\n3 2\n3 3\n",
"7\n1 2 1 3 1 2 1\n9\n2 1\n2 2\n3 1\n3 2\n3 3\n1 1\n7 1\n7 7\n7 4\n"
],
"output": [
"20\n10\n20\n10\n20\n10\n",
"2\n3\n2\n3\n2\n3\n1\n1\n3\n"
]
}
|
{
"input": [
"2\n1 10\n3\n2 2\n2 1\n1 1\n",
"2\n3922 3922\n3\n2 2\n2 1\n1 1\n",
"1\n1000000000\n1\n1 1\n",
"1\n1\n3\n1 1\n1 1\n1 1\n",
"5\n3 1 4 1 2\n15\n5 5\n5 4\n5 3\n5 2\n5 1\n4 4\n4 3\n4 2\n4 1\n3 3\n3 2\n3 1\n2 2\n2 1\n1 1\n",
"2\n392222 322\n3\n2 2\n2 1\n1 1\n"
],
"output": [
"10\n1\n10\n",
"3922\n3922\n3922\n",
"1000000000\n",
"1\n1\n1\n",
"2\n1\n4\n1\n3\n2\n4\n1\n3\n2\n4\n3\n4\n3\n4\n",
"322\n392222\n392222\n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\'' << x << '\''; }
void __print(const char *x) { cerr << '\"' << x << '\"'; }
void __print(const string &x) { cerr << '\"' << x << '\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }
template <typename T, typename V>
void __print(const pair<T, V> &x) {
cerr << '{';
__print(x.first);
cerr << ',';
__print(x.second);
cerr << '}';
}
template <typename T>
void __print(const T &x) {
int f = 0;
cerr << '{';
for (auto &i : x) cerr << (f++ ? "," : ""), __print(i);
cerr << "}";
}
void _print() { cerr << "]\n"; }
template <typename T, typename... V>
void _print(T t, V... v) {
__print(t);
if (sizeof...(v)) cerr << ", ";
_print(v...);
}
const long double PI = acos(-1.0);
long long int power(long long int x, long long int y) {
long long int res = 1;
x = x;
while (y > 0) {
if (y & 1) res = (res * x);
y = y >> 1;
x = (x * x);
}
return res;
}
long long int power(long long int x, long long int y, long long int p) {
long long int res = 1;
x = x % p;
while (y > 0) {
if (y & 1) res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
long long int gcd(long long int a, long long int b) {
if (a == 0) return b;
return gcd(b % a, a);
}
bool isPalindrome(string s) {
string t = s;
reverse(t.begin(), t.end());
return s == t;
}
void solve() {
long long int n;
cin >> n;
long long int arr[n];
map<long long int, set<long long int> > mm;
vector<long long int> v;
for (int i = 0; i < n; i++) {
cin >> arr[i];
mm[arr[i]].insert(i);
v.push_back(arr[i]);
}
sort(v.begin(), v.end());
;
long long int q;
cin >> q;
while (q--) {
long long int x, y;
cin >> x >> y;
vector<pair<long long int, long long int> > vec;
map<long long int, long long int> temp;
long long int i = n - 1;
while (vec.size() != x) {
long long int key = v[i];
;
vec.push_back(make_pair(*mm[key].begin(), key));
mm[key].erase(*mm[key].begin());
i--;
}
sort(vec.begin(), vec.end());
;
for (auto it : vec) mm[it.second].insert(it.first);
cout << vec[y - 1].second << endl;
}
}
int main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
long long int t = 1;
while (t--) {
solve();
}
return 0;
}
|
1227_D1. Optimal Subsequences (Easy Version)
|
This is the easier version of the problem. In this version 1 β€ n, m β€ 100. You can hack this problem only if you solve and lock both problems.
You are given a sequence of integers a=[a_1,a_2,...,a_n] of length n. Its subsequence is obtained by removing zero or more elements from the sequence a (they do not necessarily go consecutively). For example, for the sequence a=[11,20,11,33,11,20,11]:
* [11,20,11,33,11,20,11], [11,20,11,33,11,20], [11,11,11,11], [20], [33,20] are subsequences (these are just some of the long list);
* [40], [33,33], [33,20,20], [20,20,11,11] are not subsequences.
Suppose that an additional non-negative integer k (1 β€ k β€ n) is given, then the subsequence is called optimal if:
* it has a length of k and the sum of its elements is the maximum possible among all subsequences of length k;
* and among all subsequences of length k that satisfy the previous item, it is lexicographically minimal.
Recall that the sequence b=[b_1, b_2, ..., b_k] is lexicographically smaller than the sequence c=[c_1, c_2, ..., c_k] if the first element (from the left) in which they differ less in the sequence b than in c. Formally: there exists t (1 β€ t β€ k) such that b_1=c_1, b_2=c_2, ..., b_{t-1}=c_{t-1} and at the same time b_t<c_t. For example:
* [10, 20, 20] lexicographically less than [10, 21, 1],
* [7, 99, 99] is lexicographically less than [10, 21, 1],
* [10, 21, 0] is lexicographically less than [10, 21, 1].
You are given a sequence of a=[a_1,a_2,...,a_n] and m requests, each consisting of two numbers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j). For each query, print the value that is in the index pos_j of the optimal subsequence of the given sequence a for k=k_j.
For example, if n=4, a=[10,20,30,20], k_j=2, then the optimal subsequence is [20,30] β it is the minimum lexicographically among all subsequences of length 2 with the maximum total sum of items. Thus, the answer to the request k_j=2, pos_j=1 is the number 20, and the answer to the request k_j=2, pos_j=2 is the number 30.
Input
The first line contains an integer n (1 β€ n β€ 100) β the length of the sequence a.
The second line contains elements of the sequence a: integer numbers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9).
The third line contains an integer m (1 β€ m β€ 100) β the number of requests.
The following m lines contain pairs of integers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j) β the requests.
Output
Print m integers r_1, r_2, ..., r_m (1 β€ r_j β€ 10^9) one per line: answers to the requests in the order they appear in the input. The value of r_j should be equal to the value contained in the position pos_j of the optimal subsequence for k=k_j.
Examples
Input
3
10 20 10
6
1 1
2 1
2 2
3 1
3 2
3 3
Output
20
10
20
10
20
10
Input
7
1 2 1 3 1 2 1
9
2 1
2 2
3 1
3 2
3 3
1 1
7 1
7 7
7 4
Output
2
3
2
3
2
3
1
1
3
Note
In the first example, for a=[10,20,10] the optimal subsequences are:
* for k=1: [20],
* for k=2: [10,20],
* for k=3: [10,20,10].
|
{
"input": [
"3\n10 20 10\n6\n1 1\n2 1\n2 2\n3 1\n3 2\n3 3\n",
"7\n1 2 1 3 1 2 1\n9\n2 1\n2 2\n3 1\n3 2\n3 3\n1 1\n7 1\n7 7\n7 4\n"
],
"output": [
"20\n10\n20\n10\n20\n10\n",
"2\n3\n2\n3\n2\n3\n1\n1\n3\n"
]
}
|
{
"input": [
"2\n1 10\n3\n2 2\n2 1\n1 1\n",
"2\n3922 3922\n3\n2 2\n2 1\n1 1\n",
"1\n1000000000\n1\n1 1\n",
"1\n1\n3\n1 1\n1 1\n1 1\n",
"5\n3 1 4 1 2\n15\n5 5\n5 4\n5 3\n5 2\n5 1\n4 4\n4 3\n4 2\n4 1\n3 3\n3 2\n3 1\n2 2\n2 1\n1 1\n",
"2\n392222 322\n3\n2 2\n2 1\n1 1\n"
],
"output": [
"10\n1\n10\n",
"3922\n3922\n3922\n",
"1000000000\n",
"1\n1\n1\n",
"2\n1\n4\n1\n3\n2\n4\n1\n3\n2\n4\n3\n4\n3\n4\n",
"322\n392222\n392222\n"
]
}
|
CORRECT
|
python3
|
n = int(input())
a = list(map(int,input().split()))
for i in range(n):
a[i] = (a[i],-i)
a.sort()
m = int(input())
for j in range(m):
t = list(map(int, input().split()))
k = t[0]
p = t[1]
print(sorted(a[-k:], key = lambda x: -x[1])[p-1][0])
|
1227_D1. Optimal Subsequences (Easy Version)
|
This is the easier version of the problem. In this version 1 β€ n, m β€ 100. You can hack this problem only if you solve and lock both problems.
You are given a sequence of integers a=[a_1,a_2,...,a_n] of length n. Its subsequence is obtained by removing zero or more elements from the sequence a (they do not necessarily go consecutively). For example, for the sequence a=[11,20,11,33,11,20,11]:
* [11,20,11,33,11,20,11], [11,20,11,33,11,20], [11,11,11,11], [20], [33,20] are subsequences (these are just some of the long list);
* [40], [33,33], [33,20,20], [20,20,11,11] are not subsequences.
Suppose that an additional non-negative integer k (1 β€ k β€ n) is given, then the subsequence is called optimal if:
* it has a length of k and the sum of its elements is the maximum possible among all subsequences of length k;
* and among all subsequences of length k that satisfy the previous item, it is lexicographically minimal.
Recall that the sequence b=[b_1, b_2, ..., b_k] is lexicographically smaller than the sequence c=[c_1, c_2, ..., c_k] if the first element (from the left) in which they differ less in the sequence b than in c. Formally: there exists t (1 β€ t β€ k) such that b_1=c_1, b_2=c_2, ..., b_{t-1}=c_{t-1} and at the same time b_t<c_t. For example:
* [10, 20, 20] lexicographically less than [10, 21, 1],
* [7, 99, 99] is lexicographically less than [10, 21, 1],
* [10, 21, 0] is lexicographically less than [10, 21, 1].
You are given a sequence of a=[a_1,a_2,...,a_n] and m requests, each consisting of two numbers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j). For each query, print the value that is in the index pos_j of the optimal subsequence of the given sequence a for k=k_j.
For example, if n=4, a=[10,20,30,20], k_j=2, then the optimal subsequence is [20,30] β it is the minimum lexicographically among all subsequences of length 2 with the maximum total sum of items. Thus, the answer to the request k_j=2, pos_j=1 is the number 20, and the answer to the request k_j=2, pos_j=2 is the number 30.
Input
The first line contains an integer n (1 β€ n β€ 100) β the length of the sequence a.
The second line contains elements of the sequence a: integer numbers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9).
The third line contains an integer m (1 β€ m β€ 100) β the number of requests.
The following m lines contain pairs of integers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j) β the requests.
Output
Print m integers r_1, r_2, ..., r_m (1 β€ r_j β€ 10^9) one per line: answers to the requests in the order they appear in the input. The value of r_j should be equal to the value contained in the position pos_j of the optimal subsequence for k=k_j.
Examples
Input
3
10 20 10
6
1 1
2 1
2 2
3 1
3 2
3 3
Output
20
10
20
10
20
10
Input
7
1 2 1 3 1 2 1
9
2 1
2 2
3 1
3 2
3 3
1 1
7 1
7 7
7 4
Output
2
3
2
3
2
3
1
1
3
Note
In the first example, for a=[10,20,10] the optimal subsequences are:
* for k=1: [20],
* for k=2: [10,20],
* for k=3: [10,20,10].
|
{
"input": [
"3\n10 20 10\n6\n1 1\n2 1\n2 2\n3 1\n3 2\n3 3\n",
"7\n1 2 1 3 1 2 1\n9\n2 1\n2 2\n3 1\n3 2\n3 3\n1 1\n7 1\n7 7\n7 4\n"
],
"output": [
"20\n10\n20\n10\n20\n10\n",
"2\n3\n2\n3\n2\n3\n1\n1\n3\n"
]
}
|
{
"input": [
"2\n1 10\n3\n2 2\n2 1\n1 1\n",
"2\n3922 3922\n3\n2 2\n2 1\n1 1\n",
"1\n1000000000\n1\n1 1\n",
"1\n1\n3\n1 1\n1 1\n1 1\n",
"5\n3 1 4 1 2\n15\n5 5\n5 4\n5 3\n5 2\n5 1\n4 4\n4 3\n4 2\n4 1\n3 3\n3 2\n3 1\n2 2\n2 1\n1 1\n",
"2\n392222 322\n3\n2 2\n2 1\n1 1\n"
],
"output": [
"10\n1\n10\n",
"3922\n3922\n3922\n",
"1000000000\n",
"1\n1\n1\n",
"2\n1\n4\n1\n3\n2\n4\n1\n3\n2\n4\n3\n4\n3\n4\n",
"322\n392222\n392222\n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int N = 1e5 + 7;
const int inf = INT_MAX / 2;
const long long INF = LLONG_MAX / 3;
const int MOD = 1e9 + 7;
const double eps = 1e-6;
const string cars[] = {"π", "π", "π"};
signed main() {
cout << fixed << setprecision(4);
ios::sync_with_stdio(false);
cin.tie();
cout.tie();
int n;
cin >> n;
vector<int> a(n);
for (int& x : a) {
cin >> x;
}
auto b = a;
sort(a.rbegin(), a.rend());
int q;
cin >> q;
while (q--) {
int k, d;
cin >> k >> d;
multiset<int> s;
for (int i = 0; i < k; i++) {
s.insert(s.begin(), a[i]);
}
int l = 0;
for (int i = 0; i < n; i++) {
if (s.find(b[i]) != s.end()) {
if (++l == d) {
cout << b[i] << "\n";
break;
}
s.erase(s.find(b[i]));
}
}
}
return 0;
}
|
1227_D1. Optimal Subsequences (Easy Version)
|
This is the easier version of the problem. In this version 1 β€ n, m β€ 100. You can hack this problem only if you solve and lock both problems.
You are given a sequence of integers a=[a_1,a_2,...,a_n] of length n. Its subsequence is obtained by removing zero or more elements from the sequence a (they do not necessarily go consecutively). For example, for the sequence a=[11,20,11,33,11,20,11]:
* [11,20,11,33,11,20,11], [11,20,11,33,11,20], [11,11,11,11], [20], [33,20] are subsequences (these are just some of the long list);
* [40], [33,33], [33,20,20], [20,20,11,11] are not subsequences.
Suppose that an additional non-negative integer k (1 β€ k β€ n) is given, then the subsequence is called optimal if:
* it has a length of k and the sum of its elements is the maximum possible among all subsequences of length k;
* and among all subsequences of length k that satisfy the previous item, it is lexicographically minimal.
Recall that the sequence b=[b_1, b_2, ..., b_k] is lexicographically smaller than the sequence c=[c_1, c_2, ..., c_k] if the first element (from the left) in which they differ less in the sequence b than in c. Formally: there exists t (1 β€ t β€ k) such that b_1=c_1, b_2=c_2, ..., b_{t-1}=c_{t-1} and at the same time b_t<c_t. For example:
* [10, 20, 20] lexicographically less than [10, 21, 1],
* [7, 99, 99] is lexicographically less than [10, 21, 1],
* [10, 21, 0] is lexicographically less than [10, 21, 1].
You are given a sequence of a=[a_1,a_2,...,a_n] and m requests, each consisting of two numbers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j). For each query, print the value that is in the index pos_j of the optimal subsequence of the given sequence a for k=k_j.
For example, if n=4, a=[10,20,30,20], k_j=2, then the optimal subsequence is [20,30] β it is the minimum lexicographically among all subsequences of length 2 with the maximum total sum of items. Thus, the answer to the request k_j=2, pos_j=1 is the number 20, and the answer to the request k_j=2, pos_j=2 is the number 30.
Input
The first line contains an integer n (1 β€ n β€ 100) β the length of the sequence a.
The second line contains elements of the sequence a: integer numbers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9).
The third line contains an integer m (1 β€ m β€ 100) β the number of requests.
The following m lines contain pairs of integers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j) β the requests.
Output
Print m integers r_1, r_2, ..., r_m (1 β€ r_j β€ 10^9) one per line: answers to the requests in the order they appear in the input. The value of r_j should be equal to the value contained in the position pos_j of the optimal subsequence for k=k_j.
Examples
Input
3
10 20 10
6
1 1
2 1
2 2
3 1
3 2
3 3
Output
20
10
20
10
20
10
Input
7
1 2 1 3 1 2 1
9
2 1
2 2
3 1
3 2
3 3
1 1
7 1
7 7
7 4
Output
2
3
2
3
2
3
1
1
3
Note
In the first example, for a=[10,20,10] the optimal subsequences are:
* for k=1: [20],
* for k=2: [10,20],
* for k=3: [10,20,10].
|
{
"input": [
"3\n10 20 10\n6\n1 1\n2 1\n2 2\n3 1\n3 2\n3 3\n",
"7\n1 2 1 3 1 2 1\n9\n2 1\n2 2\n3 1\n3 2\n3 3\n1 1\n7 1\n7 7\n7 4\n"
],
"output": [
"20\n10\n20\n10\n20\n10\n",
"2\n3\n2\n3\n2\n3\n1\n1\n3\n"
]
}
|
{
"input": [
"2\n1 10\n3\n2 2\n2 1\n1 1\n",
"2\n3922 3922\n3\n2 2\n2 1\n1 1\n",
"1\n1000000000\n1\n1 1\n",
"1\n1\n3\n1 1\n1 1\n1 1\n",
"5\n3 1 4 1 2\n15\n5 5\n5 4\n5 3\n5 2\n5 1\n4 4\n4 3\n4 2\n4 1\n3 3\n3 2\n3 1\n2 2\n2 1\n1 1\n",
"2\n392222 322\n3\n2 2\n2 1\n1 1\n"
],
"output": [
"10\n1\n10\n",
"3922\n3922\n3922\n",
"1000000000\n",
"1\n1\n1\n",
"2\n1\n4\n1\n3\n2\n4\n1\n3\n2\n4\n3\n4\n3\n4\n",
"322\n392222\n392222\n"
]
}
|
CORRECT
|
python3
|
n = int(input())
l = [int(j) for j in input().split()]
m = int(input())
d = dict()
for i in range(n):
if l[i] in d:
d[l[i]].append(i)
else:
d[l[i]]=[i]
d = sorted(d.items(), reverse =True)
# print(d)
for que in range(m):
k, pos = [int(j) for j in input().split()]
min_ = []
i = 0
j = 0
while(k>0):
k-=1
min_.append(d[i][1][j])
j+=1
if j==len(d[i][1]):
i+=1
j = 0
min_.sort()
# print([l[x] for x in min_])
print(l[min_[pos-1]])
|
1227_D1. Optimal Subsequences (Easy Version)
|
This is the easier version of the problem. In this version 1 β€ n, m β€ 100. You can hack this problem only if you solve and lock both problems.
You are given a sequence of integers a=[a_1,a_2,...,a_n] of length n. Its subsequence is obtained by removing zero or more elements from the sequence a (they do not necessarily go consecutively). For example, for the sequence a=[11,20,11,33,11,20,11]:
* [11,20,11,33,11,20,11], [11,20,11,33,11,20], [11,11,11,11], [20], [33,20] are subsequences (these are just some of the long list);
* [40], [33,33], [33,20,20], [20,20,11,11] are not subsequences.
Suppose that an additional non-negative integer k (1 β€ k β€ n) is given, then the subsequence is called optimal if:
* it has a length of k and the sum of its elements is the maximum possible among all subsequences of length k;
* and among all subsequences of length k that satisfy the previous item, it is lexicographically minimal.
Recall that the sequence b=[b_1, b_2, ..., b_k] is lexicographically smaller than the sequence c=[c_1, c_2, ..., c_k] if the first element (from the left) in which they differ less in the sequence b than in c. Formally: there exists t (1 β€ t β€ k) such that b_1=c_1, b_2=c_2, ..., b_{t-1}=c_{t-1} and at the same time b_t<c_t. For example:
* [10, 20, 20] lexicographically less than [10, 21, 1],
* [7, 99, 99] is lexicographically less than [10, 21, 1],
* [10, 21, 0] is lexicographically less than [10, 21, 1].
You are given a sequence of a=[a_1,a_2,...,a_n] and m requests, each consisting of two numbers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j). For each query, print the value that is in the index pos_j of the optimal subsequence of the given sequence a for k=k_j.
For example, if n=4, a=[10,20,30,20], k_j=2, then the optimal subsequence is [20,30] β it is the minimum lexicographically among all subsequences of length 2 with the maximum total sum of items. Thus, the answer to the request k_j=2, pos_j=1 is the number 20, and the answer to the request k_j=2, pos_j=2 is the number 30.
Input
The first line contains an integer n (1 β€ n β€ 100) β the length of the sequence a.
The second line contains elements of the sequence a: integer numbers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9).
The third line contains an integer m (1 β€ m β€ 100) β the number of requests.
The following m lines contain pairs of integers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j) β the requests.
Output
Print m integers r_1, r_2, ..., r_m (1 β€ r_j β€ 10^9) one per line: answers to the requests in the order they appear in the input. The value of r_j should be equal to the value contained in the position pos_j of the optimal subsequence for k=k_j.
Examples
Input
3
10 20 10
6
1 1
2 1
2 2
3 1
3 2
3 3
Output
20
10
20
10
20
10
Input
7
1 2 1 3 1 2 1
9
2 1
2 2
3 1
3 2
3 3
1 1
7 1
7 7
7 4
Output
2
3
2
3
2
3
1
1
3
Note
In the first example, for a=[10,20,10] the optimal subsequences are:
* for k=1: [20],
* for k=2: [10,20],
* for k=3: [10,20,10].
|
{
"input": [
"3\n10 20 10\n6\n1 1\n2 1\n2 2\n3 1\n3 2\n3 3\n",
"7\n1 2 1 3 1 2 1\n9\n2 1\n2 2\n3 1\n3 2\n3 3\n1 1\n7 1\n7 7\n7 4\n"
],
"output": [
"20\n10\n20\n10\n20\n10\n",
"2\n3\n2\n3\n2\n3\n1\n1\n3\n"
]
}
|
{
"input": [
"2\n1 10\n3\n2 2\n2 1\n1 1\n",
"2\n3922 3922\n3\n2 2\n2 1\n1 1\n",
"1\n1000000000\n1\n1 1\n",
"1\n1\n3\n1 1\n1 1\n1 1\n",
"5\n3 1 4 1 2\n15\n5 5\n5 4\n5 3\n5 2\n5 1\n4 4\n4 3\n4 2\n4 1\n3 3\n3 2\n3 1\n2 2\n2 1\n1 1\n",
"2\n392222 322\n3\n2 2\n2 1\n1 1\n"
],
"output": [
"10\n1\n10\n",
"3922\n3922\n3922\n",
"1000000000\n",
"1\n1\n1\n",
"2\n1\n4\n1\n3\n2\n4\n1\n3\n2\n4\n3\n4\n3\n4\n",
"322\n392222\n392222\n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
int n, m;
bool cmp(pair<int, int> a, pair<int, int> b) {
if (a.first != b.first) return a.first > b.first;
return a.second < b.second;
}
bool cmp2(pair<int, int> a, pair<int, int> b) { return a.second < b.second; }
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
vector<pair<int, int> > v;
for (int i = 1; i <= n; i++) {
int x;
cin >> x;
v.push_back({x, i});
}
sort(v.begin(), v.end(), cmp);
cin >> m;
while (m--) {
int k, pos;
cin >> k >> pos;
vector<pair<int, int> > res;
for (int i = 0; i < k; i++) {
res.push_back(v[i]);
}
sort(res.begin(), res.end(), cmp2);
cout << res[pos - 1].first << '\n';
}
return 0;
}
|
1227_D1. Optimal Subsequences (Easy Version)
|
This is the easier version of the problem. In this version 1 β€ n, m β€ 100. You can hack this problem only if you solve and lock both problems.
You are given a sequence of integers a=[a_1,a_2,...,a_n] of length n. Its subsequence is obtained by removing zero or more elements from the sequence a (they do not necessarily go consecutively). For example, for the sequence a=[11,20,11,33,11,20,11]:
* [11,20,11,33,11,20,11], [11,20,11,33,11,20], [11,11,11,11], [20], [33,20] are subsequences (these are just some of the long list);
* [40], [33,33], [33,20,20], [20,20,11,11] are not subsequences.
Suppose that an additional non-negative integer k (1 β€ k β€ n) is given, then the subsequence is called optimal if:
* it has a length of k and the sum of its elements is the maximum possible among all subsequences of length k;
* and among all subsequences of length k that satisfy the previous item, it is lexicographically minimal.
Recall that the sequence b=[b_1, b_2, ..., b_k] is lexicographically smaller than the sequence c=[c_1, c_2, ..., c_k] if the first element (from the left) in which they differ less in the sequence b than in c. Formally: there exists t (1 β€ t β€ k) such that b_1=c_1, b_2=c_2, ..., b_{t-1}=c_{t-1} and at the same time b_t<c_t. For example:
* [10, 20, 20] lexicographically less than [10, 21, 1],
* [7, 99, 99] is lexicographically less than [10, 21, 1],
* [10, 21, 0] is lexicographically less than [10, 21, 1].
You are given a sequence of a=[a_1,a_2,...,a_n] and m requests, each consisting of two numbers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j). For each query, print the value that is in the index pos_j of the optimal subsequence of the given sequence a for k=k_j.
For example, if n=4, a=[10,20,30,20], k_j=2, then the optimal subsequence is [20,30] β it is the minimum lexicographically among all subsequences of length 2 with the maximum total sum of items. Thus, the answer to the request k_j=2, pos_j=1 is the number 20, and the answer to the request k_j=2, pos_j=2 is the number 30.
Input
The first line contains an integer n (1 β€ n β€ 100) β the length of the sequence a.
The second line contains elements of the sequence a: integer numbers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9).
The third line contains an integer m (1 β€ m β€ 100) β the number of requests.
The following m lines contain pairs of integers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j) β the requests.
Output
Print m integers r_1, r_2, ..., r_m (1 β€ r_j β€ 10^9) one per line: answers to the requests in the order they appear in the input. The value of r_j should be equal to the value contained in the position pos_j of the optimal subsequence for k=k_j.
Examples
Input
3
10 20 10
6
1 1
2 1
2 2
3 1
3 2
3 3
Output
20
10
20
10
20
10
Input
7
1 2 1 3 1 2 1
9
2 1
2 2
3 1
3 2
3 3
1 1
7 1
7 7
7 4
Output
2
3
2
3
2
3
1
1
3
Note
In the first example, for a=[10,20,10] the optimal subsequences are:
* for k=1: [20],
* for k=2: [10,20],
* for k=3: [10,20,10].
|
{
"input": [
"3\n10 20 10\n6\n1 1\n2 1\n2 2\n3 1\n3 2\n3 3\n",
"7\n1 2 1 3 1 2 1\n9\n2 1\n2 2\n3 1\n3 2\n3 3\n1 1\n7 1\n7 7\n7 4\n"
],
"output": [
"20\n10\n20\n10\n20\n10\n",
"2\n3\n2\n3\n2\n3\n1\n1\n3\n"
]
}
|
{
"input": [
"2\n1 10\n3\n2 2\n2 1\n1 1\n",
"2\n3922 3922\n3\n2 2\n2 1\n1 1\n",
"1\n1000000000\n1\n1 1\n",
"1\n1\n3\n1 1\n1 1\n1 1\n",
"5\n3 1 4 1 2\n15\n5 5\n5 4\n5 3\n5 2\n5 1\n4 4\n4 3\n4 2\n4 1\n3 3\n3 2\n3 1\n2 2\n2 1\n1 1\n",
"2\n392222 322\n3\n2 2\n2 1\n1 1\n"
],
"output": [
"10\n1\n10\n",
"3922\n3922\n3922\n",
"1000000000\n",
"1\n1\n1\n",
"2\n1\n4\n1\n3\n2\n4\n1\n3\n2\n4\n3\n4\n3\n4\n",
"322\n392222\n392222\n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
bool comp(pair<long long int, long long int> a,
pair<long long int, long long int> b) {
if (a.first != b.first)
return a.first < b.first;
else
return a.second > b.second;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int TESTS = 1;
while (TESTS--) {
long long int n;
cin >> n;
vector<pair<long long int, long long int> > v;
for (long long int i = 0; i < n; i++) {
long long int k;
cin >> k;
v.push_back({k, i});
}
sort(v.begin(), v.end(), comp);
long long int m;
cin >> m;
while (m--) {
long long int k, pos;
cin >> k >> pos;
vector<pair<long long int, long long int> > a;
for (long long int i = n - k; i < n; i++)
a.push_back({v[i].second, v[i].first});
sort(a.begin(), a.end());
cout << a[pos - 1].second << '\n';
}
}
return 0;
}
|
1227_D1. Optimal Subsequences (Easy Version)
|
This is the easier version of the problem. In this version 1 β€ n, m β€ 100. You can hack this problem only if you solve and lock both problems.
You are given a sequence of integers a=[a_1,a_2,...,a_n] of length n. Its subsequence is obtained by removing zero or more elements from the sequence a (they do not necessarily go consecutively). For example, for the sequence a=[11,20,11,33,11,20,11]:
* [11,20,11,33,11,20,11], [11,20,11,33,11,20], [11,11,11,11], [20], [33,20] are subsequences (these are just some of the long list);
* [40], [33,33], [33,20,20], [20,20,11,11] are not subsequences.
Suppose that an additional non-negative integer k (1 β€ k β€ n) is given, then the subsequence is called optimal if:
* it has a length of k and the sum of its elements is the maximum possible among all subsequences of length k;
* and among all subsequences of length k that satisfy the previous item, it is lexicographically minimal.
Recall that the sequence b=[b_1, b_2, ..., b_k] is lexicographically smaller than the sequence c=[c_1, c_2, ..., c_k] if the first element (from the left) in which they differ less in the sequence b than in c. Formally: there exists t (1 β€ t β€ k) such that b_1=c_1, b_2=c_2, ..., b_{t-1}=c_{t-1} and at the same time b_t<c_t. For example:
* [10, 20, 20] lexicographically less than [10, 21, 1],
* [7, 99, 99] is lexicographically less than [10, 21, 1],
* [10, 21, 0] is lexicographically less than [10, 21, 1].
You are given a sequence of a=[a_1,a_2,...,a_n] and m requests, each consisting of two numbers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j). For each query, print the value that is in the index pos_j of the optimal subsequence of the given sequence a for k=k_j.
For example, if n=4, a=[10,20,30,20], k_j=2, then the optimal subsequence is [20,30] β it is the minimum lexicographically among all subsequences of length 2 with the maximum total sum of items. Thus, the answer to the request k_j=2, pos_j=1 is the number 20, and the answer to the request k_j=2, pos_j=2 is the number 30.
Input
The first line contains an integer n (1 β€ n β€ 100) β the length of the sequence a.
The second line contains elements of the sequence a: integer numbers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9).
The third line contains an integer m (1 β€ m β€ 100) β the number of requests.
The following m lines contain pairs of integers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j) β the requests.
Output
Print m integers r_1, r_2, ..., r_m (1 β€ r_j β€ 10^9) one per line: answers to the requests in the order they appear in the input. The value of r_j should be equal to the value contained in the position pos_j of the optimal subsequence for k=k_j.
Examples
Input
3
10 20 10
6
1 1
2 1
2 2
3 1
3 2
3 3
Output
20
10
20
10
20
10
Input
7
1 2 1 3 1 2 1
9
2 1
2 2
3 1
3 2
3 3
1 1
7 1
7 7
7 4
Output
2
3
2
3
2
3
1
1
3
Note
In the first example, for a=[10,20,10] the optimal subsequences are:
* for k=1: [20],
* for k=2: [10,20],
* for k=3: [10,20,10].
|
{
"input": [
"3\n10 20 10\n6\n1 1\n2 1\n2 2\n3 1\n3 2\n3 3\n",
"7\n1 2 1 3 1 2 1\n9\n2 1\n2 2\n3 1\n3 2\n3 3\n1 1\n7 1\n7 7\n7 4\n"
],
"output": [
"20\n10\n20\n10\n20\n10\n",
"2\n3\n2\n3\n2\n3\n1\n1\n3\n"
]
}
|
{
"input": [
"2\n1 10\n3\n2 2\n2 1\n1 1\n",
"2\n3922 3922\n3\n2 2\n2 1\n1 1\n",
"1\n1000000000\n1\n1 1\n",
"1\n1\n3\n1 1\n1 1\n1 1\n",
"5\n3 1 4 1 2\n15\n5 5\n5 4\n5 3\n5 2\n5 1\n4 4\n4 3\n4 2\n4 1\n3 3\n3 2\n3 1\n2 2\n2 1\n1 1\n",
"2\n392222 322\n3\n2 2\n2 1\n1 1\n"
],
"output": [
"10\n1\n10\n",
"3922\n3922\n3922\n",
"1000000000\n",
"1\n1\n1\n",
"2\n1\n4\n1\n3\n2\n4\n1\n3\n2\n4\n3\n4\n3\n4\n",
"322\n392222\n392222\n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
vector<long long int> graph[200007];
long long int visited[200007] = {0};
long long int mx = 0, cnt = 0;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long int n, m, i, j;
cin >> n;
long long int a[n + 10];
vector<pair<long long int, long long int> > v, s, g;
vector<long long int> b;
for (i = 0; i < n; i++) {
cin >> a[i];
s.push_back(make_pair(a[i], i));
}
sort(s.begin(), s.end());
for (i = 0; i < n - 1; i++) {
if (s[i].first != s[i + 1].first) {
g.push_back(make_pair(s[i].first, s[i].second));
for (j = g.size() - 1; j >= 0; j--) {
v.push_back(make_pair(g[j].first, g[j].second));
}
g.clear();
} else {
g.push_back(make_pair(s[i].first, s[i].second));
}
}
g.push_back(make_pair(s[i].first, s[i].second));
for (j = g.size() - 1; j >= 0; j--) {
v.push_back(make_pair(g[j].first, g[j].second));
}
cin >> m;
while (m--) {
long long int k, p;
cin >> k >> p;
cnt = 0;
for (i = n - 1; i >= 0; i--) {
cnt++;
b.push_back(v[i].second);
if (cnt == k) {
break;
}
}
sort(b.begin(), b.end());
cout << a[b[p - 1]] << endl;
b.clear();
}
return 0;
}
|
1227_D1. Optimal Subsequences (Easy Version)
|
This is the easier version of the problem. In this version 1 β€ n, m β€ 100. You can hack this problem only if you solve and lock both problems.
You are given a sequence of integers a=[a_1,a_2,...,a_n] of length n. Its subsequence is obtained by removing zero or more elements from the sequence a (they do not necessarily go consecutively). For example, for the sequence a=[11,20,11,33,11,20,11]:
* [11,20,11,33,11,20,11], [11,20,11,33,11,20], [11,11,11,11], [20], [33,20] are subsequences (these are just some of the long list);
* [40], [33,33], [33,20,20], [20,20,11,11] are not subsequences.
Suppose that an additional non-negative integer k (1 β€ k β€ n) is given, then the subsequence is called optimal if:
* it has a length of k and the sum of its elements is the maximum possible among all subsequences of length k;
* and among all subsequences of length k that satisfy the previous item, it is lexicographically minimal.
Recall that the sequence b=[b_1, b_2, ..., b_k] is lexicographically smaller than the sequence c=[c_1, c_2, ..., c_k] if the first element (from the left) in which they differ less in the sequence b than in c. Formally: there exists t (1 β€ t β€ k) such that b_1=c_1, b_2=c_2, ..., b_{t-1}=c_{t-1} and at the same time b_t<c_t. For example:
* [10, 20, 20] lexicographically less than [10, 21, 1],
* [7, 99, 99] is lexicographically less than [10, 21, 1],
* [10, 21, 0] is lexicographically less than [10, 21, 1].
You are given a sequence of a=[a_1,a_2,...,a_n] and m requests, each consisting of two numbers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j). For each query, print the value that is in the index pos_j of the optimal subsequence of the given sequence a for k=k_j.
For example, if n=4, a=[10,20,30,20], k_j=2, then the optimal subsequence is [20,30] β it is the minimum lexicographically among all subsequences of length 2 with the maximum total sum of items. Thus, the answer to the request k_j=2, pos_j=1 is the number 20, and the answer to the request k_j=2, pos_j=2 is the number 30.
Input
The first line contains an integer n (1 β€ n β€ 100) β the length of the sequence a.
The second line contains elements of the sequence a: integer numbers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9).
The third line contains an integer m (1 β€ m β€ 100) β the number of requests.
The following m lines contain pairs of integers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j) β the requests.
Output
Print m integers r_1, r_2, ..., r_m (1 β€ r_j β€ 10^9) one per line: answers to the requests in the order they appear in the input. The value of r_j should be equal to the value contained in the position pos_j of the optimal subsequence for k=k_j.
Examples
Input
3
10 20 10
6
1 1
2 1
2 2
3 1
3 2
3 3
Output
20
10
20
10
20
10
Input
7
1 2 1 3 1 2 1
9
2 1
2 2
3 1
3 2
3 3
1 1
7 1
7 7
7 4
Output
2
3
2
3
2
3
1
1
3
Note
In the first example, for a=[10,20,10] the optimal subsequences are:
* for k=1: [20],
* for k=2: [10,20],
* for k=3: [10,20,10].
|
{
"input": [
"3\n10 20 10\n6\n1 1\n2 1\n2 2\n3 1\n3 2\n3 3\n",
"7\n1 2 1 3 1 2 1\n9\n2 1\n2 2\n3 1\n3 2\n3 3\n1 1\n7 1\n7 7\n7 4\n"
],
"output": [
"20\n10\n20\n10\n20\n10\n",
"2\n3\n2\n3\n2\n3\n1\n1\n3\n"
]
}
|
{
"input": [
"2\n1 10\n3\n2 2\n2 1\n1 1\n",
"2\n3922 3922\n3\n2 2\n2 1\n1 1\n",
"1\n1000000000\n1\n1 1\n",
"1\n1\n3\n1 1\n1 1\n1 1\n",
"5\n3 1 4 1 2\n15\n5 5\n5 4\n5 3\n5 2\n5 1\n4 4\n4 3\n4 2\n4 1\n3 3\n3 2\n3 1\n2 2\n2 1\n1 1\n",
"2\n392222 322\n3\n2 2\n2 1\n1 1\n"
],
"output": [
"10\n1\n10\n",
"3922\n3922\n3922\n",
"1000000000\n",
"1\n1\n1\n",
"2\n1\n4\n1\n3\n2\n4\n1\n3\n2\n4\n3\n4\n3\n4\n",
"322\n392222\n392222\n"
]
}
|
CORRECT
|
python3
|
n = int(input())
a = list(map(int, input().split()))
s_a = sorted(a)
m = int(input())
req = []
for _ in range(m):
req.append(list(map(int, input().split())))
d = dict()
for r in req:
if r[0] in d.keys():
print(d[r[0]][r[1] - 1])
else:
del_indx = []
for e in s_a[:n-r[0]]:
for i in reversed(range(n)):
if a[i] == e:
if i not in del_indx:
del_indx.append(i)
break
new_a = []
for i in range(n):
if i not in del_indx:
new_a.append(a[i])
d[r[0]] = new_a
print(d[r[0]][r[1] - 1])
|
1227_D1. Optimal Subsequences (Easy Version)
|
This is the easier version of the problem. In this version 1 β€ n, m β€ 100. You can hack this problem only if you solve and lock both problems.
You are given a sequence of integers a=[a_1,a_2,...,a_n] of length n. Its subsequence is obtained by removing zero or more elements from the sequence a (they do not necessarily go consecutively). For example, for the sequence a=[11,20,11,33,11,20,11]:
* [11,20,11,33,11,20,11], [11,20,11,33,11,20], [11,11,11,11], [20], [33,20] are subsequences (these are just some of the long list);
* [40], [33,33], [33,20,20], [20,20,11,11] are not subsequences.
Suppose that an additional non-negative integer k (1 β€ k β€ n) is given, then the subsequence is called optimal if:
* it has a length of k and the sum of its elements is the maximum possible among all subsequences of length k;
* and among all subsequences of length k that satisfy the previous item, it is lexicographically minimal.
Recall that the sequence b=[b_1, b_2, ..., b_k] is lexicographically smaller than the sequence c=[c_1, c_2, ..., c_k] if the first element (from the left) in which they differ less in the sequence b than in c. Formally: there exists t (1 β€ t β€ k) such that b_1=c_1, b_2=c_2, ..., b_{t-1}=c_{t-1} and at the same time b_t<c_t. For example:
* [10, 20, 20] lexicographically less than [10, 21, 1],
* [7, 99, 99] is lexicographically less than [10, 21, 1],
* [10, 21, 0] is lexicographically less than [10, 21, 1].
You are given a sequence of a=[a_1,a_2,...,a_n] and m requests, each consisting of two numbers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j). For each query, print the value that is in the index pos_j of the optimal subsequence of the given sequence a for k=k_j.
For example, if n=4, a=[10,20,30,20], k_j=2, then the optimal subsequence is [20,30] β it is the minimum lexicographically among all subsequences of length 2 with the maximum total sum of items. Thus, the answer to the request k_j=2, pos_j=1 is the number 20, and the answer to the request k_j=2, pos_j=2 is the number 30.
Input
The first line contains an integer n (1 β€ n β€ 100) β the length of the sequence a.
The second line contains elements of the sequence a: integer numbers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9).
The third line contains an integer m (1 β€ m β€ 100) β the number of requests.
The following m lines contain pairs of integers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j) β the requests.
Output
Print m integers r_1, r_2, ..., r_m (1 β€ r_j β€ 10^9) one per line: answers to the requests in the order they appear in the input. The value of r_j should be equal to the value contained in the position pos_j of the optimal subsequence for k=k_j.
Examples
Input
3
10 20 10
6
1 1
2 1
2 2
3 1
3 2
3 3
Output
20
10
20
10
20
10
Input
7
1 2 1 3 1 2 1
9
2 1
2 2
3 1
3 2
3 3
1 1
7 1
7 7
7 4
Output
2
3
2
3
2
3
1
1
3
Note
In the first example, for a=[10,20,10] the optimal subsequences are:
* for k=1: [20],
* for k=2: [10,20],
* for k=3: [10,20,10].
|
{
"input": [
"3\n10 20 10\n6\n1 1\n2 1\n2 2\n3 1\n3 2\n3 3\n",
"7\n1 2 1 3 1 2 1\n9\n2 1\n2 2\n3 1\n3 2\n3 3\n1 1\n7 1\n7 7\n7 4\n"
],
"output": [
"20\n10\n20\n10\n20\n10\n",
"2\n3\n2\n3\n2\n3\n1\n1\n3\n"
]
}
|
{
"input": [
"2\n1 10\n3\n2 2\n2 1\n1 1\n",
"2\n3922 3922\n3\n2 2\n2 1\n1 1\n",
"1\n1000000000\n1\n1 1\n",
"1\n1\n3\n1 1\n1 1\n1 1\n",
"5\n3 1 4 1 2\n15\n5 5\n5 4\n5 3\n5 2\n5 1\n4 4\n4 3\n4 2\n4 1\n3 3\n3 2\n3 1\n2 2\n2 1\n1 1\n",
"2\n392222 322\n3\n2 2\n2 1\n1 1\n"
],
"output": [
"10\n1\n10\n",
"3922\n3922\n3922\n",
"1000000000\n",
"1\n1\n1\n",
"2\n1\n4\n1\n3\n2\n4\n1\n3\n2\n4\n3\n4\n3\n4\n",
"322\n392222\n392222\n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
bool cmp(pair<int, int> a, pair<int, int> b) {
if (a.first != b.first) return a.first < b.first;
return a.second > b.second;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n;
cin >> n;
vector<pair<int, int>> a;
for (int i = 0; i < n; ++i) {
int x;
cin >> x;
a.emplace_back(x, i);
}
sort(a.begin(), a.end(), cmp);
int m;
cin >> m;
for (int i = 0; i < m; ++i) {
int k, pos;
cin >> k >> pos;
vector<pair<int, int>> now;
for (int i = n - k; i < n; ++i) {
now.emplace_back(a[i].second, a[i].first);
}
sort(now.begin(), now.end());
cout << now[pos - 1].second << "\n";
}
}
|
1227_D1. Optimal Subsequences (Easy Version)
|
This is the easier version of the problem. In this version 1 β€ n, m β€ 100. You can hack this problem only if you solve and lock both problems.
You are given a sequence of integers a=[a_1,a_2,...,a_n] of length n. Its subsequence is obtained by removing zero or more elements from the sequence a (they do not necessarily go consecutively). For example, for the sequence a=[11,20,11,33,11,20,11]:
* [11,20,11,33,11,20,11], [11,20,11,33,11,20], [11,11,11,11], [20], [33,20] are subsequences (these are just some of the long list);
* [40], [33,33], [33,20,20], [20,20,11,11] are not subsequences.
Suppose that an additional non-negative integer k (1 β€ k β€ n) is given, then the subsequence is called optimal if:
* it has a length of k and the sum of its elements is the maximum possible among all subsequences of length k;
* and among all subsequences of length k that satisfy the previous item, it is lexicographically minimal.
Recall that the sequence b=[b_1, b_2, ..., b_k] is lexicographically smaller than the sequence c=[c_1, c_2, ..., c_k] if the first element (from the left) in which they differ less in the sequence b than in c. Formally: there exists t (1 β€ t β€ k) such that b_1=c_1, b_2=c_2, ..., b_{t-1}=c_{t-1} and at the same time b_t<c_t. For example:
* [10, 20, 20] lexicographically less than [10, 21, 1],
* [7, 99, 99] is lexicographically less than [10, 21, 1],
* [10, 21, 0] is lexicographically less than [10, 21, 1].
You are given a sequence of a=[a_1,a_2,...,a_n] and m requests, each consisting of two numbers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j). For each query, print the value that is in the index pos_j of the optimal subsequence of the given sequence a for k=k_j.
For example, if n=4, a=[10,20,30,20], k_j=2, then the optimal subsequence is [20,30] β it is the minimum lexicographically among all subsequences of length 2 with the maximum total sum of items. Thus, the answer to the request k_j=2, pos_j=1 is the number 20, and the answer to the request k_j=2, pos_j=2 is the number 30.
Input
The first line contains an integer n (1 β€ n β€ 100) β the length of the sequence a.
The second line contains elements of the sequence a: integer numbers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9).
The third line contains an integer m (1 β€ m β€ 100) β the number of requests.
The following m lines contain pairs of integers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j) β the requests.
Output
Print m integers r_1, r_2, ..., r_m (1 β€ r_j β€ 10^9) one per line: answers to the requests in the order they appear in the input. The value of r_j should be equal to the value contained in the position pos_j of the optimal subsequence for k=k_j.
Examples
Input
3
10 20 10
6
1 1
2 1
2 2
3 1
3 2
3 3
Output
20
10
20
10
20
10
Input
7
1 2 1 3 1 2 1
9
2 1
2 2
3 1
3 2
3 3
1 1
7 1
7 7
7 4
Output
2
3
2
3
2
3
1
1
3
Note
In the first example, for a=[10,20,10] the optimal subsequences are:
* for k=1: [20],
* for k=2: [10,20],
* for k=3: [10,20,10].
|
{
"input": [
"3\n10 20 10\n6\n1 1\n2 1\n2 2\n3 1\n3 2\n3 3\n",
"7\n1 2 1 3 1 2 1\n9\n2 1\n2 2\n3 1\n3 2\n3 3\n1 1\n7 1\n7 7\n7 4\n"
],
"output": [
"20\n10\n20\n10\n20\n10\n",
"2\n3\n2\n3\n2\n3\n1\n1\n3\n"
]
}
|
{
"input": [
"2\n1 10\n3\n2 2\n2 1\n1 1\n",
"2\n3922 3922\n3\n2 2\n2 1\n1 1\n",
"1\n1000000000\n1\n1 1\n",
"1\n1\n3\n1 1\n1 1\n1 1\n",
"5\n3 1 4 1 2\n15\n5 5\n5 4\n5 3\n5 2\n5 1\n4 4\n4 3\n4 2\n4 1\n3 3\n3 2\n3 1\n2 2\n2 1\n1 1\n",
"2\n392222 322\n3\n2 2\n2 1\n1 1\n"
],
"output": [
"10\n1\n10\n",
"3922\n3922\n3922\n",
"1000000000\n",
"1\n1\n1\n",
"2\n1\n4\n1\n3\n2\n4\n1\n3\n2\n4\n3\n4\n3\n4\n",
"322\n392222\n392222\n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("O3")
using namespace std;
int a[1000], b[1000];
map<int, bool> mk;
int main() {
iostream::sync_with_stdio(0);
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i];
b[i] = a[i];
}
sort(a, a + n);
int m;
cin >> m;
for (int i = 0; i < m; i++) {
mk.clear();
int pos, k;
cin >> k >> pos;
vector<int> g;
g.clear();
for (int j = n - 1; j >= n - k; j--) {
int p;
for (int u = 0; u < n; u++)
if (!mk[u] && b[u] == a[j]) {
p = u;
mk[u] = 1;
break;
}
g.push_back(p);
}
sort(g.begin(), g.end());
cout << b[g[pos - 1]] << "\n";
}
}
|
1227_D1. Optimal Subsequences (Easy Version)
|
This is the easier version of the problem. In this version 1 β€ n, m β€ 100. You can hack this problem only if you solve and lock both problems.
You are given a sequence of integers a=[a_1,a_2,...,a_n] of length n. Its subsequence is obtained by removing zero or more elements from the sequence a (they do not necessarily go consecutively). For example, for the sequence a=[11,20,11,33,11,20,11]:
* [11,20,11,33,11,20,11], [11,20,11,33,11,20], [11,11,11,11], [20], [33,20] are subsequences (these are just some of the long list);
* [40], [33,33], [33,20,20], [20,20,11,11] are not subsequences.
Suppose that an additional non-negative integer k (1 β€ k β€ n) is given, then the subsequence is called optimal if:
* it has a length of k and the sum of its elements is the maximum possible among all subsequences of length k;
* and among all subsequences of length k that satisfy the previous item, it is lexicographically minimal.
Recall that the sequence b=[b_1, b_2, ..., b_k] is lexicographically smaller than the sequence c=[c_1, c_2, ..., c_k] if the first element (from the left) in which they differ less in the sequence b than in c. Formally: there exists t (1 β€ t β€ k) such that b_1=c_1, b_2=c_2, ..., b_{t-1}=c_{t-1} and at the same time b_t<c_t. For example:
* [10, 20, 20] lexicographically less than [10, 21, 1],
* [7, 99, 99] is lexicographically less than [10, 21, 1],
* [10, 21, 0] is lexicographically less than [10, 21, 1].
You are given a sequence of a=[a_1,a_2,...,a_n] and m requests, each consisting of two numbers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j). For each query, print the value that is in the index pos_j of the optimal subsequence of the given sequence a for k=k_j.
For example, if n=4, a=[10,20,30,20], k_j=2, then the optimal subsequence is [20,30] β it is the minimum lexicographically among all subsequences of length 2 with the maximum total sum of items. Thus, the answer to the request k_j=2, pos_j=1 is the number 20, and the answer to the request k_j=2, pos_j=2 is the number 30.
Input
The first line contains an integer n (1 β€ n β€ 100) β the length of the sequence a.
The second line contains elements of the sequence a: integer numbers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9).
The third line contains an integer m (1 β€ m β€ 100) β the number of requests.
The following m lines contain pairs of integers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j) β the requests.
Output
Print m integers r_1, r_2, ..., r_m (1 β€ r_j β€ 10^9) one per line: answers to the requests in the order they appear in the input. The value of r_j should be equal to the value contained in the position pos_j of the optimal subsequence for k=k_j.
Examples
Input
3
10 20 10
6
1 1
2 1
2 2
3 1
3 2
3 3
Output
20
10
20
10
20
10
Input
7
1 2 1 3 1 2 1
9
2 1
2 2
3 1
3 2
3 3
1 1
7 1
7 7
7 4
Output
2
3
2
3
2
3
1
1
3
Note
In the first example, for a=[10,20,10] the optimal subsequences are:
* for k=1: [20],
* for k=2: [10,20],
* for k=3: [10,20,10].
|
{
"input": [
"3\n10 20 10\n6\n1 1\n2 1\n2 2\n3 1\n3 2\n3 3\n",
"7\n1 2 1 3 1 2 1\n9\n2 1\n2 2\n3 1\n3 2\n3 3\n1 1\n7 1\n7 7\n7 4\n"
],
"output": [
"20\n10\n20\n10\n20\n10\n",
"2\n3\n2\n3\n2\n3\n1\n1\n3\n"
]
}
|
{
"input": [
"2\n1 10\n3\n2 2\n2 1\n1 1\n",
"2\n3922 3922\n3\n2 2\n2 1\n1 1\n",
"1\n1000000000\n1\n1 1\n",
"1\n1\n3\n1 1\n1 1\n1 1\n",
"5\n3 1 4 1 2\n15\n5 5\n5 4\n5 3\n5 2\n5 1\n4 4\n4 3\n4 2\n4 1\n3 3\n3 2\n3 1\n2 2\n2 1\n1 1\n",
"2\n392222 322\n3\n2 2\n2 1\n1 1\n"
],
"output": [
"10\n1\n10\n",
"3922\n3922\n3922\n",
"1000000000\n",
"1\n1\n1\n",
"2\n1\n4\n1\n3\n2\n4\n1\n3\n2\n4\n3\n4\n3\n4\n",
"322\n392222\n392222\n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
void solve();
void fast() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
}
template <typename T>
istream &operator>>(istream &in, vector<T> &ans) {
for (auto &d : ans) in >> d;
return in;
}
template <typename T>
ostream &operator<<(ostream &out, vector<T> &ans) {
for (auto &d : ans) out << d << " ";
return out;
}
signed main() {
fast();
solve();
}
struct node {
int key;
long long y;
int sz;
node *left, *right;
node(int _key) : key(_key) {
key = _key;
y = rand();
sz = 1;
left = nullptr;
right = nullptr;
}
};
int size(node *v) { return (v == nullptr) ? 0 : v->sz; }
void upd(node *v) { v->sz = size(v->left) + size(v->right) + 1; }
pair<node *, node *> split_key(node *root, int c) {
if (root == nullptr) return {nullptr, nullptr};
if (root->key > c) {
auto p = split_key(root->left, c);
root->left = p.second;
upd(root);
return {p.first, root};
} else {
auto p = split_key(root->right, c);
root->right = p.first;
upd(root);
return {root, p.second};
}
}
pair<node *, node *> split_size(node *root, int c) {
if (root == nullptr) return {nullptr, nullptr};
if (size(root->left) >= c) {
auto p = split_size(root->left, c);
root->left = p.second;
upd(root);
return {p.first, root};
} else {
auto p = split_size(root->right, c - size(root->left) - 1);
root->right = p.first;
upd(root);
return {root, p.second};
}
}
node *merge(node *a, node *b) {
if (a == nullptr) return b;
if (b == nullptr) return a;
if (a->y > b->key) {
a->right = merge(a->right, b);
upd(a);
return a;
} else {
b->left = merge(a, b->left);
upd(b);
return b;
}
}
node *insert(node *root, int ke) {
auto p = split_key(root, ke);
return merge(merge(p.first, new node(ke)), p.second);
}
struct qur {
int k, pos, numb;
};
bool operator<(qur a, qur b) { return a.k < b.k; }
void print(node *root) {
if (root == nullptr) return;
print(root->left);
cout << root->key << " ";
print(root->right);
}
void solve() {
int n;
cin >> n;
srand(time(0));
vector<pair<int, int>> ans(n), pas;
for (int i = 0; i < n; ++i) cin >> ans[i].first, ans[i].second = i;
int m;
cin >> m;
vector<qur> kes(m);
pas = ans;
for (int i = 0; i < m; ++i) cin >> kes[i].k >> kes[i].pos, kes[i].numb = i;
auto cmp = [](pair<int, int> a, pair<int, int> b) {
if (a.first != b.first) return a.first > b.first;
return a.second < b.second;
};
sort(kes.begin(), kes.end());
sort(ans.begin(), ans.end(), cmp);
int cnt = 0;
vector<int> otv(m);
node *root = nullptr;
for (auto &[k, pos, numb] : kes) {
while (cnt < k) root = insert(root, ans[cnt].second), cnt++;
auto p = split_size(root, pos - 1);
auto c = split_size(p.second, 1);
otv[numb] = pas[c.first->key].first;
root = merge(p.first, merge(c.first, c.second));
}
for (auto &d : otv) cout << d << endl;
}
|
1227_D1. Optimal Subsequences (Easy Version)
|
This is the easier version of the problem. In this version 1 β€ n, m β€ 100. You can hack this problem only if you solve and lock both problems.
You are given a sequence of integers a=[a_1,a_2,...,a_n] of length n. Its subsequence is obtained by removing zero or more elements from the sequence a (they do not necessarily go consecutively). For example, for the sequence a=[11,20,11,33,11,20,11]:
* [11,20,11,33,11,20,11], [11,20,11,33,11,20], [11,11,11,11], [20], [33,20] are subsequences (these are just some of the long list);
* [40], [33,33], [33,20,20], [20,20,11,11] are not subsequences.
Suppose that an additional non-negative integer k (1 β€ k β€ n) is given, then the subsequence is called optimal if:
* it has a length of k and the sum of its elements is the maximum possible among all subsequences of length k;
* and among all subsequences of length k that satisfy the previous item, it is lexicographically minimal.
Recall that the sequence b=[b_1, b_2, ..., b_k] is lexicographically smaller than the sequence c=[c_1, c_2, ..., c_k] if the first element (from the left) in which they differ less in the sequence b than in c. Formally: there exists t (1 β€ t β€ k) such that b_1=c_1, b_2=c_2, ..., b_{t-1}=c_{t-1} and at the same time b_t<c_t. For example:
* [10, 20, 20] lexicographically less than [10, 21, 1],
* [7, 99, 99] is lexicographically less than [10, 21, 1],
* [10, 21, 0] is lexicographically less than [10, 21, 1].
You are given a sequence of a=[a_1,a_2,...,a_n] and m requests, each consisting of two numbers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j). For each query, print the value that is in the index pos_j of the optimal subsequence of the given sequence a for k=k_j.
For example, if n=4, a=[10,20,30,20], k_j=2, then the optimal subsequence is [20,30] β it is the minimum lexicographically among all subsequences of length 2 with the maximum total sum of items. Thus, the answer to the request k_j=2, pos_j=1 is the number 20, and the answer to the request k_j=2, pos_j=2 is the number 30.
Input
The first line contains an integer n (1 β€ n β€ 100) β the length of the sequence a.
The second line contains elements of the sequence a: integer numbers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9).
The third line contains an integer m (1 β€ m β€ 100) β the number of requests.
The following m lines contain pairs of integers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j) β the requests.
Output
Print m integers r_1, r_2, ..., r_m (1 β€ r_j β€ 10^9) one per line: answers to the requests in the order they appear in the input. The value of r_j should be equal to the value contained in the position pos_j of the optimal subsequence for k=k_j.
Examples
Input
3
10 20 10
6
1 1
2 1
2 2
3 1
3 2
3 3
Output
20
10
20
10
20
10
Input
7
1 2 1 3 1 2 1
9
2 1
2 2
3 1
3 2
3 3
1 1
7 1
7 7
7 4
Output
2
3
2
3
2
3
1
1
3
Note
In the first example, for a=[10,20,10] the optimal subsequences are:
* for k=1: [20],
* for k=2: [10,20],
* for k=3: [10,20,10].
|
{
"input": [
"3\n10 20 10\n6\n1 1\n2 1\n2 2\n3 1\n3 2\n3 3\n",
"7\n1 2 1 3 1 2 1\n9\n2 1\n2 2\n3 1\n3 2\n3 3\n1 1\n7 1\n7 7\n7 4\n"
],
"output": [
"20\n10\n20\n10\n20\n10\n",
"2\n3\n2\n3\n2\n3\n1\n1\n3\n"
]
}
|
{
"input": [
"2\n1 10\n3\n2 2\n2 1\n1 1\n",
"2\n3922 3922\n3\n2 2\n2 1\n1 1\n",
"1\n1000000000\n1\n1 1\n",
"1\n1\n3\n1 1\n1 1\n1 1\n",
"5\n3 1 4 1 2\n15\n5 5\n5 4\n5 3\n5 2\n5 1\n4 4\n4 3\n4 2\n4 1\n3 3\n3 2\n3 1\n2 2\n2 1\n1 1\n",
"2\n392222 322\n3\n2 2\n2 1\n1 1\n"
],
"output": [
"10\n1\n10\n",
"3922\n3922\n3922\n",
"1000000000\n",
"1\n1\n1\n",
"2\n1\n4\n1\n3\n2\n4\n1\n3\n2\n4\n3\n4\n3\n4\n",
"322\n392222\n392222\n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
int a[101];
bool comp(pair<int, int> x, pair<int, int> y) {
if (x.first == y.first) {
return x.second < y.second;
}
return x.first > y.first;
}
bool comp2(pair<int, int> x, pair<int, int> y) { return x.second < y.second; }
int d[101][101];
int main() {
cin.sync_with_stdio(0);
cin.tie(0);
cout.tie(NULL);
int n;
cin >> n;
vector<pair<int, int>> v;
for (int i = 0; i < n; i++) {
cin >> a[i];
v.push_back({a[i], i});
}
sort(v.begin(), v.end(), comp);
int m;
cin >> m;
while (m--) {
int x, y;
cin >> x >> y;
vector<pair<int, int>> v2;
for (int i = 0; i < x; i++) {
v2.push_back(v[i]);
}
sort(v2.begin(), v2.end(), comp2);
cout << v2[y - 1].first << '\n';
}
}
|
1227_D1. Optimal Subsequences (Easy Version)
|
This is the easier version of the problem. In this version 1 β€ n, m β€ 100. You can hack this problem only if you solve and lock both problems.
You are given a sequence of integers a=[a_1,a_2,...,a_n] of length n. Its subsequence is obtained by removing zero or more elements from the sequence a (they do not necessarily go consecutively). For example, for the sequence a=[11,20,11,33,11,20,11]:
* [11,20,11,33,11,20,11], [11,20,11,33,11,20], [11,11,11,11], [20], [33,20] are subsequences (these are just some of the long list);
* [40], [33,33], [33,20,20], [20,20,11,11] are not subsequences.
Suppose that an additional non-negative integer k (1 β€ k β€ n) is given, then the subsequence is called optimal if:
* it has a length of k and the sum of its elements is the maximum possible among all subsequences of length k;
* and among all subsequences of length k that satisfy the previous item, it is lexicographically minimal.
Recall that the sequence b=[b_1, b_2, ..., b_k] is lexicographically smaller than the sequence c=[c_1, c_2, ..., c_k] if the first element (from the left) in which they differ less in the sequence b than in c. Formally: there exists t (1 β€ t β€ k) such that b_1=c_1, b_2=c_2, ..., b_{t-1}=c_{t-1} and at the same time b_t<c_t. For example:
* [10, 20, 20] lexicographically less than [10, 21, 1],
* [7, 99, 99] is lexicographically less than [10, 21, 1],
* [10, 21, 0] is lexicographically less than [10, 21, 1].
You are given a sequence of a=[a_1,a_2,...,a_n] and m requests, each consisting of two numbers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j). For each query, print the value that is in the index pos_j of the optimal subsequence of the given sequence a for k=k_j.
For example, if n=4, a=[10,20,30,20], k_j=2, then the optimal subsequence is [20,30] β it is the minimum lexicographically among all subsequences of length 2 with the maximum total sum of items. Thus, the answer to the request k_j=2, pos_j=1 is the number 20, and the answer to the request k_j=2, pos_j=2 is the number 30.
Input
The first line contains an integer n (1 β€ n β€ 100) β the length of the sequence a.
The second line contains elements of the sequence a: integer numbers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9).
The third line contains an integer m (1 β€ m β€ 100) β the number of requests.
The following m lines contain pairs of integers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j) β the requests.
Output
Print m integers r_1, r_2, ..., r_m (1 β€ r_j β€ 10^9) one per line: answers to the requests in the order they appear in the input. The value of r_j should be equal to the value contained in the position pos_j of the optimal subsequence for k=k_j.
Examples
Input
3
10 20 10
6
1 1
2 1
2 2
3 1
3 2
3 3
Output
20
10
20
10
20
10
Input
7
1 2 1 3 1 2 1
9
2 1
2 2
3 1
3 2
3 3
1 1
7 1
7 7
7 4
Output
2
3
2
3
2
3
1
1
3
Note
In the first example, for a=[10,20,10] the optimal subsequences are:
* for k=1: [20],
* for k=2: [10,20],
* for k=3: [10,20,10].
|
{
"input": [
"3\n10 20 10\n6\n1 1\n2 1\n2 2\n3 1\n3 2\n3 3\n",
"7\n1 2 1 3 1 2 1\n9\n2 1\n2 2\n3 1\n3 2\n3 3\n1 1\n7 1\n7 7\n7 4\n"
],
"output": [
"20\n10\n20\n10\n20\n10\n",
"2\n3\n2\n3\n2\n3\n1\n1\n3\n"
]
}
|
{
"input": [
"2\n1 10\n3\n2 2\n2 1\n1 1\n",
"2\n3922 3922\n3\n2 2\n2 1\n1 1\n",
"1\n1000000000\n1\n1 1\n",
"1\n1\n3\n1 1\n1 1\n1 1\n",
"5\n3 1 4 1 2\n15\n5 5\n5 4\n5 3\n5 2\n5 1\n4 4\n4 3\n4 2\n4 1\n3 3\n3 2\n3 1\n2 2\n2 1\n1 1\n",
"2\n392222 322\n3\n2 2\n2 1\n1 1\n"
],
"output": [
"10\n1\n10\n",
"3922\n3922\n3922\n",
"1000000000\n",
"1\n1\n1\n",
"2\n1\n4\n1\n3\n2\n4\n1\n3\n2\n4\n3\n4\n3\n4\n",
"322\n392222\n392222\n"
]
}
|
CORRECT
|
python3
|
n = int(input())
a = list(map(int, input().split()))
b = []
for i in range(n):
b.append([a[i], -i])
b.sort(reverse=True)
for i in range(int(input())):
k, pos = map(int, input().split())
ans = 0
z = []
for i in range(k):
ans += b[i][0]
z.append([-b[i][1], b[i][0]])
z.sort()
print(z[pos - 1][1])
|
1227_D1. Optimal Subsequences (Easy Version)
|
This is the easier version of the problem. In this version 1 β€ n, m β€ 100. You can hack this problem only if you solve and lock both problems.
You are given a sequence of integers a=[a_1,a_2,...,a_n] of length n. Its subsequence is obtained by removing zero or more elements from the sequence a (they do not necessarily go consecutively). For example, for the sequence a=[11,20,11,33,11,20,11]:
* [11,20,11,33,11,20,11], [11,20,11,33,11,20], [11,11,11,11], [20], [33,20] are subsequences (these are just some of the long list);
* [40], [33,33], [33,20,20], [20,20,11,11] are not subsequences.
Suppose that an additional non-negative integer k (1 β€ k β€ n) is given, then the subsequence is called optimal if:
* it has a length of k and the sum of its elements is the maximum possible among all subsequences of length k;
* and among all subsequences of length k that satisfy the previous item, it is lexicographically minimal.
Recall that the sequence b=[b_1, b_2, ..., b_k] is lexicographically smaller than the sequence c=[c_1, c_2, ..., c_k] if the first element (from the left) in which they differ less in the sequence b than in c. Formally: there exists t (1 β€ t β€ k) such that b_1=c_1, b_2=c_2, ..., b_{t-1}=c_{t-1} and at the same time b_t<c_t. For example:
* [10, 20, 20] lexicographically less than [10, 21, 1],
* [7, 99, 99] is lexicographically less than [10, 21, 1],
* [10, 21, 0] is lexicographically less than [10, 21, 1].
You are given a sequence of a=[a_1,a_2,...,a_n] and m requests, each consisting of two numbers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j). For each query, print the value that is in the index pos_j of the optimal subsequence of the given sequence a for k=k_j.
For example, if n=4, a=[10,20,30,20], k_j=2, then the optimal subsequence is [20,30] β it is the minimum lexicographically among all subsequences of length 2 with the maximum total sum of items. Thus, the answer to the request k_j=2, pos_j=1 is the number 20, and the answer to the request k_j=2, pos_j=2 is the number 30.
Input
The first line contains an integer n (1 β€ n β€ 100) β the length of the sequence a.
The second line contains elements of the sequence a: integer numbers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9).
The third line contains an integer m (1 β€ m β€ 100) β the number of requests.
The following m lines contain pairs of integers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j) β the requests.
Output
Print m integers r_1, r_2, ..., r_m (1 β€ r_j β€ 10^9) one per line: answers to the requests in the order they appear in the input. The value of r_j should be equal to the value contained in the position pos_j of the optimal subsequence for k=k_j.
Examples
Input
3
10 20 10
6
1 1
2 1
2 2
3 1
3 2
3 3
Output
20
10
20
10
20
10
Input
7
1 2 1 3 1 2 1
9
2 1
2 2
3 1
3 2
3 3
1 1
7 1
7 7
7 4
Output
2
3
2
3
2
3
1
1
3
Note
In the first example, for a=[10,20,10] the optimal subsequences are:
* for k=1: [20],
* for k=2: [10,20],
* for k=3: [10,20,10].
|
{
"input": [
"3\n10 20 10\n6\n1 1\n2 1\n2 2\n3 1\n3 2\n3 3\n",
"7\n1 2 1 3 1 2 1\n9\n2 1\n2 2\n3 1\n3 2\n3 3\n1 1\n7 1\n7 7\n7 4\n"
],
"output": [
"20\n10\n20\n10\n20\n10\n",
"2\n3\n2\n3\n2\n3\n1\n1\n3\n"
]
}
|
{
"input": [
"2\n1 10\n3\n2 2\n2 1\n1 1\n",
"2\n3922 3922\n3\n2 2\n2 1\n1 1\n",
"1\n1000000000\n1\n1 1\n",
"1\n1\n3\n1 1\n1 1\n1 1\n",
"5\n3 1 4 1 2\n15\n5 5\n5 4\n5 3\n5 2\n5 1\n4 4\n4 3\n4 2\n4 1\n3 3\n3 2\n3 1\n2 2\n2 1\n1 1\n",
"2\n392222 322\n3\n2 2\n2 1\n1 1\n"
],
"output": [
"10\n1\n10\n",
"3922\n3922\n3922\n",
"1000000000\n",
"1\n1\n1\n",
"2\n1\n4\n1\n3\n2\n4\n1\n3\n2\n4\n3\n4\n3\n4\n",
"322\n392222\n392222\n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
inline void fastio() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
}
void solve() {
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; ++i) cin >> a[i];
map<int, vector<int> > mp;
for (int i = 0; i < n; ++i) {
mp[a[i]].push_back(i);
}
vector<int> szs(0);
vector<int> ind(0);
for (auto i : mp) {
szs.push_back((int)i.second.size());
ind.push_back((int)i.first);
}
reverse(szs.begin(), szs.end());
reverse(ind.begin(), ind.end());
vector<int> ps = {szs[0]};
for (int i = 1; i < szs.size(); ++i) {
ps.push_back(ps.back() + szs[i]);
}
int m;
cin >> m;
while (m--) {
int k, pos;
cin >> k >> pos;
pos--;
set<int> psps;
int nowind = 0;
int nowel = 0;
for (int i = 0; i < k; ++i) {
psps.insert(mp[ind[nowind]][nowel]);
nowel++;
if (nowel == mp[ind[nowind]].size()) {
nowel = 0;
nowind++;
}
}
vector<int> v;
for (auto i : psps) {
v.push_back(i);
}
cout << a[v[pos]] << "\n";
}
}
signed main() {
fastio();
solve();
}
|
1227_D1. Optimal Subsequences (Easy Version)
|
This is the easier version of the problem. In this version 1 β€ n, m β€ 100. You can hack this problem only if you solve and lock both problems.
You are given a sequence of integers a=[a_1,a_2,...,a_n] of length n. Its subsequence is obtained by removing zero or more elements from the sequence a (they do not necessarily go consecutively). For example, for the sequence a=[11,20,11,33,11,20,11]:
* [11,20,11,33,11,20,11], [11,20,11,33,11,20], [11,11,11,11], [20], [33,20] are subsequences (these are just some of the long list);
* [40], [33,33], [33,20,20], [20,20,11,11] are not subsequences.
Suppose that an additional non-negative integer k (1 β€ k β€ n) is given, then the subsequence is called optimal if:
* it has a length of k and the sum of its elements is the maximum possible among all subsequences of length k;
* and among all subsequences of length k that satisfy the previous item, it is lexicographically minimal.
Recall that the sequence b=[b_1, b_2, ..., b_k] is lexicographically smaller than the sequence c=[c_1, c_2, ..., c_k] if the first element (from the left) in which they differ less in the sequence b than in c. Formally: there exists t (1 β€ t β€ k) such that b_1=c_1, b_2=c_2, ..., b_{t-1}=c_{t-1} and at the same time b_t<c_t. For example:
* [10, 20, 20] lexicographically less than [10, 21, 1],
* [7, 99, 99] is lexicographically less than [10, 21, 1],
* [10, 21, 0] is lexicographically less than [10, 21, 1].
You are given a sequence of a=[a_1,a_2,...,a_n] and m requests, each consisting of two numbers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j). For each query, print the value that is in the index pos_j of the optimal subsequence of the given sequence a for k=k_j.
For example, if n=4, a=[10,20,30,20], k_j=2, then the optimal subsequence is [20,30] β it is the minimum lexicographically among all subsequences of length 2 with the maximum total sum of items. Thus, the answer to the request k_j=2, pos_j=1 is the number 20, and the answer to the request k_j=2, pos_j=2 is the number 30.
Input
The first line contains an integer n (1 β€ n β€ 100) β the length of the sequence a.
The second line contains elements of the sequence a: integer numbers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9).
The third line contains an integer m (1 β€ m β€ 100) β the number of requests.
The following m lines contain pairs of integers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j) β the requests.
Output
Print m integers r_1, r_2, ..., r_m (1 β€ r_j β€ 10^9) one per line: answers to the requests in the order they appear in the input. The value of r_j should be equal to the value contained in the position pos_j of the optimal subsequence for k=k_j.
Examples
Input
3
10 20 10
6
1 1
2 1
2 2
3 1
3 2
3 3
Output
20
10
20
10
20
10
Input
7
1 2 1 3 1 2 1
9
2 1
2 2
3 1
3 2
3 3
1 1
7 1
7 7
7 4
Output
2
3
2
3
2
3
1
1
3
Note
In the first example, for a=[10,20,10] the optimal subsequences are:
* for k=1: [20],
* for k=2: [10,20],
* for k=3: [10,20,10].
|
{
"input": [
"3\n10 20 10\n6\n1 1\n2 1\n2 2\n3 1\n3 2\n3 3\n",
"7\n1 2 1 3 1 2 1\n9\n2 1\n2 2\n3 1\n3 2\n3 3\n1 1\n7 1\n7 7\n7 4\n"
],
"output": [
"20\n10\n20\n10\n20\n10\n",
"2\n3\n2\n3\n2\n3\n1\n1\n3\n"
]
}
|
{
"input": [
"2\n1 10\n3\n2 2\n2 1\n1 1\n",
"2\n3922 3922\n3\n2 2\n2 1\n1 1\n",
"1\n1000000000\n1\n1 1\n",
"1\n1\n3\n1 1\n1 1\n1 1\n",
"5\n3 1 4 1 2\n15\n5 5\n5 4\n5 3\n5 2\n5 1\n4 4\n4 3\n4 2\n4 1\n3 3\n3 2\n3 1\n2 2\n2 1\n1 1\n",
"2\n392222 322\n3\n2 2\n2 1\n1 1\n"
],
"output": [
"10\n1\n10\n",
"3922\n3922\n3922\n",
"1000000000\n",
"1\n1\n1\n",
"2\n1\n4\n1\n3\n2\n4\n1\n3\n2\n4\n3\n4\n3\n4\n",
"322\n392222\n392222\n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
#pragma GCC optimize("-Ofast")
using namespace std;
bool compare(pair<long long, long long> a, pair<long long, long long> b) {
if (a.first != b.first) {
return a.first > b.first;
} else {
return a.second < b.second;
}
}
int32_t main() {
long long n;
cin >> n;
long long arr[n];
vector<pair<long long, long long> > v;
for (long long i = 0; i < n; i++) {
cin >> arr[i];
v.push_back({arr[i], i});
}
sort(v.begin(), v.end(), compare);
long long m;
cin >> m;
while (m--) {
long long k, pos;
cin >> k >> pos;
vector<pair<long long, long long> > cur;
for (long long i = 0; i < k; i++) {
cur.push_back({v[i].second, v[i].first});
}
sort(cur.begin(), cur.end());
cout << cur[pos - 1].second << '\n';
}
}
|
1227_D1. Optimal Subsequences (Easy Version)
|
This is the easier version of the problem. In this version 1 β€ n, m β€ 100. You can hack this problem only if you solve and lock both problems.
You are given a sequence of integers a=[a_1,a_2,...,a_n] of length n. Its subsequence is obtained by removing zero or more elements from the sequence a (they do not necessarily go consecutively). For example, for the sequence a=[11,20,11,33,11,20,11]:
* [11,20,11,33,11,20,11], [11,20,11,33,11,20], [11,11,11,11], [20], [33,20] are subsequences (these are just some of the long list);
* [40], [33,33], [33,20,20], [20,20,11,11] are not subsequences.
Suppose that an additional non-negative integer k (1 β€ k β€ n) is given, then the subsequence is called optimal if:
* it has a length of k and the sum of its elements is the maximum possible among all subsequences of length k;
* and among all subsequences of length k that satisfy the previous item, it is lexicographically minimal.
Recall that the sequence b=[b_1, b_2, ..., b_k] is lexicographically smaller than the sequence c=[c_1, c_2, ..., c_k] if the first element (from the left) in which they differ less in the sequence b than in c. Formally: there exists t (1 β€ t β€ k) such that b_1=c_1, b_2=c_2, ..., b_{t-1}=c_{t-1} and at the same time b_t<c_t. For example:
* [10, 20, 20] lexicographically less than [10, 21, 1],
* [7, 99, 99] is lexicographically less than [10, 21, 1],
* [10, 21, 0] is lexicographically less than [10, 21, 1].
You are given a sequence of a=[a_1,a_2,...,a_n] and m requests, each consisting of two numbers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j). For each query, print the value that is in the index pos_j of the optimal subsequence of the given sequence a for k=k_j.
For example, if n=4, a=[10,20,30,20], k_j=2, then the optimal subsequence is [20,30] β it is the minimum lexicographically among all subsequences of length 2 with the maximum total sum of items. Thus, the answer to the request k_j=2, pos_j=1 is the number 20, and the answer to the request k_j=2, pos_j=2 is the number 30.
Input
The first line contains an integer n (1 β€ n β€ 100) β the length of the sequence a.
The second line contains elements of the sequence a: integer numbers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9).
The third line contains an integer m (1 β€ m β€ 100) β the number of requests.
The following m lines contain pairs of integers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j) β the requests.
Output
Print m integers r_1, r_2, ..., r_m (1 β€ r_j β€ 10^9) one per line: answers to the requests in the order they appear in the input. The value of r_j should be equal to the value contained in the position pos_j of the optimal subsequence for k=k_j.
Examples
Input
3
10 20 10
6
1 1
2 1
2 2
3 1
3 2
3 3
Output
20
10
20
10
20
10
Input
7
1 2 1 3 1 2 1
9
2 1
2 2
3 1
3 2
3 3
1 1
7 1
7 7
7 4
Output
2
3
2
3
2
3
1
1
3
Note
In the first example, for a=[10,20,10] the optimal subsequences are:
* for k=1: [20],
* for k=2: [10,20],
* for k=3: [10,20,10].
|
{
"input": [
"3\n10 20 10\n6\n1 1\n2 1\n2 2\n3 1\n3 2\n3 3\n",
"7\n1 2 1 3 1 2 1\n9\n2 1\n2 2\n3 1\n3 2\n3 3\n1 1\n7 1\n7 7\n7 4\n"
],
"output": [
"20\n10\n20\n10\n20\n10\n",
"2\n3\n2\n3\n2\n3\n1\n1\n3\n"
]
}
|
{
"input": [
"2\n1 10\n3\n2 2\n2 1\n1 1\n",
"2\n3922 3922\n3\n2 2\n2 1\n1 1\n",
"1\n1000000000\n1\n1 1\n",
"1\n1\n3\n1 1\n1 1\n1 1\n",
"5\n3 1 4 1 2\n15\n5 5\n5 4\n5 3\n5 2\n5 1\n4 4\n4 3\n4 2\n4 1\n3 3\n3 2\n3 1\n2 2\n2 1\n1 1\n",
"2\n392222 322\n3\n2 2\n2 1\n1 1\n"
],
"output": [
"10\n1\n10\n",
"3922\n3922\n3922\n",
"1000000000\n",
"1\n1\n1\n",
"2\n1\n4\n1\n3\n2\n4\n1\n3\n2\n4\n3\n4\n3\n4\n",
"322\n392222\n392222\n"
]
}
|
CORRECT
|
python3
|
n = int(input())
data = input().split()
data1 = []
for i in range(n):
data1.append((int(data[i]), i))
data1.sort(key=lambda x: (x[0], -x[1]))
for i in range(int(input())):
k, pos = map(int, input().split())
temp = sorted(data1[len(data1) - k:], key=lambda x: x[1])
print(temp[pos - 1][0])
|
1227_D1. Optimal Subsequences (Easy Version)
|
This is the easier version of the problem. In this version 1 β€ n, m β€ 100. You can hack this problem only if you solve and lock both problems.
You are given a sequence of integers a=[a_1,a_2,...,a_n] of length n. Its subsequence is obtained by removing zero or more elements from the sequence a (they do not necessarily go consecutively). For example, for the sequence a=[11,20,11,33,11,20,11]:
* [11,20,11,33,11,20,11], [11,20,11,33,11,20], [11,11,11,11], [20], [33,20] are subsequences (these are just some of the long list);
* [40], [33,33], [33,20,20], [20,20,11,11] are not subsequences.
Suppose that an additional non-negative integer k (1 β€ k β€ n) is given, then the subsequence is called optimal if:
* it has a length of k and the sum of its elements is the maximum possible among all subsequences of length k;
* and among all subsequences of length k that satisfy the previous item, it is lexicographically minimal.
Recall that the sequence b=[b_1, b_2, ..., b_k] is lexicographically smaller than the sequence c=[c_1, c_2, ..., c_k] if the first element (from the left) in which they differ less in the sequence b than in c. Formally: there exists t (1 β€ t β€ k) such that b_1=c_1, b_2=c_2, ..., b_{t-1}=c_{t-1} and at the same time b_t<c_t. For example:
* [10, 20, 20] lexicographically less than [10, 21, 1],
* [7, 99, 99] is lexicographically less than [10, 21, 1],
* [10, 21, 0] is lexicographically less than [10, 21, 1].
You are given a sequence of a=[a_1,a_2,...,a_n] and m requests, each consisting of two numbers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j). For each query, print the value that is in the index pos_j of the optimal subsequence of the given sequence a for k=k_j.
For example, if n=4, a=[10,20,30,20], k_j=2, then the optimal subsequence is [20,30] β it is the minimum lexicographically among all subsequences of length 2 with the maximum total sum of items. Thus, the answer to the request k_j=2, pos_j=1 is the number 20, and the answer to the request k_j=2, pos_j=2 is the number 30.
Input
The first line contains an integer n (1 β€ n β€ 100) β the length of the sequence a.
The second line contains elements of the sequence a: integer numbers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9).
The third line contains an integer m (1 β€ m β€ 100) β the number of requests.
The following m lines contain pairs of integers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j) β the requests.
Output
Print m integers r_1, r_2, ..., r_m (1 β€ r_j β€ 10^9) one per line: answers to the requests in the order they appear in the input. The value of r_j should be equal to the value contained in the position pos_j of the optimal subsequence for k=k_j.
Examples
Input
3
10 20 10
6
1 1
2 1
2 2
3 1
3 2
3 3
Output
20
10
20
10
20
10
Input
7
1 2 1 3 1 2 1
9
2 1
2 2
3 1
3 2
3 3
1 1
7 1
7 7
7 4
Output
2
3
2
3
2
3
1
1
3
Note
In the first example, for a=[10,20,10] the optimal subsequences are:
* for k=1: [20],
* for k=2: [10,20],
* for k=3: [10,20,10].
|
{
"input": [
"3\n10 20 10\n6\n1 1\n2 1\n2 2\n3 1\n3 2\n3 3\n",
"7\n1 2 1 3 1 2 1\n9\n2 1\n2 2\n3 1\n3 2\n3 3\n1 1\n7 1\n7 7\n7 4\n"
],
"output": [
"20\n10\n20\n10\n20\n10\n",
"2\n3\n2\n3\n2\n3\n1\n1\n3\n"
]
}
|
{
"input": [
"2\n1 10\n3\n2 2\n2 1\n1 1\n",
"2\n3922 3922\n3\n2 2\n2 1\n1 1\n",
"1\n1000000000\n1\n1 1\n",
"1\n1\n3\n1 1\n1 1\n1 1\n",
"5\n3 1 4 1 2\n15\n5 5\n5 4\n5 3\n5 2\n5 1\n4 4\n4 3\n4 2\n4 1\n3 3\n3 2\n3 1\n2 2\n2 1\n1 1\n",
"2\n392222 322\n3\n2 2\n2 1\n1 1\n"
],
"output": [
"10\n1\n10\n",
"3922\n3922\n3922\n",
"1000000000\n",
"1\n1\n1\n",
"2\n1\n4\n1\n3\n2\n4\n1\n3\n2\n4\n3\n4\n3\n4\n",
"322\n392222\n392222\n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
vector<pair<int, int>> v(n);
for (int i = 0; i < n; i++) {
cin >> v[i].first;
v[i].second = i + 1;
v[i].first *= -1;
}
sort(v.begin(), v.end());
int t;
cin >> t;
for (; t; t--) {
int x, y;
cin >> x >> y;
vector<pair<int, int>> z;
for (int i = 0; i < x; i++) {
z.push_back({v[i].second, v[i].first});
}
sort(z.begin(), z.end());
cout << -z[y - 1].second << "\n";
}
}
|
1227_D1. Optimal Subsequences (Easy Version)
|
This is the easier version of the problem. In this version 1 β€ n, m β€ 100. You can hack this problem only if you solve and lock both problems.
You are given a sequence of integers a=[a_1,a_2,...,a_n] of length n. Its subsequence is obtained by removing zero or more elements from the sequence a (they do not necessarily go consecutively). For example, for the sequence a=[11,20,11,33,11,20,11]:
* [11,20,11,33,11,20,11], [11,20,11,33,11,20], [11,11,11,11], [20], [33,20] are subsequences (these are just some of the long list);
* [40], [33,33], [33,20,20], [20,20,11,11] are not subsequences.
Suppose that an additional non-negative integer k (1 β€ k β€ n) is given, then the subsequence is called optimal if:
* it has a length of k and the sum of its elements is the maximum possible among all subsequences of length k;
* and among all subsequences of length k that satisfy the previous item, it is lexicographically minimal.
Recall that the sequence b=[b_1, b_2, ..., b_k] is lexicographically smaller than the sequence c=[c_1, c_2, ..., c_k] if the first element (from the left) in which they differ less in the sequence b than in c. Formally: there exists t (1 β€ t β€ k) such that b_1=c_1, b_2=c_2, ..., b_{t-1}=c_{t-1} and at the same time b_t<c_t. For example:
* [10, 20, 20] lexicographically less than [10, 21, 1],
* [7, 99, 99] is lexicographically less than [10, 21, 1],
* [10, 21, 0] is lexicographically less than [10, 21, 1].
You are given a sequence of a=[a_1,a_2,...,a_n] and m requests, each consisting of two numbers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j). For each query, print the value that is in the index pos_j of the optimal subsequence of the given sequence a for k=k_j.
For example, if n=4, a=[10,20,30,20], k_j=2, then the optimal subsequence is [20,30] β it is the minimum lexicographically among all subsequences of length 2 with the maximum total sum of items. Thus, the answer to the request k_j=2, pos_j=1 is the number 20, and the answer to the request k_j=2, pos_j=2 is the number 30.
Input
The first line contains an integer n (1 β€ n β€ 100) β the length of the sequence a.
The second line contains elements of the sequence a: integer numbers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9).
The third line contains an integer m (1 β€ m β€ 100) β the number of requests.
The following m lines contain pairs of integers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j) β the requests.
Output
Print m integers r_1, r_2, ..., r_m (1 β€ r_j β€ 10^9) one per line: answers to the requests in the order they appear in the input. The value of r_j should be equal to the value contained in the position pos_j of the optimal subsequence for k=k_j.
Examples
Input
3
10 20 10
6
1 1
2 1
2 2
3 1
3 2
3 3
Output
20
10
20
10
20
10
Input
7
1 2 1 3 1 2 1
9
2 1
2 2
3 1
3 2
3 3
1 1
7 1
7 7
7 4
Output
2
3
2
3
2
3
1
1
3
Note
In the first example, for a=[10,20,10] the optimal subsequences are:
* for k=1: [20],
* for k=2: [10,20],
* for k=3: [10,20,10].
|
{
"input": [
"3\n10 20 10\n6\n1 1\n2 1\n2 2\n3 1\n3 2\n3 3\n",
"7\n1 2 1 3 1 2 1\n9\n2 1\n2 2\n3 1\n3 2\n3 3\n1 1\n7 1\n7 7\n7 4\n"
],
"output": [
"20\n10\n20\n10\n20\n10\n",
"2\n3\n2\n3\n2\n3\n1\n1\n3\n"
]
}
|
{
"input": [
"2\n1 10\n3\n2 2\n2 1\n1 1\n",
"2\n3922 3922\n3\n2 2\n2 1\n1 1\n",
"1\n1000000000\n1\n1 1\n",
"1\n1\n3\n1 1\n1 1\n1 1\n",
"5\n3 1 4 1 2\n15\n5 5\n5 4\n5 3\n5 2\n5 1\n4 4\n4 3\n4 2\n4 1\n3 3\n3 2\n3 1\n2 2\n2 1\n1 1\n",
"2\n392222 322\n3\n2 2\n2 1\n1 1\n"
],
"output": [
"10\n1\n10\n",
"3922\n3922\n3922\n",
"1000000000\n",
"1\n1\n1\n",
"2\n1\n4\n1\n3\n2\n4\n1\n3\n2\n4\n3\n4\n3\n4\n",
"322\n392222\n392222\n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
int dv(pair<int, int> p1, pair<int, int> p2) {
if (p1.first < p2.first) return 0;
if (p1.first > p2.first) return 1;
if (p1.second > p2.second) return 0;
return 1;
}
int main() {
int t, n, m, p, k, a, b, c, l, r;
cin >> n;
int A[n];
for (int i = 0; i < n; i++) cin >> A[i];
vector<pair<int, int> > v;
for (int i = 0; i < n; i++) v.push_back({A[i], i});
sort(v.begin(), v.end(), dv);
cin >> m;
for (int i = 0; i < m; i++) {
vector<int> pos;
cin >> k >> p;
for (int i = 0; i < k; i++) {
pos.push_back(v[i].second);
}
sort(pos.begin(), pos.end());
cout << A[pos[p - 1]] << endl;
}
}
|
1227_D1. Optimal Subsequences (Easy Version)
|
This is the easier version of the problem. In this version 1 β€ n, m β€ 100. You can hack this problem only if you solve and lock both problems.
You are given a sequence of integers a=[a_1,a_2,...,a_n] of length n. Its subsequence is obtained by removing zero or more elements from the sequence a (they do not necessarily go consecutively). For example, for the sequence a=[11,20,11,33,11,20,11]:
* [11,20,11,33,11,20,11], [11,20,11,33,11,20], [11,11,11,11], [20], [33,20] are subsequences (these are just some of the long list);
* [40], [33,33], [33,20,20], [20,20,11,11] are not subsequences.
Suppose that an additional non-negative integer k (1 β€ k β€ n) is given, then the subsequence is called optimal if:
* it has a length of k and the sum of its elements is the maximum possible among all subsequences of length k;
* and among all subsequences of length k that satisfy the previous item, it is lexicographically minimal.
Recall that the sequence b=[b_1, b_2, ..., b_k] is lexicographically smaller than the sequence c=[c_1, c_2, ..., c_k] if the first element (from the left) in which they differ less in the sequence b than in c. Formally: there exists t (1 β€ t β€ k) such that b_1=c_1, b_2=c_2, ..., b_{t-1}=c_{t-1} and at the same time b_t<c_t. For example:
* [10, 20, 20] lexicographically less than [10, 21, 1],
* [7, 99, 99] is lexicographically less than [10, 21, 1],
* [10, 21, 0] is lexicographically less than [10, 21, 1].
You are given a sequence of a=[a_1,a_2,...,a_n] and m requests, each consisting of two numbers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j). For each query, print the value that is in the index pos_j of the optimal subsequence of the given sequence a for k=k_j.
For example, if n=4, a=[10,20,30,20], k_j=2, then the optimal subsequence is [20,30] β it is the minimum lexicographically among all subsequences of length 2 with the maximum total sum of items. Thus, the answer to the request k_j=2, pos_j=1 is the number 20, and the answer to the request k_j=2, pos_j=2 is the number 30.
Input
The first line contains an integer n (1 β€ n β€ 100) β the length of the sequence a.
The second line contains elements of the sequence a: integer numbers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9).
The third line contains an integer m (1 β€ m β€ 100) β the number of requests.
The following m lines contain pairs of integers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j) β the requests.
Output
Print m integers r_1, r_2, ..., r_m (1 β€ r_j β€ 10^9) one per line: answers to the requests in the order they appear in the input. The value of r_j should be equal to the value contained in the position pos_j of the optimal subsequence for k=k_j.
Examples
Input
3
10 20 10
6
1 1
2 1
2 2
3 1
3 2
3 3
Output
20
10
20
10
20
10
Input
7
1 2 1 3 1 2 1
9
2 1
2 2
3 1
3 2
3 3
1 1
7 1
7 7
7 4
Output
2
3
2
3
2
3
1
1
3
Note
In the first example, for a=[10,20,10] the optimal subsequences are:
* for k=1: [20],
* for k=2: [10,20],
* for k=3: [10,20,10].
|
{
"input": [
"3\n10 20 10\n6\n1 1\n2 1\n2 2\n3 1\n3 2\n3 3\n",
"7\n1 2 1 3 1 2 1\n9\n2 1\n2 2\n3 1\n3 2\n3 3\n1 1\n7 1\n7 7\n7 4\n"
],
"output": [
"20\n10\n20\n10\n20\n10\n",
"2\n3\n2\n3\n2\n3\n1\n1\n3\n"
]
}
|
{
"input": [
"2\n1 10\n3\n2 2\n2 1\n1 1\n",
"2\n3922 3922\n3\n2 2\n2 1\n1 1\n",
"1\n1000000000\n1\n1 1\n",
"1\n1\n3\n1 1\n1 1\n1 1\n",
"5\n3 1 4 1 2\n15\n5 5\n5 4\n5 3\n5 2\n5 1\n4 4\n4 3\n4 2\n4 1\n3 3\n3 2\n3 1\n2 2\n2 1\n1 1\n",
"2\n392222 322\n3\n2 2\n2 1\n1 1\n"
],
"output": [
"10\n1\n10\n",
"3922\n3922\n3922\n",
"1000000000\n",
"1\n1\n1\n",
"2\n1\n4\n1\n3\n2\n4\n1\n3\n2\n4\n3\n4\n3\n4\n",
"322\n392222\n392222\n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
vector<vector<long long>> adj;
map<long, bool> vis, viss;
vector<long long> rnk, parent, sz;
int spf[1000000 + 1];
long long binpow(long long a, long long b, long long m) {
a %= m;
long long res = 1;
while (b > 0) {
if (b & 1) res = res * a % m;
a = a * a % m;
b >>= 1;
}
return res;
}
vector<long long> v;
bool yg(long long a, long long b) {
return (v[a] > v[b] || ((v[a] == v[b]) && a < b));
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(nullptr);
long long t, temp;
long long n, m;
cin >> n;
vector<long long> indx;
for (long long i = 0; i < n; i++) {
cin >> temp;
v.push_back(temp);
indx.push_back(i);
}
sort(indx.begin(), indx.end(), yg);
cin >> m;
while (m--) {
long long k, pos;
cin >> k >> pos;
vector<long long> arr;
for (long long i = 0; i < k; i++) arr.push_back(indx[i]);
sort(arr.begin(), arr.end());
cout << v[arr[pos - 1]] << "\n";
}
}
|
1227_D1. Optimal Subsequences (Easy Version)
|
This is the easier version of the problem. In this version 1 β€ n, m β€ 100. You can hack this problem only if you solve and lock both problems.
You are given a sequence of integers a=[a_1,a_2,...,a_n] of length n. Its subsequence is obtained by removing zero or more elements from the sequence a (they do not necessarily go consecutively). For example, for the sequence a=[11,20,11,33,11,20,11]:
* [11,20,11,33,11,20,11], [11,20,11,33,11,20], [11,11,11,11], [20], [33,20] are subsequences (these are just some of the long list);
* [40], [33,33], [33,20,20], [20,20,11,11] are not subsequences.
Suppose that an additional non-negative integer k (1 β€ k β€ n) is given, then the subsequence is called optimal if:
* it has a length of k and the sum of its elements is the maximum possible among all subsequences of length k;
* and among all subsequences of length k that satisfy the previous item, it is lexicographically minimal.
Recall that the sequence b=[b_1, b_2, ..., b_k] is lexicographically smaller than the sequence c=[c_1, c_2, ..., c_k] if the first element (from the left) in which they differ less in the sequence b than in c. Formally: there exists t (1 β€ t β€ k) such that b_1=c_1, b_2=c_2, ..., b_{t-1}=c_{t-1} and at the same time b_t<c_t. For example:
* [10, 20, 20] lexicographically less than [10, 21, 1],
* [7, 99, 99] is lexicographically less than [10, 21, 1],
* [10, 21, 0] is lexicographically less than [10, 21, 1].
You are given a sequence of a=[a_1,a_2,...,a_n] and m requests, each consisting of two numbers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j). For each query, print the value that is in the index pos_j of the optimal subsequence of the given sequence a for k=k_j.
For example, if n=4, a=[10,20,30,20], k_j=2, then the optimal subsequence is [20,30] β it is the minimum lexicographically among all subsequences of length 2 with the maximum total sum of items. Thus, the answer to the request k_j=2, pos_j=1 is the number 20, and the answer to the request k_j=2, pos_j=2 is the number 30.
Input
The first line contains an integer n (1 β€ n β€ 100) β the length of the sequence a.
The second line contains elements of the sequence a: integer numbers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9).
The third line contains an integer m (1 β€ m β€ 100) β the number of requests.
The following m lines contain pairs of integers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j) β the requests.
Output
Print m integers r_1, r_2, ..., r_m (1 β€ r_j β€ 10^9) one per line: answers to the requests in the order they appear in the input. The value of r_j should be equal to the value contained in the position pos_j of the optimal subsequence for k=k_j.
Examples
Input
3
10 20 10
6
1 1
2 1
2 2
3 1
3 2
3 3
Output
20
10
20
10
20
10
Input
7
1 2 1 3 1 2 1
9
2 1
2 2
3 1
3 2
3 3
1 1
7 1
7 7
7 4
Output
2
3
2
3
2
3
1
1
3
Note
In the first example, for a=[10,20,10] the optimal subsequences are:
* for k=1: [20],
* for k=2: [10,20],
* for k=3: [10,20,10].
|
{
"input": [
"3\n10 20 10\n6\n1 1\n2 1\n2 2\n3 1\n3 2\n3 3\n",
"7\n1 2 1 3 1 2 1\n9\n2 1\n2 2\n3 1\n3 2\n3 3\n1 1\n7 1\n7 7\n7 4\n"
],
"output": [
"20\n10\n20\n10\n20\n10\n",
"2\n3\n2\n3\n2\n3\n1\n1\n3\n"
]
}
|
{
"input": [
"2\n1 10\n3\n2 2\n2 1\n1 1\n",
"2\n3922 3922\n3\n2 2\n2 1\n1 1\n",
"1\n1000000000\n1\n1 1\n",
"1\n1\n3\n1 1\n1 1\n1 1\n",
"5\n3 1 4 1 2\n15\n5 5\n5 4\n5 3\n5 2\n5 1\n4 4\n4 3\n4 2\n4 1\n3 3\n3 2\n3 1\n2 2\n2 1\n1 1\n",
"2\n392222 322\n3\n2 2\n2 1\n1 1\n"
],
"output": [
"10\n1\n10\n",
"3922\n3922\n3922\n",
"1000000000\n",
"1\n1\n1\n",
"2\n1\n4\n1\n3\n2\n4\n1\n3\n2\n4\n3\n4\n3\n4\n",
"322\n392222\n392222\n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
bool kmp(int a, int b) { return a < b; }
int main() {
int n, m;
int x = 0;
int y = 0;
int z = 0;
cin >> n;
int a[n];
int d[n][n];
int c[n];
int an[100003];
int k = 0;
int p = 0;
for (int i = 0; i < n; i++) {
cin >> a[i];
c[i] = 1;
}
cin >> m;
int b[m];
int f[m];
for (int i = 0; i < m; i++) {
cin >> b[i] >> f[i];
}
for (int i = 0; i < n; i++) {
p = 0;
k = 0;
for (int j = 0; j < n; j++) {
if (a[j] > p && c[j] == 1) {
p = a[j];
x = j;
}
}
c[x] = 0;
for (int j = 0; j < x; j++) {
if (c[j] == 0) {
k++;
}
}
for (int j = 0; j < i + 1; j++) {
if (j < k) {
d[i][j] = d[i - 1][j];
} else if (j == k) {
d[i][j] = p;
} else {
d[i][j] = d[i - 1][j - 1];
}
}
}
for (int i = 0; i < m; i++) {
cout << d[b[i] - 1][f[i] - 1] << endl;
}
return 0;
}
|
1227_D1. Optimal Subsequences (Easy Version)
|
This is the easier version of the problem. In this version 1 β€ n, m β€ 100. You can hack this problem only if you solve and lock both problems.
You are given a sequence of integers a=[a_1,a_2,...,a_n] of length n. Its subsequence is obtained by removing zero or more elements from the sequence a (they do not necessarily go consecutively). For example, for the sequence a=[11,20,11,33,11,20,11]:
* [11,20,11,33,11,20,11], [11,20,11,33,11,20], [11,11,11,11], [20], [33,20] are subsequences (these are just some of the long list);
* [40], [33,33], [33,20,20], [20,20,11,11] are not subsequences.
Suppose that an additional non-negative integer k (1 β€ k β€ n) is given, then the subsequence is called optimal if:
* it has a length of k and the sum of its elements is the maximum possible among all subsequences of length k;
* and among all subsequences of length k that satisfy the previous item, it is lexicographically minimal.
Recall that the sequence b=[b_1, b_2, ..., b_k] is lexicographically smaller than the sequence c=[c_1, c_2, ..., c_k] if the first element (from the left) in which they differ less in the sequence b than in c. Formally: there exists t (1 β€ t β€ k) such that b_1=c_1, b_2=c_2, ..., b_{t-1}=c_{t-1} and at the same time b_t<c_t. For example:
* [10, 20, 20] lexicographically less than [10, 21, 1],
* [7, 99, 99] is lexicographically less than [10, 21, 1],
* [10, 21, 0] is lexicographically less than [10, 21, 1].
You are given a sequence of a=[a_1,a_2,...,a_n] and m requests, each consisting of two numbers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j). For each query, print the value that is in the index pos_j of the optimal subsequence of the given sequence a for k=k_j.
For example, if n=4, a=[10,20,30,20], k_j=2, then the optimal subsequence is [20,30] β it is the minimum lexicographically among all subsequences of length 2 with the maximum total sum of items. Thus, the answer to the request k_j=2, pos_j=1 is the number 20, and the answer to the request k_j=2, pos_j=2 is the number 30.
Input
The first line contains an integer n (1 β€ n β€ 100) β the length of the sequence a.
The second line contains elements of the sequence a: integer numbers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9).
The third line contains an integer m (1 β€ m β€ 100) β the number of requests.
The following m lines contain pairs of integers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j) β the requests.
Output
Print m integers r_1, r_2, ..., r_m (1 β€ r_j β€ 10^9) one per line: answers to the requests in the order they appear in the input. The value of r_j should be equal to the value contained in the position pos_j of the optimal subsequence for k=k_j.
Examples
Input
3
10 20 10
6
1 1
2 1
2 2
3 1
3 2
3 3
Output
20
10
20
10
20
10
Input
7
1 2 1 3 1 2 1
9
2 1
2 2
3 1
3 2
3 3
1 1
7 1
7 7
7 4
Output
2
3
2
3
2
3
1
1
3
Note
In the first example, for a=[10,20,10] the optimal subsequences are:
* for k=1: [20],
* for k=2: [10,20],
* for k=3: [10,20,10].
|
{
"input": [
"3\n10 20 10\n6\n1 1\n2 1\n2 2\n3 1\n3 2\n3 3\n",
"7\n1 2 1 3 1 2 1\n9\n2 1\n2 2\n3 1\n3 2\n3 3\n1 1\n7 1\n7 7\n7 4\n"
],
"output": [
"20\n10\n20\n10\n20\n10\n",
"2\n3\n2\n3\n2\n3\n1\n1\n3\n"
]
}
|
{
"input": [
"2\n1 10\n3\n2 2\n2 1\n1 1\n",
"2\n3922 3922\n3\n2 2\n2 1\n1 1\n",
"1\n1000000000\n1\n1 1\n",
"1\n1\n3\n1 1\n1 1\n1 1\n",
"5\n3 1 4 1 2\n15\n5 5\n5 4\n5 3\n5 2\n5 1\n4 4\n4 3\n4 2\n4 1\n3 3\n3 2\n3 1\n2 2\n2 1\n1 1\n",
"2\n392222 322\n3\n2 2\n2 1\n1 1\n"
],
"output": [
"10\n1\n10\n",
"3922\n3922\n3922\n",
"1000000000\n",
"1\n1\n1\n",
"2\n1\n4\n1\n3\n2\n4\n1\n3\n2\n4\n3\n4\n3\n4\n",
"322\n392222\n392222\n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int getrnd(int l, int r) { return uniform_int_distribution<int>(l, r)(rng); }
template <typename T1, typename T2>
bool relax(T1& a, const T2& b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
template <typename T1, typename T2>
bool strain(T1& a, const T2& b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
void solve() {
int n;
cin >> n;
vector<int> a(n), b(n);
for (int i = 0; i < n; ++i) cin >> a[i], b[i] = a[i];
sort(b.rbegin(), b.rend());
int q;
cin >> q;
while (q--) {
int k, pos;
cin >> k >> pos;
--pos;
vector<int> was(n, 0);
for (int i = 0; i < n; ++i) {
for (int j = 0; j < k; ++j) {
if (a[i] == b[j]) was[i] = 1;
}
}
int c = 1;
for (int i = k - 2; i >= 0; --i) {
if (b[i] == b[i + 1])
++c;
else
break;
}
vector<int> temp;
for (int i = 0; i < n; ++i) {
if (a[i] == b[k - 1]) {
if (c > 0 && was[i]) {
temp.emplace_back(a[i]);
--c;
}
} else if (was[i]) {
temp.emplace_back(a[i]);
}
}
cout << temp[pos] << '\n';
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(nullptr);
cout.tie(nullptr);
srand(time(0));
int t = 1;
while (t--) solve();
return 0;
}
|
1227_D1. Optimal Subsequences (Easy Version)
|
This is the easier version of the problem. In this version 1 β€ n, m β€ 100. You can hack this problem only if you solve and lock both problems.
You are given a sequence of integers a=[a_1,a_2,...,a_n] of length n. Its subsequence is obtained by removing zero or more elements from the sequence a (they do not necessarily go consecutively). For example, for the sequence a=[11,20,11,33,11,20,11]:
* [11,20,11,33,11,20,11], [11,20,11,33,11,20], [11,11,11,11], [20], [33,20] are subsequences (these are just some of the long list);
* [40], [33,33], [33,20,20], [20,20,11,11] are not subsequences.
Suppose that an additional non-negative integer k (1 β€ k β€ n) is given, then the subsequence is called optimal if:
* it has a length of k and the sum of its elements is the maximum possible among all subsequences of length k;
* and among all subsequences of length k that satisfy the previous item, it is lexicographically minimal.
Recall that the sequence b=[b_1, b_2, ..., b_k] is lexicographically smaller than the sequence c=[c_1, c_2, ..., c_k] if the first element (from the left) in which they differ less in the sequence b than in c. Formally: there exists t (1 β€ t β€ k) such that b_1=c_1, b_2=c_2, ..., b_{t-1}=c_{t-1} and at the same time b_t<c_t. For example:
* [10, 20, 20] lexicographically less than [10, 21, 1],
* [7, 99, 99] is lexicographically less than [10, 21, 1],
* [10, 21, 0] is lexicographically less than [10, 21, 1].
You are given a sequence of a=[a_1,a_2,...,a_n] and m requests, each consisting of two numbers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j). For each query, print the value that is in the index pos_j of the optimal subsequence of the given sequence a for k=k_j.
For example, if n=4, a=[10,20,30,20], k_j=2, then the optimal subsequence is [20,30] β it is the minimum lexicographically among all subsequences of length 2 with the maximum total sum of items. Thus, the answer to the request k_j=2, pos_j=1 is the number 20, and the answer to the request k_j=2, pos_j=2 is the number 30.
Input
The first line contains an integer n (1 β€ n β€ 100) β the length of the sequence a.
The second line contains elements of the sequence a: integer numbers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9).
The third line contains an integer m (1 β€ m β€ 100) β the number of requests.
The following m lines contain pairs of integers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j) β the requests.
Output
Print m integers r_1, r_2, ..., r_m (1 β€ r_j β€ 10^9) one per line: answers to the requests in the order they appear in the input. The value of r_j should be equal to the value contained in the position pos_j of the optimal subsequence for k=k_j.
Examples
Input
3
10 20 10
6
1 1
2 1
2 2
3 1
3 2
3 3
Output
20
10
20
10
20
10
Input
7
1 2 1 3 1 2 1
9
2 1
2 2
3 1
3 2
3 3
1 1
7 1
7 7
7 4
Output
2
3
2
3
2
3
1
1
3
Note
In the first example, for a=[10,20,10] the optimal subsequences are:
* for k=1: [20],
* for k=2: [10,20],
* for k=3: [10,20,10].
|
{
"input": [
"3\n10 20 10\n6\n1 1\n2 1\n2 2\n3 1\n3 2\n3 3\n",
"7\n1 2 1 3 1 2 1\n9\n2 1\n2 2\n3 1\n3 2\n3 3\n1 1\n7 1\n7 7\n7 4\n"
],
"output": [
"20\n10\n20\n10\n20\n10\n",
"2\n3\n2\n3\n2\n3\n1\n1\n3\n"
]
}
|
{
"input": [
"2\n1 10\n3\n2 2\n2 1\n1 1\n",
"2\n3922 3922\n3\n2 2\n2 1\n1 1\n",
"1\n1000000000\n1\n1 1\n",
"1\n1\n3\n1 1\n1 1\n1 1\n",
"5\n3 1 4 1 2\n15\n5 5\n5 4\n5 3\n5 2\n5 1\n4 4\n4 3\n4 2\n4 1\n3 3\n3 2\n3 1\n2 2\n2 1\n1 1\n",
"2\n392222 322\n3\n2 2\n2 1\n1 1\n"
],
"output": [
"10\n1\n10\n",
"3922\n3922\n3922\n",
"1000000000\n",
"1\n1\n1\n",
"2\n1\n4\n1\n3\n2\n4\n1\n3\n2\n4\n3\n4\n3\n4\n",
"322\n392222\n392222\n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
const int maxn = 1e5 + 5;
using namespace std;
int n, q;
struct node {
int x, y, z;
} a[maxn], b[maxn], c[maxn];
int tot = 1, ans[maxn];
bool tmp(const node &u, const node &v) {
if (u.x != v.x) return u.x > v.x;
return u.y < v.y;
}
bool cc(const node &u, const node &v) {
if (u.x != v.x) return u.x < v.x;
return u.y < v.y;
}
bool cmp(const node &u, const node &v) { return u.y < v.y; }
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%d", &a[i].x), a[i].y = i;
sort(a + 1, a + n + 1, tmp);
scanf("%d", &q);
for (int i = 1; i <= q; i++) scanf("%d %d", &b[i].x, &b[i].y), b[i].z = i;
sort(b + 1, b + q + 1, cc);
for (int i = 1; i <= q;) {
int m = b[i].x;
for (; tot <= m; tot++) c[tot].x = a[tot].x, c[tot].y = a[tot].y;
sort(c + 1, c + tot, cmp);
for (; b[i].x == m; i++) ans[b[i].z] = c[b[i].y].x;
}
for (int i = 1; i <= q; i++) printf("%d\n", ans[i]);
return 0;
}
|
1227_D1. Optimal Subsequences (Easy Version)
|
This is the easier version of the problem. In this version 1 β€ n, m β€ 100. You can hack this problem only if you solve and lock both problems.
You are given a sequence of integers a=[a_1,a_2,...,a_n] of length n. Its subsequence is obtained by removing zero or more elements from the sequence a (they do not necessarily go consecutively). For example, for the sequence a=[11,20,11,33,11,20,11]:
* [11,20,11,33,11,20,11], [11,20,11,33,11,20], [11,11,11,11], [20], [33,20] are subsequences (these are just some of the long list);
* [40], [33,33], [33,20,20], [20,20,11,11] are not subsequences.
Suppose that an additional non-negative integer k (1 β€ k β€ n) is given, then the subsequence is called optimal if:
* it has a length of k and the sum of its elements is the maximum possible among all subsequences of length k;
* and among all subsequences of length k that satisfy the previous item, it is lexicographically minimal.
Recall that the sequence b=[b_1, b_2, ..., b_k] is lexicographically smaller than the sequence c=[c_1, c_2, ..., c_k] if the first element (from the left) in which they differ less in the sequence b than in c. Formally: there exists t (1 β€ t β€ k) such that b_1=c_1, b_2=c_2, ..., b_{t-1}=c_{t-1} and at the same time b_t<c_t. For example:
* [10, 20, 20] lexicographically less than [10, 21, 1],
* [7, 99, 99] is lexicographically less than [10, 21, 1],
* [10, 21, 0] is lexicographically less than [10, 21, 1].
You are given a sequence of a=[a_1,a_2,...,a_n] and m requests, each consisting of two numbers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j). For each query, print the value that is in the index pos_j of the optimal subsequence of the given sequence a for k=k_j.
For example, if n=4, a=[10,20,30,20], k_j=2, then the optimal subsequence is [20,30] β it is the minimum lexicographically among all subsequences of length 2 with the maximum total sum of items. Thus, the answer to the request k_j=2, pos_j=1 is the number 20, and the answer to the request k_j=2, pos_j=2 is the number 30.
Input
The first line contains an integer n (1 β€ n β€ 100) β the length of the sequence a.
The second line contains elements of the sequence a: integer numbers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9).
The third line contains an integer m (1 β€ m β€ 100) β the number of requests.
The following m lines contain pairs of integers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j) β the requests.
Output
Print m integers r_1, r_2, ..., r_m (1 β€ r_j β€ 10^9) one per line: answers to the requests in the order they appear in the input. The value of r_j should be equal to the value contained in the position pos_j of the optimal subsequence for k=k_j.
Examples
Input
3
10 20 10
6
1 1
2 1
2 2
3 1
3 2
3 3
Output
20
10
20
10
20
10
Input
7
1 2 1 3 1 2 1
9
2 1
2 2
3 1
3 2
3 3
1 1
7 1
7 7
7 4
Output
2
3
2
3
2
3
1
1
3
Note
In the first example, for a=[10,20,10] the optimal subsequences are:
* for k=1: [20],
* for k=2: [10,20],
* for k=3: [10,20,10].
|
{
"input": [
"3\n10 20 10\n6\n1 1\n2 1\n2 2\n3 1\n3 2\n3 3\n",
"7\n1 2 1 3 1 2 1\n9\n2 1\n2 2\n3 1\n3 2\n3 3\n1 1\n7 1\n7 7\n7 4\n"
],
"output": [
"20\n10\n20\n10\n20\n10\n",
"2\n3\n2\n3\n2\n3\n1\n1\n3\n"
]
}
|
{
"input": [
"2\n1 10\n3\n2 2\n2 1\n1 1\n",
"2\n3922 3922\n3\n2 2\n2 1\n1 1\n",
"1\n1000000000\n1\n1 1\n",
"1\n1\n3\n1 1\n1 1\n1 1\n",
"5\n3 1 4 1 2\n15\n5 5\n5 4\n5 3\n5 2\n5 1\n4 4\n4 3\n4 2\n4 1\n3 3\n3 2\n3 1\n2 2\n2 1\n1 1\n",
"2\n392222 322\n3\n2 2\n2 1\n1 1\n"
],
"output": [
"10\n1\n10\n",
"3922\n3922\n3922\n",
"1000000000\n",
"1\n1\n1\n",
"2\n1\n4\n1\n3\n2\n4\n1\n3\n2\n4\n3\n4\n3\n4\n",
"322\n392222\n392222\n"
]
}
|
CORRECT
|
java
|
import java.io.*;
import java.util.*;
import java.math.BigInteger;
import java.lang.Object;
public class Main {
static class sort implements Comparator<int[]>
{
public int compare(int[] a,int[] b)
{
if(a[1] == b[1]) return -b[0]+a[0];
return a[1]-b[1];
}
}
static class sort2 implements Comparator<long[]>
{
public int compare(long[] a,long[] b)
{
//if(a[0] == b[0]) return a[1]-b[1];
/*if(a[1] < b[1]) return -1;
else if(a[1] > b[1]) return 1;
return a[0]-b[0];*/
return (int)(a[0]-b[0]);
}
}
static class sort1 implements Comparator<double[]>
{
public int compare(double[] a,double[] b)
{
//if(a[0] == b[0]) return a[1]-b[1];
if(a[1] < b[1]) return -1;
else if(a[1] > b[1]) return 1;
return 0;
}
}
public static String[] F(BufferedReader bf) throws Exception
{
return (bf.readLine().split(" "));
}
public static void pr(PrintWriter out,Object o)
{
out.println(o.toString());//out.flush();
}
public static void prW(PrintWriter out,Object o)
{
out.print(o.toString());//out.flush();
}
public static int intIn(String st)
{
return Integer.parseInt(st);
}
public static void pr(Object o)
{
System.out.println(o.toString());
}
public static void prW(Object o)
{
System.out.print(o.toString());
}
public static int inInt(String s)
{
return Integer.parseInt(s);
}
public static long in(String s)
{
return Long.parseLong(s);
}
static int[] toIntArray(String[] m)
{
int[] p=new int[m.length];
for(int o=0;o<m.length;o++)
{
p[o]= inInt(m[o]);
}
return p;
}
static double[] toDArray(String[] m)
{
double[] p=new double[m.length];
for(int o=0;o<m.length;o++)
{
p[o]= Double.parseDouble(m[o]);
}
return p;
}
static long[] toLArray(String[] m)
{
long[] p=new long[m.length];
for(int o=0;o<m.length;o++)
{
p[o]= in(m[o]);
}
return p;
}
static int[][] di={{0,1},{1,0},{0,-1},{-1,0}};
static int[] dir = {4,3,2,-4,-3,-2};
public static void F(int i,int c,int[] vis,List<Integer> res,int n)
{
vis[i]=1;
res.add(i);
for(int x : dir)
{
int v = i+x;
if(v<=0 || v>n) continue;
if(vis[v] == 0)
{
F(v,c,vis,res,n);
}
}
}
static long gcd(long a, long b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
static long Gp(long l,long b,long lcm)
{
long c1,c2;
c1 = (l/lcm);
long sum=0l;
sum += (c1-1l)*(b);
long las = (l/lcm)*lcm;
sum += Math.min(l-las+1l,b);
return sum;
}
static long pow(long x, long y, long p)
{
long res = 1; // Initialize result
x = x % p; // Update x if it is more than or
// equal to p
if (x == 0)
return 0l; // In case x is divisible by p;
while (y > 0)
{
// If y is odd, multiply x with result
if ((y & 1) != 0)
res = (res * x) % p;
// y must be even now
y = y >> 1; // y = y/2
x = (x * x) % p;
}
return res;
}
public static boolean ok(int mid,int[] arr,int[][] mat,int q)
{
int tot=0;
for(int[] a : mat)
{
int p=a[0];
int l = a[1];
int ans=q+1;
int g = Math.abs(p);
if(p==0) continue;
for(int o=0;o<arr.length;o++)
{
if((arr[o]*p) < 0)
{
int sp = Math.abs(arr[o]);
int v = (g/sp);
if((g%sp) != 0) v++;
if(v<=mid)
{
ans=Math.min(ans,Math.abs(l-o));
}
}
}
if(ans > q ) return false;
tot += ans;
}
return tot<=q;
}
public static long F(int a,int b)
{
return 462161773423l*(a+0l) + 4549834777463l*(b+0l);
}
public static void update(int[] diff,int i,long k)
{
int j=(int)(k);
if(j<i) return;
diff[i] +=1;
diff[j+1] -=1;
}
public static long F(int o)
{
long ans=1l;
long g = 1l;
while(o>0)
{
if((o%2) != 0)
{
ans = (ans * g);
}
g++;
o=(o>>1);
}
return ans;
}
public static long F(int x,int y,Long[][] dp)
{
pr(y);
if(y<=0)
{
return 0l;
}
if((10-x) > y)
{
dp[x][y] = 1l;return 1l;
}
if((10-x) == y)
{
dp[x][y] = 2l;return 2l;
}
if(dp[x][y] != null) return dp[x][y];
long val = F(1,y-(10-x),dp) + F(0,y-(10-x),dp);
dp[x][y] = val;return val;
}
static TreeSet<Long> Fu(long n)
{
// Note that this loop runs till square root
TreeSet<Long> list = new TreeSet<>();
for (int i=1; i<=Math.sqrt(n); i++)
{
if (n%i==0)
{
// If divisors are equal, print only one
if (n/i == i)
list.add(i+0l);
else {
list.add(i+0l);
list.add(n/i);
}
}
}
return list;
}
public static void main (String[] args) throws Exception {
BufferedReader bf=new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(System.out);;;
//int[] map=new int[1000001];
int yy=1;//inInt(bf.readLine());
for(int w=0;w<yy;w++)
{
// String str = bf.readLine();
out.flush();
String[] xlp = bf.readLine().split(" ");;;;;;
//String st = bf.readLine();
int n,t;//boolean bol=false;
int m;//long a,b,c;
int ioo;
int k;//pr(out,"vbc");
n=inInt(xlp[0]);//m=inInt(xlp[1]);//k=inInt(xlp[2]);//t=inInt(xlp[3]);
int[] arr = toIntArray(F(bf));
int[] tem = new int[n];
for(int o=0;o<n;o++) tem[o]=arr[o];
Arrays.sort(tem);
xlp = bf.readLine().split(" ");;;;;;
m=inInt(xlp[0]);k=m;
Map<Integer,Integer> map=new HashMap<>();
for(int o=0;o<n;o++)
{
if((!map.containsKey(arr[o]))) map.put(arr[o],o);
}
for(int o=0;o<k;o++)
{
int q1,q2;
int[] ar = toIntArray(F(bf));
q1=ar[0];q2=ar[1];
int ele = tem[n-q1];
//int index = map.get()
int co=0;
for(int j=0;j<n;j++)
{
if(arr[j] > ele) co++;
}
int ol = q1-co;int ok = 0;
// pr(out,co);
for(int j=0;j<n;j++)
{
if(arr[j] > ele || (arr[j]==ele && ol>0))
{
ok++;
if(ok == q2)
{
pr(out,arr[j]);break;
}
if(arr[j] == ele) ol--;
}
}
}
}
out.close();
bf.close();
}}
/*
1
2
1 1 1 1
100663319,201326611,402653189,805306457,1610612741
Kickstart
String rp;
rp = "Case #"+(w+1)+": "+(n-ans)+" ";
static int[][] dir={{0,1},{1,0},{-1,0},{0,-1}};
static class SegmentTreeRMQ
{
int st[];
int minVal(int x, int y) {
return (x > y) ? x : y;
}
int getMid(int s, int e) {
return s + (e - s) / 2;
}
int RMQUtil(int ss, int se, int qs, int qe, int index)
{
if (qs <= ss && qe >= se)
return st[index];
// If segment of this node is outside the given range
if (se < qs || ss > qe)
return Integer.MIN_VALUE;
// If a part of this segment overlaps with the given range
int mid = getMid(ss, se);
return minVal(RMQUtil(ss, mid, qs, qe, 2 * index + 1),
RMQUtil(mid + 1, se, qs, qe, 2 * index + 2));
}
// Return minimum of elements in range from index qs (query
// start) to qe (query end). It mainly uses RMQUtil()
int RMQ(int n, int qs, int qe)
{
// Check for erroneous input values
return RMQUtil(0, n - 1, qs, qe, 0);
}
// A recursive function that constructs Segment Tree for
// array[ss..se]. si is index of current node in segment tree st
int constructSTUtil(int arr[], int ss, int se, int si)
{
// If there is one element in array, store it in current
// node of segment tree and return
if (ss == se) {
st[si] = arr[ss];
return arr[ss];
}
// If there are more than one elements, then recur for left and
// right subtrees and store the minimum of two values in this node
int mid = getMid(ss, se);
st[si] = minVal(constructSTUtil(arr, ss, mid, si * 2 + 1),
constructSTUtil(arr, mid + 1, se, si * 2 + 2));
return st[si];
}
void con(int arr[])
{
// Allocate memory for segment tree
//Height of segment tree
int n = (arr.length);
int x = (int) (Math.ceil(Math.log(n) / Math.log(2)));
//Maximum size of segment tree
int max_size = 2 * (int) Math.pow(2, x) - 1;
st = new int[max_size]; // allocate memory
// Fill the allocated memory st
constructSTUtil(arr, 0, n - 1, 0);
}
}
static class DSU {
int[] p;int[] sz;int op;int c;;
int[] last;
public void G(int n)
{
last=new int[n];
p=new int[n];
sz=new int[n];c=n;
op=n;
for(int h=0;h<n;h++)
{
sz[h]=1;p[h]=h;
last[h]=h;
}
}
public int find(int x)
{
int y=x;
while(x!=p[x]) x=p[x];
while(y!=p[y])
{
int tem=p[y];
p[y]=x;y=tem;
}
return p[y];
}
public void union(int a,int b)
{
int x,y;
x=find(a);y=find(b);
if(x==y) return;
if(sz[x]>sz[y])
{
p[y] = x;
sz[x]+=sz[y];
last[x]=Math.max(last[x],last[y]);
}
else
{
p[x]=y;sz[y]+=sz[x];
last[y]=Math.max(last[y],last[x]);
}
c--;
}}
static long pow(long x, long y, long p)
{
long res = 1; // Initialize result
x = x % p; // Update x if it is more than or
// equal to p
if (x == 0)
return 0l; // In case x is divisible by p;
while (y > 0)
{
// If y is odd, multiply x with result
if ((y & 1) != 0)
res = (res * x) % p;
// y must be even now
y = y >> 1; // y = y/2
x = (x * x) % p;
}
return res;
}
static long gcd(long a, long b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
static int gcd(int a, int b,int o)
{
if (b == 0)
return a;
return gcd(b, a % b,o);
}
Geometric median
public static double F(double[] x,double[] w)
{
double d1,d2;
double S=0.00;
for(double dp : w) S += dp;
int k = 0;
double sum = S - w[0]; // sum is the total weight of all `x[i] > x[k]`
while(sum > S/2)
{
++k;
sum -= w[k];
}
d1=x[k];
return d1;
k = w.length-1;
sum = S - w[k]; // sum is the total weight of all `x[i] > x[k]`
while(sum > S/2)
{
--k;
sum -= w[k];
}
d2=x[k];
return new double[]{d1,d2};
}
*/
|
1227_D1. Optimal Subsequences (Easy Version)
|
This is the easier version of the problem. In this version 1 β€ n, m β€ 100. You can hack this problem only if you solve and lock both problems.
You are given a sequence of integers a=[a_1,a_2,...,a_n] of length n. Its subsequence is obtained by removing zero or more elements from the sequence a (they do not necessarily go consecutively). For example, for the sequence a=[11,20,11,33,11,20,11]:
* [11,20,11,33,11,20,11], [11,20,11,33,11,20], [11,11,11,11], [20], [33,20] are subsequences (these are just some of the long list);
* [40], [33,33], [33,20,20], [20,20,11,11] are not subsequences.
Suppose that an additional non-negative integer k (1 β€ k β€ n) is given, then the subsequence is called optimal if:
* it has a length of k and the sum of its elements is the maximum possible among all subsequences of length k;
* and among all subsequences of length k that satisfy the previous item, it is lexicographically minimal.
Recall that the sequence b=[b_1, b_2, ..., b_k] is lexicographically smaller than the sequence c=[c_1, c_2, ..., c_k] if the first element (from the left) in which they differ less in the sequence b than in c. Formally: there exists t (1 β€ t β€ k) such that b_1=c_1, b_2=c_2, ..., b_{t-1}=c_{t-1} and at the same time b_t<c_t. For example:
* [10, 20, 20] lexicographically less than [10, 21, 1],
* [7, 99, 99] is lexicographically less than [10, 21, 1],
* [10, 21, 0] is lexicographically less than [10, 21, 1].
You are given a sequence of a=[a_1,a_2,...,a_n] and m requests, each consisting of two numbers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j). For each query, print the value that is in the index pos_j of the optimal subsequence of the given sequence a for k=k_j.
For example, if n=4, a=[10,20,30,20], k_j=2, then the optimal subsequence is [20,30] β it is the minimum lexicographically among all subsequences of length 2 with the maximum total sum of items. Thus, the answer to the request k_j=2, pos_j=1 is the number 20, and the answer to the request k_j=2, pos_j=2 is the number 30.
Input
The first line contains an integer n (1 β€ n β€ 100) β the length of the sequence a.
The second line contains elements of the sequence a: integer numbers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9).
The third line contains an integer m (1 β€ m β€ 100) β the number of requests.
The following m lines contain pairs of integers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j) β the requests.
Output
Print m integers r_1, r_2, ..., r_m (1 β€ r_j β€ 10^9) one per line: answers to the requests in the order they appear in the input. The value of r_j should be equal to the value contained in the position pos_j of the optimal subsequence for k=k_j.
Examples
Input
3
10 20 10
6
1 1
2 1
2 2
3 1
3 2
3 3
Output
20
10
20
10
20
10
Input
7
1 2 1 3 1 2 1
9
2 1
2 2
3 1
3 2
3 3
1 1
7 1
7 7
7 4
Output
2
3
2
3
2
3
1
1
3
Note
In the first example, for a=[10,20,10] the optimal subsequences are:
* for k=1: [20],
* for k=2: [10,20],
* for k=3: [10,20,10].
|
{
"input": [
"3\n10 20 10\n6\n1 1\n2 1\n2 2\n3 1\n3 2\n3 3\n",
"7\n1 2 1 3 1 2 1\n9\n2 1\n2 2\n3 1\n3 2\n3 3\n1 1\n7 1\n7 7\n7 4\n"
],
"output": [
"20\n10\n20\n10\n20\n10\n",
"2\n3\n2\n3\n2\n3\n1\n1\n3\n"
]
}
|
{
"input": [
"2\n1 10\n3\n2 2\n2 1\n1 1\n",
"2\n3922 3922\n3\n2 2\n2 1\n1 1\n",
"1\n1000000000\n1\n1 1\n",
"1\n1\n3\n1 1\n1 1\n1 1\n",
"5\n3 1 4 1 2\n15\n5 5\n5 4\n5 3\n5 2\n5 1\n4 4\n4 3\n4 2\n4 1\n3 3\n3 2\n3 1\n2 2\n2 1\n1 1\n",
"2\n392222 322\n3\n2 2\n2 1\n1 1\n"
],
"output": [
"10\n1\n10\n",
"3922\n3922\n3922\n",
"1000000000\n",
"1\n1\n1\n",
"2\n1\n4\n1\n3\n2\n4\n1\n3\n2\n4\n3\n4\n3\n4\n",
"322\n392222\n392222\n"
]
}
|
CORRECT
|
java
|
import java.io.*;
import java.util.*;
import java.lang.*;
import java.math.*;
import java.text.DecimalFormat;
import java.lang.reflect.Array;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.BigDecimal;
public class B{
public static PrintWriter out = new PrintWriter (new BufferedOutputStream(System.out));
//static long MOD = (long)(1e9+7);
static long MOD = 998244353;
static long MOD2 = MOD*MOD;
static FastReader sc = new FastReader();
static int pInf = Integer.MAX_VALUE;
static int nInf = Integer.MIN_VALUE;
public static void main(String[] args){
int test = 1;
//test = sc.nextInt();
while(test-->0)solve();
out.flush();
out.close();
}
public static void solve() {
int n = sc.nextInt();
Pair[] a = new Pair[n];
for(int i = 0; i < n; i++) {
a[i] = new Pair(sc.nextInt(), i);
}
Arrays.sort(a);
int q = sc.nextInt();
Map<Integer, ArrayList<Pair>> M = new HashMap<Integer, ArrayList<Pair>>();
while(q-->0) {
int k = sc.nextInt();
int j = sc.nextInt()-1;
if(M.containsKey(k)) {
out.println(M.get(k).get(j).x);
}
else {
ArrayList<Pair> A = new ArrayList<>();
for(int i = 0; i < k; i++) {
A.add(a[i]);
}
Collections.sort(A,(p1,p2)->(int)(p1.y-p2.y));
M.put(k, A);
out.println(M.get(k).get(j).x);
}
}
return;
}
public static int lowerBound(int key, int[] a) {
int s = 0;
int e = a.length-1;
if(e==-1) {
return 0;
}
int ans = -1;
while(s<=e) {
int m = s+(e-s)/2;
if(a[m]>=key) {
ans = m;
e = m-1;
}
else {
s = m+1;
}
}
return ans==-1?s:ans;
}
public static int upperBound(int key, int[] a) {
int s = 0;
int e = a.length-1;
if(e==-1) {
return 0;
}
int ans = -1;
while(s<=e) {
int m = s+(e-s)/2;
if(a[m]>key) {
ans = m;
e = m-1;
}
else {
s = m+1;
}
}
return ans==-1?s:ans;
}
public static long c2(long n) {
if((n&1)==0) {
return mul(n/2, n-1);
}
else {
return mul(n, (n-1)/2);
}
}
public static long mul(long a, long b){
return ((a%MOD)*(b%MOD))%MOD;
}
public static long add(long a, long b){
return ((a%MOD)+(b%MOD))%MOD;
}
public static long sub(long a, long b){
return ((a%MOD)-(b%MOD))%MOD;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Integer.lowestOneBit(i) Equals k where k is the position of the first one in the binary
//Integer.highestOneBit(i) Equals k where k is the position of the last one in the binary
//Integer.bitCount(i) returns the number of one-bits
//Collections.sort(A,(p1,p2)->(int)(p2.x-p1.x)) To sort ArrayList in descending order wrt values of x.
// Arrays.parallelSort(a,new Comparator<TPair>() {
// public int compare(TPair a,TPair b) {
// if(a.y==b.y) return a.x-b.x;
// return b.y-a.y;
// }
// });
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//PrimeFactors
public static ArrayList<Long> primeFactors(long n) {
ArrayList<Long> arr = new ArrayList<>();
if (n % 2 == 0)
arr.add((long) 2);
while (n % 2 == 0)
n /= 2;
for (long i = 3; i <= Math.sqrt(n); i += 2) {
int flag = 0;
while (n % i == 0) {
n /= i;
flag = 1;
}
if (flag == 1)
arr.add(i);
}
if (n > 2)
arr.add(n);
return arr;
}
//Pair Class
static class Pair implements Comparable<Pair>{
int x;
int y;
public Pair(int x, int y) {
this.x = x;
this.y = y;
}
@Override
public int compareTo(Pair o) {
if(this.x==o.x){
return (this.y-o.y);
}
return -1*(this.x-o.x);
}
}
static class TPair implements Comparable<TPair>{
int l;
int r;
int index;
public TPair(int l, int r, int index) {
this.l = l;
this.r = r;
this.index = index;
}
@Override
public int compareTo(TPair o) {
if(this.l==o.l){
return -1*(this.r-o.r);
}
return -1*(this.l-o.l);
}
}
//nCr
static long ncr(long n, long k) {
long ret = 1;
for (long x = n; x > n - k; x--) {
ret *= x;
ret /= (n - x + 1);
}
return ret;
}
static long finextDoubleMMI_fermat(long n,int M)
{
return fastExpo(n,M-2);
}
static long nCrModPFermat(int n, int r, int p)
{
if (r == 0)
return 1;
long[] fac = new long[n+1];
fac[0] = 1;
for (int i = 1 ;i <= n; i++)
fac[i] = fac[i-1] * i % p;
return (fac[n]* finextDoubleMMI_fermat(fac[r], p)% p * finextDoubleMMI_fermat(fac[n-r], p) % p) % p;
}
//Kadane's Algorithm
static long maxSubArraySum(ArrayList<Long> a) {
if(a.size()==0) {
return 0;
}
long max_so_far = a.get(0);
long curr_max = a.get(0);
for (int i = 1; i < a.size(); i++) {
curr_max = Math.max(a.get(i), curr_max+a.get(i));
max_so_far = Math.max(max_so_far, curr_max);
}
return max_so_far;
}
//Shuffle Sort
static final Random random=new Random();
static void ruffleSort(int[] a) {
int n=a.length;//shuffle, then sort
for (int i=0; i<n; i++) {
int oi=random.nextInt(n), temp=a[oi];
a[oi]=a[i]; a[i]=temp;
}
Arrays.sort(a);
}
//Merge Sort
static void merge(long arr[], int l, int m, int r)
{
// Find sizes of two subarrays to be merged
int n1 = m - l + 1;
int n2 = r - m;
/* Create temp arrays */
long L[] = new long [n1];
long R[] = new long [n2];
/*Copy data to temp arrays*/
for (int i=0; i<n1; ++i)
L[i] = arr[l + i];
for (int j=0; j<n2; ++j)
R[j] = arr[m + 1+ j];
/* Merge the temp arrays */
// Initial indexes of first and second subarrays
int i = 0, j = 0;
// Initial index of merged subarry array
int k = l;
while (i < n1 && j < n2)
{
if (L[i] <= R[j])
{
arr[k] = L[i];
i++;
}
else
{
arr[k] = R[j];
j++;
}
k++;
}
/* Copy remaining elements of L[] if any */
while (i < n1)
{
arr[k] = L[i];
i++;
k++;
}
/* Copy remaining elements of R[] if any */
while (j < n2)
{
arr[k] = R[j];
j++;
k++;
}
}
// Main function that sorts arr[l..r] using
// merge()
static void sort(long arr[], int l, int r)
{
if (l < r)
{
// Find the middle point
int m = (l+r)/2;
// Sort first and second halves
sort(arr, l, m);
sort(arr , m+1, r);
// Merge the sorted halves
merge(arr, l, m, r);
}
}
//Brian Kernighans Algorithm
static long countSetBits(long n){
if(n==0) return 0;
return 1+countSetBits(n&(n-1));
}
//Factorial
static long factorial(long n){
if(n==1) return 1;
if(n==2) return 2;
if(n==3) return 6;
return n*factorial(n-1);
}
//Euclidean Algorithm
static long gcd(long A,long B){
if(B==0) return A;
return gcd(B,A%B);
}
//Modular Exponentiation
static long fastExpo(long x,long n){
if(n==0) return 1;
if((n&1)==0) return fastExpo((x*x)%MOD,n/2)%MOD;
return ((x%MOD)*fastExpo((x*x)%MOD,(n-1)/2))%MOD;
}
//AKS Algorithm
static boolean isPrime(long n){
if(n<=1) return false;
if(n<=3) return true;
if(n%2==0 || n%3==0) return false;
for(int i=5;i<=Math.sqrt(n);i+=6)
if(n%i==0 || n%(i+2)==0) return false;
return true;
}
//Reverse an array
static <T> void reverse(T arr[],int l,int r){
Collections.reverse(Arrays.asList(arr).subList(l, r));
}
//Print array
static void print1d(int arr[]) {
out.println(Arrays.toString(arr));
}
static void print2d(int arr[][]) {
for(int a[]: arr) out.println(Arrays.toString(a));
}
//Sieve of eratosthenes
static int[] findPrimes(int n){
boolean isPrime[]=new boolean[n+1];
ArrayList<Integer> a=new ArrayList<>();
int result[];
Arrays.fill(isPrime,true);
isPrime[0]=false;
isPrime[1]=false;
for(int i=2;i*i<=n;++i){
if(isPrime[i]==true){
for(int j=i*i;j<=n;j+=i) isPrime[j]=false;
}
}
for(int i=0;i<=n;i++) if(isPrime[i]==true) a.add(i);
result=new int[a.size()];
for(int i=0;i<a.size();i++) result[i]=a.get(i);
return result;
}
//Indivisual factors of all nos till n
static ArrayList<Integer>[] indiFactors(int n){
ArrayList<Integer>[] A = new ArrayList[n+1];
for(int i = 0; i <= n; i++) {
A[i] = new ArrayList<>();
}
int[] sieve = new int[n+1];
for(int i=2;i<=n;i++) {
if(sieve[i]==0) {
for(int j=i;j<=n;j+=i) if(sieve[j]==0) {
//sieve[j]=i;
A[j].add(i);
}
}
}
return A;
}
//Segmented Sieve
static boolean[] segmentedSieve(long l, long r){
boolean[] segSieve = new boolean[(int)(r-l+1)];
Arrays.fill(segSieve, true);
int[] prePrimes = findPrimes((int)Math.sqrt(r));
for(int p:prePrimes) {
long low = (l/p)*p;
if(low < l) {
low += p;
}
if(low == p) {
low += p;
}
for(long j = low; j<= r; j += p) {
segSieve[(int) (j-l)] = false;
}
}
if(l==1) {
segSieve[0] = false;
}
return segSieve;
}
//Euler Totent function
static long countCoprimes(long n){
ArrayList<Long> prime_factors=new ArrayList<>();
long x=n,flag=0;
while(x%2==0){
if(flag==0) prime_factors.add(2L);
flag=1;
x/=2;
}
for(long i=3;i*i<=x;i+=2){
flag=0;
while(x%i==0){
if(flag==0) prime_factors.add(i);
flag=1;
x/=i;
}
}
if(x>2) prime_factors.add(x);
double ans=(double)n;
for(Long p:prime_factors){
ans*=(1.0-(Double)1.0/p);
}
return (long)ans;
}
static long modulo = (long)1e9+7;
public static long modinv(long x){
return modpow(x, modulo-2);
}
public static long modpow(long a, long b){
if(b==0){
return 1;
}
long x = modpow(a, b/2);
x = (x*x)%modulo;
if(b%2==1){
return (x*a)%modulo;
}
return x;
}
public static class FastReader {
BufferedReader br;
StringTokenizer st;
//it reads the data about the specified point and divide the data about it ,it is quite fast
//than using direct
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch (Exception r) {
r.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());//converts string to integer
}
double nextDouble() {
return Double.parseDouble(next());
}
long nextLong() {
return Long.parseLong(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (Exception r) {
r.printStackTrace();
}
return str;
}
}
}
|
1227_D1. Optimal Subsequences (Easy Version)
|
This is the easier version of the problem. In this version 1 β€ n, m β€ 100. You can hack this problem only if you solve and lock both problems.
You are given a sequence of integers a=[a_1,a_2,...,a_n] of length n. Its subsequence is obtained by removing zero or more elements from the sequence a (they do not necessarily go consecutively). For example, for the sequence a=[11,20,11,33,11,20,11]:
* [11,20,11,33,11,20,11], [11,20,11,33,11,20], [11,11,11,11], [20], [33,20] are subsequences (these are just some of the long list);
* [40], [33,33], [33,20,20], [20,20,11,11] are not subsequences.
Suppose that an additional non-negative integer k (1 β€ k β€ n) is given, then the subsequence is called optimal if:
* it has a length of k and the sum of its elements is the maximum possible among all subsequences of length k;
* and among all subsequences of length k that satisfy the previous item, it is lexicographically minimal.
Recall that the sequence b=[b_1, b_2, ..., b_k] is lexicographically smaller than the sequence c=[c_1, c_2, ..., c_k] if the first element (from the left) in which they differ less in the sequence b than in c. Formally: there exists t (1 β€ t β€ k) such that b_1=c_1, b_2=c_2, ..., b_{t-1}=c_{t-1} and at the same time b_t<c_t. For example:
* [10, 20, 20] lexicographically less than [10, 21, 1],
* [7, 99, 99] is lexicographically less than [10, 21, 1],
* [10, 21, 0] is lexicographically less than [10, 21, 1].
You are given a sequence of a=[a_1,a_2,...,a_n] and m requests, each consisting of two numbers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j). For each query, print the value that is in the index pos_j of the optimal subsequence of the given sequence a for k=k_j.
For example, if n=4, a=[10,20,30,20], k_j=2, then the optimal subsequence is [20,30] β it is the minimum lexicographically among all subsequences of length 2 with the maximum total sum of items. Thus, the answer to the request k_j=2, pos_j=1 is the number 20, and the answer to the request k_j=2, pos_j=2 is the number 30.
Input
The first line contains an integer n (1 β€ n β€ 100) β the length of the sequence a.
The second line contains elements of the sequence a: integer numbers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9).
The third line contains an integer m (1 β€ m β€ 100) β the number of requests.
The following m lines contain pairs of integers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j) β the requests.
Output
Print m integers r_1, r_2, ..., r_m (1 β€ r_j β€ 10^9) one per line: answers to the requests in the order they appear in the input. The value of r_j should be equal to the value contained in the position pos_j of the optimal subsequence for k=k_j.
Examples
Input
3
10 20 10
6
1 1
2 1
2 2
3 1
3 2
3 3
Output
20
10
20
10
20
10
Input
7
1 2 1 3 1 2 1
9
2 1
2 2
3 1
3 2
3 3
1 1
7 1
7 7
7 4
Output
2
3
2
3
2
3
1
1
3
Note
In the first example, for a=[10,20,10] the optimal subsequences are:
* for k=1: [20],
* for k=2: [10,20],
* for k=3: [10,20,10].
|
{
"input": [
"3\n10 20 10\n6\n1 1\n2 1\n2 2\n3 1\n3 2\n3 3\n",
"7\n1 2 1 3 1 2 1\n9\n2 1\n2 2\n3 1\n3 2\n3 3\n1 1\n7 1\n7 7\n7 4\n"
],
"output": [
"20\n10\n20\n10\n20\n10\n",
"2\n3\n2\n3\n2\n3\n1\n1\n3\n"
]
}
|
{
"input": [
"2\n1 10\n3\n2 2\n2 1\n1 1\n",
"2\n3922 3922\n3\n2 2\n2 1\n1 1\n",
"1\n1000000000\n1\n1 1\n",
"1\n1\n3\n1 1\n1 1\n1 1\n",
"5\n3 1 4 1 2\n15\n5 5\n5 4\n5 3\n5 2\n5 1\n4 4\n4 3\n4 2\n4 1\n3 3\n3 2\n3 1\n2 2\n2 1\n1 1\n",
"2\n392222 322\n3\n2 2\n2 1\n1 1\n"
],
"output": [
"10\n1\n10\n",
"3922\n3922\n3922\n",
"1000000000\n",
"1\n1\n1\n",
"2\n1\n4\n1\n3\n2\n4\n1\n3\n2\n4\n3\n4\n3\n4\n",
"322\n392222\n392222\n"
]
}
|
CORRECT
|
python3
|
n = int(input())
arr = [int(i) for i in input().split()]
sor = [[arr[i], n - i] for i in range(n)]
sor.sort()
m = int(input())
for i in range(m):
op = []
[q, index] = [int(i) for i in input().split()]
for j in range(q):
op.append(n - sor[-1-j][1])
op.sort()
print(arr[op[index - 1]])
|
1227_D1. Optimal Subsequences (Easy Version)
|
This is the easier version of the problem. In this version 1 β€ n, m β€ 100. You can hack this problem only if you solve and lock both problems.
You are given a sequence of integers a=[a_1,a_2,...,a_n] of length n. Its subsequence is obtained by removing zero or more elements from the sequence a (they do not necessarily go consecutively). For example, for the sequence a=[11,20,11,33,11,20,11]:
* [11,20,11,33,11,20,11], [11,20,11,33,11,20], [11,11,11,11], [20], [33,20] are subsequences (these are just some of the long list);
* [40], [33,33], [33,20,20], [20,20,11,11] are not subsequences.
Suppose that an additional non-negative integer k (1 β€ k β€ n) is given, then the subsequence is called optimal if:
* it has a length of k and the sum of its elements is the maximum possible among all subsequences of length k;
* and among all subsequences of length k that satisfy the previous item, it is lexicographically minimal.
Recall that the sequence b=[b_1, b_2, ..., b_k] is lexicographically smaller than the sequence c=[c_1, c_2, ..., c_k] if the first element (from the left) in which they differ less in the sequence b than in c. Formally: there exists t (1 β€ t β€ k) such that b_1=c_1, b_2=c_2, ..., b_{t-1}=c_{t-1} and at the same time b_t<c_t. For example:
* [10, 20, 20] lexicographically less than [10, 21, 1],
* [7, 99, 99] is lexicographically less than [10, 21, 1],
* [10, 21, 0] is lexicographically less than [10, 21, 1].
You are given a sequence of a=[a_1,a_2,...,a_n] and m requests, each consisting of two numbers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j). For each query, print the value that is in the index pos_j of the optimal subsequence of the given sequence a for k=k_j.
For example, if n=4, a=[10,20,30,20], k_j=2, then the optimal subsequence is [20,30] β it is the minimum lexicographically among all subsequences of length 2 with the maximum total sum of items. Thus, the answer to the request k_j=2, pos_j=1 is the number 20, and the answer to the request k_j=2, pos_j=2 is the number 30.
Input
The first line contains an integer n (1 β€ n β€ 100) β the length of the sequence a.
The second line contains elements of the sequence a: integer numbers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9).
The third line contains an integer m (1 β€ m β€ 100) β the number of requests.
The following m lines contain pairs of integers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j) β the requests.
Output
Print m integers r_1, r_2, ..., r_m (1 β€ r_j β€ 10^9) one per line: answers to the requests in the order they appear in the input. The value of r_j should be equal to the value contained in the position pos_j of the optimal subsequence for k=k_j.
Examples
Input
3
10 20 10
6
1 1
2 1
2 2
3 1
3 2
3 3
Output
20
10
20
10
20
10
Input
7
1 2 1 3 1 2 1
9
2 1
2 2
3 1
3 2
3 3
1 1
7 1
7 7
7 4
Output
2
3
2
3
2
3
1
1
3
Note
In the first example, for a=[10,20,10] the optimal subsequences are:
* for k=1: [20],
* for k=2: [10,20],
* for k=3: [10,20,10].
|
{
"input": [
"3\n10 20 10\n6\n1 1\n2 1\n2 2\n3 1\n3 2\n3 3\n",
"7\n1 2 1 3 1 2 1\n9\n2 1\n2 2\n3 1\n3 2\n3 3\n1 1\n7 1\n7 7\n7 4\n"
],
"output": [
"20\n10\n20\n10\n20\n10\n",
"2\n3\n2\n3\n2\n3\n1\n1\n3\n"
]
}
|
{
"input": [
"2\n1 10\n3\n2 2\n2 1\n1 1\n",
"2\n3922 3922\n3\n2 2\n2 1\n1 1\n",
"1\n1000000000\n1\n1 1\n",
"1\n1\n3\n1 1\n1 1\n1 1\n",
"5\n3 1 4 1 2\n15\n5 5\n5 4\n5 3\n5 2\n5 1\n4 4\n4 3\n4 2\n4 1\n3 3\n3 2\n3 1\n2 2\n2 1\n1 1\n",
"2\n392222 322\n3\n2 2\n2 1\n1 1\n"
],
"output": [
"10\n1\n10\n",
"3922\n3922\n3922\n",
"1000000000\n",
"1\n1\n1\n",
"2\n1\n4\n1\n3\n2\n4\n1\n3\n2\n4\n3\n4\n3\n4\n",
"322\n392222\n392222\n"
]
}
|
CORRECT
|
python3
|
from math import *
import os, sys
from io import BytesIO
input = BytesIO(os.read(0, os.fstat(0).st_size)).readline
n = int(input())
a = list(map(int, input().split()))
b = []
for i in range(n):
b.append((-a[i], i))
b.sort()
#print(b)
for i in range(int(input())):
k, pos = map(int, input().split())
tmp = []
for j in range(k):
tmp.append(b[j][1])
tmp.sort()
#print(tmp)
print(a[tmp[pos - 1]])
|
1227_D1. Optimal Subsequences (Easy Version)
|
This is the easier version of the problem. In this version 1 β€ n, m β€ 100. You can hack this problem only if you solve and lock both problems.
You are given a sequence of integers a=[a_1,a_2,...,a_n] of length n. Its subsequence is obtained by removing zero or more elements from the sequence a (they do not necessarily go consecutively). For example, for the sequence a=[11,20,11,33,11,20,11]:
* [11,20,11,33,11,20,11], [11,20,11,33,11,20], [11,11,11,11], [20], [33,20] are subsequences (these are just some of the long list);
* [40], [33,33], [33,20,20], [20,20,11,11] are not subsequences.
Suppose that an additional non-negative integer k (1 β€ k β€ n) is given, then the subsequence is called optimal if:
* it has a length of k and the sum of its elements is the maximum possible among all subsequences of length k;
* and among all subsequences of length k that satisfy the previous item, it is lexicographically minimal.
Recall that the sequence b=[b_1, b_2, ..., b_k] is lexicographically smaller than the sequence c=[c_1, c_2, ..., c_k] if the first element (from the left) in which they differ less in the sequence b than in c. Formally: there exists t (1 β€ t β€ k) such that b_1=c_1, b_2=c_2, ..., b_{t-1}=c_{t-1} and at the same time b_t<c_t. For example:
* [10, 20, 20] lexicographically less than [10, 21, 1],
* [7, 99, 99] is lexicographically less than [10, 21, 1],
* [10, 21, 0] is lexicographically less than [10, 21, 1].
You are given a sequence of a=[a_1,a_2,...,a_n] and m requests, each consisting of two numbers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j). For each query, print the value that is in the index pos_j of the optimal subsequence of the given sequence a for k=k_j.
For example, if n=4, a=[10,20,30,20], k_j=2, then the optimal subsequence is [20,30] β it is the minimum lexicographically among all subsequences of length 2 with the maximum total sum of items. Thus, the answer to the request k_j=2, pos_j=1 is the number 20, and the answer to the request k_j=2, pos_j=2 is the number 30.
Input
The first line contains an integer n (1 β€ n β€ 100) β the length of the sequence a.
The second line contains elements of the sequence a: integer numbers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9).
The third line contains an integer m (1 β€ m β€ 100) β the number of requests.
The following m lines contain pairs of integers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j) β the requests.
Output
Print m integers r_1, r_2, ..., r_m (1 β€ r_j β€ 10^9) one per line: answers to the requests in the order they appear in the input. The value of r_j should be equal to the value contained in the position pos_j of the optimal subsequence for k=k_j.
Examples
Input
3
10 20 10
6
1 1
2 1
2 2
3 1
3 2
3 3
Output
20
10
20
10
20
10
Input
7
1 2 1 3 1 2 1
9
2 1
2 2
3 1
3 2
3 3
1 1
7 1
7 7
7 4
Output
2
3
2
3
2
3
1
1
3
Note
In the first example, for a=[10,20,10] the optimal subsequences are:
* for k=1: [20],
* for k=2: [10,20],
* for k=3: [10,20,10].
|
{
"input": [
"3\n10 20 10\n6\n1 1\n2 1\n2 2\n3 1\n3 2\n3 3\n",
"7\n1 2 1 3 1 2 1\n9\n2 1\n2 2\n3 1\n3 2\n3 3\n1 1\n7 1\n7 7\n7 4\n"
],
"output": [
"20\n10\n20\n10\n20\n10\n",
"2\n3\n2\n3\n2\n3\n1\n1\n3\n"
]
}
|
{
"input": [
"2\n1 10\n3\n2 2\n2 1\n1 1\n",
"2\n3922 3922\n3\n2 2\n2 1\n1 1\n",
"1\n1000000000\n1\n1 1\n",
"1\n1\n3\n1 1\n1 1\n1 1\n",
"5\n3 1 4 1 2\n15\n5 5\n5 4\n5 3\n5 2\n5 1\n4 4\n4 3\n4 2\n4 1\n3 3\n3 2\n3 1\n2 2\n2 1\n1 1\n",
"2\n392222 322\n3\n2 2\n2 1\n1 1\n"
],
"output": [
"10\n1\n10\n",
"3922\n3922\n3922\n",
"1000000000\n",
"1\n1\n1\n",
"2\n1\n4\n1\n3\n2\n4\n1\n3\n2\n4\n3\n4\n3\n4\n",
"322\n392222\n392222\n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
void Update(const long &);
long FindSum(const long &);
long N, M, K = 0;
vector<long> BIT;
int main() {
cin >> N;
vector<pair<long, long>> A(N + 1);
vector<long> X(N + 1);
for (long i = 1; i <= N; ++i) {
cin >> A[i].first;
A[i].second = i;
X[i] = A[i].first;
}
cin >> M;
vector<pair<pair<long, long>, long>> Q(M);
for (long i = 0; i < M; ++i) {
cin >> Q[i].first.first >> Q[i].first.second;
Q[i].second = i;
}
sort(A.begin() + 1, A.end(),
[&](const pair<long, long> &X, const pair<long, long> &Y) -> bool {
if (X.first != Y.first)
return X.first > Y.first;
else
return X.second < Y.second;
});
sort(Q.begin(), Q.end());
BIT.resize(N + 1, 0);
vector<long> Answer(M);
for (long i = 0; i < M; ++i) {
while (K < Q[i].first.first) {
++K;
Update(A[K].second);
}
Answer[Q[i].second] = X[FindSum(Q[i].first.second)];
}
for (long &x : Answer) cout << x << '\n';
flush(cout);
}
void Update(const long &I) {
for (long i = I; i <= N; i += (i & -i)) ++BIT[i];
}
long FindSum(const long &S) {
long pos = 0, sum = 0, next;
for (long i = 17; i >= 0; --i) {
next = pos + (1 << i);
if ((next <= N) && (sum + BIT[next] < S)) {
pos = next;
sum += BIT[pos];
}
}
return pos + 1;
}
|
1227_D1. Optimal Subsequences (Easy Version)
|
This is the easier version of the problem. In this version 1 β€ n, m β€ 100. You can hack this problem only if you solve and lock both problems.
You are given a sequence of integers a=[a_1,a_2,...,a_n] of length n. Its subsequence is obtained by removing zero or more elements from the sequence a (they do not necessarily go consecutively). For example, for the sequence a=[11,20,11,33,11,20,11]:
* [11,20,11,33,11,20,11], [11,20,11,33,11,20], [11,11,11,11], [20], [33,20] are subsequences (these are just some of the long list);
* [40], [33,33], [33,20,20], [20,20,11,11] are not subsequences.
Suppose that an additional non-negative integer k (1 β€ k β€ n) is given, then the subsequence is called optimal if:
* it has a length of k and the sum of its elements is the maximum possible among all subsequences of length k;
* and among all subsequences of length k that satisfy the previous item, it is lexicographically minimal.
Recall that the sequence b=[b_1, b_2, ..., b_k] is lexicographically smaller than the sequence c=[c_1, c_2, ..., c_k] if the first element (from the left) in which they differ less in the sequence b than in c. Formally: there exists t (1 β€ t β€ k) such that b_1=c_1, b_2=c_2, ..., b_{t-1}=c_{t-1} and at the same time b_t<c_t. For example:
* [10, 20, 20] lexicographically less than [10, 21, 1],
* [7, 99, 99] is lexicographically less than [10, 21, 1],
* [10, 21, 0] is lexicographically less than [10, 21, 1].
You are given a sequence of a=[a_1,a_2,...,a_n] and m requests, each consisting of two numbers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j). For each query, print the value that is in the index pos_j of the optimal subsequence of the given sequence a for k=k_j.
For example, if n=4, a=[10,20,30,20], k_j=2, then the optimal subsequence is [20,30] β it is the minimum lexicographically among all subsequences of length 2 with the maximum total sum of items. Thus, the answer to the request k_j=2, pos_j=1 is the number 20, and the answer to the request k_j=2, pos_j=2 is the number 30.
Input
The first line contains an integer n (1 β€ n β€ 100) β the length of the sequence a.
The second line contains elements of the sequence a: integer numbers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9).
The third line contains an integer m (1 β€ m β€ 100) β the number of requests.
The following m lines contain pairs of integers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j) β the requests.
Output
Print m integers r_1, r_2, ..., r_m (1 β€ r_j β€ 10^9) one per line: answers to the requests in the order they appear in the input. The value of r_j should be equal to the value contained in the position pos_j of the optimal subsequence for k=k_j.
Examples
Input
3
10 20 10
6
1 1
2 1
2 2
3 1
3 2
3 3
Output
20
10
20
10
20
10
Input
7
1 2 1 3 1 2 1
9
2 1
2 2
3 1
3 2
3 3
1 1
7 1
7 7
7 4
Output
2
3
2
3
2
3
1
1
3
Note
In the first example, for a=[10,20,10] the optimal subsequences are:
* for k=1: [20],
* for k=2: [10,20],
* for k=3: [10,20,10].
|
{
"input": [
"3\n10 20 10\n6\n1 1\n2 1\n2 2\n3 1\n3 2\n3 3\n",
"7\n1 2 1 3 1 2 1\n9\n2 1\n2 2\n3 1\n3 2\n3 3\n1 1\n7 1\n7 7\n7 4\n"
],
"output": [
"20\n10\n20\n10\n20\n10\n",
"2\n3\n2\n3\n2\n3\n1\n1\n3\n"
]
}
|
{
"input": [
"2\n1 10\n3\n2 2\n2 1\n1 1\n",
"2\n3922 3922\n3\n2 2\n2 1\n1 1\n",
"1\n1000000000\n1\n1 1\n",
"1\n1\n3\n1 1\n1 1\n1 1\n",
"5\n3 1 4 1 2\n15\n5 5\n5 4\n5 3\n5 2\n5 1\n4 4\n4 3\n4 2\n4 1\n3 3\n3 2\n3 1\n2 2\n2 1\n1 1\n",
"2\n392222 322\n3\n2 2\n2 1\n1 1\n"
],
"output": [
"10\n1\n10\n",
"3922\n3922\n3922\n",
"1000000000\n",
"1\n1\n1\n",
"2\n1\n4\n1\n3\n2\n4\n1\n3\n2\n4\n3\n4\n3\n4\n",
"322\n392222\n392222\n"
]
}
|
CORRECT
|
python3
|
n = int(input())
arr = list(map(int, input().split()))
arr_sorted = [(arr[i], i) for i in range(n)]
arr_sorted.sort(key = lambda el: (el[0], -el[1]), reverse = True)
m = int(input())
for req_i in range(m):
k, pos = map(int, input().split())
pos -= 1
cur_arr = arr_sorted[:k]
cur_arr.sort(key = lambda el: el[1])
print(cur_arr[pos][0])
|
1227_D1. Optimal Subsequences (Easy Version)
|
This is the easier version of the problem. In this version 1 β€ n, m β€ 100. You can hack this problem only if you solve and lock both problems.
You are given a sequence of integers a=[a_1,a_2,...,a_n] of length n. Its subsequence is obtained by removing zero or more elements from the sequence a (they do not necessarily go consecutively). For example, for the sequence a=[11,20,11,33,11,20,11]:
* [11,20,11,33,11,20,11], [11,20,11,33,11,20], [11,11,11,11], [20], [33,20] are subsequences (these are just some of the long list);
* [40], [33,33], [33,20,20], [20,20,11,11] are not subsequences.
Suppose that an additional non-negative integer k (1 β€ k β€ n) is given, then the subsequence is called optimal if:
* it has a length of k and the sum of its elements is the maximum possible among all subsequences of length k;
* and among all subsequences of length k that satisfy the previous item, it is lexicographically minimal.
Recall that the sequence b=[b_1, b_2, ..., b_k] is lexicographically smaller than the sequence c=[c_1, c_2, ..., c_k] if the first element (from the left) in which they differ less in the sequence b than in c. Formally: there exists t (1 β€ t β€ k) such that b_1=c_1, b_2=c_2, ..., b_{t-1}=c_{t-1} and at the same time b_t<c_t. For example:
* [10, 20, 20] lexicographically less than [10, 21, 1],
* [7, 99, 99] is lexicographically less than [10, 21, 1],
* [10, 21, 0] is lexicographically less than [10, 21, 1].
You are given a sequence of a=[a_1,a_2,...,a_n] and m requests, each consisting of two numbers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j). For each query, print the value that is in the index pos_j of the optimal subsequence of the given sequence a for k=k_j.
For example, if n=4, a=[10,20,30,20], k_j=2, then the optimal subsequence is [20,30] β it is the minimum lexicographically among all subsequences of length 2 with the maximum total sum of items. Thus, the answer to the request k_j=2, pos_j=1 is the number 20, and the answer to the request k_j=2, pos_j=2 is the number 30.
Input
The first line contains an integer n (1 β€ n β€ 100) β the length of the sequence a.
The second line contains elements of the sequence a: integer numbers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9).
The third line contains an integer m (1 β€ m β€ 100) β the number of requests.
The following m lines contain pairs of integers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j) β the requests.
Output
Print m integers r_1, r_2, ..., r_m (1 β€ r_j β€ 10^9) one per line: answers to the requests in the order they appear in the input. The value of r_j should be equal to the value contained in the position pos_j of the optimal subsequence for k=k_j.
Examples
Input
3
10 20 10
6
1 1
2 1
2 2
3 1
3 2
3 3
Output
20
10
20
10
20
10
Input
7
1 2 1 3 1 2 1
9
2 1
2 2
3 1
3 2
3 3
1 1
7 1
7 7
7 4
Output
2
3
2
3
2
3
1
1
3
Note
In the first example, for a=[10,20,10] the optimal subsequences are:
* for k=1: [20],
* for k=2: [10,20],
* for k=3: [10,20,10].
|
{
"input": [
"3\n10 20 10\n6\n1 1\n2 1\n2 2\n3 1\n3 2\n3 3\n",
"7\n1 2 1 3 1 2 1\n9\n2 1\n2 2\n3 1\n3 2\n3 3\n1 1\n7 1\n7 7\n7 4\n"
],
"output": [
"20\n10\n20\n10\n20\n10\n",
"2\n3\n2\n3\n2\n3\n1\n1\n3\n"
]
}
|
{
"input": [
"2\n1 10\n3\n2 2\n2 1\n1 1\n",
"2\n3922 3922\n3\n2 2\n2 1\n1 1\n",
"1\n1000000000\n1\n1 1\n",
"1\n1\n3\n1 1\n1 1\n1 1\n",
"5\n3 1 4 1 2\n15\n5 5\n5 4\n5 3\n5 2\n5 1\n4 4\n4 3\n4 2\n4 1\n3 3\n3 2\n3 1\n2 2\n2 1\n1 1\n",
"2\n392222 322\n3\n2 2\n2 1\n1 1\n"
],
"output": [
"10\n1\n10\n",
"3922\n3922\n3922\n",
"1000000000\n",
"1\n1\n1\n",
"2\n1\n4\n1\n3\n2\n4\n1\n3\n2\n4\n3\n4\n3\n4\n",
"322\n392222\n392222\n"
]
}
|
CORRECT
|
java
|
import static java.lang.Math.max;
import static java.lang.Math.min;
import static java.lang.Math.abs;
import static java.lang.System.out;
import java.util.*;
import java.io.*;
import java.math.*;
public class Template {
static int mod = 1000000007;
public static void main(String[] args) {
FastScanner sc = new FastScanner();
int yo = 1;
while (yo-- > 0) {
Map<Integer,List<Integer>> map = new HashMap<>();
int n = sc.nextInt();
int[] a = sc.readInts(n);
Set<Integer> set = new HashSet<>();
for(int e : a) set.add(e);
for(int i = 0; i < n; i++) {
map.computeIfAbsent(a[i], x -> new ArrayList<>());
map.get(a[i]).add(i);
}
int q = sc.nextInt();
while(q-- > 0) {
int size = sc.nextInt();
int pos = sc.nextInt()-1;
List<Pair> list = new ArrayList<>();
PriorityQueue<Integer> pq = new PriorityQueue<>();
for(int e : set) pq.add(-e);
while(!pq.isEmpty()) {
int curr = -pq.poll();
List<Integer> indexes = map.get(curr);
int count = 0;
while(count < indexes.size()) {
list.add(new Pair(curr,indexes.get(count)));
if(list.size() == size) break;
count++;
}
if(list.size() == size) break;
}
Collections.sort(list, (x,y) -> x.index-y.index);
out.println(list.get(pos).num);
}
}
}
public static class Pair {
int num;
int index;
public Pair(int num, int index) {
this.num = num;
this.index = index;
}
}
public static void sort(int[] arr) {
ArrayList<Integer> ls = new ArrayList<Integer>();
for (int x : arr)
ls.add(x);
Collections.sort(ls);
for (int i = 0; i < arr.length; i++)
arr[i] = ls.get(i);
}
public static long gcd(long a, long b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
static boolean[] sieve(int N) {
boolean[] sieve = new boolean[N + 1];
for (int i = 2; i <= N; i++)
sieve[i] = true;
for (int i = 2; i <= N; i++) {
if (sieve[i]) {
for (int j = 2 * i; j <= N; j += i) {
sieve[j] = false;
}
}
}
return sieve;
}
public static long power(long x, long y, long p) {
long res = 1L;
x = x % p;
while (y > 0) {
if ((y & 1) == 1)
res = (res * x) % p;
y >>= 1;
x = (x * x) % p;
}
return res;
}
public static void print(int[] arr) {
//for debugging only
for (int x : arr)
out.print(x + " ");
out.println();
}
static class FastScanner {
private int BS = 1 << 16;
private char NC = (char) 0;
private byte[] buf = new byte[BS];
private int bId = 0, size = 0;
private char c = NC;
private double cnt = 1;
private BufferedInputStream in;
public FastScanner() {
in = new BufferedInputStream(System.in, BS);
}
public FastScanner(String s) {
try {
in = new BufferedInputStream(new FileInputStream(new File(s)), BS);
} catch (Exception e) {
in = new BufferedInputStream(System.in, BS);
}
}
private char getChar() {
while (bId == size) {
try {
size = in.read(buf);
} catch (Exception e) {
return NC;
}
if (size == -1)
return NC;
bId = 0;
}
return (char) buf[bId++];
}
public int nextInt() {
return (int) nextLong();
}
public int[] readInts(int N) {
int[] res = new int[N];
for (int i = 0; i < N; i++) {
res[i] = (int) nextLong();
}
return res;
}
public long[] readLongs(int N) {
long[] res = new long[N];
for (int i = 0; i < N; i++) {
res[i] = nextLong();
}
return res;
}
public long nextLong() {
cnt = 1;
boolean neg = false;
if (c == NC)
c = getChar();
for (; (c < '0' || c > '9'); c = getChar()) {
if (c == '-')
neg = true;
}
long res = 0;
for (; c >= '0' && c <= '9'; c = getChar()) {
res = (res << 3) + (res << 1) + c - '0';
cnt *= 10;
}
return neg ? -res : res;
}
public double nextDouble() {
double cur = nextLong();
return c != '.' ? cur : cur + nextLong() / cnt;
}
public double[] readDoubles(int N) {
double[] res = new double[N];
for (int i = 0; i < N; i++) {
res[i] = nextDouble();
}
return res;
}
public String next() {
StringBuilder res = new StringBuilder();
while (c <= 32)
c = getChar();
while (c > 32) {
res.append(c);
c = getChar();
}
return res.toString();
}
public String nextLine() {
StringBuilder res = new StringBuilder();
while (c <= 32)
c = getChar();
while (c != '\n') {
res.append(c);
c = getChar();
}
return res.toString();
}
public boolean hasNext() {
if (c > 32)
return true;
while (true) {
c = getChar();
if (c == NC)
return false;
else if (c > 32)
return true;
}
}
}
// For Input.txt and Output.txt
// FileInputStream in = new FileInputStream("input.txt");
// FileOutputStream out = new FileOutputStream("output.txt");
// PrintWriter pw = new PrintWriter(out);
// Scanner sc = new Scanner(in);
}
|
1227_D1. Optimal Subsequences (Easy Version)
|
This is the easier version of the problem. In this version 1 β€ n, m β€ 100. You can hack this problem only if you solve and lock both problems.
You are given a sequence of integers a=[a_1,a_2,...,a_n] of length n. Its subsequence is obtained by removing zero or more elements from the sequence a (they do not necessarily go consecutively). For example, for the sequence a=[11,20,11,33,11,20,11]:
* [11,20,11,33,11,20,11], [11,20,11,33,11,20], [11,11,11,11], [20], [33,20] are subsequences (these are just some of the long list);
* [40], [33,33], [33,20,20], [20,20,11,11] are not subsequences.
Suppose that an additional non-negative integer k (1 β€ k β€ n) is given, then the subsequence is called optimal if:
* it has a length of k and the sum of its elements is the maximum possible among all subsequences of length k;
* and among all subsequences of length k that satisfy the previous item, it is lexicographically minimal.
Recall that the sequence b=[b_1, b_2, ..., b_k] is lexicographically smaller than the sequence c=[c_1, c_2, ..., c_k] if the first element (from the left) in which they differ less in the sequence b than in c. Formally: there exists t (1 β€ t β€ k) such that b_1=c_1, b_2=c_2, ..., b_{t-1}=c_{t-1} and at the same time b_t<c_t. For example:
* [10, 20, 20] lexicographically less than [10, 21, 1],
* [7, 99, 99] is lexicographically less than [10, 21, 1],
* [10, 21, 0] is lexicographically less than [10, 21, 1].
You are given a sequence of a=[a_1,a_2,...,a_n] and m requests, each consisting of two numbers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j). For each query, print the value that is in the index pos_j of the optimal subsequence of the given sequence a for k=k_j.
For example, if n=4, a=[10,20,30,20], k_j=2, then the optimal subsequence is [20,30] β it is the minimum lexicographically among all subsequences of length 2 with the maximum total sum of items. Thus, the answer to the request k_j=2, pos_j=1 is the number 20, and the answer to the request k_j=2, pos_j=2 is the number 30.
Input
The first line contains an integer n (1 β€ n β€ 100) β the length of the sequence a.
The second line contains elements of the sequence a: integer numbers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9).
The third line contains an integer m (1 β€ m β€ 100) β the number of requests.
The following m lines contain pairs of integers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j) β the requests.
Output
Print m integers r_1, r_2, ..., r_m (1 β€ r_j β€ 10^9) one per line: answers to the requests in the order they appear in the input. The value of r_j should be equal to the value contained in the position pos_j of the optimal subsequence for k=k_j.
Examples
Input
3
10 20 10
6
1 1
2 1
2 2
3 1
3 2
3 3
Output
20
10
20
10
20
10
Input
7
1 2 1 3 1 2 1
9
2 1
2 2
3 1
3 2
3 3
1 1
7 1
7 7
7 4
Output
2
3
2
3
2
3
1
1
3
Note
In the first example, for a=[10,20,10] the optimal subsequences are:
* for k=1: [20],
* for k=2: [10,20],
* for k=3: [10,20,10].
|
{
"input": [
"3\n10 20 10\n6\n1 1\n2 1\n2 2\n3 1\n3 2\n3 3\n",
"7\n1 2 1 3 1 2 1\n9\n2 1\n2 2\n3 1\n3 2\n3 3\n1 1\n7 1\n7 7\n7 4\n"
],
"output": [
"20\n10\n20\n10\n20\n10\n",
"2\n3\n2\n3\n2\n3\n1\n1\n3\n"
]
}
|
{
"input": [
"2\n1 10\n3\n2 2\n2 1\n1 1\n",
"2\n3922 3922\n3\n2 2\n2 1\n1 1\n",
"1\n1000000000\n1\n1 1\n",
"1\n1\n3\n1 1\n1 1\n1 1\n",
"5\n3 1 4 1 2\n15\n5 5\n5 4\n5 3\n5 2\n5 1\n4 4\n4 3\n4 2\n4 1\n3 3\n3 2\n3 1\n2 2\n2 1\n1 1\n",
"2\n392222 322\n3\n2 2\n2 1\n1 1\n"
],
"output": [
"10\n1\n10\n",
"3922\n3922\n3922\n",
"1000000000\n",
"1\n1\n1\n",
"2\n1\n4\n1\n3\n2\n4\n1\n3\n2\n4\n3\n4\n3\n4\n",
"322\n392222\n392222\n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
vector<bool> sieve(long long n) {
vector<bool> prime(n + 1, true);
prime[0] = prime[1] = false;
for (long long i = 2; i <= n; ++i) {
if (prime[i] && (i * i) <= n)
for (long long j = i * i; j <= n; j += i) prime[j] = false;
}
return prime;
}
long long power(long long a, long long b, long long m = 1000000007) {
a %= m;
long long res = 1;
while (b > 0) {
if (b & 1) res = res * a % m;
a = a * a % m;
b >>= 1;
}
return res;
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long n;
cin >> n;
vector<long long> a(n);
for (long long i = 0; i < (long long)n; ++i) cin >> a[i];
map<long long, vector<long long> > mp;
for (long long i = 0; i < (long long)n; ++i) mp[a[i]].push_back(i);
long long t;
cin >> t;
while (t--) {
long long k, idx;
cin >> k >> idx;
auto it = mp.rbegin();
vector<long long> b;
while (k > 0) {
vector<long long> temp = it->second;
if (k >= (long long)temp.size()) {
k -= temp.size();
for (long long i = 0; i < (long long)temp.size(); ++i) {
b.push_back(temp[i]);
}
} else {
long long i = 0;
while (k--) {
b.push_back(temp[i]);
i++;
}
}
it++;
}
sort(b.begin(), b.end());
cout << a[b[idx - 1]] << '\n';
}
return 0;
}
|
1227_D1. Optimal Subsequences (Easy Version)
|
This is the easier version of the problem. In this version 1 β€ n, m β€ 100. You can hack this problem only if you solve and lock both problems.
You are given a sequence of integers a=[a_1,a_2,...,a_n] of length n. Its subsequence is obtained by removing zero or more elements from the sequence a (they do not necessarily go consecutively). For example, for the sequence a=[11,20,11,33,11,20,11]:
* [11,20,11,33,11,20,11], [11,20,11,33,11,20], [11,11,11,11], [20], [33,20] are subsequences (these are just some of the long list);
* [40], [33,33], [33,20,20], [20,20,11,11] are not subsequences.
Suppose that an additional non-negative integer k (1 β€ k β€ n) is given, then the subsequence is called optimal if:
* it has a length of k and the sum of its elements is the maximum possible among all subsequences of length k;
* and among all subsequences of length k that satisfy the previous item, it is lexicographically minimal.
Recall that the sequence b=[b_1, b_2, ..., b_k] is lexicographically smaller than the sequence c=[c_1, c_2, ..., c_k] if the first element (from the left) in which they differ less in the sequence b than in c. Formally: there exists t (1 β€ t β€ k) such that b_1=c_1, b_2=c_2, ..., b_{t-1}=c_{t-1} and at the same time b_t<c_t. For example:
* [10, 20, 20] lexicographically less than [10, 21, 1],
* [7, 99, 99] is lexicographically less than [10, 21, 1],
* [10, 21, 0] is lexicographically less than [10, 21, 1].
You are given a sequence of a=[a_1,a_2,...,a_n] and m requests, each consisting of two numbers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j). For each query, print the value that is in the index pos_j of the optimal subsequence of the given sequence a for k=k_j.
For example, if n=4, a=[10,20,30,20], k_j=2, then the optimal subsequence is [20,30] β it is the minimum lexicographically among all subsequences of length 2 with the maximum total sum of items. Thus, the answer to the request k_j=2, pos_j=1 is the number 20, and the answer to the request k_j=2, pos_j=2 is the number 30.
Input
The first line contains an integer n (1 β€ n β€ 100) β the length of the sequence a.
The second line contains elements of the sequence a: integer numbers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9).
The third line contains an integer m (1 β€ m β€ 100) β the number of requests.
The following m lines contain pairs of integers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j) β the requests.
Output
Print m integers r_1, r_2, ..., r_m (1 β€ r_j β€ 10^9) one per line: answers to the requests in the order they appear in the input. The value of r_j should be equal to the value contained in the position pos_j of the optimal subsequence for k=k_j.
Examples
Input
3
10 20 10
6
1 1
2 1
2 2
3 1
3 2
3 3
Output
20
10
20
10
20
10
Input
7
1 2 1 3 1 2 1
9
2 1
2 2
3 1
3 2
3 3
1 1
7 1
7 7
7 4
Output
2
3
2
3
2
3
1
1
3
Note
In the first example, for a=[10,20,10] the optimal subsequences are:
* for k=1: [20],
* for k=2: [10,20],
* for k=3: [10,20,10].
|
{
"input": [
"3\n10 20 10\n6\n1 1\n2 1\n2 2\n3 1\n3 2\n3 3\n",
"7\n1 2 1 3 1 2 1\n9\n2 1\n2 2\n3 1\n3 2\n3 3\n1 1\n7 1\n7 7\n7 4\n"
],
"output": [
"20\n10\n20\n10\n20\n10\n",
"2\n3\n2\n3\n2\n3\n1\n1\n3\n"
]
}
|
{
"input": [
"2\n1 10\n3\n2 2\n2 1\n1 1\n",
"2\n3922 3922\n3\n2 2\n2 1\n1 1\n",
"1\n1000000000\n1\n1 1\n",
"1\n1\n3\n1 1\n1 1\n1 1\n",
"5\n3 1 4 1 2\n15\n5 5\n5 4\n5 3\n5 2\n5 1\n4 4\n4 3\n4 2\n4 1\n3 3\n3 2\n3 1\n2 2\n2 1\n1 1\n",
"2\n392222 322\n3\n2 2\n2 1\n1 1\n"
],
"output": [
"10\n1\n10\n",
"3922\n3922\n3922\n",
"1000000000\n",
"1\n1\n1\n",
"2\n1\n4\n1\n3\n2\n4\n1\n3\n2\n4\n3\n4\n3\n4\n",
"322\n392222\n392222\n"
]
}
|
CORRECT
|
python3
|
def f(a):
return -a[1]
import sys
fin = sys.stdin
n = int(input())
arr = list(map(int, fin.readline().split()))
arr_ind = list()
for i in range(n):
arr_ind.append([arr[i], -i])
arr_ind.sort()
arr_ind.reverse()
ans_array = list()
for k in range(1, n + 1):
ans = arr_ind[:k]
ans.sort(key=f)
ans_array.append(ans)
tests = int(input())
for test in range(tests):
k, pos = map(int, input().split())
print(ans_array[k - 1][pos - 1][0])
|
1227_D1. Optimal Subsequences (Easy Version)
|
This is the easier version of the problem. In this version 1 β€ n, m β€ 100. You can hack this problem only if you solve and lock both problems.
You are given a sequence of integers a=[a_1,a_2,...,a_n] of length n. Its subsequence is obtained by removing zero or more elements from the sequence a (they do not necessarily go consecutively). For example, for the sequence a=[11,20,11,33,11,20,11]:
* [11,20,11,33,11,20,11], [11,20,11,33,11,20], [11,11,11,11], [20], [33,20] are subsequences (these are just some of the long list);
* [40], [33,33], [33,20,20], [20,20,11,11] are not subsequences.
Suppose that an additional non-negative integer k (1 β€ k β€ n) is given, then the subsequence is called optimal if:
* it has a length of k and the sum of its elements is the maximum possible among all subsequences of length k;
* and among all subsequences of length k that satisfy the previous item, it is lexicographically minimal.
Recall that the sequence b=[b_1, b_2, ..., b_k] is lexicographically smaller than the sequence c=[c_1, c_2, ..., c_k] if the first element (from the left) in which they differ less in the sequence b than in c. Formally: there exists t (1 β€ t β€ k) such that b_1=c_1, b_2=c_2, ..., b_{t-1}=c_{t-1} and at the same time b_t<c_t. For example:
* [10, 20, 20] lexicographically less than [10, 21, 1],
* [7, 99, 99] is lexicographically less than [10, 21, 1],
* [10, 21, 0] is lexicographically less than [10, 21, 1].
You are given a sequence of a=[a_1,a_2,...,a_n] and m requests, each consisting of two numbers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j). For each query, print the value that is in the index pos_j of the optimal subsequence of the given sequence a for k=k_j.
For example, if n=4, a=[10,20,30,20], k_j=2, then the optimal subsequence is [20,30] β it is the minimum lexicographically among all subsequences of length 2 with the maximum total sum of items. Thus, the answer to the request k_j=2, pos_j=1 is the number 20, and the answer to the request k_j=2, pos_j=2 is the number 30.
Input
The first line contains an integer n (1 β€ n β€ 100) β the length of the sequence a.
The second line contains elements of the sequence a: integer numbers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9).
The third line contains an integer m (1 β€ m β€ 100) β the number of requests.
The following m lines contain pairs of integers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j) β the requests.
Output
Print m integers r_1, r_2, ..., r_m (1 β€ r_j β€ 10^9) one per line: answers to the requests in the order they appear in the input. The value of r_j should be equal to the value contained in the position pos_j of the optimal subsequence for k=k_j.
Examples
Input
3
10 20 10
6
1 1
2 1
2 2
3 1
3 2
3 3
Output
20
10
20
10
20
10
Input
7
1 2 1 3 1 2 1
9
2 1
2 2
3 1
3 2
3 3
1 1
7 1
7 7
7 4
Output
2
3
2
3
2
3
1
1
3
Note
In the first example, for a=[10,20,10] the optimal subsequences are:
* for k=1: [20],
* for k=2: [10,20],
* for k=3: [10,20,10].
|
{
"input": [
"3\n10 20 10\n6\n1 1\n2 1\n2 2\n3 1\n3 2\n3 3\n",
"7\n1 2 1 3 1 2 1\n9\n2 1\n2 2\n3 1\n3 2\n3 3\n1 1\n7 1\n7 7\n7 4\n"
],
"output": [
"20\n10\n20\n10\n20\n10\n",
"2\n3\n2\n3\n2\n3\n1\n1\n3\n"
]
}
|
{
"input": [
"2\n1 10\n3\n2 2\n2 1\n1 1\n",
"2\n3922 3922\n3\n2 2\n2 1\n1 1\n",
"1\n1000000000\n1\n1 1\n",
"1\n1\n3\n1 1\n1 1\n1 1\n",
"5\n3 1 4 1 2\n15\n5 5\n5 4\n5 3\n5 2\n5 1\n4 4\n4 3\n4 2\n4 1\n3 3\n3 2\n3 1\n2 2\n2 1\n1 1\n",
"2\n392222 322\n3\n2 2\n2 1\n1 1\n"
],
"output": [
"10\n1\n10\n",
"3922\n3922\n3922\n",
"1000000000\n",
"1\n1\n1\n",
"2\n1\n4\n1\n3\n2\n4\n1\n3\n2\n4\n3\n4\n3\n4\n",
"322\n392222\n392222\n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
template <class c>
struct rge {
c b, e;
};
template <class c>
rge<c> range(c i, c j) {
return rge<c>{i, j};
}
template <class c>
auto dud(c* x) -> decltype(cerr << *x, 0);
template <class c>
char dud(...);
struct debug {
template <class c>
debug& operator<<(const c&) {
return *this;
}
};
const long long MOD = 1e9 + 7;
const long long N = 1e7 + 10;
const long long INF = 1e18 + 10;
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
long long n;
cin >> n;
vector<long long> v(n);
map<long long, vector<long long> > mp;
for (long long i = 0; i < n; i++) {
cin >> v[i];
mp[v[i]].push_back(i);
}
vector<long long> t = v;
sort(t.begin(), t.end());
long long q;
cin >> q;
while (q--) {
long long k, pos;
cin >> k >> pos;
pos--;
vector<pair<long long, long long> > ans;
map<long long, long long> cnt;
for (long long i = n - k; i < n; i++) {
ans.push_back(make_pair(mp[t[i]][cnt[t[i]]], t[i]));
cnt[t[i]]++;
}
sort(ans.begin(), ans.end());
cout << ans[pos].second << endl;
}
return 0;
}
|
1227_D1. Optimal Subsequences (Easy Version)
|
This is the easier version of the problem. In this version 1 β€ n, m β€ 100. You can hack this problem only if you solve and lock both problems.
You are given a sequence of integers a=[a_1,a_2,...,a_n] of length n. Its subsequence is obtained by removing zero or more elements from the sequence a (they do not necessarily go consecutively). For example, for the sequence a=[11,20,11,33,11,20,11]:
* [11,20,11,33,11,20,11], [11,20,11,33,11,20], [11,11,11,11], [20], [33,20] are subsequences (these are just some of the long list);
* [40], [33,33], [33,20,20], [20,20,11,11] are not subsequences.
Suppose that an additional non-negative integer k (1 β€ k β€ n) is given, then the subsequence is called optimal if:
* it has a length of k and the sum of its elements is the maximum possible among all subsequences of length k;
* and among all subsequences of length k that satisfy the previous item, it is lexicographically minimal.
Recall that the sequence b=[b_1, b_2, ..., b_k] is lexicographically smaller than the sequence c=[c_1, c_2, ..., c_k] if the first element (from the left) in which they differ less in the sequence b than in c. Formally: there exists t (1 β€ t β€ k) such that b_1=c_1, b_2=c_2, ..., b_{t-1}=c_{t-1} and at the same time b_t<c_t. For example:
* [10, 20, 20] lexicographically less than [10, 21, 1],
* [7, 99, 99] is lexicographically less than [10, 21, 1],
* [10, 21, 0] is lexicographically less than [10, 21, 1].
You are given a sequence of a=[a_1,a_2,...,a_n] and m requests, each consisting of two numbers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j). For each query, print the value that is in the index pos_j of the optimal subsequence of the given sequence a for k=k_j.
For example, if n=4, a=[10,20,30,20], k_j=2, then the optimal subsequence is [20,30] β it is the minimum lexicographically among all subsequences of length 2 with the maximum total sum of items. Thus, the answer to the request k_j=2, pos_j=1 is the number 20, and the answer to the request k_j=2, pos_j=2 is the number 30.
Input
The first line contains an integer n (1 β€ n β€ 100) β the length of the sequence a.
The second line contains elements of the sequence a: integer numbers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9).
The third line contains an integer m (1 β€ m β€ 100) β the number of requests.
The following m lines contain pairs of integers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j) β the requests.
Output
Print m integers r_1, r_2, ..., r_m (1 β€ r_j β€ 10^9) one per line: answers to the requests in the order they appear in the input. The value of r_j should be equal to the value contained in the position pos_j of the optimal subsequence for k=k_j.
Examples
Input
3
10 20 10
6
1 1
2 1
2 2
3 1
3 2
3 3
Output
20
10
20
10
20
10
Input
7
1 2 1 3 1 2 1
9
2 1
2 2
3 1
3 2
3 3
1 1
7 1
7 7
7 4
Output
2
3
2
3
2
3
1
1
3
Note
In the first example, for a=[10,20,10] the optimal subsequences are:
* for k=1: [20],
* for k=2: [10,20],
* for k=3: [10,20,10].
|
{
"input": [
"3\n10 20 10\n6\n1 1\n2 1\n2 2\n3 1\n3 2\n3 3\n",
"7\n1 2 1 3 1 2 1\n9\n2 1\n2 2\n3 1\n3 2\n3 3\n1 1\n7 1\n7 7\n7 4\n"
],
"output": [
"20\n10\n20\n10\n20\n10\n",
"2\n3\n2\n3\n2\n3\n1\n1\n3\n"
]
}
|
{
"input": [
"2\n1 10\n3\n2 2\n2 1\n1 1\n",
"2\n3922 3922\n3\n2 2\n2 1\n1 1\n",
"1\n1000000000\n1\n1 1\n",
"1\n1\n3\n1 1\n1 1\n1 1\n",
"5\n3 1 4 1 2\n15\n5 5\n5 4\n5 3\n5 2\n5 1\n4 4\n4 3\n4 2\n4 1\n3 3\n3 2\n3 1\n2 2\n2 1\n1 1\n",
"2\n392222 322\n3\n2 2\n2 1\n1 1\n"
],
"output": [
"10\n1\n10\n",
"3922\n3922\n3922\n",
"1000000000\n",
"1\n1\n1\n",
"2\n1\n4\n1\n3\n2\n4\n1\n3\n2\n4\n3\n4\n3\n4\n",
"322\n392222\n392222\n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cout.precision(20);
long long n = 0;
cin >> n;
deque<long long> posl(n);
for (auto& p : posl) cin >> p;
map<long long, long long> al;
for (auto& p : posl) al[p]++;
long long m = 0;
cin >> m;
for (long long k = 0; k < m; k++) {
long long q = 0, pos = 0, sum = 0, num = 0, need = 0;
cin >> q >> pos;
auto it = al.end(), ed = al.begin();
it--, ed--;
for (it; it != ed; it--) {
if (sum + it->second < q)
sum += it->second;
else {
need = q - sum;
num = it->first;
break;
}
}
deque<long long> ans;
for (auto& p : posl) {
if (p == num && need > 0)
ans.push_back(p), need--;
else if (p > num)
ans.push_back(p);
}
cout << ans[pos - 1] << "\n";
}
return 0;
}
|
1227_D1. Optimal Subsequences (Easy Version)
|
This is the easier version of the problem. In this version 1 β€ n, m β€ 100. You can hack this problem only if you solve and lock both problems.
You are given a sequence of integers a=[a_1,a_2,...,a_n] of length n. Its subsequence is obtained by removing zero or more elements from the sequence a (they do not necessarily go consecutively). For example, for the sequence a=[11,20,11,33,11,20,11]:
* [11,20,11,33,11,20,11], [11,20,11,33,11,20], [11,11,11,11], [20], [33,20] are subsequences (these are just some of the long list);
* [40], [33,33], [33,20,20], [20,20,11,11] are not subsequences.
Suppose that an additional non-negative integer k (1 β€ k β€ n) is given, then the subsequence is called optimal if:
* it has a length of k and the sum of its elements is the maximum possible among all subsequences of length k;
* and among all subsequences of length k that satisfy the previous item, it is lexicographically minimal.
Recall that the sequence b=[b_1, b_2, ..., b_k] is lexicographically smaller than the sequence c=[c_1, c_2, ..., c_k] if the first element (from the left) in which they differ less in the sequence b than in c. Formally: there exists t (1 β€ t β€ k) such that b_1=c_1, b_2=c_2, ..., b_{t-1}=c_{t-1} and at the same time b_t<c_t. For example:
* [10, 20, 20] lexicographically less than [10, 21, 1],
* [7, 99, 99] is lexicographically less than [10, 21, 1],
* [10, 21, 0] is lexicographically less than [10, 21, 1].
You are given a sequence of a=[a_1,a_2,...,a_n] and m requests, each consisting of two numbers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j). For each query, print the value that is in the index pos_j of the optimal subsequence of the given sequence a for k=k_j.
For example, if n=4, a=[10,20,30,20], k_j=2, then the optimal subsequence is [20,30] β it is the minimum lexicographically among all subsequences of length 2 with the maximum total sum of items. Thus, the answer to the request k_j=2, pos_j=1 is the number 20, and the answer to the request k_j=2, pos_j=2 is the number 30.
Input
The first line contains an integer n (1 β€ n β€ 100) β the length of the sequence a.
The second line contains elements of the sequence a: integer numbers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9).
The third line contains an integer m (1 β€ m β€ 100) β the number of requests.
The following m lines contain pairs of integers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j) β the requests.
Output
Print m integers r_1, r_2, ..., r_m (1 β€ r_j β€ 10^9) one per line: answers to the requests in the order they appear in the input. The value of r_j should be equal to the value contained in the position pos_j of the optimal subsequence for k=k_j.
Examples
Input
3
10 20 10
6
1 1
2 1
2 2
3 1
3 2
3 3
Output
20
10
20
10
20
10
Input
7
1 2 1 3 1 2 1
9
2 1
2 2
3 1
3 2
3 3
1 1
7 1
7 7
7 4
Output
2
3
2
3
2
3
1
1
3
Note
In the first example, for a=[10,20,10] the optimal subsequences are:
* for k=1: [20],
* for k=2: [10,20],
* for k=3: [10,20,10].
|
{
"input": [
"3\n10 20 10\n6\n1 1\n2 1\n2 2\n3 1\n3 2\n3 3\n",
"7\n1 2 1 3 1 2 1\n9\n2 1\n2 2\n3 1\n3 2\n3 3\n1 1\n7 1\n7 7\n7 4\n"
],
"output": [
"20\n10\n20\n10\n20\n10\n",
"2\n3\n2\n3\n2\n3\n1\n1\n3\n"
]
}
|
{
"input": [
"2\n1 10\n3\n2 2\n2 1\n1 1\n",
"2\n3922 3922\n3\n2 2\n2 1\n1 1\n",
"1\n1000000000\n1\n1 1\n",
"1\n1\n3\n1 1\n1 1\n1 1\n",
"5\n3 1 4 1 2\n15\n5 5\n5 4\n5 3\n5 2\n5 1\n4 4\n4 3\n4 2\n4 1\n3 3\n3 2\n3 1\n2 2\n2 1\n1 1\n",
"2\n392222 322\n3\n2 2\n2 1\n1 1\n"
],
"output": [
"10\n1\n10\n",
"3922\n3922\n3922\n",
"1000000000\n",
"1\n1\n1\n",
"2\n1\n4\n1\n3\n2\n4\n1\n3\n2\n4\n3\n4\n3\n4\n",
"322\n392222\n392222\n"
]
}
|
CORRECT
|
python3
|
n = int(input())
nar = list(map(int, input().split()))
m = int(input())
for q in range(m):
k, p = list(map(int, input().split()))
mar = []
for i in range(k):
mar.append(nar[i])
for i in range(k, n):
minItem = 0
for j in range(1, k):
if mar[j] <= mar[minItem]:
minItem = j
if nar[i] > mar[minItem]:
mar.pop(minItem)
mar.append(nar[i])
print(mar[p-1])
|
1227_D1. Optimal Subsequences (Easy Version)
|
This is the easier version of the problem. In this version 1 β€ n, m β€ 100. You can hack this problem only if you solve and lock both problems.
You are given a sequence of integers a=[a_1,a_2,...,a_n] of length n. Its subsequence is obtained by removing zero or more elements from the sequence a (they do not necessarily go consecutively). For example, for the sequence a=[11,20,11,33,11,20,11]:
* [11,20,11,33,11,20,11], [11,20,11,33,11,20], [11,11,11,11], [20], [33,20] are subsequences (these are just some of the long list);
* [40], [33,33], [33,20,20], [20,20,11,11] are not subsequences.
Suppose that an additional non-negative integer k (1 β€ k β€ n) is given, then the subsequence is called optimal if:
* it has a length of k and the sum of its elements is the maximum possible among all subsequences of length k;
* and among all subsequences of length k that satisfy the previous item, it is lexicographically minimal.
Recall that the sequence b=[b_1, b_2, ..., b_k] is lexicographically smaller than the sequence c=[c_1, c_2, ..., c_k] if the first element (from the left) in which they differ less in the sequence b than in c. Formally: there exists t (1 β€ t β€ k) such that b_1=c_1, b_2=c_2, ..., b_{t-1}=c_{t-1} and at the same time b_t<c_t. For example:
* [10, 20, 20] lexicographically less than [10, 21, 1],
* [7, 99, 99] is lexicographically less than [10, 21, 1],
* [10, 21, 0] is lexicographically less than [10, 21, 1].
You are given a sequence of a=[a_1,a_2,...,a_n] and m requests, each consisting of two numbers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j). For each query, print the value that is in the index pos_j of the optimal subsequence of the given sequence a for k=k_j.
For example, if n=4, a=[10,20,30,20], k_j=2, then the optimal subsequence is [20,30] β it is the minimum lexicographically among all subsequences of length 2 with the maximum total sum of items. Thus, the answer to the request k_j=2, pos_j=1 is the number 20, and the answer to the request k_j=2, pos_j=2 is the number 30.
Input
The first line contains an integer n (1 β€ n β€ 100) β the length of the sequence a.
The second line contains elements of the sequence a: integer numbers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9).
The third line contains an integer m (1 β€ m β€ 100) β the number of requests.
The following m lines contain pairs of integers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j) β the requests.
Output
Print m integers r_1, r_2, ..., r_m (1 β€ r_j β€ 10^9) one per line: answers to the requests in the order they appear in the input. The value of r_j should be equal to the value contained in the position pos_j of the optimal subsequence for k=k_j.
Examples
Input
3
10 20 10
6
1 1
2 1
2 2
3 1
3 2
3 3
Output
20
10
20
10
20
10
Input
7
1 2 1 3 1 2 1
9
2 1
2 2
3 1
3 2
3 3
1 1
7 1
7 7
7 4
Output
2
3
2
3
2
3
1
1
3
Note
In the first example, for a=[10,20,10] the optimal subsequences are:
* for k=1: [20],
* for k=2: [10,20],
* for k=3: [10,20,10].
|
{
"input": [
"3\n10 20 10\n6\n1 1\n2 1\n2 2\n3 1\n3 2\n3 3\n",
"7\n1 2 1 3 1 2 1\n9\n2 1\n2 2\n3 1\n3 2\n3 3\n1 1\n7 1\n7 7\n7 4\n"
],
"output": [
"20\n10\n20\n10\n20\n10\n",
"2\n3\n2\n3\n2\n3\n1\n1\n3\n"
]
}
|
{
"input": [
"2\n1 10\n3\n2 2\n2 1\n1 1\n",
"2\n3922 3922\n3\n2 2\n2 1\n1 1\n",
"1\n1000000000\n1\n1 1\n",
"1\n1\n3\n1 1\n1 1\n1 1\n",
"5\n3 1 4 1 2\n15\n5 5\n5 4\n5 3\n5 2\n5 1\n4 4\n4 3\n4 2\n4 1\n3 3\n3 2\n3 1\n2 2\n2 1\n1 1\n",
"2\n392222 322\n3\n2 2\n2 1\n1 1\n"
],
"output": [
"10\n1\n10\n",
"3922\n3922\n3922\n",
"1000000000\n",
"1\n1\n1\n",
"2\n1\n4\n1\n3\n2\n4\n1\n3\n2\n4\n3\n4\n3\n4\n",
"322\n392222\n392222\n"
]
}
|
CORRECT
|
java
|
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) {
FastReader input=new FastReader();
PrintWriter out=new PrintWriter(System.out);
int T=1;
while(T-->0)
{
int n=input.nextInt();
ArrayList<Integer> list=new ArrayList<>();
int a[]=new int[n];
for(int i=0;i<n;i++)
{
a[i]=input.nextInt();
list.add(a[i]);
}
Collections.sort(list,Collections.reverseOrder());
int m=input.nextInt();
for(int i=0;i<m;i++)
{
int k,pos;
k=input.nextInt();
pos=input.nextInt();
HashMap<Integer,Integer> map=new HashMap<>();
for(int j=0;j<k;j++)
{
if(map.containsKey(list.get(j)))
{
map.put(list.get(j),map.get(list.get(j))+1);
}
else
{
map.put(list.get(j),1);
}
}
ArrayList<Integer> ans=new ArrayList<>();
for(int j=0;j<n;j++)
{
if(map.containsKey(a[j]))
{
ans.add(a[j]);
if(map.get(a[j])==1)
{
map.remove(a[j]);
}
else
{
map.put(a[j],map.get(a[j])-1);
}
}
}
out.println(ans.get(pos-1));
}
}
out.close();
}
public static int greaterNo(ArrayList<Integer> a,int k)
{
int i=0,j=a.size();
while(i<j)
{
int mid=(i+j)/2;
if(a.get(mid)>k)
{
j=mid;
}
else
{
i=mid+1;
}
}
return i-1;
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
}
|
1227_D1. Optimal Subsequences (Easy Version)
|
This is the easier version of the problem. In this version 1 β€ n, m β€ 100. You can hack this problem only if you solve and lock both problems.
You are given a sequence of integers a=[a_1,a_2,...,a_n] of length n. Its subsequence is obtained by removing zero or more elements from the sequence a (they do not necessarily go consecutively). For example, for the sequence a=[11,20,11,33,11,20,11]:
* [11,20,11,33,11,20,11], [11,20,11,33,11,20], [11,11,11,11], [20], [33,20] are subsequences (these are just some of the long list);
* [40], [33,33], [33,20,20], [20,20,11,11] are not subsequences.
Suppose that an additional non-negative integer k (1 β€ k β€ n) is given, then the subsequence is called optimal if:
* it has a length of k and the sum of its elements is the maximum possible among all subsequences of length k;
* and among all subsequences of length k that satisfy the previous item, it is lexicographically minimal.
Recall that the sequence b=[b_1, b_2, ..., b_k] is lexicographically smaller than the sequence c=[c_1, c_2, ..., c_k] if the first element (from the left) in which they differ less in the sequence b than in c. Formally: there exists t (1 β€ t β€ k) such that b_1=c_1, b_2=c_2, ..., b_{t-1}=c_{t-1} and at the same time b_t<c_t. For example:
* [10, 20, 20] lexicographically less than [10, 21, 1],
* [7, 99, 99] is lexicographically less than [10, 21, 1],
* [10, 21, 0] is lexicographically less than [10, 21, 1].
You are given a sequence of a=[a_1,a_2,...,a_n] and m requests, each consisting of two numbers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j). For each query, print the value that is in the index pos_j of the optimal subsequence of the given sequence a for k=k_j.
For example, if n=4, a=[10,20,30,20], k_j=2, then the optimal subsequence is [20,30] β it is the minimum lexicographically among all subsequences of length 2 with the maximum total sum of items. Thus, the answer to the request k_j=2, pos_j=1 is the number 20, and the answer to the request k_j=2, pos_j=2 is the number 30.
Input
The first line contains an integer n (1 β€ n β€ 100) β the length of the sequence a.
The second line contains elements of the sequence a: integer numbers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9).
The third line contains an integer m (1 β€ m β€ 100) β the number of requests.
The following m lines contain pairs of integers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j) β the requests.
Output
Print m integers r_1, r_2, ..., r_m (1 β€ r_j β€ 10^9) one per line: answers to the requests in the order they appear in the input. The value of r_j should be equal to the value contained in the position pos_j of the optimal subsequence for k=k_j.
Examples
Input
3
10 20 10
6
1 1
2 1
2 2
3 1
3 2
3 3
Output
20
10
20
10
20
10
Input
7
1 2 1 3 1 2 1
9
2 1
2 2
3 1
3 2
3 3
1 1
7 1
7 7
7 4
Output
2
3
2
3
2
3
1
1
3
Note
In the first example, for a=[10,20,10] the optimal subsequences are:
* for k=1: [20],
* for k=2: [10,20],
* for k=3: [10,20,10].
|
{
"input": [
"3\n10 20 10\n6\n1 1\n2 1\n2 2\n3 1\n3 2\n3 3\n",
"7\n1 2 1 3 1 2 1\n9\n2 1\n2 2\n3 1\n3 2\n3 3\n1 1\n7 1\n7 7\n7 4\n"
],
"output": [
"20\n10\n20\n10\n20\n10\n",
"2\n3\n2\n3\n2\n3\n1\n1\n3\n"
]
}
|
{
"input": [
"2\n1 10\n3\n2 2\n2 1\n1 1\n",
"2\n3922 3922\n3\n2 2\n2 1\n1 1\n",
"1\n1000000000\n1\n1 1\n",
"1\n1\n3\n1 1\n1 1\n1 1\n",
"5\n3 1 4 1 2\n15\n5 5\n5 4\n5 3\n5 2\n5 1\n4 4\n4 3\n4 2\n4 1\n3 3\n3 2\n3 1\n2 2\n2 1\n1 1\n",
"2\n392222 322\n3\n2 2\n2 1\n1 1\n"
],
"output": [
"10\n1\n10\n",
"3922\n3922\n3922\n",
"1000000000\n",
"1\n1\n1\n",
"2\n1\n4\n1\n3\n2\n4\n1\n3\n2\n4\n3\n4\n3\n4\n",
"322\n392222\n392222\n"
]
}
|
CORRECT
|
python3
|
import sys
# from math
import bisect
import heapq
# from collections import deque
# from types import GeneratorType
# def bootstrap(func, stack=[]):
# def wrapped_function(*args, **kwargs):
# if stack:
# return func(*args, **kwargs)
# else:
# call = func(*args, **kwargs)
# while True:
# if type(call) is GeneratorType:
# stack.append(call)
# call = next(call)
# else:
# stack.pop()
# if not stack:
# break
# call = stack[-1].send(call)
# return call
# return wrapped_function
Ri = lambda : [int(x) for x in sys.stdin.readline().split()]
ri = lambda : sys.stdin.readline().strip()
def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
INF = 10 ** 18
MOD = 10**9+7
n = int(ri())
ar = Ri()
arr= [(ar[i],i) for i in range(n)]
arr.sort(key = lambda x: -x[0])
for _ in range(int(ri())):
a,b = Ri()
temp = []
for i in range(a):
temp.append(arr[i][1])
temp.sort()
print(ar[temp[b-1]])
|
1227_D1. Optimal Subsequences (Easy Version)
|
This is the easier version of the problem. In this version 1 β€ n, m β€ 100. You can hack this problem only if you solve and lock both problems.
You are given a sequence of integers a=[a_1,a_2,...,a_n] of length n. Its subsequence is obtained by removing zero or more elements from the sequence a (they do not necessarily go consecutively). For example, for the sequence a=[11,20,11,33,11,20,11]:
* [11,20,11,33,11,20,11], [11,20,11,33,11,20], [11,11,11,11], [20], [33,20] are subsequences (these are just some of the long list);
* [40], [33,33], [33,20,20], [20,20,11,11] are not subsequences.
Suppose that an additional non-negative integer k (1 β€ k β€ n) is given, then the subsequence is called optimal if:
* it has a length of k and the sum of its elements is the maximum possible among all subsequences of length k;
* and among all subsequences of length k that satisfy the previous item, it is lexicographically minimal.
Recall that the sequence b=[b_1, b_2, ..., b_k] is lexicographically smaller than the sequence c=[c_1, c_2, ..., c_k] if the first element (from the left) in which they differ less in the sequence b than in c. Formally: there exists t (1 β€ t β€ k) such that b_1=c_1, b_2=c_2, ..., b_{t-1}=c_{t-1} and at the same time b_t<c_t. For example:
* [10, 20, 20] lexicographically less than [10, 21, 1],
* [7, 99, 99] is lexicographically less than [10, 21, 1],
* [10, 21, 0] is lexicographically less than [10, 21, 1].
You are given a sequence of a=[a_1,a_2,...,a_n] and m requests, each consisting of two numbers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j). For each query, print the value that is in the index pos_j of the optimal subsequence of the given sequence a for k=k_j.
For example, if n=4, a=[10,20,30,20], k_j=2, then the optimal subsequence is [20,30] β it is the minimum lexicographically among all subsequences of length 2 with the maximum total sum of items. Thus, the answer to the request k_j=2, pos_j=1 is the number 20, and the answer to the request k_j=2, pos_j=2 is the number 30.
Input
The first line contains an integer n (1 β€ n β€ 100) β the length of the sequence a.
The second line contains elements of the sequence a: integer numbers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9).
The third line contains an integer m (1 β€ m β€ 100) β the number of requests.
The following m lines contain pairs of integers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j) β the requests.
Output
Print m integers r_1, r_2, ..., r_m (1 β€ r_j β€ 10^9) one per line: answers to the requests in the order they appear in the input. The value of r_j should be equal to the value contained in the position pos_j of the optimal subsequence for k=k_j.
Examples
Input
3
10 20 10
6
1 1
2 1
2 2
3 1
3 2
3 3
Output
20
10
20
10
20
10
Input
7
1 2 1 3 1 2 1
9
2 1
2 2
3 1
3 2
3 3
1 1
7 1
7 7
7 4
Output
2
3
2
3
2
3
1
1
3
Note
In the first example, for a=[10,20,10] the optimal subsequences are:
* for k=1: [20],
* for k=2: [10,20],
* for k=3: [10,20,10].
|
{
"input": [
"3\n10 20 10\n6\n1 1\n2 1\n2 2\n3 1\n3 2\n3 3\n",
"7\n1 2 1 3 1 2 1\n9\n2 1\n2 2\n3 1\n3 2\n3 3\n1 1\n7 1\n7 7\n7 4\n"
],
"output": [
"20\n10\n20\n10\n20\n10\n",
"2\n3\n2\n3\n2\n3\n1\n1\n3\n"
]
}
|
{
"input": [
"2\n1 10\n3\n2 2\n2 1\n1 1\n",
"2\n3922 3922\n3\n2 2\n2 1\n1 1\n",
"1\n1000000000\n1\n1 1\n",
"1\n1\n3\n1 1\n1 1\n1 1\n",
"5\n3 1 4 1 2\n15\n5 5\n5 4\n5 3\n5 2\n5 1\n4 4\n4 3\n4 2\n4 1\n3 3\n3 2\n3 1\n2 2\n2 1\n1 1\n",
"2\n392222 322\n3\n2 2\n2 1\n1 1\n"
],
"output": [
"10\n1\n10\n",
"3922\n3922\n3922\n",
"1000000000\n",
"1\n1\n1\n",
"2\n1\n4\n1\n3\n2\n4\n1\n3\n2\n4\n3\n4\n3\n4\n",
"322\n392222\n392222\n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
int main() {
long long int i, j, k, l, m, n;
cin >> n;
long long int a[n];
vector<long long int> v;
for (i = 0; i < n; i++) {
cin >> a[i];
v.push_back(a[i]);
}
sort(v.begin(), v.end());
long long int var;
cin >> var;
while (var--) {
cin >> k >> l;
map<long long int, long long int> mp;
for (i = n - k; i < n; i++) mp[v[i]]++;
long long int it = 0;
for (i = 0; i < n; i++) {
if (mp[a[i]]) {
if (it == l - 1) {
cout << a[i] << endl;
break;
} else
it++;
mp[a[i]]--;
}
}
}
}
|
1227_D1. Optimal Subsequences (Easy Version)
|
This is the easier version of the problem. In this version 1 β€ n, m β€ 100. You can hack this problem only if you solve and lock both problems.
You are given a sequence of integers a=[a_1,a_2,...,a_n] of length n. Its subsequence is obtained by removing zero or more elements from the sequence a (they do not necessarily go consecutively). For example, for the sequence a=[11,20,11,33,11,20,11]:
* [11,20,11,33,11,20,11], [11,20,11,33,11,20], [11,11,11,11], [20], [33,20] are subsequences (these are just some of the long list);
* [40], [33,33], [33,20,20], [20,20,11,11] are not subsequences.
Suppose that an additional non-negative integer k (1 β€ k β€ n) is given, then the subsequence is called optimal if:
* it has a length of k and the sum of its elements is the maximum possible among all subsequences of length k;
* and among all subsequences of length k that satisfy the previous item, it is lexicographically minimal.
Recall that the sequence b=[b_1, b_2, ..., b_k] is lexicographically smaller than the sequence c=[c_1, c_2, ..., c_k] if the first element (from the left) in which they differ less in the sequence b than in c. Formally: there exists t (1 β€ t β€ k) such that b_1=c_1, b_2=c_2, ..., b_{t-1}=c_{t-1} and at the same time b_t<c_t. For example:
* [10, 20, 20] lexicographically less than [10, 21, 1],
* [7, 99, 99] is lexicographically less than [10, 21, 1],
* [10, 21, 0] is lexicographically less than [10, 21, 1].
You are given a sequence of a=[a_1,a_2,...,a_n] and m requests, each consisting of two numbers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j). For each query, print the value that is in the index pos_j of the optimal subsequence of the given sequence a for k=k_j.
For example, if n=4, a=[10,20,30,20], k_j=2, then the optimal subsequence is [20,30] β it is the minimum lexicographically among all subsequences of length 2 with the maximum total sum of items. Thus, the answer to the request k_j=2, pos_j=1 is the number 20, and the answer to the request k_j=2, pos_j=2 is the number 30.
Input
The first line contains an integer n (1 β€ n β€ 100) β the length of the sequence a.
The second line contains elements of the sequence a: integer numbers a_1, a_2, ..., a_n (1 β€ a_i β€ 10^9).
The third line contains an integer m (1 β€ m β€ 100) β the number of requests.
The following m lines contain pairs of integers k_j and pos_j (1 β€ k β€ n, 1 β€ pos_j β€ k_j) β the requests.
Output
Print m integers r_1, r_2, ..., r_m (1 β€ r_j β€ 10^9) one per line: answers to the requests in the order they appear in the input. The value of r_j should be equal to the value contained in the position pos_j of the optimal subsequence for k=k_j.
Examples
Input
3
10 20 10
6
1 1
2 1
2 2
3 1
3 2
3 3
Output
20
10
20
10
20
10
Input
7
1 2 1 3 1 2 1
9
2 1
2 2
3 1
3 2
3 3
1 1
7 1
7 7
7 4
Output
2
3
2
3
2
3
1
1
3
Note
In the first example, for a=[10,20,10] the optimal subsequences are:
* for k=1: [20],
* for k=2: [10,20],
* for k=3: [10,20,10].
|
{
"input": [
"3\n10 20 10\n6\n1 1\n2 1\n2 2\n3 1\n3 2\n3 3\n",
"7\n1 2 1 3 1 2 1\n9\n2 1\n2 2\n3 1\n3 2\n3 3\n1 1\n7 1\n7 7\n7 4\n"
],
"output": [
"20\n10\n20\n10\n20\n10\n",
"2\n3\n2\n3\n2\n3\n1\n1\n3\n"
]
}
|
{
"input": [
"2\n1 10\n3\n2 2\n2 1\n1 1\n",
"2\n3922 3922\n3\n2 2\n2 1\n1 1\n",
"1\n1000000000\n1\n1 1\n",
"1\n1\n3\n1 1\n1 1\n1 1\n",
"5\n3 1 4 1 2\n15\n5 5\n5 4\n5 3\n5 2\n5 1\n4 4\n4 3\n4 2\n4 1\n3 3\n3 2\n3 1\n2 2\n2 1\n1 1\n",
"2\n392222 322\n3\n2 2\n2 1\n1 1\n"
],
"output": [
"10\n1\n10\n",
"3922\n3922\n3922\n",
"1000000000\n",
"1\n1\n1\n",
"2\n1\n4\n1\n3\n2\n4\n1\n3\n2\n4\n3\n4\n3\n4\n",
"322\n392222\n392222\n"
]
}
|
CORRECT
|
java
|
import java.io.PrintWriter;
import java.util.*;
public class OptimalSubsequences {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
PrintWriter pw = new PrintWriter(System.out);
int t = 1;
for (int i = 0; i < t; i++) {
solve(sc, pw);
}
pw.close();
}
static class Request{
int k, pos, idx;
public Request(int k, int pos, int idx) {
this.k = k;
this.pos = pos;
this.idx = idx;
}
}
static Request[] requests;
static Map<Integer, Integer> mp;
static Map<Integer, Integer> rmp;
static void solve(Scanner in, PrintWriter out){
int n = in.nextInt();
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = in.nextInt();
}
int m = in.nextInt();
requests = new Request[m];
for (int i = 0; i < m; i++) {
requests[i] = new Request(in.nextInt(), in.nextInt(), i);
}
Arrays.sort(requests, (a, b) -> (a.k - b.k));
PriorityQueue<int[]> pq = new PriorityQueue<>((a, b) -> {
if (a[0] == b[0]) return a[1] - b[1];
return b[0] - a[0];
});
for (int i = 0; i < n; i++) {
pq.add(new int[]{arr[i], i});
}
SegTree sg = new SegTree(n + 5);
// compressValue(arr);
int cnt = 0;
int[] rans = new int[m];
for (int i = 0; i < m; i++) {
int kv = requests[i].k;
while (cnt < kv){
int[] get = pq.poll();
// System.out.println(Arrays.toString(get));
sg.modify(get[1], 1);
cnt++;
}
// System.out.println("query:0-1:"+sg.query(0, 1)+" query:0-2:"+sg.query(0, 2)+" query 0-3:"+sg.query(0, 3));
int id = helper(sg, requests[i].pos, 0, n);
// System.out.println(" k:"+requests[i].k+" pos:"+requests[i].pos+" id:"+id);
rans[requests[i].idx] = arr[id];
}
for (int i = 0; i < m; i++) {
out.println(rans[i]);
}
}
static int helper(SegTree sg, int idx, int l, int r){
while (l < r){
int mid = (l + r) / 2;
if (sg.query(0, mid + 1) >= idx){
r = mid;
}else{
l = mid + 1;
}
}
return r;
}
static void compressValue(int[] arr){
mp = new TreeMap<>();
rmp = new HashMap<>();
for(int x : arr) mp.put(x, mp.getOrDefault(x, 0) + 1);
int cnt = 0;
for (int k : mp.keySet()) {
mp.put(k, cnt);
rmp.put(cnt++, k);
}
}
// Use this instead of Arrays.sort() on an array of ints. Arrays.sort() is n^2
// worst case since it uses a version of quicksort. Although this would never
// actually show up in the real world, in codeforces, people can hack, so
// this is needed.
static void ruffleSort(int[] a) {
//ruffle
int n=a.length;
Random r=new Random();
for (int i=0; i<a.length; i++) {
int oi=r.nextInt(n), temp=a[i];
a[i]=a[oi];
a[oi]=temp;
}
//then sort
Arrays.sort(a);
}
static class SegTree {
private int N;
// Let UNIQUE be a value which does NOT
// and will not appear in the segment tree
private long UNIQUE = 0;
// Segment tree values
private long[] tree;
public SegTree(int size) {
tree = new long[2 * (N = size)];
java.util.Arrays.fill(tree, UNIQUE);
}
public SegTree(long[] values) {
this(values.length);
for (int i = 0; i < N; i++) modify(i, values[i]);
}
// This is the segment tree function we are using for queries.
// The function must be an associative function, meaning
// the following property must hold: f(f(a,b),c) = f(a,f(b,c)).
// Common associative functions used with segment trees
// include: min, max, sum, product, GCD, and etc...
private long function(long a, long b) {
if (a == UNIQUE) return b;
else if (b == UNIQUE) return a;
return a + b; // sum over a range
//return (a > b) ? a : b; // maximum value over a range
//return (a < b) ? a : b; // minimum value over a range
// return a * b; // product over a range (watch out for overflow!)
}
// Adjust point i by a value, O(log(n))
public void modify(int i, long value) {
//tree[i + N] = function(tree[i + N], value);
tree[i + N] = value;
for (i += N; i > 1; i >>= 1) {
tree[i >> 1] = function(tree[i], tree[i ^ 1]);
}
}
// Query interval [l, r), O(log(n)) ----> notice the exclusion of r
public long query(int l, int r) {
long res = UNIQUE;
for (l += N, r += N; l < r; l >>= 1, r >>= 1) {
if ((l & 1) != 0) res = function(res, tree[l++]);
if ((r & 1) != 0) res = function(res, tree[--r]);
}
if (res == UNIQUE) {
//throw new IllegalStateException("UNIQUE should not be the return value.");
return 0;
}
return res;
}
}
}
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.