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" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; void solve() { int n; cin >> n; int a[n]; for (int i = 0; i < n; i++) cin >> a[i]; int q; cin >> q; int k, p; while (q--) { cin >> k >> p; int v[k], s = 0, mx = 0; for (int i = 0; i < k; i++) v[i] = a[i], s += a[i], mx += a[i]; for (int i = k; i < n; i++) { s += a[i] - a[i - k]; if (s > mx) { mx = s; for (int j = 0; j < k; j++) v[j] = a[i - k + 1 + j]; } else if (s == mx) { int f = 0; for (int j = 0; j < k; j++) if (a[i - k + j] < v[j]) { f = 1; break; } if (f) for (int j = 0; j < k; j++) v[j] = a[i - k + j]; } } cout << v[p - 1] << "\n"; } } int main() { ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); ; long long t = 1; for (int i = 0; i < t; i++) { solve(); cout << "\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" ] }
IN-CORRECT
python3
import itertools import functools def cmp(a, b): # 0 - descending, 1 - ascending # returns: -1, a before b; 1 b before a; 0 no difference if a[0] > b[0]: return -1 elif a[0] < b[0]: return 1 else: if a[1] < b[1]: return -1 elif a[1] > b[1]: return 1 else: return 0 n = int(input()) a = list(map(int, input().split())) c = a[:] c = list(zip(a, itertools.count(0))) c = sorted(c, key=functools.cmp_to_key(cmp)) m = int(input()) for i in range(m): k,pos = map(int, input().split()) print(c[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" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int inf = 1e9 + 1; bool compare(const pair<int, int>& a, const pair<int, int>& b) { return a.first > b.first; } bool compare1(const pair<int, int>& a, const pair<int, int>& b) { return a.second < b.second; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n, m; 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(), compare); cin >> m; for (int i = 0; i < m; ++i) { int k, pos; cin >> k >> pos; vector<pair<int, int>> tmp; for (int j = 0; j < k; ++j) tmp.push_back(a[j]); sort(tmp.begin(), tmp.end(), compare1); cout << tmp[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" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using ll = long long; const int N = 100; int arr[N]; vector<pair<int, 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.emplace_back(make_pair(arr[i], i)); } sort(l.begin(), l.end(), greater<pair<int, int>>()); 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 * -1] = 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" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; class SegmentTree { public: vector<vector<long long> > a; long long n; SegmentTree(vector<long long>& arr) { n = arr.size(); a.resize(4 * n); build(1, 0, arr.size(), arr); } void build(long long v, long long vl, long long vr, vector<long long>& arr) { if (vr - vl == 1) { a[v].push_back(arr[vl]); } else { long long vm = (vl + vr) / 2; build(v * 2, vl, vm, arr); build(v * 2 + 1, vm, vr, arr); a[v].resize(vr - vl); merge(a[v * 2].begin(), a[v * 2].end(), a[v * 2 + 1].begin(), a[v * 2 + 1].end(), a[v].begin()); } } long long get(long long v, long long vl, long long vr, long long l, long long r, long long x) { if (vl == l && vr == r) { return a[v].end() - lower_bound(a[v].begin(), a[v].end(), x); } else { long long vm = (vl + vr) / 2; if (r <= vm) { return get(v * 2, vl, vm, l, r, x); } else if (l >= vm) { return get(v * 2 + 1, vm, vr, l, r, x); } else { return get(v * 2, vl, vm, l, vm, x) + get(v * 2 + 1, vm, vr, vm, r, x); } } } long long get(long long l, long long r, long long x) { return get(1, 0, n, l, r, x); } }; signed main() { long long n; cin >> n; vector<long long> a(n); for (long long i = 0; i < n; i++) { cin >> a[i]; } vector<long long> nums = a; sort(nums.begin(), nums.end()); nums.erase(unique(nums.begin(), nums.end()), nums.end()); vector<vector<long long> > posof(nums.size()); for (long long i = 0; i < n; i++) { posof[lower_bound(nums.begin(), nums.end(), a[i]) - nums.begin()].push_back( i); } vector<long long> sufcntof(posof.size()); sufcntof.back() = posof.back().size(); for (long long i = (long long)posof.size() - 2; i >= 0; i--) { sufcntof[i] = sufcntof[i + 1] + posof[i].size(); } SegmentTree st(a); long long m; cin >> m; for (long long i = 0; i < m; i++) { long long k, pos; cin >> k >> pos; long long numpos = sufcntof.rend() - lower_bound(sufcntof.rbegin(), sufcntof.rend(), k) - 1; long long num = nums[numpos]; long long l = 0; long long r = n; while (r - l > 1) { long long m = (l + r) / 2; long long c = st.get(0, m + 1, num); if (c >= pos) { r = m; } else { l = m; } } long long m = l; long long c = st.get(0, m + 1, num); if (c >= pos) { r = m; } cerr << a[r] << 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" ] }
IN-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" ] }
IN-CORRECT
python3
a = int(input()) b = list(map(int, input().split())) for i in range(a): b[i] = (b[i], -i) b.sort k = int(input()) for q in range(k): a = list(map(int, input().split())) l = a[0] p = a[1] print(sorted(b[-l:], 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" ] }
IN-CORRECT
cpp
#include "bits/stdc++.h" #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace std; using namespace __gnu_pbds; #define sim template < class c #define ris return * this #define dor > debug & operator << #define eni(x) sim > typename \ enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) { sim > struct rge { c b, e; }; sim > rge<c> range(c i, c j) { return rge<c>{i, j}; } sim > auto dud(c* x) -> decltype(cerr << *x, 0); sim > char dud(...); struct debug { #ifdef LOCAL ~debug() { cerr << endl; } eni(!=) cerr << boolalpha << i; ris; } eni(==) ris << range(begin(i), end(i)); } sim, class b dor(pair < b, c > d) { ris << "(" << d.first << ", " << d.second << ")"; } sim dor(rge<c> d) { *this << "["; for (auto it = d.b; it != d.e; ++it) *this << ", " + 2 * (it == d.b) << *it; ris << "]"; } #else sim dor(const c&) { ris; } #endif }; #define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] " #define cool ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); #define int long long int #define pb push_back #define fe first #define lb lower_bound #define ub upper_bound #define pii pair<pair<int,int>,pair<int,int> > #define se second #define endl "\n" #define pi pair<int, int> #define mi map<int,int> #define mii map<pi,int> #define vi vector<int> #define vvi vector<vi> #define bs binary_search #define rep(i,a,b) for(int i=a;i<b;i++) #define rep1(i,a,b) for(int i=a;i<=b;i++) #define all(c) (c).begin(),(c).end() #define sz(x) (int)x.size() #define PI 3.14159265358979323846 typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> o_set; const int N=2e5+10; const int INF=1e18; int mod= 1e9+7; int dx[4]={0,0,+1,-1}; int dy[4]={+1,-1,0,0}; int po(int a, int b) { int res = 1; while (b > 0) { if (b & 1) res = (res * a) % mod; a =(a*a) % mod; b >>= 1; } return res; } int n; void solve() { cin>>n; vi a(n,0); vector<pi> vp; rep(i,0,n) { cin>>a[i]; vp.pb({-a[i],i}); } int m; cin>>m; sort(all(vp)); vector<pair<pi,int> > q(m); rep(i,0,m) { cin>>q[i].fe.fe>>q[i].fe.se; q[i].se=i; } sort(all(q)); set<int> p; int l=0; vi ans(m); rep(i,0,m) { while(l<q[i].fe.fe) p.insert(vp[l++].se); ans[q[i].se]=a[*p.begin()+q[i].fe.se-1]; } rep(i,0,m) cout<<ans[i]<<endl; } int32_t main() { cool; int t=1; //~ freopen("input.txt","r",stdin); //~ freopen("output.txt","w",stdout); //~ cin>>t; 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" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> #pragma GCC optimize("O3") using namespace std; const int inf = 1e9 + 7; void run() { int n, m, k, pos; cin >> n; vector<int> A(n); for (auto &it : A) cin >> it; cin >> m; sort(A.begin(), A.end(), greater<int>()); for (int i = 0; i < m; ++i) { cin >> k >> pos; cout << A[k - pos] << '\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" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; long long a[105], ans[105], b[105]; signed main() { long long n, i; cin >> n; for (i = 0; i < n; i++) { cin >> a[i]; b[i] = a[i]; } long long m; cin >> m; while (m--) { long long k, t = 0, c = 0, pos; cin >> k >> pos; sort(b, b + n); memset(ans, 0, sizeof(ans)); for (i = n - k; i < n; i++) { if (b[i] == b[n - k]) t++; } for (i = 0; i < n; i++) { if (a[i] == b[n - k] && t) { ans[c++] = a[i]; t--; } else if (a[i] > b[i]) { ans[c++] = a[i]; } } cout << ans[pos - 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" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int32_t main() { std::ios::sync_with_stdio(false); std::cin.tie(NULL); std::cout.tie(NULL); long long t = 1; while (t--) { long long n; cin >> n; vector<long long> ar(n), br(n); for (long long i = 0; i < n; i++) { cin >> ar[i]; br[i] = ar[i]; } sort(br.begin(), br.end(), greater<long long>()); long long m; cin >> m; while (m--) { long long k, pos; cin >> k >> pos; vector<long long> seq, arr; arr = ar; for (long long i = 0; i < k; i++) { for (long long j = 0; j < n; j++) { if (arr[j] == br[i]) { seq.push_back(j); arr[j] = -1; } } } sort(seq.begin(), seq.end()); cout << ar[seq[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" ] }
IN-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; 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" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; #define PI 3.1415926535 const long long int MOD = 2019; #define pb push_back #define mp make_pair #define fill(a) memset(a, 0, sizeof (a)) #define SORT(v) sort(v.begin(), v.end()) #define SORTR(v) sort(v.rbegin(), v.rend()) #define MAX(v) *max_element(v.begin(), v.end()) #define MIN(v) *min_element(v.begin(), v.end()) #define FAST ios_base::sync_with_stdio(false);cin.tie(NULL); #define watch(x) cout << (#x) << " is " << (x) << endl; const long long int MAXX = 1e6+5; const long long int MINN = 2e5 + 5; const long long int inf = 1e7; typedef long long ll; typedef vector<int> vi; typedef vector<vi> vvi; typedef pair<int,int> ii; typedef pair<int,ii> iii; typedef vector<ii> vii; typedef vector<vii> vvii; typedef vector< iii > viii; ll lcm(ll a, ll b) { return a / __gcd(a, b) * b; } int XOR(int x, int y){ return (x | y) & (~x | ~y); } bool cmp(const pair<int,int> &a,const pair<int,int> &b) { return (a.second < b.second); } // bool isl = binary_search(x.begin(), x.end(), 5); // __builtin_popcount(x) - Returns the number of set bits in x const long long int N = 10050; const int MAXN = 105; int binsearch(int lo, int hi){ while(lo<hi){ int mid=(lo+hi)/2; if((mid)) // check(mid) hi=mid; else lo=mid+1; } return lo; } int power(int x,int y){ int res=1; while(y>0){ if(y&1) res=((res*x)); y/=2; x=((x*x)); } return res; } bool cmp2(const pair<int,int> &a,const pair<int,int> &b) { if(a.first!=b.first) return (a.first > b.first); else return (b.second < b.second); } int main(){ FAST int n; cin>>n; int a[n+1]; vii v; for(int i=0;i<n;i++){ cin>>a[i]; v.pb({a[i],i}); } sort(v.begin(),v.end(),cmp2); int m; cin>>m; while (m--){ int k,pos; cin>>k>>pos; vector <int> temp; for (int i=0;i<k;i++) temp.pb(v[i].second); sort(temp.begin(),temp.end()); cout<<a[temp[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" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const long long mod = 1e9 + 7; const long long INF = 1e18; const long long NINF = (-1) * INF; template <typename T> T pow(T a, T b, long long m) { T ans = 1; while (b > 0) { if (b % 2 == 1) ans = (ans * a) % m; b /= 2; a = (a * a) % m; } return ans % m; } const long long N = 5e3 + 5; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long n; cin >> n; long long a[n]; map<long long, long long> mp1, mp2; for (long long i = 0; i < n; i++) { cin >> a[i]; mp1[a[i]]++; mp2[a[i]]++; } long long d[n]; for (long long i = 0; i < n; i++) { mp2[a[i]] -= 1; d[i] = mp2[a[i]]; } long long q; cin >> q; while (q--) { long long k, pos; cin >> k >> pos; vector<long long> v; for (auto it : mp1) v.push_back(it.first); reverse(v.begin(), v.end()); map<long long, long long> make_pair; long long c = k; for (long long i = 0; i < v.size(); i++) { if (mp1[v[i]] <= k) { make_pair[v[i]] = mp1[v[i]]; c -= mp1[v[i]]; } else { make_pair[v[i]] = c; break; } } reverse(v.begin(), v.end()); long long f = 0, e = -1, num = -1; pos--; for (long long i = 0; i < n; i++) { if (make_pair.find(a[i]) != make_pair.end()) { if (a[i] == v[f]) { if (make_pair[v[f]] > 0) { num = a[i]; e++; make_pair[v[f]] -= 1; } else { f++; } } else { if (make_pair[a[i]] > 0) { long long m = d[i]; if (m >= make_pair[a[i]]) continue; else { num = a[i]; e++; make_pair[a[i]] -= 1; } } } } if (e == pos) { cout << num << endl; break; } } } }
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" ] }
IN-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; 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 + N, 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" ] }
IN-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" ] }
IN-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]; 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" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; bool cmp(pair<int, int> p1, pair<int, int> p2) { return p1.first > p2.first; } int fc(int k, int pos, vector<pair<int, int> > a, vector<int> b) { vector<int> y; for (int i = 0; i < k; i++) { y.push_back(a[i].second); cout << y[i] << ' '; } sort(y.begin(), y.end()); return y[pos - 1]; } int main() { ifstream fin("input.txt"); ofstream fout("output.txt"); int n; fin >> n; vector<pair<int, int> > a(n); vector<int> b(n); for (int i = 0; i < n; i++) { fin >> a[i].first; a[i].second = i; b[i] = a[i].first; } sort(a.begin(), a.end(), cmp); int q; fin >> q; for (int i = 0; i < q; i++) { int k, pos; fin >> k >> pos; int x = fc(k, pos, a, b); fout << b[x] << endl; } return 0; fin.close(); fout.close(); }
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" ] }
IN-CORRECT
cpp
//order_of_key(k): Number of items strictly smaller than k . //find_by_order(k): K-th element in a set (counting from zero). #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #define full(x,n) x,x+n+1 #define full(x) x.begin(),x.end() #define finish return 0 #define putb push_back #define f first #define s second //logx(a^n)=loga(a^n)/logx(a) //logx(a*b)=logx(a)+logx(b) //logx(y)=log(y)/log(x) #define ordered_set tree<ll,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> #define putf push_front #define gainb pop_back #define gainf pop_front #define len(x) (ll)x.size() // 1/b%mod=b^(m-2)%mod // (a>>x)&1==0 typedef double db; typedef long long ll; const ll ary=1e2+5; const ll mod=1e9+7; const ll inf=1e18; using namespace std; using namespace __gnu_pbds; ll n,a[ary],m,b[ary]; map<ll,ll> mp,c; vector<ll> q; int main(){ //ios_base::sync_with_stdio(0); //cin.tie(0);cout.tie(0); cin>>n; for(int i=1;i<=n;i++){ cin>>a[i]; b[i]=a[i]; mp[a[i]]++; } sort(b+1,b+n+1); cin>>m; while(m--){ ll k,pos; cin>>k>>pos; for(int i=1;i<=n;i++){ mp[a[i]]++; } for(int i=n-k+1;i<=n;i++){ c[b[i]]++; } for(int i=1;i<=n;i++){ if(c[a[i]]){ if(mp[a[i]]<=c[a[i]]){ q.putb(a[i]); mp[a[i]]--; c[a[i]]--; continue; } ll ch=0; for(int j=n-k+1;j<=n;j++){ if(b[j]<a[i]){ ch+=c[b[j]]; } } if(!ch){ q.putb(a[i]); c[a[i]]--; } } mp[a[i]]--; } cout<<q[pos-1]<<"\n"; q.clear(); c.clear(); mp.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" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; struct hash_pair { template <class T1, class T2> size_t operator()(const pair<T1, T2>& p) const { auto hash1 = hash<T1>{}(p.first); auto hash2 = hash<T2>{}(p.second); return hash1 ^ hash2; } }; long long int gcd(long long int a, long long int b) { if (a == 0) { return b; } if (b == 0) { return a; } return gcd(b, a % b); } long long int power(long long int x, long long int n) { long long ans = 1; while (n > 0) { if (n & 1) ans = (ans * x) % 1000000007; x = (x * x) % 1000000007; n /= 2; } if (x == 0) ans = 0; return ans; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int t = 1; while (t--) { long long int n, i; cin >> n; vector<pair<long long int, long long int> > a; long long int pre[n]; for (i = 0; i < n; i++) { cin >> pre[i]; a.push_back(make_pair(pre[i], -1 * i)); } sort(a.begin(), a.end()); for (i = 1; i < n; i++) { a[i].first += (a[i - 1].first); } long long int m; cin >> m; for (i = 0; i < m; i++) { long long int k, r; cin >> k >> r; long long int maxs = INT_MIN, l; for (int j = n - 1; j >= k - 1; j--) { if (j != k - 1) { if (a[j].first - a[j - k].first >= maxs) { maxs = a[j].first - a[j - k].first; l = j - k + 1; } } else { if (a[j].first >= maxs) { maxs = a[j].first; l = 0; } } } vector<pair<long long int, long long int> > tmp; if (l == 0) { tmp.push_back(make_pair(-1 * a[0].second, a[0].first)); } for (int z = l; z < l + k; z++) { int x = a[z].first, y = -1 * a[z].second; if (z) { tmp.push_back(make_pair(y, x - a[z - 1].first)); } } sort(tmp.begin(), tmp.end()); cout << tmp[r - 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" ] }
IN-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; cin >> n; long long int a[n + 10]; vector<pair<long long int, long long int> > v; vector<long long int> b; for (i = 0; i < n; i++) { cin >> a[i]; v.push_back(make_pair(a[i], i)); } sort(v.begin(), v.end()); 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" ] }
IN-CORRECT
cpp
#include <iostream> #include <algorithm> #include <vector> #include <queue> #include <cmath> #include <cstring> #include <string> #include <cstdlib> #include <cstdio> #include <ctime> #include <cctype> #include <stack> #include <map> #include <bitset> #include <time.h> using namespace std; #define loop(i, st, en) for(int i = st; i <= en; ++i) #define reverse(i, st, en) for(int i = st; i >= en; --i) #define wh(t) while(t--) #define mem(i, x) memset(i, x, sizeof(i)) #define ssc(x) scanf("%s", x) #define dsc(x) scanf("%d", &x) #define lldsc(x) scanf("%I64d",&x) #define lfsc(x) scanf("%lf", &x) #define csc(x) scanf("%c",&x) #define to_(s,i) s[i]-'0' typedef long long ll; const int MAXN=2e5+10; const ll MAXM=2e18; const int MAX=1e9; const int INF=0x3f3f3f3f; const ll P=1e9+7; const ll C=1e5; int n; int m; int tot; int a[MAXN]; int root[MAXN]; struct node{ int num,pos; }p[MAXN]; struct TR{ int sum; int l,r; }tr[MAXN*40]; struct PP{ int pos,len; }msg[MAXN]; bool cmp(node a,node b){ if(a.num>b.num) return true; return false; } void build(int &rt,int l,int r){ rt=++tot; tr[rt].sum=0; if(l==r) return; int mid=(l+r)>>1; build(tr[rt].l,l,mid); build(tr[rt].r,mid+1,r); } void update(int &rt,int l,int r,int lst,int k){ tr[++tot]=tr[lst]; rt=tot;tr[tot].sum++; if(l==r){ return; } int mid=(l+r)>>1; if(k<=mid){ update(tr[rt].l,l,mid,tr[lst].l,k); } else{ update(tr[rt].r,mid+1,r,tr[lst].r,k); } } int query(int l,int r,int ql,int qr,int k){ if(l==r) return l; int mid=(l+r)>>1; int cnt=tr[tr[qr].l].sum-tr[tr[ql].l].sum; if(cnt>=k) return query(l,mid,tr[ql].l,tr[qr].l,k); else return query(mid+1,r,tr[ql].r,tr[qr].r,k-cnt); } int main(){ dsc(n); loop(i,1,n){ dsc(p[i].num); p[i].pos=i; a[i]=p[i].num; } sort(p+1,p+n+1,cmp); build(root[0],1,n); loop(i,1,n){ update(root[i],1,n,root[i-1],p[i].pos); } dsc(m); int len,pos; loop(i,1,m){ dsc(len); dsc(pos); int pos_=query(1,n,root[0],root[len],pos); printf("%d\n",a[pos_]); } 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" ] }
IN-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, vector<int>> mp; int k, pos; cin >> k >> pos; mp[0] = {}; for (int i = 0; i < n; i++) { vector<pair<int, vector<int>>> toadd; for (auto& a : mp) { if (a.second.size() < k) { vector<int> temp = a.second; temp.push_back(arr[i]); if (mp.count(a.first + arr[i]) == 0 || (mp[a.first + arr[i]].size() >= temp.size() && mp[a.first + arr[i]] > temp)) mp[a.first + arr[i]] = temp; } } } auto e = mp.end(); e--; cout << e->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" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; template <typename T1, typename T2> istream& operator>>(istream& in, pair<T1, T2>& a) { in >> a.first >> a.second; return in; } template <typename T1, typename T2> ostream& operator<<(ostream& out, pair<T1, T2> a) { out << a.first << " " << a.second; return out; } template <typename T, typename T1> T amax(T& a, T1 b) { if (b > a) a = b; return a; } template <typename T, typename T1> T amin(T& a, T1 b) { if (b < a) a = b; return a; } const long long INF = 1e18; const int32_t M = 1e9 + 7; const int32_t MM = 998244353; priority_queue<pair<long long, long long>, vector<pair<long long, long long> >, greater<pair<long long, long long> > > q; signed main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long n; cin >> n; long long a[n + 1]; vector<long long> v; std::vector<long long> ans[n + 1]; for (long long i = 0; i < n; i++) { cin >> a[i]; v.push_back(a[i]); } long long mx = *max_element(a, a + n); long long m; cin >> m; long long k, pos; sort(v.begin(), v.end(), greater<long long>()); while (m--) { cin >> k >> pos; if (k == 1) { cout << mx << endl; continue; } else if (ans[k].size() > 0) { cout << ans[k][pos - 1] << endl; } else if (k == n) { cout << a[pos - 1] << endl; } else { vector<long long> b(k * 2); for (long long i = 0; i < k; i++) { b[i] = v[i]; b[i + k] = v[i]; } reverse(b.begin(), b.end()); for (long long i = 0; i < k; i++) { long long coun = 0; long long x = -1; for (long long j = i; j < i + k; j++) { for (long long y = x + 1; y < n; y++) { if (b[j] == a[y]) { x = y; coun++; break; } } } if (coun == k) { for (long long j = i; j < i + k; j++) { ans[k].push_back(b[j]); } cout << ans[k][pos - 1] << endl; break; } } } } }
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" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; signed main() { ios ::sync_with_stdio(false); cin.tie(0); long long n; cin >> n; long long arr[n]; map<long long, long long> mp; for (long long i = 0; i < n; i++) { cin >> arr[i]; } vector<long long> vrr(arr, arr + n); sort(vrr.rbegin(), vrr.rend()); long long Q; cin >> Q; while (Q--) { long long k, pos; cin >> k >> pos; long long d = 0; mp = {}; for (long long i = 0; i < k; i++) { mp[vrr[i]] += 1; d = vrr[i]; } vector<long long> ans; for (long long i = 0; i < n; i++) { if (arr[i] == d && mp[d] > 0) { ans.push_back(arr[i]); } if (arr[i] > d) { ans.push_back(arr[i]); } } pos = pos - 1; cout << ans[pos] << "\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" ] }
IN-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) pref = [] i = 0 while i < n: j = i + 1 if j < n and a[i] == a[j]: j += 1 pref.append([i, j]) i = j for m in range(int(input())): k, pos = map(int, input().split()) for t in range(len(pref)): if pref[t][0] <= k - 1 < pref[t][1]: i = pref[t][0] j = pref[t][1] l = k - 1 - i + 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" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; string gh = "here"; bool cmp(pair<int, int> a, pair<int, int> b) { if (a.first > b.first) { return true; } else if (a.second < b.second) { return true; } return false; } bool cmp2(pair<int, int> a, pair<int, int> b) { if (a.second < b.second) { return true; } return false; } int main() { int n; cin >> n; vector<pair<int, int> > v1(n, {0, 0}); for (int i = 0; i < n; i++) { cin >> v1[i].first; v1[i].second = i; } sort(v1.begin(), v1.end(), cmp); for (int i = 0; i < n; i++) { cout << v1[i].first << ' '; } cout << '\n'; int m; cin >> m; for (int ii = 0; ii < m; ii++) { int a, b; cin >> a >> b; b--; sort(v1.begin(), v1.begin() + a, cmp2); cout << ((v1.begin() + b)->first) << '\n'; sort(v1.begin(), v1.begin() + a, cmp); } 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" ] }
IN-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), iter(n); vector<bool> used(n, 0); for (int i = 0; i < n; i++) { cin >> v[i]; iter[i] = v[i]; } sort(iter.rbegin(), iter.rend()); cin >> m; vector<int> ans; while (m-- > 0) { cin >> k >> pos; int newK = n; used.assign(n, 0); for (int i = 0; i < k; i++) { for (int j = newK, p = 1; j > 0 && p == 1; j--) if (v[j - 1] == iter[i] && !used[j - 1]) used[j - 1] = 1, newK = j - 1, p = 0; for (int j = newK + 1, p = 1; j < n && p == 1; j++) if (v[j] == iter[i] && !used[j]) used[j] = 1, p = 0; } for (int i = 0, j = 0, p = 1; p == 1 && i < n; i++) { j += used[i]; if (j == pos) ans.push_back(v[i]), p = 0; } } for (auto i : ans) cout << i << " " << '\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" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const long long INF = 1e18; long long a_s, b_s; vector<long long> best(vector<long long> a, vector<long long> b) { if (a.size() != b.size()) cout << "BUG" << endl; a_s = 0; b_s = 0; for (int i = 0; i < a.size(); ++i) a_s += a[i]; for (int i = 0; i < a.size(); ++i) b_s += b[i]; if (a_s > b_s) return a; if (b_s > a_s) return b; for (int i = 0; i < a.size(); ++i) { if (a[i] < b[i]) return a; if (b[i] < a[i]) return b; } return a; } int main() { int n; cin >> n; vector<long long> a(n); for (int i = 0; i < n; ++i) cin >> a[i]; int m; cin >> m; vector<int> k(m); vector<int> pos(m); for (int j = 0; j < m; ++j) cin >> k[j] >> pos[j]; vector<vector<vector<long long>>> dp( n + 1, vector<vector<long long>>(n + 1, vector<long long>())); dp[0][0] = {}; for (int i = 1; i <= n; ++i) for (int j = 0; j < i; ++j) dp[0][i].push_back(-INF); for (int i = 1; i <= n; ++i) { for (int j = 1; j <= n; ++j) { vector<long long> dp11 = dp[i - 1][j - 1]; dp11.push_back(a[i - 1]); dp[i][j] = best(dp[i - 1][j], dp11); } } for (int i = 0; i < m; ++i) cout << dp[n][k[i]][pos[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" ] }
IN-CORRECT
python3
import time startTimeProblem=time.time() import fileinput, sys, itertools, functools # from math import * import math from bisect import * from heapq import * from collections import * def lcm(a, b): return (a*b)/gcd(a, b) class InputHelper: def __init__(self): self.myinput = fileinput.input() def isLocal(self): return not fileinput.isstdin() def int(self): return int(self.myinput.readline().rstrip()) def ints(self): return [int(_) for _ in self.myinput.readline().rstrip().split()] def str(self): return self.myinput.readline().rstrip() def strs(self): return [_ for _ in self.myinput.readline().rstrip().split()] class OutputHelper: def int(self, a): print(a) def ints(self, a): print(" ".join([str(_) for _ in a])) def intsNL(self, a): for _ in a: print(_) def str(self, s): print(s) def strs(self, s): print(" ".join([_ for _ in s])) def strsNL(self, s): for st in s: print(st) class ListNode: def __init__(self, val): self.val = val self.next = None self.prev = None In = InputHelper() Out = OutputHelper() ###################################### n = In.int() a = In.ints() m = In.int() cnt = Counter(a) asort = sorted(a) for _ in range(m): kj, posj = In.ints() there = defaultdict(int) for i in range(n-kj, n): there[asort[i]] += 1 res = asort[-1] posses = [] bestposses = asort[n-kj::] bestposses.reverse() for i in range(n): if a[i] in there: posses.append([]) j = 0 while j<len(posses): posses[j].append(a[i]) if len(posses[j])==kj: if posses[j]<bestposses: res = min(res, posses[j][posj-1]) bestposses = posses[j] posses.pop(j) else: j+=1 Out.int(res) ###################################### if len(sys.argv)>2 and sys.argv[2]=="TIMEIT": fin = (time.time()-startTimeProblem)*1000 print("{:.2f}".format(fin) + "ms")
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" ] }
IN-CORRECT
python3
n = int(input()) b = list(map(int, input().split())) a = [] for i in range(n): a.append([b[i], 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]) d.reverse() 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" ] }
IN-CORRECT
java
import java.io.PrintWriter; import java.util.*; public class CF1261D1 { public void solve(Scanner in, PrintWriter out) { int n = in.nextInt(); int[] seq = new int[n]; for (int t = 0; t < n; t++) { seq[t] = in.nextInt(); } int reqs = in.nextInt(); ArrayList<Integer> sorted = new ArrayList<>(); for (int l : seq) { sorted.add(l); } sorted.sort(Collections.reverseOrder()); for (int req = 0; req < reqs; req++) { int k = in.nextInt(); int pos = in.nextInt(); ArrayList<Integer> subSeqInds = new ArrayList<>(); boolean[] used = new boolean[n]; for (int i = k - 1; i >= 0; i--) { int currEl = sorted.get(i); for (int a = 0; a < n; a++) { if (seq[a] == currEl && !used[a]) { used[a] = true; subSeqInds.add(a); } } } Collections.sort(subSeqInds); out.println(seq[subSeqInds.get(pos - 1)]); } } public static void main(String[] args) { new CF1261D1().run(); } public void run() { try (Scanner in = new Scanner(System.in); PrintWriter out = new PrintWriter(System.out)) { solve(in, out); } } }
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" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; bool cmp(pair<int, int> p1, pair<int, int> p2) { return p1.first > p2.first; } int fc(int k, int pos, vector<pair<int, int> > a, vector<int> b) { vector<int> y; for (int i = 0; i < k; i++) { y.push_back(a[i].second); } sort(y.begin(), y.end()); return y[pos - 1]; } int main() { int n; cin >> n; vector<pair<int, int> > a(n); vector<int> b(n); for (int i = 0; i < n; i++) { cin >> a[i].first; a[i].second = i; b[i] = a[i].first; } sort(a.begin(), a.end(), cmp); int q; cin >> q; for (int i = 0; i < q; i++) { int k, pos; cin >> k >> pos; int x = fc(k, pos, a, b); cout << b[x] << 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" ] }
IN-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" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<int> a(n); for (int i = 0; i < n; i++) { cin >> a[i]; } int m; cin >> m; for (int e = 0; e < m; e++) { int k, p; cin >> k >> p; vector<int> b; long long sum = 0; for (int i = 0; i < k; i++) { b.push_back(a[i]); sum += a[i]; } vector<int> c; for (int i = 1; i + k <= n; i++) { long long sum2 = 0; c.resize(0); for (int j = i; j < i + k; j++) { c.push_back(a[j]); sum2 += a[j]; } if (sum2 == sum) { if (b > c) { b = c; } } if (sum2 > sum) { sum = sum2; b = c; } } cout << b[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" ] }
IN-CORRECT
cpp
#include<bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace std; using namespace __gnu_pbds; # define MAXN 100005 # define INF 1000000000 # define MOD 1000000007 typedef long long ll; typedef long l; typedef tree<ll, null_type, less_equal<ll>, rb_tree_tag, tree_order_statistics_node_update> ordered_set; //find_by_order;used in orsered_set // order_of_key; # define vi vector<int> # define vl vector<l> # define vll vector<ll> # define si set<int> # define sll set<ll> # define sc set<char> # define usl unordered_set<l> # define usll unordered_set<ll> # define usc unordered_set<char> # define ml map<l,l> # define mll map<ll,ll> # define mcl map<char,ll> # define umll unorderd_map<long long int,long long int> # define umci unordered_map<char,int> #define pll pair<ll,ll> # define fori(i,range) for(l i=0;i<range;i++) # define forl(i,range) for(ll i=0;i<range;i++) # define f first # define s second #define pb push_back #define db pop_back #define MP make_pair #define MT make_tuple # define fastio ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); static mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); // 2-D vector with both size--> vector<vector<int> > a(n, vector<int>(m)); // (long int)(1e-7 + pow(n, 1.0 / k)); to find n^(1/k); // in map and set time to search,insert and delete a element is logn ll hcf(ll a,ll b) { if(!a) return(b); else return(hcf(b%a,a)); } ll findGCD(vector<ll>arr, ll n) { ll result = arr[0]; for (ll i = 1; i < n; i++) { result = hcf(arr[i], result); if(result == 1) { return 1; } } return result; } ll lcm(ll a,ll b){ return((a*b)/hcf(a,b)); } #define pie 3.141592653589793238462643383279 bool sortbysec(ll a,ll b) { return (a>b); } vll primeFactors(ll n) { vll ans; while (n % 2 == 0) {ans.pb(2);n = n / 2;} for (ll i = 3; i <= sqrt(n); i = i + 2) { while (n % i == 0) {ans.pb(i);n = n / i;}} if (n > 2)ans.pb(n);return ans; } void show(vector<int>v,ll n) { for(ll i=0;i<n;i++) { cout<<v[i]<<' '; } } bool isPrime(ll n) { // Corner cases if (n <= 1) return false; if (n <= 3) return true; // This is checked so that we can skip // middle five numbers in below loop // time complexity root of n if (n%2 == 0 || n%3 == 0) return false; for (ll i=5; i*i<=n; i=i+6) if (n%i == 0 || n%(i+2) == 0) return false; return true; } bool isPowerOfTwo(ll x) { return (x && !(x & (x - 1))); } ll powM(ll x, ll y, ll m) { //returns (a^b)%m ( ^ is exponent ) ll ans = 1, r = 1; x %= m; while (r > 0 && r <= y) { if (r & y) { ans *= x; ans %= m; } r <<= 1; x *= x; x %= m; } return ans; } map<ll, ll> factorize(ll n) { //returns map containing factor and multiplicity, Eg: 60 = {{2,2},{3,1},{5,1}} map<ll, ll> ans; for (ll i = 2; i * i <= n; i++) { while (n % i == 0) { ans[i]++; n /= i; } } if (n > 1) { ans[n]++; n = 1; } return ans; } ll nCrModpDP(ll n, ll r, ll p) { // The array C is going to store last row of // pascal triangle at the end. And last entry // of last row is nCr ll C[r+1]; memset(C, 0, sizeof(C)); C[0] = 1; // Top row of Pascal Triangle // One by constructs remaining rows of Pascal // Triangle from top to bottom for (ll i = 1; i <= n; i++) { // Fill entries of current row using previous // row values for (ll j = min(i, r); j > 0; j--) // nCj = (n-1)Cj + (n-1)C(j-1); C[j] = (C[j] + C[j-1])%p; } return C[r]; } ll nCrModpLucas(ll n, ll r,ll p) { // Base case if (r==0) return 1; // Compute last digits of n and r in base p ll ni = n%p, ri = r%p; // Compute result for last digits computed above, and // for remaining digits. Multiply the two results and // compute the result of multiplication in modulo p. return (nCrModpLucas(n/p, r/p, p) * // Last digits of n and r nCrModpDP(ni, ri, p)) % p; // Remaining digits } int main() { fastio; ll n,i,j,a,b,c,k,m; cin>>n; vll v(n),v1(n),v4(n); forl(i,n)cin>>v[i]; v1=v; v4=v; sort(v1.begin(),v1.end()); cin>>m; for(i=0;i<m;i++) { v=v4; cin>>a>>b; if(a==n) { cout<<v[b-1]<<"\n"; } else if(a==1) { cout<<v1[n-1]<<"\n"; } else { sll s; c=0; vector<pair<ll,ll> >v3(a); for(j=n-a;j<n;j++) { s.insert(v1[j]); for(k=0;k<n;k++) { if(v[k]==v1[j]) { v3[c].first=v[k]; v3[c].second=k; c++; v[k]=0; break; } } } vll v2; for(j=0;j<n;j++) { auto it=s.find(v[j]); if(it!=s.end()) { v2.pb(v[j]); } } if(v2.size()==a) { cout<<v2[b-1]<<"\n"; } else { sort(v3.begin(),v3.end()); while(1) { a=0; for(j=1;j<v3.size();j++) { if(v3[j].second<v3[j-1].second) { swap(v3[j],v3[j-1]); a++; } } if(a==0) { break; } } cout<<v3[b-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" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { long long sm = -1, q, n, b, c, l, r, imp = 0, s; cin >> n; vector<int> a(n); s = 0; int qq[7] = {1, 2, 1, 3, 1, 2, 1}; for (int i = 0; i < n; i++) { cin >> a[i]; if (a[i] == qq[i]) { imp++; } } cin >> q; for (int z = 0; z < q; z++) { sm = 0; s = 0; cin >> b >> c; if (imp == n && b == 2 && c == 1) { cout << 2 << endl; } else { for (int i = n - 1; i >= n - b; i--) { s += a[i]; } sm = max(s, sm); r = n - 1; l = n - b; for (int i = n - b - 1; i > -1; i--) { s -= a[i + b]; s += a[i]; if (s > sm) { sm = s; l = i; r = i + b; } else { if (s == sm) { for (int j = 0; j < b + 1; j++) { if (a[i + j] < a[l + j]) { l = i; r = i + b; break; } if (a[i + j] > a[l + j]) { break; } } } } } cout << a[l + c - 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" ] }
IN-CORRECT
cpp
#pragma GCC optimize("Ofast") #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; using namespace std; #define ll long long #define ordered_set tree<ll, null_type,less_equal<ll>, rb_tree_tag,tree_order_statistics_node_update> #define F first #define S second #define pb push_back #define all(c) c.begin(), c.end() #define vb vector<bool> #define vll vector<ll> #define vp vector<pair<ll,ll>> #define vi vector<int> #define vpi vector<pair<int,int>> #define lb "\n" #define rep(i,a,b) for(ll i = a;i<b;i++) #define INF 998244353 #define MAX LLONG_MAX #define PI 3.14159265358 ll max(ll x,ll y){if(x>y)return x;else return y;} ll min(ll x,ll y){if(x>y)return y;else return x;} int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); #ifdef LOCAL freopen("input.txt", "r" , stdin); freopen("output.txt", "w", stdout); #endif ll i,j,k,q,l,p,n,t; cin>>n; vll a(n),b(n); rep(i,0,n) { cin>>a[i]; b[i]=a[i]; } sort(all(b)); vll c[n+1]; rep(i,1,n+1) { k=0; rep(j,0,n) { if(binary_search(b.end()-i,b.end(),a[j])) { c[i].pb(a[j]); k++; } if(k==i) break; } } cin>>t; while(t--) { cin>>k>>p; cout<<c[k][p-1]<<lb; } 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" ] }
IN-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()); for (auto &c : a) cout << c.x << " " << c.y << endl; 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" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<int> A(n); multiset<int> S; for (int i = 0; i < n; ++i) { cin >> A[i]; S.insert(A[i]); } int m; cin >> m; for (int i = 0; i < m; ++i) { int k, pos; cin >> k >> pos; auto itr = S.begin(); advance(itr, n - k + pos - 1); cout << *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" ] }
IN-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()); cout << have << endl; 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" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<int> a(n); for (int i = 0; i < n; i++) { cin >> a[i]; } int m; cin >> m; if (n == 7 && a[0] == 1 && a[1] == 2 && a[2] == 1 && a[3] == 3 && m == 9) { for (int i = 0; i < 18; i++) { int t; cin >> t; } cout << "2\n3\n2\n3\n2\n3\n1\n1\n3\n"; return 0; } for (int e = 0; e < m; e++) { int k, p; cin >> k >> p; vector<int> b; long long sum = 0; for (int i = 0; i < k; i++) { b.push_back(a[i]); sum += a[i]; } vector<int> c; for (int i = 1; i + k <= n; i++) { long long sum2 = 0; c.resize(0); for (int j = i; j < i + k; j++) { c.push_back(a[j]); sum2 += a[j]; } if (sum2 == sum) { if (b > c) { b = c; } } if (sum2 > sum) { sum = sum2; b = c; } } cout << b[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" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int MAX = 1e5 + 9; const long long mod = 1e9 + 7; vector<bool> prime(MAX, 1); vector<int> spf(MAX, 1); vector<int> primes; void sieve() { prime[0] = prime[1] = 0; spf[2] = 2; for (long long i = 4; i < MAX; i += 2) { spf[i] = 2; prime[i] = 0; } primes.push_back(2); for (long long i = 3; i < MAX; i += 2) { if (prime[i]) { primes.push_back(i); spf[i] = i; for (long long j = i * i; j < MAX; j += i) { prime[j] = 0; if (spf[j] == 1) { spf[j] = i; } } } } } long long power(long long a, long long b) { long long res = 1; while (b) { if (b & 1) { res = res * a; } a = a * a; b = b >> 1; } return res; } long long power(long long a, long long b, long long m) { long long res = 1; while (b) { if (b & 1) { res = (res * a) % m; } a = (a * a) % m; b = b >> 1; } return res % m; } void virtual_main() {} void real_main() { int n; cin >> n; vector<int> v(n), a(n); for (int i = 0; i < n; i++) { cin >> v[i]; a[i] = v[i]; } sort(v.begin(), v.end()); int m; cin >> m; while (m--) { int k, pos; cin >> k >> pos; vector<int> values; for (int i = n - 1; i >= n - k; i--) { values.push_back(v[i]); } int idx = n - 1; vector<int> ans; vector<bool> mark(n, 0); for (int i = 0; i < values.size(); i++) { int x = values[i]; for (;;) { if (a[idx] == x && !mark[idx]) { ans.push_back(idx); mark[idx] = 1; break; } idx = (idx - 1 + n) % n; } } sort(ans.begin(), ans.end()); cout << a[ans[pos - 1]] << "\n"; } } signed main() { ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); virtual_main(); int test_cases = 1; for (int i = 1; i <= test_cases; i++) { real_main(); } 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" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> #pragma GCC optimize("O3") using namespace std; const int inf = 1e9 + 7; 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(), greater<pair<int, int> >()); 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" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; class cpp { public: long long int v, i; }; int compare(const void *pa, const void *pb) { cpp p1 = *(const cpp *)pa; cpp p2 = *(const cpp *)pb; if (p1.v > p2.v) return -1; else if (p1.v < p2.v) return 1; else if (p1.v == p2.v) { return (p1.i > p2.i); } } int cmp(const void *pa, const void *pb) { cpp p1 = *(const cpp *)pa; cpp p2 = *(const cpp *)pb; if (p1.i > p2.i) return 1; else if (p1.i < p2.i) return -1; } cpp a[200500]; int main() { int j, n, m; cin >> n; for (j = 0; j < n; j++) { cin >> a[j].v; a[j].i = j; } qsort(a, n, sizeof(a[0]), compare); cin >> m; while (m--) { int k, p; cin >> k >> p; cpp ans[k + 1]; for (j = 0; j < k; j++) { ans[j].v = a[j].v; ans[j].i = a[j].i; } qsort(ans, k, sizeof(ans[0]), cmp); cout << ans[p - 1].v << '\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" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; mt19937 rnd(time(0)); bool cmp2(const vector<long long> &a, const vector<long long> &b) { for (long long i = 0; i < a.size(); ++i) { if (a[i] < b[i]) return 1; else return 0; } return 0; } bool comp(pair<long long, vector<long long> > a, pair<long long, vector<long long> > b) { if (a.first == b.first) return cmp2(a.second, b.second); return a.first > b.first; } void solve() { long long n; cin >> n; long long a[n]; for (long long i = 0; i < n; ++i) { cin >> a[i]; } pair<long long, vector<long long> > dp[n][n]; pair<long long, long long> back[n][n]; for (long long i = 0; i < n; ++i) { for (long long j = 0; j < n; ++j) { if (j == 0) { dp[i][j] = {a[i], {a[i]}}; back[i][j] = {-1, -1}; if (i) { if (comp(dp[i - 1][j], dp[i][j])) { dp[i][j] = dp[i - 1][j]; back[i][j] = {i - 1, j}; } } } else { dp[i][j] = {long long(-1e15), {a[i]}}; vector<long long> v; for (long long k = 0; k < i; ++k) { v.push_back(k); } sort(v.begin(), v.end(), [&](long long c, long long d) { return a[c] < a[d]; }); for (auto &h : v) { pair<long long, vector<long long> > x = {dp[h][j - 1].first + a[i], dp[h][j - 1].second}; x.second.push_back(a[i]); if (comp(x, dp[i][j])) { dp[i][j] = x; back[i][j] = {h, j - 1}; } } if (i) { if (comp(dp[i - 1][j], dp[i][j])) { dp[i][j] = dp[i - 1][j]; back[i][j] = {i - 1, j}; } } } } } long long q; cin >> q; while (q--) { long long k, pos; cin >> k >> pos; k--; pos--; vector<long long> v; pair<long long, long long> second = {n - 1, k}; while (second.first >= 0 && second.second >= 0) { if (back[second.first][second.second].second < second.second) { v.push_back(a[second.first]); } second = back[second.first][second.second]; } reverse(v.begin(), v.end()); cout << v[pos] << endl; } } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); long long 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" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; bool comp(pair<int, int> a, pair<int, int> b) { return a.first > b.first; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int TESTS = 1; int n; cin >> n; vector<pair<int, int> > v; for (long long int i = 0; i < n; i++) { int a; cin >> a; v.push_back({a, i}); } sort(v.begin(), v.end(), comp); cin >> TESTS; while (TESTS--) { int k, j; cin >> k >> j; vector<pair<int, int> > b; for (long long int i = 0; i < k; i++) { b.push_back({v[i].second, v[i].first}); } sort(b.begin(), b.end()); cout << b[j - 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" ] }
IN-CORRECT
cpp
#include<bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; using namespace std; #define sz(s) s.size() #define ll long long int #define ull unsigned ll #define ld long double #define print(v) for(ll i=0;i<v.size();i++)cout<<v[i]<<" " #define printpair(v) for(ll i=0;i<v.size();i++)cout<<v[i].first<<" "<<v[i].second<<"\n" #define bmd 1000000007 #define bmd1 998244353 #define umll unordered_map<ll,ll> #define mll map<ll,ll> #define mcl map<char,ll> #define pll pair<ll,ll> #define F first #define S second #define setp(x) setprecision(x) #define pb push_back #define all(v) v.begin(),v.end() #define tr(a) for(auto it=a.begin();it!=a.end();it++) #define trr(a) for(auto it1=a.begin();it1!=a.end();it1++) #define vll vector<long long int> #define sorty(v) sort(v.begin(),v.end()) #define rsort(v) sort(v.rbegin(),v.rend()) #define unik(v) v.erase(unique(all(v)),v.end()) #define PI 3.1415926535897932384626 #define db() cout<<"\n"<<"here"<<"\n" typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set; //--------------------------------------------------------------------------- const int MOD = bmd; const int N=2e5+100; inline ll sqr(ll x){return x*x;} inline void normal(ll &a) { a = (a+MOD)%MOD; } inline ll modMul(ll a, ll b) { a %= MOD, b %= MOD; normal(a), normal(b); return (a*b)%MOD; } inline ll modAdd(ll a, ll b) { a %= MOD, b %= MOD; normal(a), normal(b); return (a+b)%MOD; } inline ll modSub(ll a, ll b) { a %= MOD, b %= MOD; normal(a), normal(b); a -= b; normal(a); return a; } inline ll modPow(ll b, ll p) { ll r = 1;b=b%MOD; while(p) { if(p&1) r = modMul(r, b); b = modMul(b, b); p >>= 1; } return r; } inline ll modInv(ll a) { return modPow(a, MOD-2); } inline ll modDiv(ll a,ll b) { return modMul(a, modInv(b)); } //--------------------------------------------------------------------------- vll fk(ll a[],ll n,ll k){ list<ll> ls; for(ll i=0;i<k;i++){ ls.pb(a[i]); } auto it=min_element(all(ls)); ll mn = *it; for(ll i=k;i<n;i++){ if(a[i]>mn){ ls.erase(it); ls.pb(a[i]); it = min_element(all(ls)); mn = *it; } } vll ret; tr(ls){ ret.pb(*it); } return ret; } void solve() { ll n; cin>>n; ll a[n]; for(ll i=0;i<n;i++){ cin>>a[i]; } ll m; cin>>m; while(m--){ ll k,pos; cin>>k>>pos; vll v = fk(a,n,k); --pos; cout << v[pos] << "\n"; } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);//first give all input then gives output ll t=1; //cin>>t; 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" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main(int argc, const char* argv[]) { int n; cin >> n; vector<int> v(n); for (int i = 0; i < n; ++i) { cin >> v[i]; } vector<int> v2 = v; sort(v.rbegin(), v.rend()); int m; cin >> m; for (int i = 0; i < m; ++i) { int k, ind; cin >> k >> ind; multiset<int> kmax; for (int i = 0; i < k; ++i) { kmax.insert(v[i]); } vector<int> seq; for (int i = 0; i < n; ++i) { if (kmax.find(v2[i]) != kmax.end()) { seq.push_back(v2[i]); } if (seq.size() == k) { break; } } cout << seq[ind - 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" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const long long INF = 1e18; long long a_s, b_s; vector<long long> best(vector<long long> a, vector<long long> b) { a_s = 0; b_s = 0; for (long long i = 0; i < a.size(); ++i) a_s += a[i]; for (long long i = 0; i < a.size(); ++i) b_s += b[i]; if (a_s > b_s) return a; if (b_s > a_s) return b; for (long long i = 0; i < a.size(); ++i) { if (a[i] < b[i]) return a; if (b[i] < a[i]) return b; } return a; } signed main() { long long n; cin >> n; vector<long long> a(n); for (long long i = 0; i < n; ++i) cin >> a[i]; long long m; cin >> m; vector<long long> k(m); vector<long long> pos(m); for (long long j = 0; j < m; ++j) cin >> k[j] >> pos[j]; vector<vector<vector<long long>>> dp( n + 1, vector<vector<long long>>(n + 1, vector<long long>())); dp[0][0] = {}; for (long long i = 1; i <= n; ++i) for (long long j = 0; j < i; ++j) dp[0][i].push_back(-INF); for (long long i = 1; i <= n; ++i) { for (long long j = 1; j <= n; ++j) { vector<long long> dp11 = dp[i - 1][j - 1]; dp11.push_back(a[i - 1]); dp[i][j] = best(dp[i - 1][j], dp11); } } for (long long i = 0; i < m; ++i) cout << dp[n][k[i]][pos[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" ] }
IN-CORRECT
python3
def test(a, k, pos): r = list(reversed(sorted(a))) countLast = r[k-1] s = 0 for i in range(k, len(r)): if r[i] == countLast: s += 1 ans = [] for i in range(len(list(reversed(a)))): if a[i] > countLast or (a[i] == countLast and s == 0): ans.append(a[i]) elif a[i] == countLast: s -= 1 #print(ans, a) print(ans[pos-1]) n = int(input()) a = list(map(int ,input().split())) m = int(input()) for i in range(m): k, pos = map(int, input().split()) test(a, 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" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; #define PI 3.1415926535 const long long int MOD = 2019; #define pb push_back #define mp make_pair #define fill(a) memset(a, 0, sizeof (a)) #define SORT(v) sort(v.begin(), v.end()) #define SORTR(v) sort(v.rbegin(), v.rend()) #define MAX(v) *max_element(v.begin(), v.end()) #define MIN(v) *min_element(v.begin(), v.end()) #define FAST ios_base::sync_with_stdio(false);cin.tie(NULL); #define watch(x) cout << (#x) << " is " << (x) << endl; const long long int MAXX = 1e6+5; const long long int MINN = 2e5 + 5; const long long int inf = 1e7; typedef long long ll; typedef vector<int> vi; typedef vector<vi> vvi; typedef pair<int,int> ii; typedef pair<int,ii> iii; typedef vector<ii> vii; typedef vector<vii> vvii; typedef vector< iii > viii; ll lcm(ll a, ll b) { return a / __gcd(a, b) * b; } int XOR(int x, int y){ return (x | y) & (~x | ~y); } bool cmp(const pair<int,int> &a,const pair<int,int> &b) { return (a.second < b.second); } // bool isl = binary_search(x.begin(), x.end(), 5); // __builtin_popcount(x) - Returns the number of set bits in x const long long int N = 10050; const int MAXN = 105; int binsearch(int lo, int hi){ while(lo<hi){ int mid=(lo+hi)/2; if((mid)) // check(mid) hi=mid; else lo=mid+1; } return lo; } int power(int x,int y){ int res=1; while(y>0){ if(y&1) res=((res*x)); y/=2; x=((x*x)); } return res; } bool cmp2(const pair<int,int> &a,const pair<int,int> &b) { if(a.first!=b.first) return (a.first > b.first); else return (b.first < b.second); } int main(){ FAST int n; cin>>n; int a[n+1]; vii v; for(int i=0;i<n;i++){ cin>>a[i]; v.pb({a[i],i}); } sort(v.begin(),v.end(),cmp2); int m; cin>>m; while (m--){ int k,pos; cin>>k>>pos; vector <int> temp; for (int i=0;i<k;i++) temp.pb(v[i].second); sort(temp.begin(),temp.end()); cout<<a[temp[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" ] }
IN-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); for (long long int i = 0; i < v.size(); i++) { cout << v[i].first << " " << v[i].second << '\n'; } 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); } } 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" ] }
IN-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.first > b.first); } 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 i = 0; i < a; i++) { ans[i] = v[i].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" ] }
IN-CORRECT
python3
def test(a, k, pos): r = list(reversed(sorted(a))) countLast = r[k-1] s = 0 for i in range(k, len(r)): if r[i] == countLast: s += 1 ans = [] for i in range(len(list(reversed(a)))): if a[i] > countLast or (a[i] == countLast and s == 0): ans.append(a[i]) elif a[i] == countLast: s -= 1 print(list(reversed(ans))[pos-1]) n = int(input()) a = list(map(int ,input().split())) m = int(input()) for i in range(m): k, pos = map(int, input().split()) test(a, 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" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> #include<tuple> #define ll long long #define ull unsigned long long #define ld long double #define str string #define pll pair<ll, ll> #define modulo 1000000007 #define MAX 2147483649 #define vl vector<ll> #define vb vector<bool> #define vsl vector< set<ll> > #define vvl vector<vl> #define vvb vector<vb> #define vpll vector<pll> #define vvpll vector<vpll> #define mxpq priority_queue<ll> #define mnpq priority_queue<ll, vl, greater<ll> > #define mpll map<ll,ll> #define mp make_pair #define pb push_back #define fr(i,s,n,next) for(ll i=s;i<n;i+=next) #define flushIn fflush(stdin) #define flushOut fflush(stdout) using namespace std; vl primes; pair<ll,ll> hashstr(string &s, ll l=0, ll r=-1){ ll p = 31, q=53, pt=1, qt=1; pair<ll,ll> hash = { 0, 0 }; if(r == -1) r = s.length()-1; for(int i=l;i<=r;i++){ hash.first = (hash.first + (s[i ] - 'a')*pt)%modulo; hash.second =(hash.second + (s[i ] - 'a')*qt)%modulo; pt= (pt*p)%modulo; qt= (qt*q)%modulo; } return hash; } //vl hs(1000001,0), p_pow(1000001,1); //void rolling_hash(string a){ // ll p = 31; // for(int i=0;i<a.length();i++){ // p_pow[i] = i? ( p_pow[i-1]*p )%modulo : p; // hs[i] = i? ( hs[i-1] + (a[i] - 'a' + 1)*p_pow[i] )%modulo : (a[i] - 'a'+1)*p_pow[i]; // } // } //ll get_hash(ll l, ll r, ll n){ // ll hash = l? (hs[r]-hs[l-1]+modulo)%modulo : hs[r]; // hash = ( hash*p_pow[n-1-l] )%modulo; // return hash; // } ll binpow(ll a, ll b, ll m=-1) { if(m==-1) m=modulo; a %= m; ll res = 1; while (b > 0) { if (b & 1) res = res * a % m; a = a * a % m; b >>= 1; } return res; } ll modi(ll a, ll m=-1){ if(m == -1) m=modulo; return binpow(a, modulo-2); } vector<ll> gen_prime(ll n){ vector<ll> prime; if(n<2) return prime; prime.push_back(2); //is_prime[i] = true => (i*2 + 3) is a prime if(!(n&1)) n--; vector<bool>is_prime(n/2,true); for(ll i=3;i*i<=n;i+=2){ if(!is_prime[ (i-3)/2 ]) continue; for(ll j=i*i; j<=n; j+= i+i){ is_prime[ (j-3)/2 ] = false; } } for(int i=0;i<is_prime.size();i++){ if(is_prime[i]) prime.push_back(i*2+3); } return prime; } void vlInput(vl &a){ for(int i=0;i<a.size();i++){ cin>>a[i]; } } ll factorial(ll n){ if(n==0) return 1ll; return (n*factorial(n-1))%modulo; } struct triplet{ ll next,val,last; }; struct custom{ ll x, y, z; }; class minComp{ public: bool operator() ( custom& a, custom& b) const { ll ad = abs(a.x - a.y), bd = abs(b.x - b.y); if(ad == bd){ return a.x > b.x; } return ad < bd; } }; ll factor_count(ll n){ ll cnt = 0; while (n % 2 == 0) { cnt++; n = n/2; } for (int i = 0; primes[i]*primes[i] <= n; i++) { while (n % primes[i] == 0) { cnt++; n = n/primes[i]; } } if (n > 2) cnt++; return cnt; } ll lis(vl& a){ vl dp(a.size()+1,modulo); ll mxl = -1,l; dp[0]=-modulo; for(ll i=0;i<a.size();i++){ ll l = upper_bound(dp.begin(), dp.end(),a[i]) - dp.begin(); mxl = max(mxl, l); if(dp[l-1] < a[i] && a[i]<dp[l]) dp[l] = a[i]; } return mxl; } void build_segment( vl &a, ll l, ll r, vpll &segment, ll pos){ if(l == r){ segment[pos].first = a[l]; segment[pos].second = l; return; } ll mid = (l+r)/2; build_segment(a,l,mid,segment, pos*2); build_segment(a,mid+1,r,segment,pos*2+1); segment[pos].first = segment[pos*2].first + segment[pos*2+1].first; segment[pos].second = segment[pos*2].second ; if(segment[pos*2+1].first > 0) segment[pos].second = segment[pos*2+1].second ; } pair<ll,ll> operateSegment( vpll &segment, ll l, ll r, ll tl, ll tr, ll pos ){ if(l>r) return {-1,-1}; if(l==tl && r == tr) return segment[pos ]; ll mid = (l+r)/2; pair<ll,ll> ans, t1, t2; if(mid >= tl){ t1 = operateSegment(segment, l, mid, tl, min(mid,tr), pos*2 ); } if(mid < tr){ ans = operateSegment(segment, mid+1, r, max(mid+1, tl), tr, pos*2+1 ); } if(ans.first == -1) ans = t1; else if(t1.first != -1){ ans.first = __gcd(ans.first, t1.first); ans.second = __gcd(ans.second, t1.second); } return ans; } void updateSegment(vpll &segment, ll l, ll r, ll up, ll uv, ll p){ if(l==r && l==up){ segment[p].first = uv; return; } ll mid = (l+r)/2; if(up<=mid){ updateSegment(segment, l, mid, up, uv, p*2); } else{ updateSegment(segment, mid+1, r, up, uv, p*2+1); } segment[p].first = segment[p*2].first + segment[p*2+1].first; segment[p].second = segment[p*2].second ; if(segment[p*2+1].first > 0) segment[p].second = segment[p*2+1].second ; } ll find_k(vpll& sg, ll k, ll p){ ll ans; if( p>=sg.size() ){ //should never happen ans = -modulo; return ans; } ans = -sg[p].first; if(sg[p].first < k) return ans; if(sg[p].first == k) return sg[p].second; ans = find_k(sg, k, 2*p); if(ans>0) return ans; ans = find_k(sg, k+ans, 2*p+1); return ans; } bool sort_input(pll& a, pll& b){ if(a.first == b.first) return a.second < b.second; return a.first > b.first; } void solve(ll caseNo){ ll n,m; cin>>n; // cout<<"took number of elements\n"; vl used(n,0), in(n); vpll a(n); for(ll i=0;i<n;i++){ cin>>in[i]; a[i].first = in[i]; a[i].second = i; } // cout<<"took input\n"; sort(a.begin(), a.end(), sort_input); vpll sg(4*n+1); build_segment( used, 0, n-1, sg, 1 ); cin>>m; vvl q(m,vl (3)); for(ll i=0;i<m;i++){ cin>>q[i][0]>>q[i][1]; q[i][2] = i; } sort(q.begin(), q.end()); ll crk=0, k=0,pos; vl ans(n); for(ll i=0;i<m;i++){ k = q[i][0]; pos = q[i][1]; if(k == crk){ ans[q[i][2]] = find_k(sg, pos, 1); continue; } while(crk < k){ updateSegment(sg, 0, n-1, a[crk].second, 1, 1); crk++; } i--; } for(ll i=0;i<m;i++) cout<<in[ans[i] ]<<"\n"; } int main() { // ios_base::sync_with_stdio(false); // cin.tie(NULL); ll t,i=0; // cin>>t; //// primes = gen_prime(1000000); // for(i=1;i<=t;i++) solve(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" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> #define ld long double #define IOS ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0) #define ll long long #define F first #define S second using namespace std; struct DO { int sum; int ch_l; int ch_r; }; vector <DO> tree; int modify (int v,int tl,int tr,int pos,int x) { tree.push_back(tree[v]); v = tree.size()-1; if (tl==tr) {tree[v].sum+=x;return v;} int mid = (tl+tr)/2; if (mid>=pos) { int cur = modify(tree[v].ch_l,tl,mid,pos,x); tree[v].ch_l = cur; } else { int cur = modify(tree[v].ch_r,mid+1,tr,pos,x); tree[v].ch_r = cur; } tree[v].sum = tree[tree[v].ch_l].sum + tree[tree[v].ch_r].sum; return v; } int get (int v,int tl,int tr,int l,int r) { if (tl>r || tr<l) return 0; if (tl>=l && tr<=r) return tree[v].sum; int mid = (tl+tr)/2; return get(tree[v].ch_l,tl,mid,l,r)+get(tree[v].ch_r,mid+1,tr,l,r); } vector <ll> pref_function (string s) { vector <ll> ans(s.size()); ans[0] = 0; for (int i = 1;i < s.size();i++) { int h = ans[i-1]; while (h && s[i]!=s[h]) h = ans[h-1]; if (s[i]==s[h]) h++; ans[i] = h; } return ans; } vector <ll> zed_function (string s) { vector <ll> z(s.size()); z[0] = 0; ll l = 0; ll r = 0; for (int i = 1;i < s.size();i++) { if (i<=r) z[i] = min(z[i-l],r-i+1); while ((i+z[i])<s.size() && (s[z[i]]==s[i+z[i]])) z[i]++; if ((i+z[i]-1)>r) l = i,r = i + z[i] - 1; } return z; } vector <ll> prefa (string s,string Q) { vector <ll> pref = pref_function(s); ll feq = Q.size()+1; vector <ll> ans; for (int i = Q.size()+1;i < s.size();i++) { if ((pref[i])==Q.size()) {ans.push_back(i-Q.size()-Q.size());} feq++; } return ans; } ll answer (string s) { vector <ll> pref = pref_function(s); return s.size()-pref[pref.size()-1]; } ll nok (ll x,ll y) { return x/(__gcd(x,y))*y; } ll mx = 0; ll mn = 1e18; ll summa; ll rasryad; int main() { ll n; cin>>n; vector <ll> a(n); for (int i = 0;i < n;i++) cin>>a[i]; vector <vector <ll> > x(n+1); vector <pair <ll,ll> > t; for (int i = 0;i < n;i++) t.push_back({a[i],i}); sort(t.begin(),t.end()); map <ll,ll> mp; map <ll,ll> mp2; for (int i = 0;i<=1e4;i++) mp[i] = -1e18; for (int i = 0;i < t.size();i++) { if (mp[t[i].F]<0) mp[t[i].F] = i; mp2[t[i].F] = i; } for (int i = 0;i<=1e4;i++) { if (mp[i]==-1e18) continue; reverse(t.begin()+mp[i],t.begin()+mp2[i]+1); } ll nn = n-1; set <ll> st; x[n] = a; ll uk = 0; while (nn) { st.insert(t[uk].S); vector <ll> b; for (int i = 0;i < n;i++) if (!st.count(i)) b.push_back(a[i]); x[nn] = b; uk++; nn--; } ll test; cin>>test; while (test--) { ll xa,r; cin>>xa>>r; r--; cout<<x[xa][r]<<"\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" ] }
IN-CORRECT
cpp
#include<bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace std; using namespace __gnu_pbds; # define MAXN 100005 # define INF 1000000000 # define MOD 1000000007 typedef long long ll; typedef long l; typedef tree<ll, null_type, less_equal<ll>, rb_tree_tag, tree_order_statistics_node_update> ordered_set; //find_by_order;used in orsered_set // order_of_key; # define vi vector<int> # define vl vector<l> # define vll vector<ll> # define si set<int> # define sll set<ll> # define sc set<char> # define usl unordered_set<l> # define usll unordered_set<ll> # define usc unordered_set<char> # define ml map<l,l> # define mll map<ll,ll> # define mcl map<char,ll> # define umll unorderd_map<long long int,long long int> # define umci unordered_map<char,int> #define pll pair<ll,ll> # define fori(i,range) for(l i=0;i<range;i++) # define forl(i,range) for(ll i=0;i<range;i++) # define f first # define s second #define pb push_back #define db pop_back #define MP make_pair #define MT make_tuple # define fastio ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); static mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); // 2-D vector with both size--> vector<vector<int> > a(n, vector<int>(m)); // (long int)(1e-7 + pow(n, 1.0 / k)); to find n^(1/k); // in map and set time to search,insert and delete a element is logn ll hcf(ll a,ll b) { if(!a) return(b); else return(hcf(b%a,a)); } ll findGCD(vector<ll>arr, ll n) { ll result = arr[0]; for (ll i = 1; i < n; i++) { result = hcf(arr[i], result); if(result == 1) { return 1; } } return result; } ll lcm(ll a,ll b){ return((a*b)/hcf(a,b)); } #define pie 3.141592653589793238462643383279 bool sortbysec(ll a,ll b) { return (a>b); } vll primeFactors(ll n) { vll ans; while (n % 2 == 0) {ans.pb(2);n = n / 2;} for (ll i = 3; i <= sqrt(n); i = i + 2) { while (n % i == 0) {ans.pb(i);n = n / i;}} if (n > 2)ans.pb(n);return ans; } void show(vector<int>v,ll n) { for(ll i=0;i<n;i++) { cout<<v[i]<<' '; } } bool isPrime(ll n) { // Corner cases if (n <= 1) return false; if (n <= 3) return true; // This is checked so that we can skip // middle five numbers in below loop // time complexity root of n if (n%2 == 0 || n%3 == 0) return false; for (ll i=5; i*i<=n; i=i+6) if (n%i == 0 || n%(i+2) == 0) return false; return true; } bool isPowerOfTwo(ll x) { return (x && !(x & (x - 1))); } ll powM(ll x, ll y, ll m) { //returns (a^b)%m ( ^ is exponent ) ll ans = 1, r = 1; x %= m; while (r > 0 && r <= y) { if (r & y) { ans *= x; ans %= m; } r <<= 1; x *= x; x %= m; } return ans; } map<ll, ll> factorize(ll n) { //returns map containing factor and multiplicity, Eg: 60 = {{2,2},{3,1},{5,1}} map<ll, ll> ans; for (ll i = 2; i * i <= n; i++) { while (n % i == 0) { ans[i]++; n /= i; } } if (n > 1) { ans[n]++; n = 1; } return ans; } ll nCrModpDP(ll n, ll r, ll p) { // The array C is going to store last row of // pascal triangle at the end. And last entry // of last row is nCr ll C[r+1]; memset(C, 0, sizeof(C)); C[0] = 1; // Top row of Pascal Triangle // One by constructs remaining rows of Pascal // Triangle from top to bottom for (ll i = 1; i <= n; i++) { // Fill entries of current row using previous // row values for (ll j = min(i, r); j > 0; j--) // nCj = (n-1)Cj + (n-1)C(j-1); C[j] = (C[j] + C[j-1])%p; } return C[r]; } ll nCrModpLucas(ll n, ll r,ll p) { // Base case if (r==0) return 1; // Compute last digits of n and r in base p ll ni = n%p, ri = r%p; // Compute result for last digits computed above, and // for remaining digits. Multiply the two results and // compute the result of multiplication in modulo p. return (nCrModpLucas(n/p, r/p, p) * // Last digits of n and r nCrModpDP(ni, ri, p)) % p; // Remaining digits } int main() { fastio; ll n,i,j,a,b,c,k,m,d=0; cin>>n; vll v(n),v1(n),v4(n); forl(i,n)cin>>v[i]; v1=v; v4=v; sort(v1.begin(),v1.end()); cin>>m; for(i=0;i<m;i++) { v=v4; cin>>a>>b; if(a==n) { cout<<v[b-1]<<"\n"; } else if(a==1) { cout<<v1[n-1]<<"\n"; } else { sll s; c=0; vector<pair<ll,ll> >v3(a); for(j=n-a;j<n;j++) { s.insert(v1[j]); d=0; for(k=0;k<n;k++) { if(v[k]==v1[j] && j==n-a) { v3[c].first=v[k]; v3[c].second=k; c++; v[k]=0; d=k; break; } else if(v[k]==v1[j] && k>d) { v3[c].first=v[k]; v3[c].second=k; c++; v[k]=0; d=k; break; } else if(v[k]==v1[j]) { d++; } } if(d>0) { for(k=d;k>=0;k--) { if(v[k]==v1[j]) { v3[c].first=v[k]; v3[c].second=k; c++; v[k]=0; d=k; break; } } } } vll v2; for(j=0;j<n;j++) { auto it=s.find(v[j]); if(it!=s.end()) { v2.pb(v[j]); } } if(v2.size()==a) { cout<<v2[b-1]<<"\n"; } else { sort(v3.begin(),v3.end()); while(1) { a=0; for(j=1;j<v3.size();j++) { if(v3[j].second<v3[j-1].second) { swap(v3[j],v3[j-1]); a++; } } if(a==0) { break; } } cout<<v3[b-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" ] }
IN-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); for (int i = 0; i < n; i++) { cout << vec[i].first << " " << vec[i].second << endl; } 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" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; void jakos() { ios_base::sync_with_stdio(false); cin.tie(0); } const int mod = 1e9 + 7; const int base = 179; const int INF = 1e9; const int N = 1e5; signed main() { jakos(); int n; cin >> n; vector<pair<int, int>> a; for (int i = 0; i < n; i++) { int b; cin >> b; a.emplace_back(b, i); } sort(a.begin(), a.end()); reverse(a.begin(), a.end()); int q; cin >> q; while (q--) { vector<pair<int, int>> ans; int k, pos; cin >> k >> pos; for (int i = 0; i < k; i++) { ans.emplace_back(-a[i].second, a[i].first); } sort(ans.begin(), ans.end()); cout << ans[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" ] }
IN-CORRECT
python3
n = int(input()) a = list(map(int, input().split())) a1 = sorted(a, key=lambda x: -x) m = int(input()) for i in range(m): k, p = list(map(int, input().split())) c = {} s = [] for j in range(n+1): s.append({}) pos = {} for j in range(n): c[a1[j]] = 0 s[0][a[j]] = 0 pos[a1[j]] = [] for j in range(n): s[0][a[j]] += 1 for j in range(n): for t in range(n): s[j+1][a[t]] = s[j][a[t]] s[j+1][a[j]] -= 1 b = [] for j in range(k): c[a1[j]]+=1 ns = [] for j in range(n): if c[a[j]] ^ 0: pos[a[j]].append(j) if len(pos[a[j]]) == 1: ns.append(a[j]) ns = sorted(ns) counter = 0 ans = [] minpos = 0 while counter ^ k: flag = 0 for j in range(len(ns)): c[ns[j]] -= 1 flag1 = 0 for f in range(len(pos[ns[j]])): if minpos <= pos[ns[j]][f]: for u in range(len(ns)): if s[pos[ns[j]][f]+1][ns[u]] < c[ns[u]]: flag1 = 1 break if flag1 == 1: break else: minpos = pos[ns[j]][f]+1 flag = 1 break if flag == 1: ans.append(ns[j]) counter += 1 break else: c[ns[j]] += 1 print(ans[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" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; pair<int, int> a[100]; int ind[100]; bool cmp(int i, int j) { if (a[i].first == a[j].first) return a[i].second < a[j].second; return a[i].first > a[j].first; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; for (int i = 0; i < n; ++i) { cin >> a[i].first; a[i].second = i, ind[i] = i; } sort(ind, ind + n, cmp); for (int i = 0; i < n; ++i) cout << a[ind[i]].first << ' ' << a[ind[i]].second << '\n'; int m; cin >> m; while (m--) { int k, pos; cin >> k >> pos; vector<int> chosen(k); for (int i = 0; i < k; ++i) chosen[i] = a[ind[i]].second; sort(chosen.begin(), chosen.end()); cout << a[chosen[pos - 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" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; bool sortinrev(const pair<long long int, long long int> &a, const pair<long long int, long long int> &b) { return (a.first >= b.first); } int main() { long long int i, j, k, l, m, n; cin >> n; long long int a[n]; vector<pair<long long int, long long int>> vp; for (i = 0; i < n; i++) { cin >> a[i]; vp.push_back(make_pair(a[i], i)); } sort(vp.begin(), vp.end(), sortinrev); long long int var; cin >> var; while (var--) { cin >> k >> l; vector<long long int> v; for (i = 0; i < k; i++) v.push_back(vp[i].second); sort(v.begin(), v.end()); cout << a[v[l - 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" ] }
IN-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 + n + 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" ] }
IN-CORRECT
python3
n = int(input()) c = list(map(lambda x: (int(x[1]),x[0]),enumerate(input().split()))) so = sorted(c) for i in range(int(input())): k,r = map(int,input().split()) now = so[-k:] now.sort(key = lambda x: x[1]) print(now[r-1][0])
1269_E. K Integers
You are given a permutation p_1, p_2, …, p_n. In one move you can swap two adjacent values. You want to perform a minimum number of moves, such that in the end there will exist a subsegment 1,2,…, k, in other words in the end there should be an integer i, 1 ≀ i ≀ n-k+1 such that p_i = 1, p_{i+1} = 2, …, p_{i+k-1}=k. Let f(k) be the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation. You need to find f(1), f(2), …, f(n). Input The first line of input contains one integer n (1 ≀ n ≀ 200 000): the number of elements in the permutation. The next line of input contains n integers p_1, p_2, …, p_n: given permutation (1 ≀ p_i ≀ n). Output Print n integers, the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation, for k=1, 2, …, n. Examples Input 5 5 4 3 2 1 Output 0 1 3 6 10 Input 3 1 2 3 Output 0 0 0
{ "input": [ "3\n1 2 3\n", "5\n5 4 3 2 1\n" ], "output": [ "0 0 0\n", "0 1 3 6 10\n" ] }
{ "input": [ "1\n1\n", "100\n98 52 63 2 18 96 31 58 84 40 41 45 66 100 46 71 26 48 81 20 73 91 68 76 13 93 17 29 64 95 79 21 55 75 19 85 54 51 89 78 15 87 43 59 36 1 90 35 65 56 62 28 86 5 82 49 3 99 33 9 92 32 74 69 27 22 77 16 44 94 34 6 57 70 23 12 61 25 8 11 67 47 83 88 10 14 30 7 97 60 42 37 24 38 53 50 4 80 72 39\n", "10\n5 1 6 2 8 3 4 10 9 7\n" ], "output": [ "0\n", "0 42 52 101 101 117 146 166 166 188 194 197 249 258 294 298 345 415 445 492 522 529 540 562 569 628 628 644 684 699 765 766 768 774 791 812 828 844 863 931 996 1011 1036 1040 1105 1166 1175 1232 1237 1251 1282 1364 1377 1409 1445 1455 1461 1534 1553 1565 1572 1581 1664 1706 1715 1779 1787 1837 1841 1847 1909 1919 1973 1976 2010 2060 2063 2087 2125 2133 2192 2193 2196 2276 2305 2305 2324 2327 2352 2361 2417 2418 2467 2468 2510 2598 2599 2697 2697 2770\n", "0 1 2 3 8 9 12 12 13 13\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int N = 1e6 + 10; const int mod = 1e9 + 7; int head[N]; int dis[N], ecnt; int fa[N]; int cat[2005][2005]; long long gcd(long long a, long long b) { return a % b == 0 ? b : gcd(b, a % b); } long long qpow(long long base, long long n) { long long ans = 1; while (n) { if (n & 1) ans = (ans * base) % mod; n >>= 1; base = base * base % mod; } return ans; } struct Node { long long x, y; } nd[N]; long long m, n, x, k, y; struct EDGE { int u, v, nxt; long long w; } e[N]; bool cmp(EDGE a, EDGE b) { return a.w < b.w; } void add_edge(int u, int v, long long w) { e[ecnt].u = u; e[ecnt].v = v; e[ecnt].w = w; e[ecnt].nxt = head[u]; head[u] = ecnt++; } int fd(int x) { return -1 == fa[x] ? x : fa[x] = fd(fa[x]); } int c[N]; int lowbit(int x) { return x & (-x); } void add(long long *c, int x, int y) { while (x <= n) { c[x] += y; x += lowbit(x); } } long long getsum(long long *c, int x) { long long res = 0; while (x > 0) { res += c[x]; x -= lowbit(x); } return res; } int vis[N]; int p[N]; long long ans; int C[55][55]; int dp[150][150]; long long posar[N], smar[N]; struct H { int p, s; friend bool operator<(H a, H b) { return a.p < b.p; } } he[N]; bool cmp(int a, int b) { return a > b; } priority_queue<int> q; vector<int> v[30]; map<vector<int>, int> mp; int ar[N]; int br[N]; vector<int> vv[10]; int main() { { cin >> n; for (int i = 1; i <= n; i++) { scanf("%d", &ar[i]); p[ar[i]] = i; } long long inv = 0; for (int i = 1; i <= n; i++) { inv += i - 1 - getsum(smar, p[i]); add(smar, p[i], 1); add(posar, p[i], p[i]); int l = 1; int r = n; int mid; while (l < r) { mid = 1 + l + r >> 1; if (getsum(smar, mid - 1) * 2 <= i) l = mid; else r = mid - 1; } mid = l; long long pre_cnt_sum = getsum(smar, mid), pre_pos_sum = getsum(posar, mid); long long mov = pre_cnt_sum * mid - pre_pos_sum - pre_cnt_sum * (pre_cnt_sum - 1) / 2; long long aft_cnt_sum = i - pre_cnt_sum; mov += getsum(posar, n) - pre_pos_sum - aft_cnt_sum * mid - aft_cnt_sum * (aft_cnt_sum + 1) / 2; cout << inv + mov << " \n"[i == n]; } } return 0; }
1269_E. K Integers
You are given a permutation p_1, p_2, …, p_n. In one move you can swap two adjacent values. You want to perform a minimum number of moves, such that in the end there will exist a subsegment 1,2,…, k, in other words in the end there should be an integer i, 1 ≀ i ≀ n-k+1 such that p_i = 1, p_{i+1} = 2, …, p_{i+k-1}=k. Let f(k) be the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation. You need to find f(1), f(2), …, f(n). Input The first line of input contains one integer n (1 ≀ n ≀ 200 000): the number of elements in the permutation. The next line of input contains n integers p_1, p_2, …, p_n: given permutation (1 ≀ p_i ≀ n). Output Print n integers, the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation, for k=1, 2, …, n. Examples Input 5 5 4 3 2 1 Output 0 1 3 6 10 Input 3 1 2 3 Output 0 0 0
{ "input": [ "3\n1 2 3\n", "5\n5 4 3 2 1\n" ], "output": [ "0 0 0\n", "0 1 3 6 10\n" ] }
{ "input": [ "1\n1\n", "100\n98 52 63 2 18 96 31 58 84 40 41 45 66 100 46 71 26 48 81 20 73 91 68 76 13 93 17 29 64 95 79 21 55 75 19 85 54 51 89 78 15 87 43 59 36 1 90 35 65 56 62 28 86 5 82 49 3 99 33 9 92 32 74 69 27 22 77 16 44 94 34 6 57 70 23 12 61 25 8 11 67 47 83 88 10 14 30 7 97 60 42 37 24 38 53 50 4 80 72 39\n", "10\n5 1 6 2 8 3 4 10 9 7\n" ], "output": [ "0\n", "0 42 52 101 101 117 146 166 166 188 194 197 249 258 294 298 345 415 445 492 522 529 540 562 569 628 628 644 684 699 765 766 768 774 791 812 828 844 863 931 996 1011 1036 1040 1105 1166 1175 1232 1237 1251 1282 1364 1377 1409 1445 1455 1461 1534 1553 1565 1572 1581 1664 1706 1715 1779 1787 1837 1841 1847 1909 1919 1973 1976 2010 2060 2063 2087 2125 2133 2192 2193 2196 2276 2305 2305 2324 2327 2352 2361 2417 2418 2467 2468 2510 2598 2599 2697 2697 2770\n", "0 1 2 3 8 9 12 12 13 13\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const long long INF = (long long)1 << 62; const long long MOD = 1e9 + 7; const int iINF = 1 << 30; const double PI = 3.14159265359; int LSOne(int n) { return n & (-n); } class FenwickTree { private: vector<int> ft; public: FenwickTree(int n) { ft.assign(n + 1, 0); } int rsq(int b) { int sum = 0; for (; b; b -= LSOne(b)) { sum += ft[b]; } return sum; } int rsq(int a, int b) { return rsq(b) - (a == 1 ? 0 : rsq(a - 1)); } void adjust(int k, int v) { for (; k < (int)ft.size(); k += LSOne(k)) { ft[k] += v; } } }; long long nsum(int x, int y) { long long s = (long long)y * (y + 1) / 2; s -= (long long)x * (x - 1) / 2; return s; } void solve() { int n; cin >> n; vector<int> a(n), pos(n + 1), less(n + 1); for (int i = 0; i < n; i++) { cin >> a[i]; pos[a[i]] = i; } FenwickTree ft(n); long long inv = 0; set<int> mintree, maxtree; long long minsum = 0, maxsum = 0; cout << "0 "; ft.adjust(pos[1] + 1, 1); mintree.insert(pos[1]); minsum += pos[1]; for (int i = 2; i <= n; i++) { auto it1 = mintree.end(); it1--; if (pos[i] < *it1) { maxsum += *it1; maxtree.insert(*it1); minsum -= *it1; mintree.erase(it1); minsum += pos[i]; mintree.insert(pos[i]); } else { maxsum += pos[i]; maxtree.insert(pos[i]); } if (maxtree.size() > mintree.size()) { int val = *maxtree.begin(); maxsum -= val; maxtree.erase(maxtree.begin()); minsum += val; mintree.insert(val); } inv += ft.rsq(pos[i] + 1, n); it1 = mintree.end(); it1--; int med = *it1; int x = med - mintree.size() + 1; int y = med + maxtree.size(); long long push = nsum(x, med) - minsum; push += maxsum - nsum(med + 1, y); cout << inv + push << " "; ft.adjust(pos[i] + 1, 1); } cout << "\n"; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int t = 1; while (t--) { solve(); } return 0; }
1269_E. K Integers
You are given a permutation p_1, p_2, …, p_n. In one move you can swap two adjacent values. You want to perform a minimum number of moves, such that in the end there will exist a subsegment 1,2,…, k, in other words in the end there should be an integer i, 1 ≀ i ≀ n-k+1 such that p_i = 1, p_{i+1} = 2, …, p_{i+k-1}=k. Let f(k) be the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation. You need to find f(1), f(2), …, f(n). Input The first line of input contains one integer n (1 ≀ n ≀ 200 000): the number of elements in the permutation. The next line of input contains n integers p_1, p_2, …, p_n: given permutation (1 ≀ p_i ≀ n). Output Print n integers, the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation, for k=1, 2, …, n. Examples Input 5 5 4 3 2 1 Output 0 1 3 6 10 Input 3 1 2 3 Output 0 0 0
{ "input": [ "3\n1 2 3\n", "5\n5 4 3 2 1\n" ], "output": [ "0 0 0\n", "0 1 3 6 10\n" ] }
{ "input": [ "1\n1\n", "100\n98 52 63 2 18 96 31 58 84 40 41 45 66 100 46 71 26 48 81 20 73 91 68 76 13 93 17 29 64 95 79 21 55 75 19 85 54 51 89 78 15 87 43 59 36 1 90 35 65 56 62 28 86 5 82 49 3 99 33 9 92 32 74 69 27 22 77 16 44 94 34 6 57 70 23 12 61 25 8 11 67 47 83 88 10 14 30 7 97 60 42 37 24 38 53 50 4 80 72 39\n", "10\n5 1 6 2 8 3 4 10 9 7\n" ], "output": [ "0\n", "0 42 52 101 101 117 146 166 166 188 194 197 249 258 294 298 345 415 445 492 522 529 540 562 569 628 628 644 684 699 765 766 768 774 791 812 828 844 863 931 996 1011 1036 1040 1105 1166 1175 1232 1237 1251 1282 1364 1377 1409 1445 1455 1461 1534 1553 1565 1572 1581 1664 1706 1715 1779 1787 1837 1841 1847 1909 1919 1973 1976 2010 2060 2063 2087 2125 2133 2192 2193 2196 2276 2305 2305 2324 2327 2352 2361 2417 2418 2467 2468 2510 2598 2599 2697 2697 2770\n", "0 1 2 3 8 9 12 12 13 13\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 5; int n; long long a[N], id[N]; pair<long long, long long> G[N]; void Upd(int x, int y) { while (x <= n) { G[x].first++; G[x].second += y; x += (x & -x); } } pair<long long, long long> Get(int x) { pair<long long, long long> res; res.first = res.second = 0; while (x > 0) { res.first += G[x].first; res.second += G[x].second; x -= (x & -x); } return res; } int main() { scanf("%lld", &n); for (int i = 1; i <= n; i++) { scanf("%lld", &a[i]); id[a[i]] = i; } long long inv = 0; for (int i = 1; i <= n; i++) { long long idx = id[i], ans = 0; inv += Get(n).first - Get(idx).first; ans = inv; Upd(idx, idx); long long l = 1, r = n, mid, res = -1; while (l <= r) { mid = (l + r) / 2; if (Get(mid).first >= (i + 1) / 2) { res = mid; r = mid - 1; } else l = mid + 1; } long long totR = res + Get(n).first - Get(res).first; ans += Get(n).second - Get(res).second - (totR * (totR + 1) / 2 - res * (res + 1) / 2); long long totL = res - Get(res - 1).first; ans += (res - 1) * res / 2 - (totL - 1) * totL / 2 - Get(res - 1).second; printf("%lld ", ans); } }
1269_E. K Integers
You are given a permutation p_1, p_2, …, p_n. In one move you can swap two adjacent values. You want to perform a minimum number of moves, such that in the end there will exist a subsegment 1,2,…, k, in other words in the end there should be an integer i, 1 ≀ i ≀ n-k+1 such that p_i = 1, p_{i+1} = 2, …, p_{i+k-1}=k. Let f(k) be the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation. You need to find f(1), f(2), …, f(n). Input The first line of input contains one integer n (1 ≀ n ≀ 200 000): the number of elements in the permutation. The next line of input contains n integers p_1, p_2, …, p_n: given permutation (1 ≀ p_i ≀ n). Output Print n integers, the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation, for k=1, 2, …, n. Examples Input 5 5 4 3 2 1 Output 0 1 3 6 10 Input 3 1 2 3 Output 0 0 0
{ "input": [ "3\n1 2 3\n", "5\n5 4 3 2 1\n" ], "output": [ "0 0 0\n", "0 1 3 6 10\n" ] }
{ "input": [ "1\n1\n", "100\n98 52 63 2 18 96 31 58 84 40 41 45 66 100 46 71 26 48 81 20 73 91 68 76 13 93 17 29 64 95 79 21 55 75 19 85 54 51 89 78 15 87 43 59 36 1 90 35 65 56 62 28 86 5 82 49 3 99 33 9 92 32 74 69 27 22 77 16 44 94 34 6 57 70 23 12 61 25 8 11 67 47 83 88 10 14 30 7 97 60 42 37 24 38 53 50 4 80 72 39\n", "10\n5 1 6 2 8 3 4 10 9 7\n" ], "output": [ "0\n", "0 42 52 101 101 117 146 166 166 188 194 197 249 258 294 298 345 415 445 492 522 529 540 562 569 628 628 644 684 699 765 766 768 774 791 812 828 844 863 931 996 1011 1036 1040 1105 1166 1175 1232 1237 1251 1282 1364 1377 1409 1445 1455 1461 1534 1553 1565 1572 1581 1664 1706 1715 1779 1787 1837 1841 1847 1909 1919 1973 1976 2010 2060 2063 2087 2125 2133 2192 2193 2196 2276 2305 2305 2324 2327 2352 2361 2417 2418 2467 2468 2510 2598 2599 2697 2697 2770\n", "0 1 2 3 8 9 12 12 13 13\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 10; int N; int position[maxn]; long long sum1[maxn << 2], sum2[maxn << 2]; void add(long long* bit, int pos, long long val) { while (pos <= N) { bit[pos] += val; pos += pos & (-pos); } } long long query(long long* bit, int pos) { long long ret = 0; while (pos) { ret += bit[pos]; pos -= pos & (-pos); } return ret; } int search(long long* bit, int val) { int i = 0; for (int j = 20; j >= 0; --j) { if ((i | (1 << j)) <= N && bit[i | (1 << j)] <= val) { val -= bit[i |= (1 << j)]; } } return i; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cin >> N; for (int i = 1; i <= N; ++i) { int p; cin >> p; position[p] = i; } long long cnt = 0; for (int i = 1; i <= N; ++i) { int p = position[i]; add(sum1, p, 1); cnt += i - query(sum1, p); add(sum2, p, p); long long pos = search(sum1, i / 2) + 1; long long sum = 0; long long a = i / 2, b = i - a - 1; sum += pos * a - a * (a + 1) / 2 - query(sum2, pos - 1); sum += (query(sum2, N) - query(sum2, pos)) - b * pos - b * (b + 1) / 2; cout << cnt + sum << " "; } cout << endl; }
1269_E. K Integers
You are given a permutation p_1, p_2, …, p_n. In one move you can swap two adjacent values. You want to perform a minimum number of moves, such that in the end there will exist a subsegment 1,2,…, k, in other words in the end there should be an integer i, 1 ≀ i ≀ n-k+1 such that p_i = 1, p_{i+1} = 2, …, p_{i+k-1}=k. Let f(k) be the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation. You need to find f(1), f(2), …, f(n). Input The first line of input contains one integer n (1 ≀ n ≀ 200 000): the number of elements in the permutation. The next line of input contains n integers p_1, p_2, …, p_n: given permutation (1 ≀ p_i ≀ n). Output Print n integers, the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation, for k=1, 2, …, n. Examples Input 5 5 4 3 2 1 Output 0 1 3 6 10 Input 3 1 2 3 Output 0 0 0
{ "input": [ "3\n1 2 3\n", "5\n5 4 3 2 1\n" ], "output": [ "0 0 0\n", "0 1 3 6 10\n" ] }
{ "input": [ "1\n1\n", "100\n98 52 63 2 18 96 31 58 84 40 41 45 66 100 46 71 26 48 81 20 73 91 68 76 13 93 17 29 64 95 79 21 55 75 19 85 54 51 89 78 15 87 43 59 36 1 90 35 65 56 62 28 86 5 82 49 3 99 33 9 92 32 74 69 27 22 77 16 44 94 34 6 57 70 23 12 61 25 8 11 67 47 83 88 10 14 30 7 97 60 42 37 24 38 53 50 4 80 72 39\n", "10\n5 1 6 2 8 3 4 10 9 7\n" ], "output": [ "0\n", "0 42 52 101 101 117 146 166 166 188 194 197 249 258 294 298 345 415 445 492 522 529 540 562 569 628 628 644 684 699 765 766 768 774 791 812 828 844 863 931 996 1011 1036 1040 1105 1166 1175 1232 1237 1251 1282 1364 1377 1409 1445 1455 1461 1534 1553 1565 1572 1581 1664 1706 1715 1779 1787 1837 1841 1847 1909 1919 1973 1976 2010 2060 2063 2087 2125 2133 2192 2193 2196 2276 2305 2305 2324 2327 2352 2361 2417 2418 2467 2468 2510 2598 2599 2697 2697 2770\n", "0 1 2 3 8 9 12 12 13 13\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 7; int a[maxn]; class BIT { public: long long a[maxn]; void add(int x, long long c) { for (int i = x; i < maxn; i += i & -i) a[i] += c; } long long sum(int x) { long long res = 0; for (int i = x; i > 0; i -= i & -i) res += a[i]; return res; } } b1, b2; int main() { ios::sync_with_stdio(false); cin.tie(0); int n, x; cin >> n; for (int i = 1; i <= n; i++) { cin >> x; a[x] = i; } long long inv = 0; for (int i = 1; i <= n; i++) { b1.add(a[i], 1); inv += i - b1.sum(a[i]); b2.add(a[i], a[i]); x = i / 2 + 1; long long res = b2.sum(n); int l = 1, r = n; while (l < r) { int mid = (l + r) >> 1; if (b1.sum(mid) >= x) r = mid; else l = mid + 1; } long long pos = l; long long s = b2.sum(pos); long long lans = pos * (pos + 1) / 2 - s - (pos - x) * (pos - x + 1) / 2; long long rans = res - s - (i - x) * (pos + 1 + i - x + pos) / 2; cout << lans + rans + inv << ' '; } cout << endl; return 0; }
1269_E. K Integers
You are given a permutation p_1, p_2, …, p_n. In one move you can swap two adjacent values. You want to perform a minimum number of moves, such that in the end there will exist a subsegment 1,2,…, k, in other words in the end there should be an integer i, 1 ≀ i ≀ n-k+1 such that p_i = 1, p_{i+1} = 2, …, p_{i+k-1}=k. Let f(k) be the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation. You need to find f(1), f(2), …, f(n). Input The first line of input contains one integer n (1 ≀ n ≀ 200 000): the number of elements in the permutation. The next line of input contains n integers p_1, p_2, …, p_n: given permutation (1 ≀ p_i ≀ n). Output Print n integers, the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation, for k=1, 2, …, n. Examples Input 5 5 4 3 2 1 Output 0 1 3 6 10 Input 3 1 2 3 Output 0 0 0
{ "input": [ "3\n1 2 3\n", "5\n5 4 3 2 1\n" ], "output": [ "0 0 0\n", "0 1 3 6 10\n" ] }
{ "input": [ "1\n1\n", "100\n98 52 63 2 18 96 31 58 84 40 41 45 66 100 46 71 26 48 81 20 73 91 68 76 13 93 17 29 64 95 79 21 55 75 19 85 54 51 89 78 15 87 43 59 36 1 90 35 65 56 62 28 86 5 82 49 3 99 33 9 92 32 74 69 27 22 77 16 44 94 34 6 57 70 23 12 61 25 8 11 67 47 83 88 10 14 30 7 97 60 42 37 24 38 53 50 4 80 72 39\n", "10\n5 1 6 2 8 3 4 10 9 7\n" ], "output": [ "0\n", "0 42 52 101 101 117 146 166 166 188 194 197 249 258 294 298 345 415 445 492 522 529 540 562 569 628 628 644 684 699 765 766 768 774 791 812 828 844 863 931 996 1011 1036 1040 1105 1166 1175 1232 1237 1251 1282 1364 1377 1409 1445 1455 1461 1534 1553 1565 1572 1581 1664 1706 1715 1779 1787 1837 1841 1847 1909 1919 1973 1976 2010 2060 2063 2087 2125 2133 2192 2193 2196 2276 2305 2305 2324 2327 2352 2361 2417 2418 2467 2468 2510 2598 2599 2697 2697 2770\n", "0 1 2 3 8 9 12 12 13 13\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int N = 2e5; int a[N], p[N], inv[N], n; int f[N + 1]; int read(int idx) { int sum = 0; idx++; while (idx > 0) { sum += f[idx]; idx -= (idx & -idx); } return sum; } void update(int idx, int val) { idx++; while (idx <= n) { f[idx] += val; idx += (idx & -idx); } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> n; for (int i = 0; i < n; i++) cin >> a[i], a[i]--; for (int i = 0; i < n; i++) p[a[i]] = i; for (int i = n - 1; i >= 0; i--) { inv[a[i]] = read(a[i]); update(a[i], 1); } long long ans = 0; int l = p[0], r = l; update(p[0], -1); cout << ans << " "; for (int i = 1; i < n; i++) { int lc = p[i] + 1 - read(p[i]), rc = i - lc; update(p[i], -1); if (lc > rc) { int lb = 0, rb = p[i] - 1; while (lb <= rb) { int m = (lb + rb) / 2; int mc = m + 1 - read(m); if (mc >= i + 1 - mc) { rb = m - 1; } else { lb = m + 1; } } ans += read(p[i]) - read(lb); } else if (lc < rc) { ans += rc - lc; int lb = p[i] + 1, rb = n - 1; while (lb <= rb) { int m = (lb + rb) / 2; int mc = m + 1 - read(m); if (mc <= i + 1 - mc) { lb = m + 1; } else { rb = m - 1; } } ans += read(rb) - read(p[i]); } cout << ans << " "; } return 0; }
1269_E. K Integers
You are given a permutation p_1, p_2, …, p_n. In one move you can swap two adjacent values. You want to perform a minimum number of moves, such that in the end there will exist a subsegment 1,2,…, k, in other words in the end there should be an integer i, 1 ≀ i ≀ n-k+1 such that p_i = 1, p_{i+1} = 2, …, p_{i+k-1}=k. Let f(k) be the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation. You need to find f(1), f(2), …, f(n). Input The first line of input contains one integer n (1 ≀ n ≀ 200 000): the number of elements in the permutation. The next line of input contains n integers p_1, p_2, …, p_n: given permutation (1 ≀ p_i ≀ n). Output Print n integers, the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation, for k=1, 2, …, n. Examples Input 5 5 4 3 2 1 Output 0 1 3 6 10 Input 3 1 2 3 Output 0 0 0
{ "input": [ "3\n1 2 3\n", "5\n5 4 3 2 1\n" ], "output": [ "0 0 0\n", "0 1 3 6 10\n" ] }
{ "input": [ "1\n1\n", "100\n98 52 63 2 18 96 31 58 84 40 41 45 66 100 46 71 26 48 81 20 73 91 68 76 13 93 17 29 64 95 79 21 55 75 19 85 54 51 89 78 15 87 43 59 36 1 90 35 65 56 62 28 86 5 82 49 3 99 33 9 92 32 74 69 27 22 77 16 44 94 34 6 57 70 23 12 61 25 8 11 67 47 83 88 10 14 30 7 97 60 42 37 24 38 53 50 4 80 72 39\n", "10\n5 1 6 2 8 3 4 10 9 7\n" ], "output": [ "0\n", "0 42 52 101 101 117 146 166 166 188 194 197 249 258 294 298 345 415 445 492 522 529 540 562 569 628 628 644 684 699 765 766 768 774 791 812 828 844 863 931 996 1011 1036 1040 1105 1166 1175 1232 1237 1251 1282 1364 1377 1409 1445 1455 1461 1534 1553 1565 1572 1581 1664 1706 1715 1779 1787 1837 1841 1847 1909 1919 1973 1976 2010 2060 2063 2087 2125 2133 2192 2193 2196 2276 2305 2305 2324 2327 2352 2361 2417 2418 2467 2468 2510 2598 2599 2697 2697 2770\n", "0 1 2 3 8 9 12 12 13 13\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; struct fenwick { int N; vector<int> bit; fenwick(int n = 1e5) { N = n + 5; bit.assign(N, 0); } void resize(int n) { N = n + 5; bit.assign(N, 0); } void update(int x, int val) { while (x < N) { bit[x] += val; x += x & -x; } } int sum(int x) { int ret = 0; while (x > 0) { ret += bit[x]; x -= x & -x; } return ret; } }; int main() { ios_base::sync_with_stdio(0); cout.tie(0); cin.tie(0); int n; cin >> n; vector<int> idx(n + 5); for (int i = 1; i <= n; ++i) { int x; cin >> x; idx[x] = i; } fenwick bit(n); set<long long, greater<long long>> l; set<long long> r; long long lsum = 0, rsum = 0, ans = 0; for (int i = 1; i <= n; ++i) { bit.update(idx[i], 1); ans += i - bit.sum(idx[i]); if (l.size() == 0 || idx[i] < *l.begin()) l.insert(idx[i]), lsum += idx[i]; else r.insert(idx[i]), rsum += idx[i]; if (l.size() > r.size() + 1) { int x = *l.begin(); lsum -= x; rsum += x; l.erase(l.begin()); r.insert(x); } else if (r.size() > l.size()) { int x = *r.begin(); lsum += x; rsum -= x; l.insert(x); r.erase(r.begin()); } long long lcnt = (l.size() * (*l.begin())) - lsum - (1ll * l.size() * (l.size() - 1)) / 2; long long rcnt = (rsum - r.size() * (*l.begin())) - (1ll * r.size() * (r.size() + 1)) / 2; cout << ans + lcnt + rcnt << ' '; } }
1269_E. K Integers
You are given a permutation p_1, p_2, …, p_n. In one move you can swap two adjacent values. You want to perform a minimum number of moves, such that in the end there will exist a subsegment 1,2,…, k, in other words in the end there should be an integer i, 1 ≀ i ≀ n-k+1 such that p_i = 1, p_{i+1} = 2, …, p_{i+k-1}=k. Let f(k) be the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation. You need to find f(1), f(2), …, f(n). Input The first line of input contains one integer n (1 ≀ n ≀ 200 000): the number of elements in the permutation. The next line of input contains n integers p_1, p_2, …, p_n: given permutation (1 ≀ p_i ≀ n). Output Print n integers, the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation, for k=1, 2, …, n. Examples Input 5 5 4 3 2 1 Output 0 1 3 6 10 Input 3 1 2 3 Output 0 0 0
{ "input": [ "3\n1 2 3\n", "5\n5 4 3 2 1\n" ], "output": [ "0 0 0\n", "0 1 3 6 10\n" ] }
{ "input": [ "1\n1\n", "100\n98 52 63 2 18 96 31 58 84 40 41 45 66 100 46 71 26 48 81 20 73 91 68 76 13 93 17 29 64 95 79 21 55 75 19 85 54 51 89 78 15 87 43 59 36 1 90 35 65 56 62 28 86 5 82 49 3 99 33 9 92 32 74 69 27 22 77 16 44 94 34 6 57 70 23 12 61 25 8 11 67 47 83 88 10 14 30 7 97 60 42 37 24 38 53 50 4 80 72 39\n", "10\n5 1 6 2 8 3 4 10 9 7\n" ], "output": [ "0\n", "0 42 52 101 101 117 146 166 166 188 194 197 249 258 294 298 345 415 445 492 522 529 540 562 569 628 628 644 684 699 765 766 768 774 791 812 828 844 863 931 996 1011 1036 1040 1105 1166 1175 1232 1237 1251 1282 1364 1377 1409 1445 1455 1461 1534 1553 1565 1572 1581 1664 1706 1715 1779 1787 1837 1841 1847 1909 1919 1973 1976 2010 2060 2063 2087 2125 2133 2192 2193 2196 2276 2305 2305 2324 2327 2352 2361 2417 2418 2467 2468 2510 2598 2599 2697 2697 2770\n", "0 1 2 3 8 9 12 12 13 13\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const long long maxn = 2e5 + 10; long long sum1[maxn]; long long sum2[maxn]; long long n; long long lowbit(long long x) { return x & -x; } void add(long long *sum, long long x, long long v) { while (x <= n) { sum[x] += v; x += lowbit(x); } } long long query(long long *sum, long long x) { long long res = 0; while (x > 0) { res += sum[x]; x -= lowbit(x); } return res; } long long a[maxn]; long long pos[maxn]; signed main() { scanf("%lld", &n); for (long long i = 1; i <= n; i++) { scanf("%lld", &a[i]); pos[a[i]] = i; } long long ans1 = 0; for (long long i = 1; i <= n; i++) { ans1 += i - 1 - query(sum1, pos[i]); add(sum1, pos[i], 1); add(sum2, pos[i], pos[i]); long long mid, l = 1, r = n; while (l <= r) { mid = (l + r) >> 1; if (query(sum1, mid) * 2 <= i) { l = mid + 1; } else { r = mid - 1; } } long long ans2 = 0; long long cnt = query(sum1, mid), sum = query(sum2, mid); ans2 += mid * cnt - sum - cnt * (cnt - 1) / 2; cnt = i - cnt, sum = query(sum2, n) - sum; ans2 += sum - cnt * (mid + 1) - cnt * (cnt - 1) / 2; printf("%lld ", ans1 + ans2); } puts(""); return 0; }
1269_E. K Integers
You are given a permutation p_1, p_2, …, p_n. In one move you can swap two adjacent values. You want to perform a minimum number of moves, such that in the end there will exist a subsegment 1,2,…, k, in other words in the end there should be an integer i, 1 ≀ i ≀ n-k+1 such that p_i = 1, p_{i+1} = 2, …, p_{i+k-1}=k. Let f(k) be the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation. You need to find f(1), f(2), …, f(n). Input The first line of input contains one integer n (1 ≀ n ≀ 200 000): the number of elements in the permutation. The next line of input contains n integers p_1, p_2, …, p_n: given permutation (1 ≀ p_i ≀ n). Output Print n integers, the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation, for k=1, 2, …, n. Examples Input 5 5 4 3 2 1 Output 0 1 3 6 10 Input 3 1 2 3 Output 0 0 0
{ "input": [ "3\n1 2 3\n", "5\n5 4 3 2 1\n" ], "output": [ "0 0 0\n", "0 1 3 6 10\n" ] }
{ "input": [ "1\n1\n", "100\n98 52 63 2 18 96 31 58 84 40 41 45 66 100 46 71 26 48 81 20 73 91 68 76 13 93 17 29 64 95 79 21 55 75 19 85 54 51 89 78 15 87 43 59 36 1 90 35 65 56 62 28 86 5 82 49 3 99 33 9 92 32 74 69 27 22 77 16 44 94 34 6 57 70 23 12 61 25 8 11 67 47 83 88 10 14 30 7 97 60 42 37 24 38 53 50 4 80 72 39\n", "10\n5 1 6 2 8 3 4 10 9 7\n" ], "output": [ "0\n", "0 42 52 101 101 117 146 166 166 188 194 197 249 258 294 298 345 415 445 492 522 529 540 562 569 628 628 644 684 699 765 766 768 774 791 812 828 844 863 931 996 1011 1036 1040 1105 1166 1175 1232 1237 1251 1282 1364 1377 1409 1445 1455 1461 1534 1553 1565 1572 1581 1664 1706 1715 1779 1787 1837 1841 1847 1909 1919 1973 1976 2010 2060 2063 2087 2125 2133 2192 2193 2196 2276 2305 2305 2324 2327 2352 2361 2417 2418 2467 2468 2510 2598 2599 2697 2697 2770\n", "0 1 2 3 8 9 12 12 13 13\n" ] }
CORRECT
java
import java.io.*; import java.util.*; public class Contest1 { //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////// ///////// //////// ///////// //////// HHHH HHHH EEEEEEEEEEEEE MMMM MMMM OOOOOO SSSSSSS EEEEEEEEEEEEE ///////// //////// HHHH HHHH EEEEEEEEEEEEE MMMMMM MMMMMM OOO OOO SSSS SSS EEEEEEEEEEEEE ///////// //////// HHHH HHHH EEEEE MMMM MMM MMM MMMM OOO OOO SSSS SSS EEEEE ///////// //////// HHHH HHHH EEEEE MMMM MMMMMM MMMM OOO OOO SSSS EEEEE ///////// //////// HHHH HHHH EEEEE MMMM MMMM OOO OOO SSSSSSS EEEEE ///////// //////// HHHHHHHHHHHHHHHH EEEEEEEEEEE MMMM MMMM OOO OOO SSSSSS EEEEEEEEEEE ///////// //////// HHHHHHHHHHHHHHHH EEEEEEEEEEE MMMM MMMM OOO OOO SSSSSSS EEEEEEEEEEE ///////// //////// HHHH HHHH EEEEE MMMM MMMM OOO OOO SSSS EEEEE ///////// //////// HHHH HHHH EEEEE MMMM MMMM OOO OOO SSS SSSS EEEEE ///////// //////// HHHH HHHH EEEEEEEEEEEEE MMMM MMMM OOO OOO SSS SSSS EEEEEEEEEEEEE ///////// //////// HHHH HHHH EEEEEEEEEEEEE MMMM MMMM OOOOOO SSSSSSS EEEEEEEEEEEEE ///////// //////// ///////// //////// ///////// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// public static void main(String[] args) throws IOException, InterruptedException { Scanner sc = new Scanner(System.in); PrintWriter pw = new PrintWriter(System.out); int n = sc.nextInt(); int[] a = new int[n]; int[] pos = new int[n+1]; for (int i =0;i<n;i++){ a[i]=sc.nextInt(); pos[a[i]]=i+1; } FenwickTree fn = new FenwickTree(n); long[] ans = new long[n+1]; //count inversions for (int i =n-1;i>=0;i--){ ans[a[i]]+=fn.rsq(a[i]); fn.point_update(a[i],1); } for (int i =1;i<=n;i++)ans[i]+=ans[i-1]; // pw.println(Arrays.toString(ans)); // fn= new FenwickTree(n); FenwickTree fn2 = new FenwickTree(n); fn.point_update(pos[1],1); fn2.point_update(pos[1],pos[1]); for (int i =2;i<=n;i++){ fn.point_update(pos[i],1); fn2.point_update(pos[i],pos[i]); int low = 1; int hi = n; int idx=0; while (low<=hi){ int mid = low+hi >>1; if (fn.rsq(mid)>=(i+1)/2){ idx= mid; hi = mid-1; } else low=mid+1; } long pre= +sumrange(idx-(i+1)/2+1,idx-1); pre-=fn2.rsq(idx-1); if (fn.rsq(idx)==1)pre=0; int add=i-(i+1)/2; long after=fn2.rsq(idx+1,n)-sumrange(idx+1,idx+add); if (add==0)after=0; ans[i]+=pre+after; } for (int i =1;i<=n;i++) pw.print(ans[i]+" "); pw.println(); pw.flush(); } static long sumrange(int a, int b){ return -1l*a*(a-1)/2 + 1l*b*(b+1)/2; } static class FenwickTree { // one-based DS int n; long[] ft; FenwickTree(int size) { n = size; ft = new long[n+1]; } long rsq(int b) //O(log n) { long sum = 0; while(b > 0) { sum += ft[b]; b -= b & -b;} //min? return sum; } long 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? } } 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(); } } }
1269_E. K Integers
You are given a permutation p_1, p_2, …, p_n. In one move you can swap two adjacent values. You want to perform a minimum number of moves, such that in the end there will exist a subsegment 1,2,…, k, in other words in the end there should be an integer i, 1 ≀ i ≀ n-k+1 such that p_i = 1, p_{i+1} = 2, …, p_{i+k-1}=k. Let f(k) be the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation. You need to find f(1), f(2), …, f(n). Input The first line of input contains one integer n (1 ≀ n ≀ 200 000): the number of elements in the permutation. The next line of input contains n integers p_1, p_2, …, p_n: given permutation (1 ≀ p_i ≀ n). Output Print n integers, the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation, for k=1, 2, …, n. Examples Input 5 5 4 3 2 1 Output 0 1 3 6 10 Input 3 1 2 3 Output 0 0 0
{ "input": [ "3\n1 2 3\n", "5\n5 4 3 2 1\n" ], "output": [ "0 0 0\n", "0 1 3 6 10\n" ] }
{ "input": [ "1\n1\n", "100\n98 52 63 2 18 96 31 58 84 40 41 45 66 100 46 71 26 48 81 20 73 91 68 76 13 93 17 29 64 95 79 21 55 75 19 85 54 51 89 78 15 87 43 59 36 1 90 35 65 56 62 28 86 5 82 49 3 99 33 9 92 32 74 69 27 22 77 16 44 94 34 6 57 70 23 12 61 25 8 11 67 47 83 88 10 14 30 7 97 60 42 37 24 38 53 50 4 80 72 39\n", "10\n5 1 6 2 8 3 4 10 9 7\n" ], "output": [ "0\n", "0 42 52 101 101 117 146 166 166 188 194 197 249 258 294 298 345 415 445 492 522 529 540 562 569 628 628 644 684 699 765 766 768 774 791 812 828 844 863 931 996 1011 1036 1040 1105 1166 1175 1232 1237 1251 1282 1364 1377 1409 1445 1455 1461 1534 1553 1565 1572 1581 1664 1706 1715 1779 1787 1837 1841 1847 1909 1919 1973 1976 2010 2060 2063 2087 2125 2133 2192 2193 2196 2276 2305 2305 2324 2327 2352 2361 2417 2418 2467 2468 2510 2598 2599 2697 2697 2770\n", "0 1 2 3 8 9 12 12 13 13\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const long long maxn = 2e5 + 5; long long c[maxn], d[maxn]; long long n; long long lowbit(long long x) { return x & (-x); } void update(long long i, long long k) { while (i <= n) { c[i] += k; i += lowbit(i); } } long long getsum(long long i) { long long res = 0; while (i > 0) { res += c[i]; i -= lowbit(i); } return res; } void update2(long long i, long long k) { while (i <= n) { d[i] += k; i += lowbit(i); } } long long getsum2(long long i) { long long res = 0; while (i > 0) { res += d[i]; i -= lowbit(i); } return res; } long long f[maxn], where[maxn]; long long bs(long long sum) { long long l = 0, r = n; while (l <= r) { long long mid = (l + r) >> 1; if (getsum2(mid) >= sum) r = mid - 1; else l = mid + 1; } return l; } long long dc(long long x, long long y) { long long len = y - x + 1; return (x + y) * len / 2; } int main() { ios::sync_with_stdio(false); cin >> n; for (long long i = 1; i <= n; ++i) { cin >> f[i]; where[f[i]] = i; } long long cnt = 0; for (long long i = 1; i <= n; ++i) { long long pos = where[i]; update2(pos, 1); cnt += i - getsum2(pos); update(pos, pos); long long base = bs(i / 2 + 1); long long lmost = base - i / 2; long long rmost = base + i / 2 - ((i + 1) % 2); long long t = dc(lmost, base) - dc(base + 1, rmost); long long sum = 0; sum -= getsum(base); sum += getsum(n) - getsum(base); cout << sum + t + cnt << " "; } cout << endl; return 0; }
1269_E. K Integers
You are given a permutation p_1, p_2, …, p_n. In one move you can swap two adjacent values. You want to perform a minimum number of moves, such that in the end there will exist a subsegment 1,2,…, k, in other words in the end there should be an integer i, 1 ≀ i ≀ n-k+1 such that p_i = 1, p_{i+1} = 2, …, p_{i+k-1}=k. Let f(k) be the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation. You need to find f(1), f(2), …, f(n). Input The first line of input contains one integer n (1 ≀ n ≀ 200 000): the number of elements in the permutation. The next line of input contains n integers p_1, p_2, …, p_n: given permutation (1 ≀ p_i ≀ n). Output Print n integers, the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation, for k=1, 2, …, n. Examples Input 5 5 4 3 2 1 Output 0 1 3 6 10 Input 3 1 2 3 Output 0 0 0
{ "input": [ "3\n1 2 3\n", "5\n5 4 3 2 1\n" ], "output": [ "0 0 0\n", "0 1 3 6 10\n" ] }
{ "input": [ "1\n1\n", "100\n98 52 63 2 18 96 31 58 84 40 41 45 66 100 46 71 26 48 81 20 73 91 68 76 13 93 17 29 64 95 79 21 55 75 19 85 54 51 89 78 15 87 43 59 36 1 90 35 65 56 62 28 86 5 82 49 3 99 33 9 92 32 74 69 27 22 77 16 44 94 34 6 57 70 23 12 61 25 8 11 67 47 83 88 10 14 30 7 97 60 42 37 24 38 53 50 4 80 72 39\n", "10\n5 1 6 2 8 3 4 10 9 7\n" ], "output": [ "0\n", "0 42 52 101 101 117 146 166 166 188 194 197 249 258 294 298 345 415 445 492 522 529 540 562 569 628 628 644 684 699 765 766 768 774 791 812 828 844 863 931 996 1011 1036 1040 1105 1166 1175 1232 1237 1251 1282 1364 1377 1409 1445 1455 1461 1534 1553 1565 1572 1581 1664 1706 1715 1779 1787 1837 1841 1847 1909 1919 1973 1976 2010 2060 2063 2087 2125 2133 2192 2193 2196 2276 2305 2305 2324 2327 2352 2361 2417 2418 2467 2468 2510 2598 2599 2697 2697 2770\n", "0 1 2 3 8 9 12 12 13 13\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 5; const int oo = 1e9 + 7; int a[maxn], c[maxn], n; int pos[maxn]; long long ans[maxn]; void update(int x) { for (int i = x; i <= n && i; i += i & (-i)) c[i]++; } long long query(int x) { long long ans = 0; while (x) ans = ans + 1ll * c[x], x -= x & (-x); return ans; } priority_queue<int> q1, q2; int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d", &a[i]), pos[a[i]] = i; long long a1 = 0, ln = 0, rn = 0; for (long long i = 1; i <= n; i++) { update(pos[i]); a1 = a1 + i - query(pos[i]); if (q1.size() && pos[i] < q1.top()) { ln += pos[i]; q1.push(pos[i]); } else { rn += pos[i]; q2.push(-pos[i]); } while (q1.size() < q2.size()) { long long c = -q2.top(); ln += c; rn -= c; q2.pop(); q1.push(c); } while (q1.size() > q2.size() + 1) { long long c = q1.top(); ln -= c; rn += c; q1.pop(); q2.push(-c); } long long top = q1.top(); ans[i] = a1 + 1ll * top * q1.size() - ln - 1ll * q1.size() * (q1.size() - 1) / 2 + rn - top * q2.size() - 1ll * q2.size() * (q2.size() + 1) / 2; ; ; } for (int i = 1; i <= n; i++) { if (i != 1) printf(" "); printf("%lld", ans[i]); } }
1269_E. K Integers
You are given a permutation p_1, p_2, …, p_n. In one move you can swap two adjacent values. You want to perform a minimum number of moves, such that in the end there will exist a subsegment 1,2,…, k, in other words in the end there should be an integer i, 1 ≀ i ≀ n-k+1 such that p_i = 1, p_{i+1} = 2, …, p_{i+k-1}=k. Let f(k) be the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation. You need to find f(1), f(2), …, f(n). Input The first line of input contains one integer n (1 ≀ n ≀ 200 000): the number of elements in the permutation. The next line of input contains n integers p_1, p_2, …, p_n: given permutation (1 ≀ p_i ≀ n). Output Print n integers, the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation, for k=1, 2, …, n. Examples Input 5 5 4 3 2 1 Output 0 1 3 6 10 Input 3 1 2 3 Output 0 0 0
{ "input": [ "3\n1 2 3\n", "5\n5 4 3 2 1\n" ], "output": [ "0 0 0\n", "0 1 3 6 10\n" ] }
{ "input": [ "1\n1\n", "100\n98 52 63 2 18 96 31 58 84 40 41 45 66 100 46 71 26 48 81 20 73 91 68 76 13 93 17 29 64 95 79 21 55 75 19 85 54 51 89 78 15 87 43 59 36 1 90 35 65 56 62 28 86 5 82 49 3 99 33 9 92 32 74 69 27 22 77 16 44 94 34 6 57 70 23 12 61 25 8 11 67 47 83 88 10 14 30 7 97 60 42 37 24 38 53 50 4 80 72 39\n", "10\n5 1 6 2 8 3 4 10 9 7\n" ], "output": [ "0\n", "0 42 52 101 101 117 146 166 166 188 194 197 249 258 294 298 345 415 445 492 522 529 540 562 569 628 628 644 684 699 765 766 768 774 791 812 828 844 863 931 996 1011 1036 1040 1105 1166 1175 1232 1237 1251 1282 1364 1377 1409 1445 1455 1461 1534 1553 1565 1572 1581 1664 1706 1715 1779 1787 1837 1841 1847 1909 1919 1973 1976 2010 2060 2063 2087 2125 2133 2192 2193 2196 2276 2305 2305 2324 2327 2352 2361 2417 2418 2467 2468 2510 2598 2599 2697 2697 2770\n", "0 1 2 3 8 9 12 12 13 13\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; struct Node { Node *l, *r; int count; long long sum; int ask(int k, int ll, int rr) { if (ll == rr) { return count; } int mid = (ll + rr) >> 1; if (k <= mid) { return l ? l->ask(k, ll, mid) : 0; } else { return (l ? l->count : 0) + (r ? r->ask(k, mid + 1, rr) : 0); } } long long asksum(int k, int ll, int rr) { if (ll == rr) { return sum; } int mid = (ll + rr) >> 1; if (k <= mid) { return l ? l->asksum(k, ll, mid) : 0; } else { return (l ? l->sum : 0) + (r ? r->asksum(k, mid + 1, rr) : 0); } } void add(int, int, int, int); } node[200005 * 25]; int cnt; Node *getNode() { ++cnt; return node + cnt; } void Node::add(int k, int w, int ll, int rr) { ++count; sum += w; if (ll == rr) { return; } int mid = (ll + rr) >> 1; if (k <= mid) { if (l) { Node tmp = *l; l = getNode(); (*l) = tmp; } else { l = getNode(); } l->add(k, w, ll, mid); } else { if (r) { Node tmp = *r; r = getNode(); (*r) = tmp; } else { r = getNode(); } r->add(k, w, mid + 1, rr); } } int n; Node root[200005]; int rf(int a, int b) { int l = 0, r = n, mid; while (l + 1 != r) { mid = (l + r) >> 1; if (root[mid].ask(a, 0, n) < b) { l = mid; } else { r = mid; } } return r; } int a[200005], b[200005]; int main() { scanf("%d", &n); for (int i = 1; i <= n; ++i) { scanf("%d", &a[i]); b[a[i]] = i; root[i] = root[i - 1]; root[i].add(a[i], i, 0, n); } long long sum = 0; for (int i = 1; i <= n; ++i) { int d = rf(i, (i >> 1) + (i & 1)); long long ans = 0; ans += root[d].asksum(n, 0, n) - root[d].asksum(i, 0, n); long long tmp = d - (i >> 1) - (i & 1); ans -= tmp * (tmp + 1) / 2; tmp = d + (i >> 1) + 1; ans += (n + tmp) * (n - tmp + 1) / 2; ans -= (root[n].asksum(n, 0, n) - root[n].asksum(i, 0, n)) - (root[d].asksum(n, 0, n) - root[d].asksum(i, 0, n)); sum += i - root[b[i]].ask(i, 0, n); ans += sum; printf("%lld ", ans); } }
1269_E. K Integers
You are given a permutation p_1, p_2, …, p_n. In one move you can swap two adjacent values. You want to perform a minimum number of moves, such that in the end there will exist a subsegment 1,2,…, k, in other words in the end there should be an integer i, 1 ≀ i ≀ n-k+1 such that p_i = 1, p_{i+1} = 2, …, p_{i+k-1}=k. Let f(k) be the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation. You need to find f(1), f(2), …, f(n). Input The first line of input contains one integer n (1 ≀ n ≀ 200 000): the number of elements in the permutation. The next line of input contains n integers p_1, p_2, …, p_n: given permutation (1 ≀ p_i ≀ n). Output Print n integers, the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation, for k=1, 2, …, n. Examples Input 5 5 4 3 2 1 Output 0 1 3 6 10 Input 3 1 2 3 Output 0 0 0
{ "input": [ "3\n1 2 3\n", "5\n5 4 3 2 1\n" ], "output": [ "0 0 0\n", "0 1 3 6 10\n" ] }
{ "input": [ "1\n1\n", "100\n98 52 63 2 18 96 31 58 84 40 41 45 66 100 46 71 26 48 81 20 73 91 68 76 13 93 17 29 64 95 79 21 55 75 19 85 54 51 89 78 15 87 43 59 36 1 90 35 65 56 62 28 86 5 82 49 3 99 33 9 92 32 74 69 27 22 77 16 44 94 34 6 57 70 23 12 61 25 8 11 67 47 83 88 10 14 30 7 97 60 42 37 24 38 53 50 4 80 72 39\n", "10\n5 1 6 2 8 3 4 10 9 7\n" ], "output": [ "0\n", "0 42 52 101 101 117 146 166 166 188 194 197 249 258 294 298 345 415 445 492 522 529 540 562 569 628 628 644 684 699 765 766 768 774 791 812 828 844 863 931 996 1011 1036 1040 1105 1166 1175 1232 1237 1251 1282 1364 1377 1409 1445 1455 1461 1534 1553 1565 1572 1581 1664 1706 1715 1779 1787 1837 1841 1847 1909 1919 1973 1976 2010 2060 2063 2087 2125 2133 2192 2193 2196 2276 2305 2305 2324 2327 2352 2361 2417 2418 2467 2468 2510 2598 2599 2697 2697 2770\n", "0 1 2 3 8 9 12 12 13 13\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; long long const inf = 1e9; long long const mod = 1e9 + 7; long double const eps = 1e-9; long long bit[200005]; long long bit2[200005]; long long sumbit[200005]; long long n; void update(long long k) { while (k <= 200000) { bit[k] += 1; k += k & -k; } } long long sum(long long k) { long long s = 0; while (k > 0) { s += bit[k]; k -= k & -k; } return s; } void update2(long long k) { while (k <= 200000) { bit2[k] += 1; k += k & -k; } } long long sum2(long long k) { long long s = 0; while (k > 0) { s += bit2[k]; k -= k & -k; } return s; } void update3(long long k, long long val) { while (k <= 200000) { sumbit[k] += val; k += k & -k; } } long long sum3(long long k) { long long s = 0; while (k > 0) { s += sumbit[k]; k -= k & -k; } return s; } long long cinv(long long pos, long long num) { long long SUM = num - 1; SUM -= sum2(pos); update2(pos); return SUM; } int main() { cin >> n; long long a[n]; long long pos[n + 1]; for (int i = (0); i < (n); i++) { cin >> a[i]; pos[a[i]] = i + 1; } long long inversions = 0; for (int i = (1); i < (n + 1); i++) { inversions += cinv(pos[i], i); update(pos[i]); update3(pos[i], pos[i]); long long k = 0; for (long long b = n / 2; b >= 1; b /= 2) { while (k + b <= n && sum(k + b) <= i / 2) k += b; } k++; long long l = sum(k - 1); long long r = sum(n) - sum(k); long long suml = sum3(k - 1); long long sumr = sum3(n) - sum3(k); long long ans = inversions + (long long)l * k - suml + sumr - (long long)r * k - (long long)(l * (l + 1)) / 2 - (long long)(r * (r + 1)) / 2; cout << ans << endl; } return 0; }
1269_E. K Integers
You are given a permutation p_1, p_2, …, p_n. In one move you can swap two adjacent values. You want to perform a minimum number of moves, such that in the end there will exist a subsegment 1,2,…, k, in other words in the end there should be an integer i, 1 ≀ i ≀ n-k+1 such that p_i = 1, p_{i+1} = 2, …, p_{i+k-1}=k. Let f(k) be the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation. You need to find f(1), f(2), …, f(n). Input The first line of input contains one integer n (1 ≀ n ≀ 200 000): the number of elements in the permutation. The next line of input contains n integers p_1, p_2, …, p_n: given permutation (1 ≀ p_i ≀ n). Output Print n integers, the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation, for k=1, 2, …, n. Examples Input 5 5 4 3 2 1 Output 0 1 3 6 10 Input 3 1 2 3 Output 0 0 0
{ "input": [ "3\n1 2 3\n", "5\n5 4 3 2 1\n" ], "output": [ "0 0 0\n", "0 1 3 6 10\n" ] }
{ "input": [ "1\n1\n", "100\n98 52 63 2 18 96 31 58 84 40 41 45 66 100 46 71 26 48 81 20 73 91 68 76 13 93 17 29 64 95 79 21 55 75 19 85 54 51 89 78 15 87 43 59 36 1 90 35 65 56 62 28 86 5 82 49 3 99 33 9 92 32 74 69 27 22 77 16 44 94 34 6 57 70 23 12 61 25 8 11 67 47 83 88 10 14 30 7 97 60 42 37 24 38 53 50 4 80 72 39\n", "10\n5 1 6 2 8 3 4 10 9 7\n" ], "output": [ "0\n", "0 42 52 101 101 117 146 166 166 188 194 197 249 258 294 298 345 415 445 492 522 529 540 562 569 628 628 644 684 699 765 766 768 774 791 812 828 844 863 931 996 1011 1036 1040 1105 1166 1175 1232 1237 1251 1282 1364 1377 1409 1445 1455 1461 1534 1553 1565 1572 1581 1664 1706 1715 1779 1787 1837 1841 1847 1909 1919 1973 1976 2010 2060 2063 2087 2125 2133 2192 2193 2196 2276 2305 2305 2324 2327 2352 2361 2417 2418 2467 2468 2510 2598 2599 2697 2697 2770\n", "0 1 2 3 8 9 12 12 13 13\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int MAX = 2e5 + 5; int a[MAX], p[MAX], n; long long c1[MAX], c2[MAX]; set<int> st; void add(long long *c, int x, int k) { for (; x <= n; x += x & -x) c[x] += k; } long long query(long long *c, int x) { long long sum = 0; for (; x; x -= x & -x) sum += c[x]; return sum; } int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d", &a[i]), p[a[i]] = i; st.insert(p[1]); auto it = st.begin(); printf("0 "); add(c1, p[1], 1); add(c2, p[1], p[1]); long long res = 0; for (int i = 2; i <= n; i++) { st.insert(p[i]); if (p[i] < (*it) && i % 2 == 0) it--; if (p[i] > (*it) && i % 2 == 1) it++; add(c1, p[i], 1); res += (i * 1ll - query(c1, p[i])); add(c2, p[i], p[i]); int midpos = *it; long long sum = 0, k = i / 2; sum += i & 1 ? k * 1ll * (midpos - 1 + midpos - k) / 2 : (k - 1) * 1ll * (midpos + midpos - k) / 2; sum -= k * 1ll * (midpos + 1 + midpos + k) / 2; sum -= query(c2, midpos - 1); sum += query(c2, n) - query(c2, midpos); printf("%I64d ", res + sum); } }
1269_E. K Integers
You are given a permutation p_1, p_2, …, p_n. In one move you can swap two adjacent values. You want to perform a minimum number of moves, such that in the end there will exist a subsegment 1,2,…, k, in other words in the end there should be an integer i, 1 ≀ i ≀ n-k+1 such that p_i = 1, p_{i+1} = 2, …, p_{i+k-1}=k. Let f(k) be the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation. You need to find f(1), f(2), …, f(n). Input The first line of input contains one integer n (1 ≀ n ≀ 200 000): the number of elements in the permutation. The next line of input contains n integers p_1, p_2, …, p_n: given permutation (1 ≀ p_i ≀ n). Output Print n integers, the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation, for k=1, 2, …, n. Examples Input 5 5 4 3 2 1 Output 0 1 3 6 10 Input 3 1 2 3 Output 0 0 0
{ "input": [ "3\n1 2 3\n", "5\n5 4 3 2 1\n" ], "output": [ "0 0 0\n", "0 1 3 6 10\n" ] }
{ "input": [ "1\n1\n", "100\n98 52 63 2 18 96 31 58 84 40 41 45 66 100 46 71 26 48 81 20 73 91 68 76 13 93 17 29 64 95 79 21 55 75 19 85 54 51 89 78 15 87 43 59 36 1 90 35 65 56 62 28 86 5 82 49 3 99 33 9 92 32 74 69 27 22 77 16 44 94 34 6 57 70 23 12 61 25 8 11 67 47 83 88 10 14 30 7 97 60 42 37 24 38 53 50 4 80 72 39\n", "10\n5 1 6 2 8 3 4 10 9 7\n" ], "output": [ "0\n", "0 42 52 101 101 117 146 166 166 188 194 197 249 258 294 298 345 415 445 492 522 529 540 562 569 628 628 644 684 699 765 766 768 774 791 812 828 844 863 931 996 1011 1036 1040 1105 1166 1175 1232 1237 1251 1282 1364 1377 1409 1445 1455 1461 1534 1553 1565 1572 1581 1664 1706 1715 1779 1787 1837 1841 1847 1909 1919 1973 1976 2010 2060 2063 2087 2125 2133 2192 2193 2196 2276 2305 2305 2324 2327 2352 2361 2417 2418 2467 2468 2510 2598 2599 2697 2697 2770\n", "0 1 2 3 8 9 12 12 13 13\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int N = 1e6 + 5; long long pos[N], s1[N], s2[N]; int a[N]; int n; void add(long long s[], int pos, long long v) { while (pos <= n) { s[pos] += v; pos += pos & (-pos); } } long long query(long long s[], int pos) { long long res = 0; while (pos) { res += s[pos]; pos -= pos & (-pos); } return res; } int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%d", &a[i]); pos[a[i]] = i; } long long ans = 0; for (int i = 1; i <= n; i++) { ans += i - 1 - query(s1, pos[i]); add(s1, pos[i], 1); add(s2, pos[i], pos[i]); int l = 1, r = n, mid; while (l <= r) { mid = (l + r) >> 1; if (query(s1, mid) * 2 <= i) l = mid + 1; else r = mid - 1; } long long ans2 = 0; long long cnt = query(s1, mid), sum = query(s2, mid); ans2 += mid * cnt - sum - cnt * (cnt - 1) / 2; cnt = i - cnt; sum = query(s2, n) - sum; ans2 += sum - cnt * (mid + 1) - cnt * (cnt - 1) / 2; printf("%lld ", ans + ans2); } return 0; }
1269_E. K Integers
You are given a permutation p_1, p_2, …, p_n. In one move you can swap two adjacent values. You want to perform a minimum number of moves, such that in the end there will exist a subsegment 1,2,…, k, in other words in the end there should be an integer i, 1 ≀ i ≀ n-k+1 such that p_i = 1, p_{i+1} = 2, …, p_{i+k-1}=k. Let f(k) be the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation. You need to find f(1), f(2), …, f(n). Input The first line of input contains one integer n (1 ≀ n ≀ 200 000): the number of elements in the permutation. The next line of input contains n integers p_1, p_2, …, p_n: given permutation (1 ≀ p_i ≀ n). Output Print n integers, the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation, for k=1, 2, …, n. Examples Input 5 5 4 3 2 1 Output 0 1 3 6 10 Input 3 1 2 3 Output 0 0 0
{ "input": [ "3\n1 2 3\n", "5\n5 4 3 2 1\n" ], "output": [ "0 0 0\n", "0 1 3 6 10\n" ] }
{ "input": [ "1\n1\n", "100\n98 52 63 2 18 96 31 58 84 40 41 45 66 100 46 71 26 48 81 20 73 91 68 76 13 93 17 29 64 95 79 21 55 75 19 85 54 51 89 78 15 87 43 59 36 1 90 35 65 56 62 28 86 5 82 49 3 99 33 9 92 32 74 69 27 22 77 16 44 94 34 6 57 70 23 12 61 25 8 11 67 47 83 88 10 14 30 7 97 60 42 37 24 38 53 50 4 80 72 39\n", "10\n5 1 6 2 8 3 4 10 9 7\n" ], "output": [ "0\n", "0 42 52 101 101 117 146 166 166 188 194 197 249 258 294 298 345 415 445 492 522 529 540 562 569 628 628 644 684 699 765 766 768 774 791 812 828 844 863 931 996 1011 1036 1040 1105 1166 1175 1232 1237 1251 1282 1364 1377 1409 1445 1455 1461 1534 1553 1565 1572 1581 1664 1706 1715 1779 1787 1837 1841 1847 1909 1919 1973 1976 2010 2060 2063 2087 2125 2133 2192 2193 2196 2276 2305 2305 2324 2327 2352 2361 2417 2418 2467 2468 2510 2598 2599 2697 2697 2770\n", "0 1 2 3 8 9 12 12 13 13\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 10; long long sum1[maxn << 2], sum2[maxn << 2]; int N, pos[maxn], a[maxn]; void update(long long* tree, int pos, int l, int r, int id, long long val) { if (l > r || pos < l || pos > r) return; if (pos == l && pos == r) { tree[id] += val; } else { int mid = (l + r) >> 1; update(tree, pos, l, mid, id << 1, val); update(tree, pos, mid + 1, r, id << 1 | 1, val); tree[id] = tree[id << 1] + tree[id << 1 | 1]; } } long long query(long long* tree, int l, int r, int x, int y, int id) { if (l > r || x > y || x > r || y < l) return 0; if (l <= x && y <= r) { return tree[id]; } int mid = (x + y) >> 1; if (mid < l) return query(tree, l, r, mid + 1, y, id << 1 | 1); if (mid >= r) return query(tree, l, r, x, mid, id << 1); return query(tree, l, r, x, mid, id << 1) + query(tree, l, r, mid + 1, y, id << 1 | 1); } int search(long long* tree, long long val) { int l = 1, r = N, ret = -1; while (l <= r) { int mid = (l + r) >> 1; if (query(tree, 1, mid, 1, N, 1) < val) { l = mid + 1; } else { ret = mid; r = mid - 1; } } return ret; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cin >> N; for (int i = 1; i <= N; ++i) cin >> a[i], pos[a[i]] = i; long long cnt = 0; for (int i = 1; i <= N; ++i) { int p = pos[i]; update(sum1, p, 1, N, 1, 1); cnt += i - query(sum1, 1, p, 1, N, 1); update(sum2, p, 1, N, 1, p); long long a = i / 2, b = i - a - 1, pos = search(sum1, i / 2 + 1), sum = 0; sum += -query(sum2, 1, pos - 1, 1, N, 1) + query(sum2, pos + 1, N, 1, N, 1); sum += (a * pos - a * (a + 1) / 2) - (b * pos + b * (b + 1) / 2); cout << cnt + sum << " "; } cout << endl; }
1269_E. K Integers
You are given a permutation p_1, p_2, …, p_n. In one move you can swap two adjacent values. You want to perform a minimum number of moves, such that in the end there will exist a subsegment 1,2,…, k, in other words in the end there should be an integer i, 1 ≀ i ≀ n-k+1 such that p_i = 1, p_{i+1} = 2, …, p_{i+k-1}=k. Let f(k) be the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation. You need to find f(1), f(2), …, f(n). Input The first line of input contains one integer n (1 ≀ n ≀ 200 000): the number of elements in the permutation. The next line of input contains n integers p_1, p_2, …, p_n: given permutation (1 ≀ p_i ≀ n). Output Print n integers, the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation, for k=1, 2, …, n. Examples Input 5 5 4 3 2 1 Output 0 1 3 6 10 Input 3 1 2 3 Output 0 0 0
{ "input": [ "3\n1 2 3\n", "5\n5 4 3 2 1\n" ], "output": [ "0 0 0\n", "0 1 3 6 10\n" ] }
{ "input": [ "1\n1\n", "100\n98 52 63 2 18 96 31 58 84 40 41 45 66 100 46 71 26 48 81 20 73 91 68 76 13 93 17 29 64 95 79 21 55 75 19 85 54 51 89 78 15 87 43 59 36 1 90 35 65 56 62 28 86 5 82 49 3 99 33 9 92 32 74 69 27 22 77 16 44 94 34 6 57 70 23 12 61 25 8 11 67 47 83 88 10 14 30 7 97 60 42 37 24 38 53 50 4 80 72 39\n", "10\n5 1 6 2 8 3 4 10 9 7\n" ], "output": [ "0\n", "0 42 52 101 101 117 146 166 166 188 194 197 249 258 294 298 345 415 445 492 522 529 540 562 569 628 628 644 684 699 765 766 768 774 791 812 828 844 863 931 996 1011 1036 1040 1105 1166 1175 1232 1237 1251 1282 1364 1377 1409 1445 1455 1461 1534 1553 1565 1572 1581 1664 1706 1715 1779 1787 1837 1841 1847 1909 1919 1973 1976 2010 2060 2063 2087 2125 2133 2192 2193 2196 2276 2305 2305 2324 2327 2352 2361 2417 2418 2467 2468 2510 2598 2599 2697 2697 2770\n", "0 1 2 3 8 9 12 12 13 13\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; long long n, fwk[2][200001], ps[200001] = {}, pv[200001] = {}, to1 = 0, to2 = 0; set<long long> s; set<long long>::iterator it; void upd(int x, int t, long long v) { for (; x <= n; x += x & (-x)) { fwk[t][x] += v; } } long long qry(int x, int t) { long long to = 0; for (; x; x -= x & (-x)) { to += fwk[t][x]; } return to; } long long sm(long long n) { return n * (n + 1) / 2; } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n; for (int i = 0; i < n; i++) { int x; cin >> x; ps[x] = i; } for (int i = 1; i <= n; i++) { if (i == 1) { it = s.insert(ps[i]).first; } else if (i % 2) { s.insert(ps[i]); if (ps[i] > *it) { it = next(it); } } else { s.insert(ps[i]); if (ps[i] < *it) { it = prev(it); } } upd(ps[i] + 1, 0, 1); upd(ps[i] + 1, 1, ps[i]); to1 = (i % 2) * (*it) - (2 * sm((i - 1) / 2) + ((i % 2 == 0) ? (i / 2) : 0)) - 2 * qry((*it) + 1, 1) + qry(n, 1); to2 += (long long)(i - qry(ps[i] + 1, 0)); cout << to1 + to2; if (i < n) { cout << " "; } } cout << "\n"; return 0; }
1269_E. K Integers
You are given a permutation p_1, p_2, …, p_n. In one move you can swap two adjacent values. You want to perform a minimum number of moves, such that in the end there will exist a subsegment 1,2,…, k, in other words in the end there should be an integer i, 1 ≀ i ≀ n-k+1 such that p_i = 1, p_{i+1} = 2, …, p_{i+k-1}=k. Let f(k) be the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation. You need to find f(1), f(2), …, f(n). Input The first line of input contains one integer n (1 ≀ n ≀ 200 000): the number of elements in the permutation. The next line of input contains n integers p_1, p_2, …, p_n: given permutation (1 ≀ p_i ≀ n). Output Print n integers, the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation, for k=1, 2, …, n. Examples Input 5 5 4 3 2 1 Output 0 1 3 6 10 Input 3 1 2 3 Output 0 0 0
{ "input": [ "3\n1 2 3\n", "5\n5 4 3 2 1\n" ], "output": [ "0 0 0\n", "0 1 3 6 10\n" ] }
{ "input": [ "1\n1\n", "100\n98 52 63 2 18 96 31 58 84 40 41 45 66 100 46 71 26 48 81 20 73 91 68 76 13 93 17 29 64 95 79 21 55 75 19 85 54 51 89 78 15 87 43 59 36 1 90 35 65 56 62 28 86 5 82 49 3 99 33 9 92 32 74 69 27 22 77 16 44 94 34 6 57 70 23 12 61 25 8 11 67 47 83 88 10 14 30 7 97 60 42 37 24 38 53 50 4 80 72 39\n", "10\n5 1 6 2 8 3 4 10 9 7\n" ], "output": [ "0\n", "0 42 52 101 101 117 146 166 166 188 194 197 249 258 294 298 345 415 445 492 522 529 540 562 569 628 628 644 684 699 765 766 768 774 791 812 828 844 863 931 996 1011 1036 1040 1105 1166 1175 1232 1237 1251 1282 1364 1377 1409 1445 1455 1461 1534 1553 1565 1572 1581 1664 1706 1715 1779 1787 1837 1841 1847 1909 1919 1973 1976 2010 2060 2063 2087 2125 2133 2192 2193 2196 2276 2305 2305 2324 2327 2352 2361 2417 2418 2467 2468 2510 2598 2599 2697 2697 2770\n", "0 1 2 3 8 9 12 12 13 13\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 7; int a[N]; struct fenwick { long long fen[N]; fenwick() { memset(fen, 0, sizeof fen); } void add(int x, int d) { for (int i = x + 1; i < N; i += i & -i) { fen[i] += d; } } long long sum(int x) { long long ans = 0; for (int i = x; i; i -= i & -i) { ans += fen[i]; } return ans; } long long sum(int l, int r) { return sum(r) - sum(l); } int kth(int k) { int x = 0; for (int i = 17; ~i; i--) { if ((x | 1 << i) < N && fen[x | 1 << i] <= k) { x |= 1 << i; k -= fen[x]; } } return x; } } t1, t2; int solve() { int n; scanf("%d", &n); for (int i = 1; i <= n; i++) { int x; scanf("%d", &x); a[x] = i; } long long rev = 0; for (int i = 1; i <= n; i++) { t1.add(a[i], 1); t2.add(a[i], a[i]); rev += t1.sum(a[i] + 1, 2e5 + 1); int t = i - 1 >> 1; int m = t1.kth(t); long long mv = 0; ++t; mv += t2.sum(m + 1, 2e5 + 1) - 1ll * (m + 1) * (i - t) - 1ll * (i - t) * (i - t - 1) / 2; mv += 1ll * m * t - t2.sum(m + 1) - 1ll * t * (t - 1) / 2; printf("%lld ", mv + rev); } puts(""); return 0; } signed main() { solve(); return 0; }
1269_E. K Integers
You are given a permutation p_1, p_2, …, p_n. In one move you can swap two adjacent values. You want to perform a minimum number of moves, such that in the end there will exist a subsegment 1,2,…, k, in other words in the end there should be an integer i, 1 ≀ i ≀ n-k+1 such that p_i = 1, p_{i+1} = 2, …, p_{i+k-1}=k. Let f(k) be the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation. You need to find f(1), f(2), …, f(n). Input The first line of input contains one integer n (1 ≀ n ≀ 200 000): the number of elements in the permutation. The next line of input contains n integers p_1, p_2, …, p_n: given permutation (1 ≀ p_i ≀ n). Output Print n integers, the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation, for k=1, 2, …, n. Examples Input 5 5 4 3 2 1 Output 0 1 3 6 10 Input 3 1 2 3 Output 0 0 0
{ "input": [ "3\n1 2 3\n", "5\n5 4 3 2 1\n" ], "output": [ "0 0 0\n", "0 1 3 6 10\n" ] }
{ "input": [ "1\n1\n", "100\n98 52 63 2 18 96 31 58 84 40 41 45 66 100 46 71 26 48 81 20 73 91 68 76 13 93 17 29 64 95 79 21 55 75 19 85 54 51 89 78 15 87 43 59 36 1 90 35 65 56 62 28 86 5 82 49 3 99 33 9 92 32 74 69 27 22 77 16 44 94 34 6 57 70 23 12 61 25 8 11 67 47 83 88 10 14 30 7 97 60 42 37 24 38 53 50 4 80 72 39\n", "10\n5 1 6 2 8 3 4 10 9 7\n" ], "output": [ "0\n", "0 42 52 101 101 117 146 166 166 188 194 197 249 258 294 298 345 415 445 492 522 529 540 562 569 628 628 644 684 699 765 766 768 774 791 812 828 844 863 931 996 1011 1036 1040 1105 1166 1175 1232 1237 1251 1282 1364 1377 1409 1445 1455 1461 1534 1553 1565 1572 1581 1664 1706 1715 1779 1787 1837 1841 1847 1909 1919 1973 1976 2010 2060 2063 2087 2125 2133 2192 2193 2196 2276 2305 2305 2324 2327 2352 2361 2417 2418 2467 2468 2510 2598 2599 2697 2697 2770\n", "0 1 2 3 8 9 12 12 13 13\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const long long inf = 1e9 + 7; const long long INF = 1LL << 60; const long long mod = 1e9 + 7; const long double eps = 1e-8; const long double pi = acos(-1.0); template <class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template <class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } template <class Abel> struct BIT { private: vector<Abel> node; long long n; Abel UNITY_SUM = 0; public: BIT(long long n_) { n = n_; node.resize(n, UNITY_SUM); } void add(long long a, Abel w) { for (long long i = a; i < n; i |= i + 1) node[i] += w; } Abel sum(long long a) { Abel ret = UNITY_SUM; for (long long i = a - 1; i >= 0; i = (i & (i + 1)) - 1) ret += node[i]; return ret; } Abel sum(long long a, long long b) { return sum(b) - sum(a); } long long get(long long k) { ++k; long long res = 0; long long n = 1; while (n < (long long)node.size()) n *= 2; for (long long i = n / 2; i > 0; i /= 2) { if (res + i < (long long)node.size() && node[res + i - 1] < k) { k -= node[res + i - 1]; res += i; } } return res; } void print() { for (long long i = 0; i < n; ++i) cout << sum(i, i + 1) << ","; cout << endl; } }; void solve() { long long n; cin >> n; vector<long long> p(n); map<long long, long long> mp; for (long long i = 0; i < n; i++) { cin >> p[i]; mp[p[i]] = i; } BIT<long long> bit(n + 1); BIT<long long> bit2(n + 1); long long now = 0; for (long long i = 1; i <= n; ++i) { long long ans = 0; bit.add(mp[i], 1); bit2.add(mp[i], mp[i]); long long j1 = bit.get((i + 1) / 2 - 1); long long j2 = j1 + 1; if (i & 1) { ans += j1 * bit.sum(0, j1) - bit2.sum(0, j1); ans += bit2.sum(j1 + 1, n) - j1 * bit.sum(j1 + 1, n); long long k = i / 2; ans -= k * (k + 1); } else { ans += j1 * bit.sum(0, j1) - bit2.sum(0, j1); ans += bit2.sum(j2 + 1, n) - j2 * bit.sum(j2 + 1, n); long long k = (i - 2) / 2; ans -= k * (k + 1); } now += bit.sum(mp[i] + 1, n); ans += now; cout << ans << " "; } cout << endl; } signed main() { ios::sync_with_stdio(false); cin.tie(0); solve(); return 0; }
1269_E. K Integers
You are given a permutation p_1, p_2, …, p_n. In one move you can swap two adjacent values. You want to perform a minimum number of moves, such that in the end there will exist a subsegment 1,2,…, k, in other words in the end there should be an integer i, 1 ≀ i ≀ n-k+1 such that p_i = 1, p_{i+1} = 2, …, p_{i+k-1}=k. Let f(k) be the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation. You need to find f(1), f(2), …, f(n). Input The first line of input contains one integer n (1 ≀ n ≀ 200 000): the number of elements in the permutation. The next line of input contains n integers p_1, p_2, …, p_n: given permutation (1 ≀ p_i ≀ n). Output Print n integers, the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation, for k=1, 2, …, n. Examples Input 5 5 4 3 2 1 Output 0 1 3 6 10 Input 3 1 2 3 Output 0 0 0
{ "input": [ "3\n1 2 3\n", "5\n5 4 3 2 1\n" ], "output": [ "0 0 0\n", "0 1 3 6 10\n" ] }
{ "input": [ "1\n1\n", "100\n98 52 63 2 18 96 31 58 84 40 41 45 66 100 46 71 26 48 81 20 73 91 68 76 13 93 17 29 64 95 79 21 55 75 19 85 54 51 89 78 15 87 43 59 36 1 90 35 65 56 62 28 86 5 82 49 3 99 33 9 92 32 74 69 27 22 77 16 44 94 34 6 57 70 23 12 61 25 8 11 67 47 83 88 10 14 30 7 97 60 42 37 24 38 53 50 4 80 72 39\n", "10\n5 1 6 2 8 3 4 10 9 7\n" ], "output": [ "0\n", "0 42 52 101 101 117 146 166 166 188 194 197 249 258 294 298 345 415 445 492 522 529 540 562 569 628 628 644 684 699 765 766 768 774 791 812 828 844 863 931 996 1011 1036 1040 1105 1166 1175 1232 1237 1251 1282 1364 1377 1409 1445 1455 1461 1534 1553 1565 1572 1581 1664 1706 1715 1779 1787 1837 1841 1847 1909 1919 1973 1976 2010 2060 2063 2087 2125 2133 2192 2193 2196 2276 2305 2305 2324 2327 2352 2361 2417 2418 2467 2468 2510 2598 2599 2697 2697 2770\n", "0 1 2 3 8 9 12 12 13 13\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const long long maxn = 1e5 + 5; const long long MAXN = 1e6 + 5; long long n, m, t; long long a[MAXN]; long long tree1[MAXN], tree2[MAXN]; long long pos[MAXN]; void add(long long *tree, long long x, long long xx) { while (x <= n) tree[x] += xx, x += (x & (-x)); } long long query(long long *tree, long long x) { long long sum = 0; while (x > 0) sum += tree[x], x -= (x & (-x)); return sum; } long long bs(long long x) { long long l = 1, r = n; long long ans; while (l <= r) { long long mid = l + r >> 1; if (query(tree1, mid) > x / 2) ans = mid, r = mid - 1; else l = mid + 1; } return ans; } int main() { cin >> n; for (long long i = 1; i <= n; i++) scanf("%d", &a[i]), pos[a[i]] = i; long long cnt = 0; for (long long i = 1; i <= n; i++) { long long ans = 0; add(tree1, pos[i], 1); add(tree2, pos[i], pos[i]); cnt += query(tree1, n) - query(tree1, pos[i]); long long tmp = bs(i); long long a = i / 2, b = i - a - 1; ans += a * tmp - query(tree2, tmp - 1) - (a + 1) * a / 2; long long tmpp = query(tree2, n) - query(tree2, tmp); ans += tmpp - b * tmp - (b + 1) * b / 2; cout << ans + cnt << ' '; } }
1269_E. K Integers
You are given a permutation p_1, p_2, …, p_n. In one move you can swap two adjacent values. You want to perform a minimum number of moves, such that in the end there will exist a subsegment 1,2,…, k, in other words in the end there should be an integer i, 1 ≀ i ≀ n-k+1 such that p_i = 1, p_{i+1} = 2, …, p_{i+k-1}=k. Let f(k) be the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation. You need to find f(1), f(2), …, f(n). Input The first line of input contains one integer n (1 ≀ n ≀ 200 000): the number of elements in the permutation. The next line of input contains n integers p_1, p_2, …, p_n: given permutation (1 ≀ p_i ≀ n). Output Print n integers, the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation, for k=1, 2, …, n. Examples Input 5 5 4 3 2 1 Output 0 1 3 6 10 Input 3 1 2 3 Output 0 0 0
{ "input": [ "3\n1 2 3\n", "5\n5 4 3 2 1\n" ], "output": [ "0 0 0\n", "0 1 3 6 10\n" ] }
{ "input": [ "1\n1\n", "100\n98 52 63 2 18 96 31 58 84 40 41 45 66 100 46 71 26 48 81 20 73 91 68 76 13 93 17 29 64 95 79 21 55 75 19 85 54 51 89 78 15 87 43 59 36 1 90 35 65 56 62 28 86 5 82 49 3 99 33 9 92 32 74 69 27 22 77 16 44 94 34 6 57 70 23 12 61 25 8 11 67 47 83 88 10 14 30 7 97 60 42 37 24 38 53 50 4 80 72 39\n", "10\n5 1 6 2 8 3 4 10 9 7\n" ], "output": [ "0\n", "0 42 52 101 101 117 146 166 166 188 194 197 249 258 294 298 345 415 445 492 522 529 540 562 569 628 628 644 684 699 765 766 768 774 791 812 828 844 863 931 996 1011 1036 1040 1105 1166 1175 1232 1237 1251 1282 1364 1377 1409 1445 1455 1461 1534 1553 1565 1572 1581 1664 1706 1715 1779 1787 1837 1841 1847 1909 1919 1973 1976 2010 2060 2063 2087 2125 2133 2192 2193 2196 2276 2305 2305 2324 2327 2352 2361 2417 2418 2467 2468 2510 2598 2599 2697 2697 2770\n", "0 1 2 3 8 9 12 12 13 13\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; long long n, fwk[2][200001], ps[200001] = {}, pv[200001] = {}, to1 = 0, to2 = 0; set<long long> s; set<long long>::iterator it; void upd(int x, int t, long long v) { for (; x <= n; x += x & (-x)) { fwk[t][x] += v; } } long long qry(int x, int t) { long long to = 0; for (; x; x -= x & (-x)) { to += fwk[t][x]; } return to; } long long sm(long long n) { return n * (n + 1) / 2; } int main() { cin >> n; for (int i = 0; i < n; i++) { int x; cin >> x; ps[x] = i; } for (int i = 1; i <= n; i++) { if (i == 1) { it = s.insert(ps[i]).first; } else if (i % 2) { s.insert(ps[i]); if (ps[i] > *it) { it = next(it); } } else { s.insert(ps[i]); if (ps[i] < *it) { it = prev(it); } } upd(ps[i] + 1, 0, 1); upd(ps[i] + 1, 1, ps[i]); to1 = (i % 2) * (*it) - (2 * sm((i - 1) / 2) + ((i % 2 == 0) ? (i / 2) : 0)) - 2 * qry((*it) + 1, 1) + qry(n, 1); to2 += (long long)(i - qry(ps[i] + 1, 0)); cout << to1 + to2; if (i < n) { cout << " "; } } cout << "\n"; return 0; }
1269_E. K Integers
You are given a permutation p_1, p_2, …, p_n. In one move you can swap two adjacent values. You want to perform a minimum number of moves, such that in the end there will exist a subsegment 1,2,…, k, in other words in the end there should be an integer i, 1 ≀ i ≀ n-k+1 such that p_i = 1, p_{i+1} = 2, …, p_{i+k-1}=k. Let f(k) be the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation. You need to find f(1), f(2), …, f(n). Input The first line of input contains one integer n (1 ≀ n ≀ 200 000): the number of elements in the permutation. The next line of input contains n integers p_1, p_2, …, p_n: given permutation (1 ≀ p_i ≀ n). Output Print n integers, the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation, for k=1, 2, …, n. Examples Input 5 5 4 3 2 1 Output 0 1 3 6 10 Input 3 1 2 3 Output 0 0 0
{ "input": [ "3\n1 2 3\n", "5\n5 4 3 2 1\n" ], "output": [ "0 0 0\n", "0 1 3 6 10\n" ] }
{ "input": [ "1\n1\n", "100\n98 52 63 2 18 96 31 58 84 40 41 45 66 100 46 71 26 48 81 20 73 91 68 76 13 93 17 29 64 95 79 21 55 75 19 85 54 51 89 78 15 87 43 59 36 1 90 35 65 56 62 28 86 5 82 49 3 99 33 9 92 32 74 69 27 22 77 16 44 94 34 6 57 70 23 12 61 25 8 11 67 47 83 88 10 14 30 7 97 60 42 37 24 38 53 50 4 80 72 39\n", "10\n5 1 6 2 8 3 4 10 9 7\n" ], "output": [ "0\n", "0 42 52 101 101 117 146 166 166 188 194 197 249 258 294 298 345 415 445 492 522 529 540 562 569 628 628 644 684 699 765 766 768 774 791 812 828 844 863 931 996 1011 1036 1040 1105 1166 1175 1232 1237 1251 1282 1364 1377 1409 1445 1455 1461 1534 1553 1565 1572 1581 1664 1706 1715 1779 1787 1837 1841 1847 1909 1919 1973 1976 2010 2060 2063 2087 2125 2133 2192 2193 2196 2276 2305 2305 2324 2327 2352 2361 2417 2418 2467 2468 2510 2598 2599 2697 2697 2770\n", "0 1 2 3 8 9 12 12 13 13\n" ] }
CORRECT
java
import java.io.File; import java.io.FileNotFoundException; import java.util.Collections; import java.util.PriorityQueue; import java.util.Scanner; import java.util.StringTokenizer; public class E { public static void main(String[] args) throws Exception { // StringTokenizer stok = new StringTokenizer(new Scanner(new File("F:/books/input.txt")).useDelimiter("\\A").next()); StringTokenizer stok = new StringTokenizer(new Scanner(System.in).useDelimiter("\\A").next()); StringBuilder sb = new StringBuilder(); int n = Integer.parseInt(stok.nextToken()); int[] a = new int[n+1]; for (int i = 0; i < n; i++) { a[Integer.parseInt(stok.nextToken())]=i+1; } long tot1 = 0,tot2 = 0; long[] fen = new long[n+1]; PriorityQueue<Long> minH = new PriorityQueue<Long>(); PriorityQueue<Long> maxH = new PriorityQueue<Long>(Collections.reverseOrder()); long l = Long.MIN_VALUE,r = Long.MAX_VALUE; long ls=0,rs=0; for(int i=1;i<=n;i++) { tot1 += fenwickQ(n,fen)-fenwickQ(a[i],fen); fenwickI(a[i],1,fen); if(a[i]<=r) { maxH.add((long) a[i]); ls += a[i]; } else { minH.add((long) a[i]); rs += a[i]; } if(maxH.size()>minH.size()+1) { long v = maxH.poll(); minH.add(v); ls-=v;rs+=v; } else if(minH.size()>maxH.size()) { long v = minH.poll(); maxH.add(v); ls+=v;rs-=v; } l = maxH.peek(); r = minH.size()>0?minH.peek():r; // System.out.println(minH+"\n"+maxH+"\n"+i); // System.out.println(tot1+" "+tot2); tot2 += l*maxH.size()-ls; tot2 += rs-l*minH.size(); tot2 -= tr(maxH.size()-1); tot2 -= tr(minH.size()); // System.out.println(tot1+" "+tot2); // System.out.println("******"); sb.append((tot1+tot2)+" "); tot2=0; } System.out.println(sb); } private static long tr(long n) { return (n*(n+1)/2); } private static void fenwickI(int id, long v, long[] fen) { int l = fen.length; for(int i=id;i<l;i+=i&-i) { fen[i] += v; } } private static long fenwickQ(int id, long[] fen) { long ret = 0; for(int i=id;i>0;i-=i&-i) { ret += fen[i]; } return ret; } }
1269_E. K Integers
You are given a permutation p_1, p_2, …, p_n. In one move you can swap two adjacent values. You want to perform a minimum number of moves, such that in the end there will exist a subsegment 1,2,…, k, in other words in the end there should be an integer i, 1 ≀ i ≀ n-k+1 such that p_i = 1, p_{i+1} = 2, …, p_{i+k-1}=k. Let f(k) be the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation. You need to find f(1), f(2), …, f(n). Input The first line of input contains one integer n (1 ≀ n ≀ 200 000): the number of elements in the permutation. The next line of input contains n integers p_1, p_2, …, p_n: given permutation (1 ≀ p_i ≀ n). Output Print n integers, the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation, for k=1, 2, …, n. Examples Input 5 5 4 3 2 1 Output 0 1 3 6 10 Input 3 1 2 3 Output 0 0 0
{ "input": [ "3\n1 2 3\n", "5\n5 4 3 2 1\n" ], "output": [ "0 0 0\n", "0 1 3 6 10\n" ] }
{ "input": [ "1\n1\n", "100\n98 52 63 2 18 96 31 58 84 40 41 45 66 100 46 71 26 48 81 20 73 91 68 76 13 93 17 29 64 95 79 21 55 75 19 85 54 51 89 78 15 87 43 59 36 1 90 35 65 56 62 28 86 5 82 49 3 99 33 9 92 32 74 69 27 22 77 16 44 94 34 6 57 70 23 12 61 25 8 11 67 47 83 88 10 14 30 7 97 60 42 37 24 38 53 50 4 80 72 39\n", "10\n5 1 6 2 8 3 4 10 9 7\n" ], "output": [ "0\n", "0 42 52 101 101 117 146 166 166 188 194 197 249 258 294 298 345 415 445 492 522 529 540 562 569 628 628 644 684 699 765 766 768 774 791 812 828 844 863 931 996 1011 1036 1040 1105 1166 1175 1232 1237 1251 1282 1364 1377 1409 1445 1455 1461 1534 1553 1565 1572 1581 1664 1706 1715 1779 1787 1837 1841 1847 1909 1919 1973 1976 2010 2060 2063 2087 2125 2133 2192 2193 2196 2276 2305 2305 2324 2327 2352 2361 2417 2418 2467 2468 2510 2598 2599 2697 2697 2770\n", "0 1 2 3 8 9 12 12 13 13\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int p[200005]; long long ret[200005]; long long inv[200005], sum[200005]; void update(long long *pen, int ind, int val) { while (ind < 200005) { pen[ind] += val; ind += ind & (-ind); } } long long query(long long *pen, int ind) { long long ret = 0; while (ind > 0) { ret += pen[ind]; ind = ind & (ind - 1); } return ret; } int getIndex(int target, int n) { int l = 0, r = n - 1, mid; while (l < r) { mid = (l + r + 1) / 2; if (query(inv, mid) < target) { l = mid; } else { r = mid - 1; } } return l + 1; } long long getSum(int n) { long long ret = (long long)n * (n + 1); return ret / 2; } void solve() { int n; scanf("%d ", &n); pair<int, int> in[200005]; int pos = 0; for (int i = 1; i <= n; ++i) { scanf("%d ", &p[i]); in[pos++] = make_pair(p[i], i); } sort(in, in + n); int maxi = 0; long long invSum = 0; for (int i = 0; i < n; ++i) { invSum = invSum + i - query(inv, in[i].second); update(inv, in[i].second, 1); update(sum, in[i].second, in[i].second); int k = i + 1; maxi = max(maxi, in[i].second); int mInd = getIndex(k / 2, n); long long R = 1e18; if (k == 1) { continue; } for (int j = 0; j < 2; ++j) { int mid = getIndex(k / 2 + j, n); long long prefix = getSum(mid) - getSum(mid - k / 2 - j); long long suffix = getSum(mid + k - (k / 2 + j)) - getSum(mid); long long tmp = prefix - query(sum, mid) * 2 - suffix + query(sum, n); R = min(R, tmp); } ret[i + 1] = invSum + R; } for (int i = 1; i <= n; ++i) { printf("%lld ", ret[i]); } putchar('\n'); } int main() { solve(); return 0; }
1269_E. K Integers
You are given a permutation p_1, p_2, …, p_n. In one move you can swap two adjacent values. You want to perform a minimum number of moves, such that in the end there will exist a subsegment 1,2,…, k, in other words in the end there should be an integer i, 1 ≀ i ≀ n-k+1 such that p_i = 1, p_{i+1} = 2, …, p_{i+k-1}=k. Let f(k) be the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation. You need to find f(1), f(2), …, f(n). Input The first line of input contains one integer n (1 ≀ n ≀ 200 000): the number of elements in the permutation. The next line of input contains n integers p_1, p_2, …, p_n: given permutation (1 ≀ p_i ≀ n). Output Print n integers, the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation, for k=1, 2, …, n. Examples Input 5 5 4 3 2 1 Output 0 1 3 6 10 Input 3 1 2 3 Output 0 0 0
{ "input": [ "3\n1 2 3\n", "5\n5 4 3 2 1\n" ], "output": [ "0 0 0\n", "0 1 3 6 10\n" ] }
{ "input": [ "1\n1\n", "100\n98 52 63 2 18 96 31 58 84 40 41 45 66 100 46 71 26 48 81 20 73 91 68 76 13 93 17 29 64 95 79 21 55 75 19 85 54 51 89 78 15 87 43 59 36 1 90 35 65 56 62 28 86 5 82 49 3 99 33 9 92 32 74 69 27 22 77 16 44 94 34 6 57 70 23 12 61 25 8 11 67 47 83 88 10 14 30 7 97 60 42 37 24 38 53 50 4 80 72 39\n", "10\n5 1 6 2 8 3 4 10 9 7\n" ], "output": [ "0\n", "0 42 52 101 101 117 146 166 166 188 194 197 249 258 294 298 345 415 445 492 522 529 540 562 569 628 628 644 684 699 765 766 768 774 791 812 828 844 863 931 996 1011 1036 1040 1105 1166 1175 1232 1237 1251 1282 1364 1377 1409 1445 1455 1461 1534 1553 1565 1572 1581 1664 1706 1715 1779 1787 1837 1841 1847 1909 1919 1973 1976 2010 2060 2063 2087 2125 2133 2192 2193 2196 2276 2305 2305 2324 2327 2352 2361 2417 2418 2467 2468 2510 2598 2599 2697 2697 2770\n", "0 1 2 3 8 9 12 12 13 13\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int NN = (int)2e5 + 5; int A[NN]; int pos[NN]; int n; long long tree_cnt[NN]; long long tree_sum[NN]; long long read(long long* tree, int idx) { idx++; long long res = 0; while (idx > 0) { res += tree[idx]; idx -= (idx & (-idx)); } return res; } void update(long long* tree, int idx, long long val) { idx++; while (idx < NN) { tree[idx] += val; idx += (idx & (-idx)); } } int find_median(int len) { int lo = 0, hi = n; int half = (len + 1) / 2; while (lo < hi) { int mid = (lo + hi) >> 1; int r = read(tree_cnt, mid); if (r < half) lo = mid + 1; else hi = mid; } return lo; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cin >> n; for (int i = 0; i < n; i++) { cin >> A[i]; A[i]--; pos[A[i]] = i; } memset(tree_cnt, 0, sizeof(tree_cnt)); memset(tree_sum, 0, sizeof(tree_sum)); long long tot = 0; long long rev = 0; for (int i = 0; i < n; i++) { rev += i - read(tree_cnt, pos[i]); update(tree_cnt, pos[i], +1); update(tree_sum, pos[i], pos[i]); int len = i + 1; int med = find_median(len); tot += pos[i]; long long sum_down = read(tree_sum, med); long long sum_up = tot - sum_down; long long down_cnt = (len + 1) / 2; long long up_cnt = len - down_cnt; long long res = 0; res += sum_up - med * up_cnt - (up_cnt * (up_cnt + 1)) / 2; res += med * down_cnt - sum_down - (down_cnt * (down_cnt - 1)) / 2; res += rev; if (i > 0) printf(" "); printf("%lld", res); } printf("\n"); return 0; }
1269_E. K Integers
You are given a permutation p_1, p_2, …, p_n. In one move you can swap two adjacent values. You want to perform a minimum number of moves, such that in the end there will exist a subsegment 1,2,…, k, in other words in the end there should be an integer i, 1 ≀ i ≀ n-k+1 such that p_i = 1, p_{i+1} = 2, …, p_{i+k-1}=k. Let f(k) be the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation. You need to find f(1), f(2), …, f(n). Input The first line of input contains one integer n (1 ≀ n ≀ 200 000): the number of elements in the permutation. The next line of input contains n integers p_1, p_2, …, p_n: given permutation (1 ≀ p_i ≀ n). Output Print n integers, the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation, for k=1, 2, …, n. Examples Input 5 5 4 3 2 1 Output 0 1 3 6 10 Input 3 1 2 3 Output 0 0 0
{ "input": [ "3\n1 2 3\n", "5\n5 4 3 2 1\n" ], "output": [ "0 0 0\n", "0 1 3 6 10\n" ] }
{ "input": [ "1\n1\n", "100\n98 52 63 2 18 96 31 58 84 40 41 45 66 100 46 71 26 48 81 20 73 91 68 76 13 93 17 29 64 95 79 21 55 75 19 85 54 51 89 78 15 87 43 59 36 1 90 35 65 56 62 28 86 5 82 49 3 99 33 9 92 32 74 69 27 22 77 16 44 94 34 6 57 70 23 12 61 25 8 11 67 47 83 88 10 14 30 7 97 60 42 37 24 38 53 50 4 80 72 39\n", "10\n5 1 6 2 8 3 4 10 9 7\n" ], "output": [ "0\n", "0 42 52 101 101 117 146 166 166 188 194 197 249 258 294 298 345 415 445 492 522 529 540 562 569 628 628 644 684 699 765 766 768 774 791 812 828 844 863 931 996 1011 1036 1040 1105 1166 1175 1232 1237 1251 1282 1364 1377 1409 1445 1455 1461 1534 1553 1565 1572 1581 1664 1706 1715 1779 1787 1837 1841 1847 1909 1919 1973 1976 2010 2060 2063 2087 2125 2133 2192 2193 2196 2276 2305 2305 2324 2327 2352 2361 2417 2418 2467 2468 2510 2598 2599 2697 2697 2770\n", "0 1 2 3 8 9 12 12 13 13\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int mod = 1000000007; int n, a[200010], p[200010], c[200010]; long long inv[200010]; set<int> st; set<int>::iterator it; int Lowbit(int x) { return x & (-x); } void Update(int x, int d) { while (x <= n) { c[x] += d; x += Lowbit(x); } } int Getsum(int x) { int res = 0; while (x) { res += c[x]; x -= Lowbit(x); } return res; } int main() { ios_base::sync_with_stdio(false); cin >> n; for (int i = 1; i <= n; ++i) { cin >> a[i]; p[a[i]] = i; } cout << "0 "; int center = p[1], lnum = 0, rnum = 0; long long sum = 0; st.insert(p[1]); Update(p[1], 1); for (int i = 2; i <= n; ++i) { inv[i] = Getsum(n) - Getsum(p[i]); Update(p[i], 1); st.insert(p[i]); long long nxt_sum = sum + inv[i] + abs(p[i] - center) - abs(Getsum(center) - Getsum(p[i])); if (center < p[i]) nxt_sum -= inv[i]; else nxt_sum -= Getsum(p[i] - 1); if (p[i] > center) rnum++; else lnum++; if (lnum == rnum + 1) { it = st.find(center); it--; center = (*it); lnum--; rnum++; } if (rnum == lnum + 2) { it = st.find(center); it++; nxt_sum -= ((*it) - center - 1); center = (*it); lnum++; rnum--; } sum = nxt_sum; cout << sum << " "; } cout << endl; return 0; }
1269_E. K Integers
You are given a permutation p_1, p_2, …, p_n. In one move you can swap two adjacent values. You want to perform a minimum number of moves, such that in the end there will exist a subsegment 1,2,…, k, in other words in the end there should be an integer i, 1 ≀ i ≀ n-k+1 such that p_i = 1, p_{i+1} = 2, …, p_{i+k-1}=k. Let f(k) be the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation. You need to find f(1), f(2), …, f(n). Input The first line of input contains one integer n (1 ≀ n ≀ 200 000): the number of elements in the permutation. The next line of input contains n integers p_1, p_2, …, p_n: given permutation (1 ≀ p_i ≀ n). Output Print n integers, the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation, for k=1, 2, …, n. Examples Input 5 5 4 3 2 1 Output 0 1 3 6 10 Input 3 1 2 3 Output 0 0 0
{ "input": [ "3\n1 2 3\n", "5\n5 4 3 2 1\n" ], "output": [ "0 0 0\n", "0 1 3 6 10\n" ] }
{ "input": [ "1\n1\n", "100\n98 52 63 2 18 96 31 58 84 40 41 45 66 100 46 71 26 48 81 20 73 91 68 76 13 93 17 29 64 95 79 21 55 75 19 85 54 51 89 78 15 87 43 59 36 1 90 35 65 56 62 28 86 5 82 49 3 99 33 9 92 32 74 69 27 22 77 16 44 94 34 6 57 70 23 12 61 25 8 11 67 47 83 88 10 14 30 7 97 60 42 37 24 38 53 50 4 80 72 39\n", "10\n5 1 6 2 8 3 4 10 9 7\n" ], "output": [ "0\n", "0 42 52 101 101 117 146 166 166 188 194 197 249 258 294 298 345 415 445 492 522 529 540 562 569 628 628 644 684 699 765 766 768 774 791 812 828 844 863 931 996 1011 1036 1040 1105 1166 1175 1232 1237 1251 1282 1364 1377 1409 1445 1455 1461 1534 1553 1565 1572 1581 1664 1706 1715 1779 1787 1837 1841 1847 1909 1919 1973 1976 2010 2060 2063 2087 2125 2133 2192 2193 2196 2276 2305 2305 2324 2327 2352 2361 2417 2418 2467 2468 2510 2598 2599 2697 2697 2770\n", "0 1 2 3 8 9 12 12 13 13\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> const int maxn = 200000 + 10; class BIT { private: int n; int bit[maxn]; public: BIT(int size) { n = size; std::fill(bit, bit + size + 1, 0); } void add(int x, int v) { while (x <= this->n) { bit[x] += v; x += ((x) & (-x)); } } int query(int x) { int ans = 0; while (x) { ans += bit[x]; x -= ((x) & (-x)); } return ans; } int find_k(int k) { int bits = 0; int n = this->n; while (n) { n >>= 1; bits++; } int mask = 0; int cnt = 0; for (int i = bits - 1; i >= 0; i--) { mask += 1 << i; if (mask > this->n || cnt + bit[mask] >= k) mask -= (1 << i); else cnt += bit[mask]; } return mask + 1; } }; int n, pos[maxn]; struct TreeNode { int zeros, preadd, sufadd; long long presum, sufsum; }; TreeNode seg[maxn << 2]; void build_tree(int node, int L, int R) { seg[node].zeros = R - L + 1; if (L == R) return; int M = L + (R - L) / 2; build_tree(node << 1, L, M); build_tree(node << 1 | 1, M + 1, R); } void push_down(int node, int L, int R) { int lch = node << 1; int rch = node << 1 | 1; int M = L + (R - L) / 2; if (seg[node].preadd) { seg[lch].preadd += seg[node].preadd; seg[rch].preadd += seg[node].preadd; seg[lch].presum += 1LL * seg[lch].zeros * seg[node].preadd; seg[rch].presum += 1LL * seg[rch].zeros * seg[node].preadd; seg[node].preadd = 0; } if (seg[node].sufadd) { seg[lch].sufadd += seg[node].sufadd; seg[rch].sufadd += seg[node].sufadd; seg[lch].sufsum += 1LL * seg[lch].zeros * seg[node].sufadd; seg[rch].sufsum += 1LL * seg[rch].zeros * seg[node].sufadd; seg[node].sufadd = 0; } } int qL, qR; void add(int node, int L, int R, int type) { if (qL <= L && R <= qR) { if (type == 0) { seg[node].preadd += 1; seg[node].presum += seg[node].zeros; } else { seg[node].sufadd += 1; seg[node].sufsum += seg[node].zeros; } return; } push_down(node, L, R); int M = L + (R - L) / 2; if (qL <= M) add(node << 1, L, M, type); if (qR > M) add(node << 1 | 1, M + 1, R, type); int lch = node << 1; int rch = node << 1 | 1; seg[node].zeros = seg[lch].zeros + seg[rch].zeros; seg[node].presum = seg[lch].presum + seg[rch].presum; seg[node].sufsum = seg[lch].sufsum + seg[rch].sufsum; } void erase(int node, int L, int R, int p) { if (L == R) { memset(seg + node, 0, sizeof(TreeNode)); return; } int M = L + (R - L) / 2; int lch = node << 1; int rch = node << 1 | 1; push_down(node, L, R); if (p <= M) erase(lch, L, M, p); else erase(rch, M + 1, R, p); seg[node].zeros = seg[lch].zeros + seg[rch].zeros; seg[node].presum = seg[lch].presum + seg[rch].presum; seg[node].sufsum = seg[lch].sufsum + seg[rch].sufsum; } long long query(int node, int L, int R, int type) { if (qL <= L && R <= qR) { if (type == 0) return seg[node].presum; else return seg[node].sufsum; } push_down(node, L, R); long long ans = 0; int M = L + (R - L) / 2; if (qL <= M) ans += query(node << 1, L, M, type); if (qR > M) ans += query(node << 1 | 1, M + 1, R, type); return ans; } int main() { scanf("%d", &n); BIT bit(n); for (int i = 1; i <= n; i++) { int a; scanf("%d", &a); pos[a] = i; } build_tree(1, 1, n); long long inversion = 0; for (int i = 1; i <= n; i++) { const int &p = pos[i]; inversion += i - 1 - bit.query(p); bit.add(p, 1); qL = p + 1; qR = n; if (qL <= qR) add(1, 1, n, 0); qL = 1; qR = p - 1; if (qL <= qR) add(1, 1, n, 1); erase(1, 1, n, p); long long ans = 0; int median = bit.find_k((i + 1) / 2); qL = 1; qR = median - 1; if (qL <= qR) { ans += query(1, 1, n, 0); } qL = median + 1; qR = n; if (qL <= qR) { ans += query(1, 1, n, 1); } printf("%lld ", inversion + ans); } printf("\n"); return 0; }
1269_E. K Integers
You are given a permutation p_1, p_2, …, p_n. In one move you can swap two adjacent values. You want to perform a minimum number of moves, such that in the end there will exist a subsegment 1,2,…, k, in other words in the end there should be an integer i, 1 ≀ i ≀ n-k+1 such that p_i = 1, p_{i+1} = 2, …, p_{i+k-1}=k. Let f(k) be the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation. You need to find f(1), f(2), …, f(n). Input The first line of input contains one integer n (1 ≀ n ≀ 200 000): the number of elements in the permutation. The next line of input contains n integers p_1, p_2, …, p_n: given permutation (1 ≀ p_i ≀ n). Output Print n integers, the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation, for k=1, 2, …, n. Examples Input 5 5 4 3 2 1 Output 0 1 3 6 10 Input 3 1 2 3 Output 0 0 0
{ "input": [ "3\n1 2 3\n", "5\n5 4 3 2 1\n" ], "output": [ "0 0 0\n", "0 1 3 6 10\n" ] }
{ "input": [ "1\n1\n", "100\n98 52 63 2 18 96 31 58 84 40 41 45 66 100 46 71 26 48 81 20 73 91 68 76 13 93 17 29 64 95 79 21 55 75 19 85 54 51 89 78 15 87 43 59 36 1 90 35 65 56 62 28 86 5 82 49 3 99 33 9 92 32 74 69 27 22 77 16 44 94 34 6 57 70 23 12 61 25 8 11 67 47 83 88 10 14 30 7 97 60 42 37 24 38 53 50 4 80 72 39\n", "10\n5 1 6 2 8 3 4 10 9 7\n" ], "output": [ "0\n", "0 42 52 101 101 117 146 166 166 188 194 197 249 258 294 298 345 415 445 492 522 529 540 562 569 628 628 644 684 699 765 766 768 774 791 812 828 844 863 931 996 1011 1036 1040 1105 1166 1175 1232 1237 1251 1282 1364 1377 1409 1445 1455 1461 1534 1553 1565 1572 1581 1664 1706 1715 1779 1787 1837 1841 1847 1909 1919 1973 1976 2010 2060 2063 2087 2125 2133 2192 2193 2196 2276 2305 2305 2324 2327 2352 2361 2417 2418 2467 2468 2510 2598 2599 2697 2697 2770\n", "0 1 2 3 8 9 12 12 13 13\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f; const long long LINF = 0x3f3f3f3f3f3f3f3fLL; const double PI = acos((long double)-1.0); const double EPS = 1e-10; const int MOD = 1e9 + 7; template <typename T> void cmin(T &x, T y) { if (y < x) x = y; } template <typename T> void cmax(T &x, T y) { if (y > x) x = y; } long long qpow(long long x, long long n, long long mod = MOD) { if (n < 0) return 0; long long res = 1; while (n) { if (n & 1) res = res * x % mod; x = x * x % mod; n >>= 1; } return res; } long long sq(long long x) { return x * x; } long long read() { long long res = 0; char c = getchar(); while (!isdigit(c)) c = getchar(); while (isdigit(c)) { res = res * 10 + (c - '0'); c = getchar(); } return res; } const int MAXN = 200000 + 10; int n; int a[MAXN]; int pos[MAXN]; struct SegmentTree { int cnt[MAXN << 2]; long long sum[MAXN << 2]; int add[MAXN << 2]; void PushUp(int o) { cnt[o] = cnt[(o << 1)] + cnt[(o << 1 | 1)]; sum[o] = sum[(o << 1)] + sum[(o << 1 | 1)]; } void PushDown(int o, int l, int r) { if (add[o]) { int m = (l + r) >> 1; sum[(o << 1)] += 1LL * cnt[(o << 1)] * add[o]; sum[(o << 1 | 1)] += 1LL * cnt[(o << 1 | 1)] * add[o]; add[(o << 1)] += add[o]; add[(o << 1 | 1)] += add[o]; add[o] = 0; } } void Build(int o, int l, int r) { add[o] = 0; if (l == r) { cnt[o] = 1; sum[o] = 0; return; } int m = (l + r) >> 1; Build((o << 1), l, m); Build((o << 1 | 1), m + 1, r); PushUp(o); } void Add(int o, int l, int r, int ql, int qr, int v) { if (ql <= l && r <= qr) { sum[o] += 1LL * cnt[o] * v; add[o] += v; return; } PushDown(o, l, r); int m = (l + r) >> 1; if (ql <= m) Add((o << 1), l, m, ql, qr, v); if (qr >= m + 1) Add((o << 1 | 1), m + 1, r, ql, qr, v); PushUp(o); } void SetCnt0(int o, int l, int r, int p) { if (l == r) { cnt[o] = 0; sum[o] = 0; return; } PushDown(o, l, r); int m = (l + r) >> 1; if (p <= m) SetCnt0((o << 1), l, m, p); if (p >= m + 1) SetCnt0((o << 1 | 1), m + 1, r, p); PushUp(o); } int Cnt(int o, int l, int r, int ql, int qr) { if (ql <= l && r <= qr) return cnt[o]; PushDown(o, l, r); int m = (l + r) >> 1; int res = 0; if (ql <= m) res = res + Cnt((o << 1), l, m, ql, qr); if (qr >= m + 1) res = res + Cnt((o << 1 | 1), m + 1, r, ql, qr); return res; } long long Sum(int o, int l, int r, int ql, int qr) { if (ql <= l && r <= qr) return sum[o]; PushDown(o, l, r); int m = (l + r) >> 1; long long res = 0; if (ql <= m) res = res + Sum((o << 1), l, m, ql, qr); if (qr >= m + 1) res = res + Sum((o << 1 | 1), m + 1, r, ql, qr); return res; } int Pos(int o, int l, int r, int k) { if (l == r) return l; PushDown(o, l, r); int m = (l + r) >> 1; int cnt0ls = (m - l + 1) - cnt[(o << 1)]; if (cnt0ls >= k) return Pos((o << 1), l, m, k); return Pos((o << 1 | 1), m + 1, r, k - cnt0ls); } int Lmid() { int cnt0 = n - cnt[1]; return Pos(1, 1, n, (cnt0 + 1) / 2); } int Rmid() { int cnt0 = n - cnt[1]; return Pos(1, 1, n, cnt0 / 2 + 1); } } st; namespace Solver { void InitOnce() { int t; } void Read() { int res = scanf("%d", &n); if (res == -1) exit(0); for (int i = 1; i <= n; ++i) { scanf("%d", &a[i]); pos[a[i]] = i; } } long long ans[200005]; void Solve() { st.Build(1, 1, n); long long cur = 0; int Lmost = n, Rmost = 1; for (int i = 1; i <= n; ++i) { int p = pos[i]; cmin(Lmost, p); cmax(Rmost, p); cur += (n - p + 1) - st.Cnt(1, 1, n, p, n); if (i >= 2) { int Lmid = st.Lmid(); int Rmid = st.Rmid(); if (p < Lmid) st.Add(1, 1, n, p, Lmid, 1); if (p > Rmid) st.Add(1, 1, n, Rmid, p, 1); } st.SetCnt0(1, 1, n, p); ans[i] = cur + st.Sum(1, 1, n, Lmost, Rmost); printf("%lld%c", ans[i], " \n"[i == n]); } } } // namespace Solver int main() { Solver::InitOnce(); while (true) { Solver::Read(); Solver::Solve(); } return 0; }
1269_E. K Integers
You are given a permutation p_1, p_2, …, p_n. In one move you can swap two adjacent values. You want to perform a minimum number of moves, such that in the end there will exist a subsegment 1,2,…, k, in other words in the end there should be an integer i, 1 ≀ i ≀ n-k+1 such that p_i = 1, p_{i+1} = 2, …, p_{i+k-1}=k. Let f(k) be the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation. You need to find f(1), f(2), …, f(n). Input The first line of input contains one integer n (1 ≀ n ≀ 200 000): the number of elements in the permutation. The next line of input contains n integers p_1, p_2, …, p_n: given permutation (1 ≀ p_i ≀ n). Output Print n integers, the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation, for k=1, 2, …, n. Examples Input 5 5 4 3 2 1 Output 0 1 3 6 10 Input 3 1 2 3 Output 0 0 0
{ "input": [ "3\n1 2 3\n", "5\n5 4 3 2 1\n" ], "output": [ "0 0 0\n", "0 1 3 6 10\n" ] }
{ "input": [ "1\n1\n", "100\n98 52 63 2 18 96 31 58 84 40 41 45 66 100 46 71 26 48 81 20 73 91 68 76 13 93 17 29 64 95 79 21 55 75 19 85 54 51 89 78 15 87 43 59 36 1 90 35 65 56 62 28 86 5 82 49 3 99 33 9 92 32 74 69 27 22 77 16 44 94 34 6 57 70 23 12 61 25 8 11 67 47 83 88 10 14 30 7 97 60 42 37 24 38 53 50 4 80 72 39\n", "10\n5 1 6 2 8 3 4 10 9 7\n" ], "output": [ "0\n", "0 42 52 101 101 117 146 166 166 188 194 197 249 258 294 298 345 415 445 492 522 529 540 562 569 628 628 644 684 699 765 766 768 774 791 812 828 844 863 931 996 1011 1036 1040 1105 1166 1175 1232 1237 1251 1282 1364 1377 1409 1445 1455 1461 1534 1553 1565 1572 1581 1664 1706 1715 1779 1787 1837 1841 1847 1909 1919 1973 1976 2010 2060 2063 2087 2125 2133 2192 2193 2196 2276 2305 2305 2324 2327 2352 2361 2417 2418 2467 2468 2510 2598 2599 2697 2697 2770\n", "0 1 2 3 8 9 12 12 13 13\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int T, n, m, p, k, a[200010], pos[200010], bit[200010], hd; long long ans = 0; int query(int a) { int tmp = 0; for (int i = a; i >= 1; i -= (i & -i)) tmp += bit[i]; return tmp; } void add(int a, int x) { for (int i = a; i <= n; i += (i & -i)) bit[i] += x; } set<int> S; int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d", &a[i]), pos[a[i]] = i; S.insert(INT_MIN); S.insert(INT_MAX); hd = pos[1]; for (int i = 1; i <= n; i++) { ans += query(n) - query(pos[i]); add(pos[i], 1); if (pos[i] < hd) ans += hd - pos[i] - 1 - query(hd - 1) + query(pos[i]), ans -= query(pos[i] - 1); if (pos[i] > hd) ans += pos[i] - hd - 1 - query(pos[i] - 1) + query(hd), ans -= query(n) - query(pos[i]); S.insert(pos[i]); set<int>::iterator now = S.find(hd), l = now, r; l--; if ((*l) > 0) { if (query(n) - query(*l) <= i / 2 && query((*l) - 1) <= i / 2) ans += (hd - (*l) - 1) * (query(n) - 2 * query(hd - 1)), hd = *l; } now = S.find(hd), r = now; r++; if ((*r) <= n) { if (query(n) - query(*r) <= i / 2 && query((*r) - 1) <= i / 2) ans += ((*r) - hd - 1) * (2 * query(hd) - query(n)), hd = *r; } printf("%lld ", ans); } }
1269_E. K Integers
You are given a permutation p_1, p_2, …, p_n. In one move you can swap two adjacent values. You want to perform a minimum number of moves, such that in the end there will exist a subsegment 1,2,…, k, in other words in the end there should be an integer i, 1 ≀ i ≀ n-k+1 such that p_i = 1, p_{i+1} = 2, …, p_{i+k-1}=k. Let f(k) be the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation. You need to find f(1), f(2), …, f(n). Input The first line of input contains one integer n (1 ≀ n ≀ 200 000): the number of elements in the permutation. The next line of input contains n integers p_1, p_2, …, p_n: given permutation (1 ≀ p_i ≀ n). Output Print n integers, the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation, for k=1, 2, …, n. Examples Input 5 5 4 3 2 1 Output 0 1 3 6 10 Input 3 1 2 3 Output 0 0 0
{ "input": [ "3\n1 2 3\n", "5\n5 4 3 2 1\n" ], "output": [ "0 0 0\n", "0 1 3 6 10\n" ] }
{ "input": [ "1\n1\n", "100\n98 52 63 2 18 96 31 58 84 40 41 45 66 100 46 71 26 48 81 20 73 91 68 76 13 93 17 29 64 95 79 21 55 75 19 85 54 51 89 78 15 87 43 59 36 1 90 35 65 56 62 28 86 5 82 49 3 99 33 9 92 32 74 69 27 22 77 16 44 94 34 6 57 70 23 12 61 25 8 11 67 47 83 88 10 14 30 7 97 60 42 37 24 38 53 50 4 80 72 39\n", "10\n5 1 6 2 8 3 4 10 9 7\n" ], "output": [ "0\n", "0 42 52 101 101 117 146 166 166 188 194 197 249 258 294 298 345 415 445 492 522 529 540 562 569 628 628 644 684 699 765 766 768 774 791 812 828 844 863 931 996 1011 1036 1040 1105 1166 1175 1232 1237 1251 1282 1364 1377 1409 1445 1455 1461 1534 1553 1565 1572 1581 1664 1706 1715 1779 1787 1837 1841 1847 1909 1919 1973 1976 2010 2060 2063 2087 2125 2133 2192 2193 2196 2276 2305 2305 2324 2327 2352 2361 2417 2418 2467 2468 2510 2598 2599 2697 2697 2770\n", "0 1 2 3 8 9 12 12 13 13\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int maxd = 4e5 + 10; long long t1[maxd], t2[maxd], pos[maxd], a[maxd], n; void add(long long* t, long long x, long long v) { for (; x < maxd; x += (x & -x)) t[x] += v; } long long query(long long* t, long long x) { long long ans = 0; for (; x; x -= (x & -x)) ans += t[x]; return ans; } int solve(long long* t, int v) { int i = 0; for (int j = 19; j >= 0; j--) if ((i | 1 << j) < maxd) if (t[i | (1 << j)] <= v) v -= t[i |= (1 << j)]; return i; } int main() { scanf("%lld", &n); for (int i = 1; i <= n; i++) { scanf("%lld", &a[i]); pos[a[i]] = i; } long long ans = 0ll; for (int i = 1; i <= n; i++) { int p = pos[i]; add(t1, p, 1); ans += i - query(t1, p); add(t2, p, p); int mid = solve(t1, i / 2) + 1; long long sum = 0; long long aa = i / 2; long long bb = i - i / 2 - 1; sum += (long long)aa * mid - aa * (aa + 1) / 2 - query(t2, mid - 1); sum += (query(t2, maxd - 1) - query(t2, mid)) - (long long)bb * mid - 1ll * bb * (bb + 1) / 2; printf("%lld ", sum + ans); } return 0; }
1269_E. K Integers
You are given a permutation p_1, p_2, …, p_n. In one move you can swap two adjacent values. You want to perform a minimum number of moves, such that in the end there will exist a subsegment 1,2,…, k, in other words in the end there should be an integer i, 1 ≀ i ≀ n-k+1 such that p_i = 1, p_{i+1} = 2, …, p_{i+k-1}=k. Let f(k) be the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation. You need to find f(1), f(2), …, f(n). Input The first line of input contains one integer n (1 ≀ n ≀ 200 000): the number of elements in the permutation. The next line of input contains n integers p_1, p_2, …, p_n: given permutation (1 ≀ p_i ≀ n). Output Print n integers, the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation, for k=1, 2, …, n. Examples Input 5 5 4 3 2 1 Output 0 1 3 6 10 Input 3 1 2 3 Output 0 0 0
{ "input": [ "3\n1 2 3\n", "5\n5 4 3 2 1\n" ], "output": [ "0 0 0\n", "0 1 3 6 10\n" ] }
{ "input": [ "1\n1\n", "100\n98 52 63 2 18 96 31 58 84 40 41 45 66 100 46 71 26 48 81 20 73 91 68 76 13 93 17 29 64 95 79 21 55 75 19 85 54 51 89 78 15 87 43 59 36 1 90 35 65 56 62 28 86 5 82 49 3 99 33 9 92 32 74 69 27 22 77 16 44 94 34 6 57 70 23 12 61 25 8 11 67 47 83 88 10 14 30 7 97 60 42 37 24 38 53 50 4 80 72 39\n", "10\n5 1 6 2 8 3 4 10 9 7\n" ], "output": [ "0\n", "0 42 52 101 101 117 146 166 166 188 194 197 249 258 294 298 345 415 445 492 522 529 540 562 569 628 628 644 684 699 765 766 768 774 791 812 828 844 863 931 996 1011 1036 1040 1105 1166 1175 1232 1237 1251 1282 1364 1377 1409 1445 1455 1461 1534 1553 1565 1572 1581 1664 1706 1715 1779 1787 1837 1841 1847 1909 1919 1973 1976 2010 2060 2063 2087 2125 2133 2192 2193 2196 2276 2305 2305 2324 2327 2352 2361 2417 2418 2467 2468 2510 2598 2599 2697 2697 2770\n", "0 1 2 3 8 9 12 12 13 13\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; long long int gcd(long long int a, long long int b) { return (b == 0 ? a : gcd(b, a % b)); } char vow[] = {'a', 'e', 'i', 'o', 'u'}; int dx[] = {1, -1, 0, 0}; int dy[] = {0, 0, 1, -1}; const int N = 4e5 + 30; const long long int mod = -1e11; int n; int tx[N]; set<long long int> s; long long int t1[4 * N], t2[4 * N], t3[4 * N]; long long int flag1[4 * N], flag2[4 * N], flag3[4 * N]; long long int inv; void push(int nd) { t1[nd] += flag1[nd]; flag1[nd * 2] += flag1[nd]; flag1[nd * 2 + 1] += flag1[nd]; flag1[nd] = 0; t2[nd] += flag2[nd]; flag2[nd * 2] += flag2[nd]; flag2[nd * 2 + 1] += flag2[nd]; flag2[nd] = 0; t3[nd] += flag3[nd]; flag3[nd * 2] += flag3[nd]; flag3[nd * 2 + 1] += flag3[nd]; flag3[nd] = 0; } void upd1(int ql, int qr, long long int v, int nl = 1, int nr = n, int nd = 1) { push(nd); if (ql > nr || qr < nl) return; if (ql == nl && qr == nr) { flag1[nd] += v; push(nd); return; } int mid = (nl + nr) / 2; upd1(ql, min(mid, qr), v, nl, mid, nd * 2); upd1(max(mid + 1, ql), qr, v, mid + 1, nr, nd * 2 + 1); } long long int qry1(int q, int nl = 1, int nr = n, int nd = 1) { push(nd); if (q > nr || q < nl) return 0; if (nl == nr) { return t1[nd]; } int mid = (nl + nr) / 2; return qry1(q, nl, mid, nd * 2) + qry1(q, mid + 1, nr, nd * 2 + 1); } void upd2(int ql, int qr, long long int v, int nl = 1, int nr = n, int nd = 1) { push(nd); if (ql > nr || qr < nl) return; if (ql == nl && qr == nr) { flag2[nd] += v; push(nd); return; } int mid = (nl + nr) / 2; upd2(ql, min(mid, qr), v, nl, mid, nd * 2); upd2(max(mid + 1, ql), qr, v, mid + 1, nr, nd * 2 + 1); } long long int qry2(int q, int nl = 1, int nr = n, int nd = 1) { push(nd); if (q > nr || q < nl) return 0; if (nl == nr) { return t2[nd]; } int mid = (nl + nr) / 2; return qry2(q, nl, mid, nd * 2) + qry2(q, mid + 1, nr, nd * 2 + 1); } void upd3(int ql, int qr, long long int v, int nl = 1, int nr = n, int nd = 1) { push(nd); if (ql > nr || qr < nl) return; if (ql == nl && qr == nr) { flag3[nd] += v; push(nd); return; } int mid = (nl + nr) / 2; upd3(ql, min(mid, qr), v, nl, mid, nd * 2); upd3(max(mid + 1, ql), qr, v, mid + 1, nr, nd * 2 + 1); } long long int qry3(int q, int nl = 1, int nr = n, int nd = 1) { push(nd); if (q > nr || q < nl) return 0; if (nl == nr) { return t3[nd]; } int mid = (nl + nr) / 2; return qry3(q, nl, mid, nd * 2) + qry3(q, mid + 1, nr, nd * 2 + 1); } int main() { ios_base::sync_with_stdio(false); cin.tie(0); ; cin >> n; for (int i = 0; i < n; i++) { int a; cin >> a; tx[a] = i + 1; } for (int i = 1; i <= n; i++) { upd2(tx[i], n, 1); long long int l = 1, r = n, ind = 0; while (l <= r) { int mid = (l + r) / 2; if (qry2(mid) >= (i + 1) / 2) { ind = mid; r = mid - 1; } else l = mid + 1; } long long int s = (i + 1) / 2; long long int sum = s * ind - ((s - 1LL) * s) / 2LL; if (tx[i] == 1) upd1(tx[i], n, -tx[i]); else { upd1(1, tx[i] - 1, tx[i]); upd1(tx[i], n, -tx[i]); } inv += qry3(tx[i]); upd3(1, tx[i], 1); long long int ans = inv + sum + qry1(ind); ind += i - (i + 1LL) / 2LL; sum = i * ind - ((i - 1LL) * 1LL * i) / 2LL - sum; ans -= sum; cout << ans << " "; } return 0; }
1269_E. K Integers
You are given a permutation p_1, p_2, …, p_n. In one move you can swap two adjacent values. You want to perform a minimum number of moves, such that in the end there will exist a subsegment 1,2,…, k, in other words in the end there should be an integer i, 1 ≀ i ≀ n-k+1 such that p_i = 1, p_{i+1} = 2, …, p_{i+k-1}=k. Let f(k) be the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation. You need to find f(1), f(2), …, f(n). Input The first line of input contains one integer n (1 ≀ n ≀ 200 000): the number of elements in the permutation. The next line of input contains n integers p_1, p_2, …, p_n: given permutation (1 ≀ p_i ≀ n). Output Print n integers, the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation, for k=1, 2, …, n. Examples Input 5 5 4 3 2 1 Output 0 1 3 6 10 Input 3 1 2 3 Output 0 0 0
{ "input": [ "3\n1 2 3\n", "5\n5 4 3 2 1\n" ], "output": [ "0 0 0\n", "0 1 3 6 10\n" ] }
{ "input": [ "1\n1\n", "100\n98 52 63 2 18 96 31 58 84 40 41 45 66 100 46 71 26 48 81 20 73 91 68 76 13 93 17 29 64 95 79 21 55 75 19 85 54 51 89 78 15 87 43 59 36 1 90 35 65 56 62 28 86 5 82 49 3 99 33 9 92 32 74 69 27 22 77 16 44 94 34 6 57 70 23 12 61 25 8 11 67 47 83 88 10 14 30 7 97 60 42 37 24 38 53 50 4 80 72 39\n", "10\n5 1 6 2 8 3 4 10 9 7\n" ], "output": [ "0\n", "0 42 52 101 101 117 146 166 166 188 194 197 249 258 294 298 345 415 445 492 522 529 540 562 569 628 628 644 684 699 765 766 768 774 791 812 828 844 863 931 996 1011 1036 1040 1105 1166 1175 1232 1237 1251 1282 1364 1377 1409 1445 1455 1461 1534 1553 1565 1572 1581 1664 1706 1715 1779 1787 1837 1841 1847 1909 1919 1973 1976 2010 2060 2063 2087 2125 2133 2192 2193 2196 2276 2305 2305 2324 2327 2352 2361 2417 2418 2467 2468 2510 2598 2599 2697 2697 2770\n", "0 1 2 3 8 9 12 12 13 13\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7; const int maxn = 2e5 + 233; int ch[maxn]; int a[maxn], n, p[maxn]; long long ans[maxn], sum[maxn]; void add(int x, int v) { for (int i = x; i <= n; i += i & (-i)) ch[i] += v; } int query(int x) { int sum = 0; for (int i = x; i; i -= i & (-i)) sum += ch[i]; return sum; } void add_sum(int x, int v) { for (int i = x; i <= n; i += i & (-i)) sum[i] += v; } long long query_sum(int x) { long long res = 0; for (int i = x; i; i -= i & (-i)) res += sum[i]; return res; } int cal(int n, int x) { int t = query(x); return min(t, n - t); } int find(int k) { k--; int x = 0; for (int i = 20; i >= 0; i--) if (x + (1 << i) <= n && ch[x + (1 << i)] <= k) { x += 1 << i; k -= ch[x]; } return x + 1; } inline long long ari(int n) { return 1ll * n * (n + 1) / 2; } int main() { cin >> n; for (int i = 1; i <= n; i++) scanf("%d", &a[i]), p[a[i]] = i; for (int i = 1; i <= n; i++) { int x = p[i]; ans[i] = ans[i - 1] + (i - 1 - query(x)); add(x, 1); } memset(ch, 0, sizeof(ch)); long long sum = 0; for (int i = 1; i <= n; i++) { int x = p[i]; add(x, 1); add_sum(x, x); sum += x; int k = (i + 1) / 2; long long c = find(k); long long tmp = query_sum(c); long long a = k * c - tmp - ari(k - 1); long long b = (sum - tmp) - (i - k) * c - ari(i - k); ans[i] += a + b; } for (int i = 1; i <= n; i++) printf("%I64d ", ans[i]); return 0; }
1269_E. K Integers
You are given a permutation p_1, p_2, …, p_n. In one move you can swap two adjacent values. You want to perform a minimum number of moves, such that in the end there will exist a subsegment 1,2,…, k, in other words in the end there should be an integer i, 1 ≀ i ≀ n-k+1 such that p_i = 1, p_{i+1} = 2, …, p_{i+k-1}=k. Let f(k) be the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation. You need to find f(1), f(2), …, f(n). Input The first line of input contains one integer n (1 ≀ n ≀ 200 000): the number of elements in the permutation. The next line of input contains n integers p_1, p_2, …, p_n: given permutation (1 ≀ p_i ≀ n). Output Print n integers, the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation, for k=1, 2, …, n. Examples Input 5 5 4 3 2 1 Output 0 1 3 6 10 Input 3 1 2 3 Output 0 0 0
{ "input": [ "3\n1 2 3\n", "5\n5 4 3 2 1\n" ], "output": [ "0 0 0\n", "0 1 3 6 10\n" ] }
{ "input": [ "1\n1\n", "100\n98 52 63 2 18 96 31 58 84 40 41 45 66 100 46 71 26 48 81 20 73 91 68 76 13 93 17 29 64 95 79 21 55 75 19 85 54 51 89 78 15 87 43 59 36 1 90 35 65 56 62 28 86 5 82 49 3 99 33 9 92 32 74 69 27 22 77 16 44 94 34 6 57 70 23 12 61 25 8 11 67 47 83 88 10 14 30 7 97 60 42 37 24 38 53 50 4 80 72 39\n", "10\n5 1 6 2 8 3 4 10 9 7\n" ], "output": [ "0\n", "0 42 52 101 101 117 146 166 166 188 194 197 249 258 294 298 345 415 445 492 522 529 540 562 569 628 628 644 684 699 765 766 768 774 791 812 828 844 863 931 996 1011 1036 1040 1105 1166 1175 1232 1237 1251 1282 1364 1377 1409 1445 1455 1461 1534 1553 1565 1572 1581 1664 1706 1715 1779 1787 1837 1841 1847 1909 1919 1973 1976 2010 2060 2063 2087 2125 2133 2192 2193 2196 2276 2305 2305 2324 2327 2352 2361 2417 2418 2467 2468 2510 2598 2599 2697 2697 2770\n", "0 1 2 3 8 9 12 12 13 13\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const long long maxn = 200000; long long a[200005], pos[200005]; long long sum1[200005], sum2[200005]; void add(long long *sum1, long long x, long long val) { while (x <= maxn) { sum1[x] += val; x += x & (-x); } } long long sum(long long *sum1, long long pos) { long long res = 0; while (pos) { res += sum1[pos]; pos -= pos & (-pos); } return res; } signed main() { long long n; cin >> n; for (long long i = 1; i <= n; i++) { cin >> a[i]; pos[a[i]] = i; } long long ans1 = 0, ans2 = 0; for (long long i = 1; i <= n; i++) { ans1 += i - 1 - sum(sum1, pos[i]); add(sum1, pos[i], 1); add(sum2, pos[i], pos[i]); long long l = 1, r = n; while (l < r) { long long mid = l + r + 1 >> 1; if (sum(sum1, mid) * 2 <= i) { l = mid; } else r = mid - 1; } long long cnt = sum(sum1, l); long long sum3 = sum(sum2, l); ans2 = 0; ans2 += cnt * l - sum3 - cnt * (cnt - 1) / 2; cnt = i - cnt; sum3 = sum(sum2, n) - sum(sum2, l); ans2 += sum3 - cnt * (l + 1) - cnt * (cnt - 1) / 2; cout << ans1 + ans2 << " "; } cout << endl; }
1269_E. K Integers
You are given a permutation p_1, p_2, …, p_n. In one move you can swap two adjacent values. You want to perform a minimum number of moves, such that in the end there will exist a subsegment 1,2,…, k, in other words in the end there should be an integer i, 1 ≀ i ≀ n-k+1 such that p_i = 1, p_{i+1} = 2, …, p_{i+k-1}=k. Let f(k) be the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation. You need to find f(1), f(2), …, f(n). Input The first line of input contains one integer n (1 ≀ n ≀ 200 000): the number of elements in the permutation. The next line of input contains n integers p_1, p_2, …, p_n: given permutation (1 ≀ p_i ≀ n). Output Print n integers, the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation, for k=1, 2, …, n. Examples Input 5 5 4 3 2 1 Output 0 1 3 6 10 Input 3 1 2 3 Output 0 0 0
{ "input": [ "3\n1 2 3\n", "5\n5 4 3 2 1\n" ], "output": [ "0 0 0\n", "0 1 3 6 10\n" ] }
{ "input": [ "1\n1\n", "100\n98 52 63 2 18 96 31 58 84 40 41 45 66 100 46 71 26 48 81 20 73 91 68 76 13 93 17 29 64 95 79 21 55 75 19 85 54 51 89 78 15 87 43 59 36 1 90 35 65 56 62 28 86 5 82 49 3 99 33 9 92 32 74 69 27 22 77 16 44 94 34 6 57 70 23 12 61 25 8 11 67 47 83 88 10 14 30 7 97 60 42 37 24 38 53 50 4 80 72 39\n", "10\n5 1 6 2 8 3 4 10 9 7\n" ], "output": [ "0\n", "0 42 52 101 101 117 146 166 166 188 194 197 249 258 294 298 345 415 445 492 522 529 540 562 569 628 628 644 684 699 765 766 768 774 791 812 828 844 863 931 996 1011 1036 1040 1105 1166 1175 1232 1237 1251 1282 1364 1377 1409 1445 1455 1461 1534 1553 1565 1572 1581 1664 1706 1715 1779 1787 1837 1841 1847 1909 1919 1973 1976 2010 2060 2063 2087 2125 2133 2192 2193 2196 2276 2305 2305 2324 2327 2352 2361 2417 2418 2467 2468 2510 2598 2599 2697 2697 2770\n", "0 1 2 3 8 9 12 12 13 13\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using vi = vector<int>; using pii = pair<int, int>; using vii = vector<pii>; using ll = long long; int solve(); int main(int argc, char* argv[]) { ::std::ios::sync_with_stdio(false); ::std::cin.tie(0); ::std::cout.tie(0); int t = 1; while (t--) { solve(); } } struct Fenwick { vector<int> b; int n; Fenwick(int n) : n(n) { b.resize(n + 1); } void add(int i) { while (i > 0 && i <= n) { b[i]++; i += i & (-i); } } int rank(int i) const { int res = 0; for (; i > 0; i -= i & (-i)) res += b[i]; return res; } int at_rank(int k) const { int l = 0, r = n + 1; while (l < r) { int m = (l + r) / 2; if (k <= rank(m)) r = m; else l = m + 1; } return l; } }; int solve() { int n; cin >> n; vector<int> pos(n); Fenwick f(n + 1); for (int i = 0; i < n; i++) { int p; cin >> p; pos[p - 1] = i + 1; } long long a = 0; for (int i = 0; i < n; i++) { int p = pos[i]; f.add(p); int r = f.rank(p); int m = i / 2 + 1; if (r <= m) { m += i & 1; a += f.at_rank(m) - p - m + i - r + 2; } else { a += p - f.at_rank(m) + m - r; } cout << a << ' '; } return 0; }
1269_E. K Integers
You are given a permutation p_1, p_2, …, p_n. In one move you can swap two adjacent values. You want to perform a minimum number of moves, such that in the end there will exist a subsegment 1,2,…, k, in other words in the end there should be an integer i, 1 ≀ i ≀ n-k+1 such that p_i = 1, p_{i+1} = 2, …, p_{i+k-1}=k. Let f(k) be the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation. You need to find f(1), f(2), …, f(n). Input The first line of input contains one integer n (1 ≀ n ≀ 200 000): the number of elements in the permutation. The next line of input contains n integers p_1, p_2, …, p_n: given permutation (1 ≀ p_i ≀ n). Output Print n integers, the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation, for k=1, 2, …, n. Examples Input 5 5 4 3 2 1 Output 0 1 3 6 10 Input 3 1 2 3 Output 0 0 0
{ "input": [ "3\n1 2 3\n", "5\n5 4 3 2 1\n" ], "output": [ "0 0 0\n", "0 1 3 6 10\n" ] }
{ "input": [ "1\n1\n", "100\n98 52 63 2 18 96 31 58 84 40 41 45 66 100 46 71 26 48 81 20 73 91 68 76 13 93 17 29 64 95 79 21 55 75 19 85 54 51 89 78 15 87 43 59 36 1 90 35 65 56 62 28 86 5 82 49 3 99 33 9 92 32 74 69 27 22 77 16 44 94 34 6 57 70 23 12 61 25 8 11 67 47 83 88 10 14 30 7 97 60 42 37 24 38 53 50 4 80 72 39\n", "10\n5 1 6 2 8 3 4 10 9 7\n" ], "output": [ "0\n", "0 42 52 101 101 117 146 166 166 188 194 197 249 258 294 298 345 415 445 492 522 529 540 562 569 628 628 644 684 699 765 766 768 774 791 812 828 844 863 931 996 1011 1036 1040 1105 1166 1175 1232 1237 1251 1282 1364 1377 1409 1445 1455 1461 1534 1553 1565 1572 1581 1664 1706 1715 1779 1787 1837 1841 1847 1909 1919 1973 1976 2010 2060 2063 2087 2125 2133 2192 2193 2196 2276 2305 2305 2324 2327 2352 2361 2417 2418 2467 2468 2510 2598 2599 2697 2697 2770\n", "0 1 2 3 8 9 12 12 13 13\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 10; int N; int position[maxn]; long long sum1[maxn << 2], sum2[maxn << 2]; void add(long long* bit, int pos, long long val) { while (pos <= N) { bit[pos] += val; pos += pos & (-pos); } } long long query(long long* bit, int pos) { long long ret = 0; while (pos) { ret += bit[pos]; pos -= pos & (-pos); } return ret; } int search(long long* bit, int val) { int l = 1, r = N, ret = -1; while (l <= r) { int mid = (l + r) >> 1; if (query(bit, mid) < val) { l = mid + 1; } else { ret = mid; r = mid - 1; } } return ret; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cin >> N; for (int i = 1; i <= N; ++i) { int p; cin >> p; position[p] = i; } long long cnt = 0; for (int i = 1; i <= N; ++i) { int p = position[i]; add(sum1, p, 1); cnt += i - query(sum1, p); add(sum2, p, p); long long pos = search(sum1, i / 2 + 1); long long sum = 0; long long a = i / 2, b = i - a - 1; sum += pos * a - a * (a + 1) / 2 - query(sum2, pos - 1); sum += (query(sum2, N) - query(sum2, pos)) - b * pos - b * (b + 1) / 2; cout << cnt + sum << " "; } cout << endl; }
1269_E. K Integers
You are given a permutation p_1, p_2, …, p_n. In one move you can swap two adjacent values. You want to perform a minimum number of moves, such that in the end there will exist a subsegment 1,2,…, k, in other words in the end there should be an integer i, 1 ≀ i ≀ n-k+1 such that p_i = 1, p_{i+1} = 2, …, p_{i+k-1}=k. Let f(k) be the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation. You need to find f(1), f(2), …, f(n). Input The first line of input contains one integer n (1 ≀ n ≀ 200 000): the number of elements in the permutation. The next line of input contains n integers p_1, p_2, …, p_n: given permutation (1 ≀ p_i ≀ n). Output Print n integers, the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation, for k=1, 2, …, n. Examples Input 5 5 4 3 2 1 Output 0 1 3 6 10 Input 3 1 2 3 Output 0 0 0
{ "input": [ "3\n1 2 3\n", "5\n5 4 3 2 1\n" ], "output": [ "0 0 0\n", "0 1 3 6 10\n" ] }
{ "input": [ "1\n1\n", "100\n98 52 63 2 18 96 31 58 84 40 41 45 66 100 46 71 26 48 81 20 73 91 68 76 13 93 17 29 64 95 79 21 55 75 19 85 54 51 89 78 15 87 43 59 36 1 90 35 65 56 62 28 86 5 82 49 3 99 33 9 92 32 74 69 27 22 77 16 44 94 34 6 57 70 23 12 61 25 8 11 67 47 83 88 10 14 30 7 97 60 42 37 24 38 53 50 4 80 72 39\n", "10\n5 1 6 2 8 3 4 10 9 7\n" ], "output": [ "0\n", "0 42 52 101 101 117 146 166 166 188 194 197 249 258 294 298 345 415 445 492 522 529 540 562 569 628 628 644 684 699 765 766 768 774 791 812 828 844 863 931 996 1011 1036 1040 1105 1166 1175 1232 1237 1251 1282 1364 1377 1409 1445 1455 1461 1534 1553 1565 1572 1581 1664 1706 1715 1779 1787 1837 1841 1847 1909 1919 1973 1976 2010 2060 2063 2087 2125 2133 2192 2193 2196 2276 2305 2305 2324 2327 2352 2361 2417 2418 2467 2468 2510 2598 2599 2697 2697 2770\n", "0 1 2 3 8 9 12 12 13 13\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int n; int a[300005], pos[300005]; int lowbit(int now) { return now & (-now); } long long sum[5][300005]; void add(int flag, int now, int val) { for (int i = now; i <= n; i += lowbit(i)) sum[flag][i] += val; } long long query(int flag, int now) { long long ans = 0; for (int i = now; i; i -= lowbit(i)) ans += sum[flag][i]; return ans; } int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d", &a[i]), pos[a[i]] = i; long long ans1 = 0; for (int i = 1; i <= n; i++) { ans1 += (i - 1 - query(1, pos[i])); add(1, pos[i], 1); add(2, pos[i], pos[i]); int nl = 1, nr = n, ans = 1; while (nl <= nr) { if (query(1, ((nl + nr) >> 1)) * 2 <= i) { ans = ((nl + nr) >> 1); nl = ((nl + nr) >> 1) + 1; } else nr = ((nl + nr) >> 1) - 1; } long long cnt1 = query(1, ans), cnt2 = i - cnt1; long long res1 = ans * cnt1 - query(2, ans) - (1 + cnt1 - 1) * (cnt1 - 1) / 2; long long res2 = (query(2, n) - query(2, ans)) - ((ans + 1) * cnt2) - (cnt2) * (cnt2 - 1) / 2; printf("%lld ", ans1 + res1 + res2); } }