code
stringlengths
46
24k
language
stringclasses
6 values
AST_depth
int64
3
30
alphanumeric_fraction
float64
0.2
0.75
max_line_length
int64
13
399
avg_line_length
float64
5.01
139
num_lines
int64
7
299
task
stringlengths
151
14k
source
stringclasses
3 values
import sys import math from collections import defaultdict,Counter input = sys.stdin.readline def I(): return input() def II(): return int(input()) def MII(): return map(int, input().split()) def LI(): return list(input().split()) def LII(): return list(map(int, input().split())) def GMI(): return map(lambda x: int(x) - 1, input().split()) def LGMI(): return list(map(lambda x: int(x) - 1, input().split())) def WRITE(out): return print('\n'.join(map(str, out))) ''' Oh... misunderstood question If n==k: print(0) else: check set of largest(n-k) iterate from n to n-k if not in set: ans += 1 ''' t=II() for _ in range(t): n,k=MII() a=LII() s = sorted(a) use = set(s[k:]) ans=0 for i in range(k,n): if a[i] not in use: ans += 1 print(ans)
python
12
0.545558
59
7.12963
108
A. Wonderful Permutationtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputGod's Blessing on This PermutationForces!A Random PebbleYou are given a permutation $$$p_1,p_2,\ldots,p_n$$$ of length $$$n$$$ and a positive integer $$$k \le n$$$. In one operation you can choose two indices $$$i$$$ and $$$j$$$ ($$$1 \le i < j \le n$$$) and swap $$$p_i$$$ with $$$p_j$$$.Find the minimum number of operations needed to make the sum $$$p_1 + p_2 + \ldots + p_k$$$ as small as possible.A permutation is an array consisting of $$$n$$$ distinct integers from $$$1$$$ to $$$n$$$ in arbitrary order. For example, $$$[2,3,1,5,4]$$$ is a permutation, but $$$[1,2,2]$$$ is not a permutation ($$$2$$$ appears twice in the array) and $$$[1,3,4]$$$ is also not a permutation ($$$n=3$$$ but there is $$$4$$$ in the array).InputEach test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows.The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le k \le n \le 100$$$).The second line of each test case contains $$$n$$$ integers $$$p_1,p_2,\ldots,p_n$$$ ($$$1 \le p_i \le n$$$). It is guaranteed that the given numbers form a permutation of length $$$n$$$.OutputFor each test case print one integer — the minimum number of operations needed to make the sum $$$p_1 + p_2 + \ldots + p_k$$$ as small as possible.ExampleInput 43 12 3 13 31 2 34 23 4 1 21 11Output 1 0 2 0 NoteIn the first test case, the value of $$$p_1 + p_2 + \ldots + p_k$$$ is initially equal to $$$2$$$, but the smallest possible value is $$$1$$$. You can achieve it by swapping $$$p_1$$$ with $$$p_3$$$, resulting in the permutation $$$[1, 3, 2]$$$.In the second test case, the sum is already as small as possible, so the answer is $$$0$$$.
cf
#include <stdio.h> int main() { int t; scanf("%d",&t); int n,k; int a[101]; while(t--) { scanf("%d %d",&n,&k); for(int i=0;i<n;i++) { scanf("%d",&a[i]); } int c=0; for(int i=0;i<k;i++) { if(a[i]>k) { c++; } } printf("%d\n",c); } return 0; }
c++
13
0.277778
28
6.830189
53
A. Wonderful Permutationtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputGod's Blessing on This PermutationForces!A Random PebbleYou are given a permutation $$$p_1,p_2,\ldots,p_n$$$ of length $$$n$$$ and a positive integer $$$k \le n$$$. In one operation you can choose two indices $$$i$$$ and $$$j$$$ ($$$1 \le i < j \le n$$$) and swap $$$p_i$$$ with $$$p_j$$$.Find the minimum number of operations needed to make the sum $$$p_1 + p_2 + \ldots + p_k$$$ as small as possible.A permutation is an array consisting of $$$n$$$ distinct integers from $$$1$$$ to $$$n$$$ in arbitrary order. For example, $$$[2,3,1,5,4]$$$ is a permutation, but $$$[1,2,2]$$$ is not a permutation ($$$2$$$ appears twice in the array) and $$$[1,3,4]$$$ is also not a permutation ($$$n=3$$$ but there is $$$4$$$ in the array).InputEach test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows.The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le k \le n \le 100$$$).The second line of each test case contains $$$n$$$ integers $$$p_1,p_2,\ldots,p_n$$$ ($$$1 \le p_i \le n$$$). It is guaranteed that the given numbers form a permutation of length $$$n$$$.OutputFor each test case print one integer — the minimum number of operations needed to make the sum $$$p_1 + p_2 + \ldots + p_k$$$ as small as possible.ExampleInput 43 12 3 13 31 2 34 23 4 1 21 11Output 1 0 2 0 NoteIn the first test case, the value of $$$p_1 + p_2 + \ldots + p_k$$$ is initially equal to $$$2$$$, but the smallest possible value is $$$1$$$. You can achieve it by swapping $$$p_1$$$ with $$$p_3$$$, resulting in the permutation $$$[1, 3, 2]$$$.In the second test case, the sum is already as small as possible, so the answer is $$$0$$$.
cf
for i in range(int(input())): soll=0 lst1=list(map(int,input().split())) lst2=list(map(int,input().split())) for h in range(lst1[1]): if(lst2[h]>lst1[1]): soll+=1 print(soll)
python
13
0.518182
39
13.733333
15
A. Wonderful Permutationtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputGod's Blessing on This PermutationForces!A Random PebbleYou are given a permutation $$$p_1,p_2,\ldots,p_n$$$ of length $$$n$$$ and a positive integer $$$k \le n$$$. In one operation you can choose two indices $$$i$$$ and $$$j$$$ ($$$1 \le i < j \le n$$$) and swap $$$p_i$$$ with $$$p_j$$$.Find the minimum number of operations needed to make the sum $$$p_1 + p_2 + \ldots + p_k$$$ as small as possible.A permutation is an array consisting of $$$n$$$ distinct integers from $$$1$$$ to $$$n$$$ in arbitrary order. For example, $$$[2,3,1,5,4]$$$ is a permutation, but $$$[1,2,2]$$$ is not a permutation ($$$2$$$ appears twice in the array) and $$$[1,3,4]$$$ is also not a permutation ($$$n=3$$$ but there is $$$4$$$ in the array).InputEach test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows.The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le k \le n \le 100$$$).The second line of each test case contains $$$n$$$ integers $$$p_1,p_2,\ldots,p_n$$$ ($$$1 \le p_i \le n$$$). It is guaranteed that the given numbers form a permutation of length $$$n$$$.OutputFor each test case print one integer — the minimum number of operations needed to make the sum $$$p_1 + p_2 + \ldots + p_k$$$ as small as possible.ExampleInput 43 12 3 13 31 2 34 23 4 1 21 11Output 1 0 2 0 NoteIn the first test case, the value of $$$p_1 + p_2 + \ldots + p_k$$$ is initially equal to $$$2$$$, but the smallest possible value is $$$1$$$. You can achieve it by swapping $$$p_1$$$ with $$$p_3$$$, resulting in the permutation $$$[1, 3, 2]$$$.In the second test case, the sum is already as small as possible, so the answer is $$$0$$$.
cf
#include <bits/stdc++.h> using namespace std; void test() { int n, k; cin >> n >> k; vector<int> v(n); for (int& i : v) { cin >> i; --i; } if (k == n) { cout << 0 << endl; return; } for (int i = 0;; ++i) { auto al = max_element(v.begin(), v.begin() + k); auto bl = min_element(v.begin() + k, v.begin() + n); if (*al < *bl) { cout << i << endl; return; } iter_swap(al, bl); } } int main() { int t; for (cin >> t; t--; test()) ; }
c++
12
0.378472
60
17.612903
31
A. Wonderful Permutationtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputGod's Blessing on This PermutationForces!A Random PebbleYou are given a permutation $$$p_1,p_2,\ldots,p_n$$$ of length $$$n$$$ and a positive integer $$$k \le n$$$. In one operation you can choose two indices $$$i$$$ and $$$j$$$ ($$$1 \le i < j \le n$$$) and swap $$$p_i$$$ with $$$p_j$$$.Find the minimum number of operations needed to make the sum $$$p_1 + p_2 + \ldots + p_k$$$ as small as possible.A permutation is an array consisting of $$$n$$$ distinct integers from $$$1$$$ to $$$n$$$ in arbitrary order. For example, $$$[2,3,1,5,4]$$$ is a permutation, but $$$[1,2,2]$$$ is not a permutation ($$$2$$$ appears twice in the array) and $$$[1,3,4]$$$ is also not a permutation ($$$n=3$$$ but there is $$$4$$$ in the array).InputEach test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows.The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le k \le n \le 100$$$).The second line of each test case contains $$$n$$$ integers $$$p_1,p_2,\ldots,p_n$$$ ($$$1 \le p_i \le n$$$). It is guaranteed that the given numbers form a permutation of length $$$n$$$.OutputFor each test case print one integer — the minimum number of operations needed to make the sum $$$p_1 + p_2 + \ldots + p_k$$$ as small as possible.ExampleInput 43 12 3 13 31 2 34 23 4 1 21 11Output 1 0 2 0 NoteIn the first test case, the value of $$$p_1 + p_2 + \ldots + p_k$$$ is initially equal to $$$2$$$, but the smallest possible value is $$$1$$$. You can achieve it by swapping $$$p_1$$$ with $$$p_3$$$, resulting in the permutation $$$[1, 3, 2]$$$.In the second test case, the sum is already as small as possible, so the answer is $$$0$$$.
cf
#include <bits/stdc++.h> using namespace std; #pragma GCC optimize("Ofast") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native") static const auto fast = []() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); return 0; } (); ; #define mod 1e9+7 #define imod ((int)mod) typedef long long ll; typedef unsigned long long ull; int main() { long long int t; cin>>t; for(int i=0;i<t;i++){ long long int n,k; cin>>n>>k; int a[n]; int count=0; for(int i=0;i<n;i++){ cin>>a[i]; if(i<k&&a[i]>k){ count++; } } cout<<count<<endl; } }
c++
13
0.532164
82
8.927536
69
A. Wonderful Permutationtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputGod's Blessing on This PermutationForces!A Random PebbleYou are given a permutation $$$p_1,p_2,\ldots,p_n$$$ of length $$$n$$$ and a positive integer $$$k \le n$$$. In one operation you can choose two indices $$$i$$$ and $$$j$$$ ($$$1 \le i < j \le n$$$) and swap $$$p_i$$$ with $$$p_j$$$.Find the minimum number of operations needed to make the sum $$$p_1 + p_2 + \ldots + p_k$$$ as small as possible.A permutation is an array consisting of $$$n$$$ distinct integers from $$$1$$$ to $$$n$$$ in arbitrary order. For example, $$$[2,3,1,5,4]$$$ is a permutation, but $$$[1,2,2]$$$ is not a permutation ($$$2$$$ appears twice in the array) and $$$[1,3,4]$$$ is also not a permutation ($$$n=3$$$ but there is $$$4$$$ in the array).InputEach test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows.The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le k \le n \le 100$$$).The second line of each test case contains $$$n$$$ integers $$$p_1,p_2,\ldots,p_n$$$ ($$$1 \le p_i \le n$$$). It is guaranteed that the given numbers form a permutation of length $$$n$$$.OutputFor each test case print one integer — the minimum number of operations needed to make the sum $$$p_1 + p_2 + \ldots + p_k$$$ as small as possible.ExampleInput 43 12 3 13 31 2 34 23 4 1 21 11Output 1 0 2 0 NoteIn the first test case, the value of $$$p_1 + p_2 + \ldots + p_k$$$ is initially equal to $$$2$$$, but the smallest possible value is $$$1$$$. You can achieve it by swapping $$$p_1$$$ with $$$p_3$$$, resulting in the permutation $$$[1, 3, 2]$$$.In the second test case, the sum is already as small as possible, so the answer is $$$0$$$.
cf
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.*; public class Problem1 { static class Reader { static BufferedReader reader; static StringTokenizer tokenizer; /** call this method to initialize reader for InputStream */ static void init(InputStream input) { reader = new BufferedReader( new InputStreamReader(input) ); tokenizer = new StringTokenizer(""); } /** get next word */ static String next() throws IOException { while ( ! tokenizer.hasMoreTokens() ) { //TODO add check for eof if necessary tokenizer = new StringTokenizer( reader.readLine() ); } return tokenizer.nextToken(); } static int nextInt() throws IOException { return Integer.parseInt( next() ); } static double nextDouble() throws IOException { return Double.parseDouble( next() ); } } public static void main(String[] args) throws Exception { Reader.init(System.in); // connect Reader to an input stream int t=Reader.nextInt(); while(t>0){ int n =Reader.nextInt(); int k =Reader.nextInt(); int[] arr1=new int[n]; for(int i =0;i<n ;i++){ arr1[i]=Reader.nextInt(); } Set<Integer> set=new HashSet<>(); for (int i =0;i<k ;i++){ set.add(arr1[i]); } int ans =0; Arrays.sort(arr1); for(int i =0;i<k;i++){ if(!set.contains(arr1[i])) ans++; } System.out.println(ans); t--; } } }
java
15
0.502362
68
20.166667
90
A. Wonderful Permutationtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputGod's Blessing on This PermutationForces!A Random PebbleYou are given a permutation $$$p_1,p_2,\ldots,p_n$$$ of length $$$n$$$ and a positive integer $$$k \le n$$$. In one operation you can choose two indices $$$i$$$ and $$$j$$$ ($$$1 \le i < j \le n$$$) and swap $$$p_i$$$ with $$$p_j$$$.Find the minimum number of operations needed to make the sum $$$p_1 + p_2 + \ldots + p_k$$$ as small as possible.A permutation is an array consisting of $$$n$$$ distinct integers from $$$1$$$ to $$$n$$$ in arbitrary order. For example, $$$[2,3,1,5,4]$$$ is a permutation, but $$$[1,2,2]$$$ is not a permutation ($$$2$$$ appears twice in the array) and $$$[1,3,4]$$$ is also not a permutation ($$$n=3$$$ but there is $$$4$$$ in the array).InputEach test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows.The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le k \le n \le 100$$$).The second line of each test case contains $$$n$$$ integers $$$p_1,p_2,\ldots,p_n$$$ ($$$1 \le p_i \le n$$$). It is guaranteed that the given numbers form a permutation of length $$$n$$$.OutputFor each test case print one integer — the minimum number of operations needed to make the sum $$$p_1 + p_2 + \ldots + p_k$$$ as small as possible.ExampleInput 43 12 3 13 31 2 34 23 4 1 21 11Output 1 0 2 0 NoteIn the first test case, the value of $$$p_1 + p_2 + \ldots + p_k$$$ is initially equal to $$$2$$$, but the smallest possible value is $$$1$$$. You can achieve it by swapping $$$p_1$$$ with $$$p_3$$$, resulting in the permutation $$$[1, 3, 2]$$$.In the second test case, the sum is already as small as possible, so the answer is $$$0$$$.
cf
for t in range(int(input())): n, k = map(int, input().split()) ans = 0 for i in list(map(int, input().split()))[:k]: if i > k: ans+=1 print(ans)
python
13
0.485549
49
14.818182
11
A. Wonderful Permutationtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputGod's Blessing on This PermutationForces!A Random PebbleYou are given a permutation $$$p_1,p_2,\ldots,p_n$$$ of length $$$n$$$ and a positive integer $$$k \le n$$$. In one operation you can choose two indices $$$i$$$ and $$$j$$$ ($$$1 \le i < j \le n$$$) and swap $$$p_i$$$ with $$$p_j$$$.Find the minimum number of operations needed to make the sum $$$p_1 + p_2 + \ldots + p_k$$$ as small as possible.A permutation is an array consisting of $$$n$$$ distinct integers from $$$1$$$ to $$$n$$$ in arbitrary order. For example, $$$[2,3,1,5,4]$$$ is a permutation, but $$$[1,2,2]$$$ is not a permutation ($$$2$$$ appears twice in the array) and $$$[1,3,4]$$$ is also not a permutation ($$$n=3$$$ but there is $$$4$$$ in the array).InputEach test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows.The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le k \le n \le 100$$$).The second line of each test case contains $$$n$$$ integers $$$p_1,p_2,\ldots,p_n$$$ ($$$1 \le p_i \le n$$$). It is guaranteed that the given numbers form a permutation of length $$$n$$$.OutputFor each test case print one integer — the minimum number of operations needed to make the sum $$$p_1 + p_2 + \ldots + p_k$$$ as small as possible.ExampleInput 43 12 3 13 31 2 34 23 4 1 21 11Output 1 0 2 0 NoteIn the first test case, the value of $$$p_1 + p_2 + \ldots + p_k$$$ is initially equal to $$$2$$$, but the smallest possible value is $$$1$$$. You can achieve it by swapping $$$p_1$$$ with $$$p_3$$$, resulting in the permutation $$$[1, 3, 2]$$$.In the second test case, the sum is already as small as possible, so the answer is $$$0$$$.
cf
#include<bits/stdc++.h> using namespace std; #define ll long long void solve(){ int n, k, cnt=0; cin >> n >> k; int a[n+1]; for(int i=1;i<=n;i++){ cin >> a[i]; if(i<=k && a[i]>k) ++cnt; } cout << cnt << endl; } int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); int t; cin >> t; while(t--){ solve(); } return 0; }
c++
11
0.5
32
6.913043
46
A. Wonderful Permutationtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputGod's Blessing on This PermutationForces!A Random PebbleYou are given a permutation $$$p_1,p_2,\ldots,p_n$$$ of length $$$n$$$ and a positive integer $$$k \le n$$$. In one operation you can choose two indices $$$i$$$ and $$$j$$$ ($$$1 \le i < j \le n$$$) and swap $$$p_i$$$ with $$$p_j$$$.Find the minimum number of operations needed to make the sum $$$p_1 + p_2 + \ldots + p_k$$$ as small as possible.A permutation is an array consisting of $$$n$$$ distinct integers from $$$1$$$ to $$$n$$$ in arbitrary order. For example, $$$[2,3,1,5,4]$$$ is a permutation, but $$$[1,2,2]$$$ is not a permutation ($$$2$$$ appears twice in the array) and $$$[1,3,4]$$$ is also not a permutation ($$$n=3$$$ but there is $$$4$$$ in the array).InputEach test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows.The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le k \le n \le 100$$$).The second line of each test case contains $$$n$$$ integers $$$p_1,p_2,\ldots,p_n$$$ ($$$1 \le p_i \le n$$$). It is guaranteed that the given numbers form a permutation of length $$$n$$$.OutputFor each test case print one integer — the minimum number of operations needed to make the sum $$$p_1 + p_2 + \ldots + p_k$$$ as small as possible.ExampleInput 43 12 3 13 31 2 34 23 4 1 21 11Output 1 0 2 0 NoteIn the first test case, the value of $$$p_1 + p_2 + \ldots + p_k$$$ is initially equal to $$$2$$$, but the smallest possible value is $$$1$$$. You can achieve it by swapping $$$p_1$$$ with $$$p_3$$$, resulting in the permutation $$$[1, 3, 2]$$$.In the second test case, the sum is already as small as possible, so the answer is $$$0$$$.
cf
#include <bits/stdc++.h> #include <iostream> #include <vector> #include <cmath> #include <string> #include <algorithm> #include <map> #include <set> #include <numeric> #include <iomanip> using namespace std; const int N = 1e7; using I = int; using SI = short int; using LI = long int; using LLI = long long int; using UI = unsigned int; using USI = unsigned short int; using ULI = unsigned long int; using ULLI = unsigned long long int; #define vi vector<int> #define pi pair<int, int> #define umap unordered_map #define uset unordered_set #define all(a) a.begin(), a.end() - 1 #define rall(a) a.rbegin(), a.rend() #define min3(a, b, c) min(a, min(b, c)) #define max3(a, b, c) max(a, max(b, c)) #define ws1(a) cout << a << " " #define wr1(a) cout << a << endl #define wr2(a, b) cout << a << " " << b << endl #define wr3(a, b, c) cout << a << " " << b << " " << c << endl #define wr4(a, b, c, d) cout << a << " " << b << " " << c << " " << d << endl #define ww1(a) cout << a << "\n" #define ww2(a, b) cout << a << " " << b << "\n" #define ww3(a, b, c) cout << a << " " << b << " " << c << "\n" #define ww4(a, b, c, d) cout << a << " " << b << " " << c << " " << d << "\n" #define FOR(i, s, e) for (int i = s; i < e; ++i) #define FORR(i, s, e) for (int i = s; i >= e; --i) #define FT first #define SC second #define pb push_back #define pob pop_back #define mp make_pair #define PRE cout << fixed << setprecision(10); #define FIO \ ios_base::sync_with_stdio(false); \ cin.tie(NULL); \ cout.tie(NULL); vector<pair<int, int>> sides = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}}; vector<pair<int, int>> movements = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1}}; clock_t startTime; void getCurrentTime() { cerr << (double)(clock() - startTime) / CLOCKS_PER_SEC; } int sum() { return 0; } template <typename T, typename... Args> auto sum(T a, Args... args) { return a + sum(args...); } #define yes cout << "YES\n"; #define no cout << "NO\n"; // S@ndip : Shree Ganeshay Namah int main() { FIO int t; cin >> t; while(t--){ int n, k; cin >> n >> k; vector <int> p(n, 0); for (int i = 0; i < n; i++) { cin >> p[i]; } // 3 4 1 2 vector <int> a(k, 0); // 1 2 set <int> s; for (int i = 0; i < k; i++) { s.insert(i + 1); a[i] = p[i]; } // 3 4 12 // 1 2 for (int i = 0; i < k; i++) { if(s.find(a[i]) != s.end()) s.erase(a[i]); } ww1(s.size()); } return 0; }
c++
14
0.461593
106
11.844749
219
A. Wonderful Permutationtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputGod's Blessing on This PermutationForces!A Random PebbleYou are given a permutation $$$p_1,p_2,\ldots,p_n$$$ of length $$$n$$$ and a positive integer $$$k \le n$$$. In one operation you can choose two indices $$$i$$$ and $$$j$$$ ($$$1 \le i < j \le n$$$) and swap $$$p_i$$$ with $$$p_j$$$.Find the minimum number of operations needed to make the sum $$$p_1 + p_2 + \ldots + p_k$$$ as small as possible.A permutation is an array consisting of $$$n$$$ distinct integers from $$$1$$$ to $$$n$$$ in arbitrary order. For example, $$$[2,3,1,5,4]$$$ is a permutation, but $$$[1,2,2]$$$ is not a permutation ($$$2$$$ appears twice in the array) and $$$[1,3,4]$$$ is also not a permutation ($$$n=3$$$ but there is $$$4$$$ in the array).InputEach test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows.The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le k \le n \le 100$$$).The second line of each test case contains $$$n$$$ integers $$$p_1,p_2,\ldots,p_n$$$ ($$$1 \le p_i \le n$$$). It is guaranteed that the given numbers form a permutation of length $$$n$$$.OutputFor each test case print one integer — the minimum number of operations needed to make the sum $$$p_1 + p_2 + \ldots + p_k$$$ as small as possible.ExampleInput 43 12 3 13 31 2 34 23 4 1 21 11Output 1 0 2 0 NoteIn the first test case, the value of $$$p_1 + p_2 + \ldots + p_k$$$ is initially equal to $$$2$$$, but the smallest possible value is $$$1$$$. You can achieve it by swapping $$$p_1$$$ with $$$p_3$$$, resulting in the permutation $$$[1, 3, 2]$$$.In the second test case, the sum is already as small as possible, so the answer is $$$0$$$.
cf
#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ Created on Sat Jan 28 01:29:46 2023 @author: manisarthak """ import sys input = lambda: sys.stdin.readline().rstrip() def solve (): [n, k] = list(map(int, input().split())) arr = list(map(int, input().split())) s = set() for i in range(k): if arr[i] <= k : s.add(arr[i]) print(k - len(s)) # solve() for _ in range(int(input())): solve ()
python
13
0.47561
45
7.355932
59
A. Wonderful Permutationtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputGod's Blessing on This PermutationForces!A Random PebbleYou are given a permutation $$$p_1,p_2,\ldots,p_n$$$ of length $$$n$$$ and a positive integer $$$k \le n$$$. In one operation you can choose two indices $$$i$$$ and $$$j$$$ ($$$1 \le i < j \le n$$$) and swap $$$p_i$$$ with $$$p_j$$$.Find the minimum number of operations needed to make the sum $$$p_1 + p_2 + \ldots + p_k$$$ as small as possible.A permutation is an array consisting of $$$n$$$ distinct integers from $$$1$$$ to $$$n$$$ in arbitrary order. For example, $$$[2,3,1,5,4]$$$ is a permutation, but $$$[1,2,2]$$$ is not a permutation ($$$2$$$ appears twice in the array) and $$$[1,3,4]$$$ is also not a permutation ($$$n=3$$$ but there is $$$4$$$ in the array).InputEach test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows.The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le k \le n \le 100$$$).The second line of each test case contains $$$n$$$ integers $$$p_1,p_2,\ldots,p_n$$$ ($$$1 \le p_i \le n$$$). It is guaranteed that the given numbers form a permutation of length $$$n$$$.OutputFor each test case print one integer — the minimum number of operations needed to make the sum $$$p_1 + p_2 + \ldots + p_k$$$ as small as possible.ExampleInput 43 12 3 13 31 2 34 23 4 1 21 11Output 1 0 2 0 NoteIn the first test case, the value of $$$p_1 + p_2 + \ldots + p_k$$$ is initially equal to $$$2$$$, but the smallest possible value is $$$1$$$. You can achieve it by swapping $$$p_1$$$ with $$$p_3$$$, resulting in the permutation $$$[1, 3, 2]$$$.In the second test case, the sum is already as small as possible, so the answer is $$$0$$$.
cf
t = int(input()) for f in range(t): n,k = list(map(int,input().split())) arr = list(map(int,input().split())) ct = 0 for i in range(k): if arr[i] > k: ct = ct + 1 print(ct)
python
13
0.434783
40
9.454545
22
A. Wonderful Permutationtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputGod's Blessing on This PermutationForces!A Random PebbleYou are given a permutation $$$p_1,p_2,\ldots,p_n$$$ of length $$$n$$$ and a positive integer $$$k \le n$$$. In one operation you can choose two indices $$$i$$$ and $$$j$$$ ($$$1 \le i < j \le n$$$) and swap $$$p_i$$$ with $$$p_j$$$.Find the minimum number of operations needed to make the sum $$$p_1 + p_2 + \ldots + p_k$$$ as small as possible.A permutation is an array consisting of $$$n$$$ distinct integers from $$$1$$$ to $$$n$$$ in arbitrary order. For example, $$$[2,3,1,5,4]$$$ is a permutation, but $$$[1,2,2]$$$ is not a permutation ($$$2$$$ appears twice in the array) and $$$[1,3,4]$$$ is also not a permutation ($$$n=3$$$ but there is $$$4$$$ in the array).InputEach test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows.The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le k \le n \le 100$$$).The second line of each test case contains $$$n$$$ integers $$$p_1,p_2,\ldots,p_n$$$ ($$$1 \le p_i \le n$$$). It is guaranteed that the given numbers form a permutation of length $$$n$$$.OutputFor each test case print one integer — the minimum number of operations needed to make the sum $$$p_1 + p_2 + \ldots + p_k$$$ as small as possible.ExampleInput 43 12 3 13 31 2 34 23 4 1 21 11Output 1 0 2 0 NoteIn the first test case, the value of $$$p_1 + p_2 + \ldots + p_k$$$ is initially equal to $$$2$$$, but the smallest possible value is $$$1$$$. You can achieve it by swapping $$$p_1$$$ with $$$p_3$$$, resulting in the permutation $$$[1, 3, 2]$$$.In the second test case, the sum is already as small as possible, so the answer is $$$0$$$.
cf
#include <iostream> using namespace std; int n,k,x,t,f; int main(){cin>>t;while(t--){cin>>n>>k;f=0;for(int i=0;i<n;i++){cin>>x;f+=(i<k&&x>k);}cout<<f<<endl;}}
c++
12
0.57764
102
22.142857
7
A. Wonderful Permutationtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputGod's Blessing on This PermutationForces!A Random PebbleYou are given a permutation $$$p_1,p_2,\ldots,p_n$$$ of length $$$n$$$ and a positive integer $$$k \le n$$$. In one operation you can choose two indices $$$i$$$ and $$$j$$$ ($$$1 \le i < j \le n$$$) and swap $$$p_i$$$ with $$$p_j$$$.Find the minimum number of operations needed to make the sum $$$p_1 + p_2 + \ldots + p_k$$$ as small as possible.A permutation is an array consisting of $$$n$$$ distinct integers from $$$1$$$ to $$$n$$$ in arbitrary order. For example, $$$[2,3,1,5,4]$$$ is a permutation, but $$$[1,2,2]$$$ is not a permutation ($$$2$$$ appears twice in the array) and $$$[1,3,4]$$$ is also not a permutation ($$$n=3$$$ but there is $$$4$$$ in the array).InputEach test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows.The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le k \le n \le 100$$$).The second line of each test case contains $$$n$$$ integers $$$p_1,p_2,\ldots,p_n$$$ ($$$1 \le p_i \le n$$$). It is guaranteed that the given numbers form a permutation of length $$$n$$$.OutputFor each test case print one integer — the minimum number of operations needed to make the sum $$$p_1 + p_2 + \ldots + p_k$$$ as small as possible.ExampleInput 43 12 3 13 31 2 34 23 4 1 21 11Output 1 0 2 0 NoteIn the first test case, the value of $$$p_1 + p_2 + \ldots + p_k$$$ is initially equal to $$$2$$$, but the smallest possible value is $$$1$$$. You can achieve it by swapping $$$p_1$$$ with $$$p_3$$$, resulting in the permutation $$$[1, 3, 2]$$$.In the second test case, the sum is already as small as possible, so the answer is $$$0$$$.
cf
t=int(input()) for ii in range(t): n,k=list(map(int,input().split())) a=list(map(int,input().split())) #a=sorted(a) s=0 dd=[] for i in range(k): if a[i]>k: s+=1 print(s)
python
13
0.53125
35
7.391304
23
A. Wonderful Permutationtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputGod's Blessing on This PermutationForces!A Random PebbleYou are given a permutation $$$p_1,p_2,\ldots,p_n$$$ of length $$$n$$$ and a positive integer $$$k \le n$$$. In one operation you can choose two indices $$$i$$$ and $$$j$$$ ($$$1 \le i < j \le n$$$) and swap $$$p_i$$$ with $$$p_j$$$.Find the minimum number of operations needed to make the sum $$$p_1 + p_2 + \ldots + p_k$$$ as small as possible.A permutation is an array consisting of $$$n$$$ distinct integers from $$$1$$$ to $$$n$$$ in arbitrary order. For example, $$$[2,3,1,5,4]$$$ is a permutation, but $$$[1,2,2]$$$ is not a permutation ($$$2$$$ appears twice in the array) and $$$[1,3,4]$$$ is also not a permutation ($$$n=3$$$ but there is $$$4$$$ in the array).InputEach test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows.The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le k \le n \le 100$$$).The second line of each test case contains $$$n$$$ integers $$$p_1,p_2,\ldots,p_n$$$ ($$$1 \le p_i \le n$$$). It is guaranteed that the given numbers form a permutation of length $$$n$$$.OutputFor each test case print one integer — the minimum number of operations needed to make the sum $$$p_1 + p_2 + \ldots + p_k$$$ as small as possible.ExampleInput 43 12 3 13 31 2 34 23 4 1 21 11Output 1 0 2 0 NoteIn the first test case, the value of $$$p_1 + p_2 + \ldots + p_k$$$ is initially equal to $$$2$$$, but the smallest possible value is $$$1$$$. You can achieve it by swapping $$$p_1$$$ with $$$p_3$$$, resulting in the permutation $$$[1, 3, 2]$$$.In the second test case, the sum is already as small as possible, so the answer is $$$0$$$.
cf
#include <bits/stdc++.h> using namespace std; #define ll long long int main() { ll t; cin>>t; while(t--){ ll n,k,c=0; cin>>n>>k; ll a[n],b[n]; for(ll i=0;i<n;i++){ cin>>a[i]; b[i]=a[i]; } sort(b,b+n); for(ll i=0;i<k;i++){ for(ll j=0;j<k;j++){ if(b[j]==a[i]){ c++; b[j]=-1;} } } cout<<k-c<<endl; } return 0; }
c++
15
0.32582
42
8.384615
52
A. Wonderful Permutationtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputGod's Blessing on This PermutationForces!A Random PebbleYou are given a permutation $$$p_1,p_2,\ldots,p_n$$$ of length $$$n$$$ and a positive integer $$$k \le n$$$. In one operation you can choose two indices $$$i$$$ and $$$j$$$ ($$$1 \le i < j \le n$$$) and swap $$$p_i$$$ with $$$p_j$$$.Find the minimum number of operations needed to make the sum $$$p_1 + p_2 + \ldots + p_k$$$ as small as possible.A permutation is an array consisting of $$$n$$$ distinct integers from $$$1$$$ to $$$n$$$ in arbitrary order. For example, $$$[2,3,1,5,4]$$$ is a permutation, but $$$[1,2,2]$$$ is not a permutation ($$$2$$$ appears twice in the array) and $$$[1,3,4]$$$ is also not a permutation ($$$n=3$$$ but there is $$$4$$$ in the array).InputEach test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows.The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le k \le n \le 100$$$).The second line of each test case contains $$$n$$$ integers $$$p_1,p_2,\ldots,p_n$$$ ($$$1 \le p_i \le n$$$). It is guaranteed that the given numbers form a permutation of length $$$n$$$.OutputFor each test case print one integer — the minimum number of operations needed to make the sum $$$p_1 + p_2 + \ldots + p_k$$$ as small as possible.ExampleInput 43 12 3 13 31 2 34 23 4 1 21 11Output 1 0 2 0 NoteIn the first test case, the value of $$$p_1 + p_2 + \ldots + p_k$$$ is initially equal to $$$2$$$, but the smallest possible value is $$$1$$$. You can achieve it by swapping $$$p_1$$$ with $$$p_3$$$, resulting in the permutation $$$[1, 3, 2]$$$.In the second test case, the sum is already as small as possible, so the answer is $$$0$$$.
cf
t = int(input()) for _ in range(t): n, k = [int(i) for i in input().split()] a = [int(i) for i in input().split()] req = 0 if n == k: print(0) continue for i in range(k): if a[i] > k: req += 1 print(req)
python
11
0.387755
44
7.428571
35
A. Wonderful Permutationtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputGod's Blessing on This PermutationForces!A Random PebbleYou are given a permutation $$$p_1,p_2,\ldots,p_n$$$ of length $$$n$$$ and a positive integer $$$k \le n$$$. In one operation you can choose two indices $$$i$$$ and $$$j$$$ ($$$1 \le i < j \le n$$$) and swap $$$p_i$$$ with $$$p_j$$$.Find the minimum number of operations needed to make the sum $$$p_1 + p_2 + \ldots + p_k$$$ as small as possible.A permutation is an array consisting of $$$n$$$ distinct integers from $$$1$$$ to $$$n$$$ in arbitrary order. For example, $$$[2,3,1,5,4]$$$ is a permutation, but $$$[1,2,2]$$$ is not a permutation ($$$2$$$ appears twice in the array) and $$$[1,3,4]$$$ is also not a permutation ($$$n=3$$$ but there is $$$4$$$ in the array).InputEach test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows.The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le k \le n \le 100$$$).The second line of each test case contains $$$n$$$ integers $$$p_1,p_2,\ldots,p_n$$$ ($$$1 \le p_i \le n$$$). It is guaranteed that the given numbers form a permutation of length $$$n$$$.OutputFor each test case print one integer — the minimum number of operations needed to make the sum $$$p_1 + p_2 + \ldots + p_k$$$ as small as possible.ExampleInput 43 12 3 13 31 2 34 23 4 1 21 11Output 1 0 2 0 NoteIn the first test case, the value of $$$p_1 + p_2 + \ldots + p_k$$$ is initially equal to $$$2$$$, but the smallest possible value is $$$1$$$. You can achieve it by swapping $$$p_1$$$ with $$$p_3$$$, resulting in the permutation $$$[1, 3, 2]$$$.In the second test case, the sum is already as small as possible, so the answer is $$$0$$$.
cf
import java.util.*; public class cf { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int testcase = scan.nextInt(); // int testcase = 1; for(int v = 0; v < testcase; v++){ int n = scan.nextInt(); int k = scan.nextInt(); int []arr = new int[n]; for(int i = 0; i < n; i++){ arr[i] = scan.nextInt(); } int count = 0; for(int i = 0; i < k; i++){ if(arr[i] > k){ count++; } } System.out.println(count); } } }
java
13
0.365682
46
8.608108
74
A. Wonderful Permutationtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputGod's Blessing on This PermutationForces!A Random PebbleYou are given a permutation $$$p_1,p_2,\ldots,p_n$$$ of length $$$n$$$ and a positive integer $$$k \le n$$$. In one operation you can choose two indices $$$i$$$ and $$$j$$$ ($$$1 \le i < j \le n$$$) and swap $$$p_i$$$ with $$$p_j$$$.Find the minimum number of operations needed to make the sum $$$p_1 + p_2 + \ldots + p_k$$$ as small as possible.A permutation is an array consisting of $$$n$$$ distinct integers from $$$1$$$ to $$$n$$$ in arbitrary order. For example, $$$[2,3,1,5,4]$$$ is a permutation, but $$$[1,2,2]$$$ is not a permutation ($$$2$$$ appears twice in the array) and $$$[1,3,4]$$$ is also not a permutation ($$$n=3$$$ but there is $$$4$$$ in the array).InputEach test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows.The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le k \le n \le 100$$$).The second line of each test case contains $$$n$$$ integers $$$p_1,p_2,\ldots,p_n$$$ ($$$1 \le p_i \le n$$$). It is guaranteed that the given numbers form a permutation of length $$$n$$$.OutputFor each test case print one integer — the minimum number of operations needed to make the sum $$$p_1 + p_2 + \ldots + p_k$$$ as small as possible.ExampleInput 43 12 3 13 31 2 34 23 4 1 21 11Output 1 0 2 0 NoteIn the first test case, the value of $$$p_1 + p_2 + \ldots + p_k$$$ is initially equal to $$$2$$$, but the smallest possible value is $$$1$$$. You can achieve it by swapping $$$p_1$$$ with $$$p_3$$$, resulting in the permutation $$$[1, 3, 2]$$$.In the second test case, the sum is already as small as possible, so the answer is $$$0$$$.
cf
T = int(input()) for t in range(1, T+1): n, k = list(map(int, input().split())) a = list(map(int, input().split())) mp = {} for i in range(n): mp[a[i]] = i cnt = 0 for i in range(k): if a[i] > k: cnt += 1 print(cnt)
python
13
0.415493
42
10.833333
24
A. Wonderful Permutationtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputGod's Blessing on This PermutationForces!A Random PebbleYou are given a permutation $$$p_1,p_2,\ldots,p_n$$$ of length $$$n$$$ and a positive integer $$$k \le n$$$. In one operation you can choose two indices $$$i$$$ and $$$j$$$ ($$$1 \le i < j \le n$$$) and swap $$$p_i$$$ with $$$p_j$$$.Find the minimum number of operations needed to make the sum $$$p_1 + p_2 + \ldots + p_k$$$ as small as possible.A permutation is an array consisting of $$$n$$$ distinct integers from $$$1$$$ to $$$n$$$ in arbitrary order. For example, $$$[2,3,1,5,4]$$$ is a permutation, but $$$[1,2,2]$$$ is not a permutation ($$$2$$$ appears twice in the array) and $$$[1,3,4]$$$ is also not a permutation ($$$n=3$$$ but there is $$$4$$$ in the array).InputEach test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows.The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le k \le n \le 100$$$).The second line of each test case contains $$$n$$$ integers $$$p_1,p_2,\ldots,p_n$$$ ($$$1 \le p_i \le n$$$). It is guaranteed that the given numbers form a permutation of length $$$n$$$.OutputFor each test case print one integer — the minimum number of operations needed to make the sum $$$p_1 + p_2 + \ldots + p_k$$$ as small as possible.ExampleInput 43 12 3 13 31 2 34 23 4 1 21 11Output 1 0 2 0 NoteIn the first test case, the value of $$$p_1 + p_2 + \ldots + p_k$$$ is initially equal to $$$2$$$, but the smallest possible value is $$$1$$$. You can achieve it by swapping $$$p_1$$$ with $$$p_3$$$, resulting in the permutation $$$[1, 3, 2]$$$.In the second test case, the sum is already as small as possible, so the answer is $$$0$$$.
cf
# slow one # t = int(input()) import sys # for _ in range(t): # n, k = [int(i) for i in input().split()] # a = [int(i) for i in input().split()] # req = 0 # if n == k: # print(0) # continue # for i in range(k): # if a[i] > k: # req += 1 # print(req) # fast one def solve(): inp = sys.stdin.readline() n, k = map(int, inp.split()) inp = sys.stdin.readline() a = list(map(int, inp.split())) r = 0 if n == k: print(0) else: for i in range(k): if a[i] > k: r += 1 print(r) def main(): for i in range(int(sys.stdin.readline())): solve() if __name__ == '__main__': main()
python
12
0.39924
46
7.21875
96
A. Wonderful Permutationtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputGod's Blessing on This PermutationForces!A Random PebbleYou are given a permutation $$$p_1,p_2,\ldots,p_n$$$ of length $$$n$$$ and a positive integer $$$k \le n$$$. In one operation you can choose two indices $$$i$$$ and $$$j$$$ ($$$1 \le i < j \le n$$$) and swap $$$p_i$$$ with $$$p_j$$$.Find the minimum number of operations needed to make the sum $$$p_1 + p_2 + \ldots + p_k$$$ as small as possible.A permutation is an array consisting of $$$n$$$ distinct integers from $$$1$$$ to $$$n$$$ in arbitrary order. For example, $$$[2,3,1,5,4]$$$ is a permutation, but $$$[1,2,2]$$$ is not a permutation ($$$2$$$ appears twice in the array) and $$$[1,3,4]$$$ is also not a permutation ($$$n=3$$$ but there is $$$4$$$ in the array).InputEach test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows.The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le k \le n \le 100$$$).The second line of each test case contains $$$n$$$ integers $$$p_1,p_2,\ldots,p_n$$$ ($$$1 \le p_i \le n$$$). It is guaranteed that the given numbers form a permutation of length $$$n$$$.OutputFor each test case print one integer — the minimum number of operations needed to make the sum $$$p_1 + p_2 + \ldots + p_k$$$ as small as possible.ExampleInput 43 12 3 13 31 2 34 23 4 1 21 11Output 1 0 2 0 NoteIn the first test case, the value of $$$p_1 + p_2 + \ldots + p_k$$$ is initially equal to $$$2$$$, but the smallest possible value is $$$1$$$. You can achieve it by swapping $$$p_1$$$ with $$$p_3$$$, resulting in the permutation $$$[1, 3, 2]$$$.In the second test case, the sum is already as small as possible, so the answer is $$$0$$$.
cf
t=int(input()) for i in range(t): g=set() n,k=map(int,input().split()) a=(list(map(int,input().split()))) b=sorted(a) m=0 for j in range(k): g.add(a[j]) for j in range(k): if b[j] not in g: m+=1 print(m)
python
14
0.443636
38
10.04
25
A. Wonderful Permutationtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputGod's Blessing on This PermutationForces!A Random PebbleYou are given a permutation $$$p_1,p_2,\ldots,p_n$$$ of length $$$n$$$ and a positive integer $$$k \le n$$$. In one operation you can choose two indices $$$i$$$ and $$$j$$$ ($$$1 \le i < j \le n$$$) and swap $$$p_i$$$ with $$$p_j$$$.Find the minimum number of operations needed to make the sum $$$p_1 + p_2 + \ldots + p_k$$$ as small as possible.A permutation is an array consisting of $$$n$$$ distinct integers from $$$1$$$ to $$$n$$$ in arbitrary order. For example, $$$[2,3,1,5,4]$$$ is a permutation, but $$$[1,2,2]$$$ is not a permutation ($$$2$$$ appears twice in the array) and $$$[1,3,4]$$$ is also not a permutation ($$$n=3$$$ but there is $$$4$$$ in the array).InputEach test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows.The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le k \le n \le 100$$$).The second line of each test case contains $$$n$$$ integers $$$p_1,p_2,\ldots,p_n$$$ ($$$1 \le p_i \le n$$$). It is guaranteed that the given numbers form a permutation of length $$$n$$$.OutputFor each test case print one integer — the minimum number of operations needed to make the sum $$$p_1 + p_2 + \ldots + p_k$$$ as small as possible.ExampleInput 43 12 3 13 31 2 34 23 4 1 21 11Output 1 0 2 0 NoteIn the first test case, the value of $$$p_1 + p_2 + \ldots + p_k$$$ is initially equal to $$$2$$$, but the smallest possible value is $$$1$$$. You can achieve it by swapping $$$p_1$$$ with $$$p_3$$$, resulting in the permutation $$$[1, 3, 2]$$$.In the second test case, the sum is already as small as possible, so the answer is $$$0$$$.
cf
import java.util.*; public class cf813A { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int t=sc.nextInt(); while(t-->0){ int n=sc.nextInt(); int k=sc.nextInt(); int arr[]=new int[n]; for(int i=0;i<n;i++){ arr[i]=sc.nextInt(); } int count=0; for(int i=0;i<k;i++){ int min=Integer.MAX_VALUE; int ind=0; for(int j=0;j<arr.length;j++){ if(min>arr[j] && arr[j]!=0){ min=arr[j]; ind=j; } } arr[ind]=0; if(ind+1>k){ count++; } } System.out.println(count); } } }
java
16
0.331873
48
13.265625
64
A. Wonderful Permutationtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputGod's Blessing on This PermutationForces!A Random PebbleYou are given a permutation $$$p_1,p_2,\ldots,p_n$$$ of length $$$n$$$ and a positive integer $$$k \le n$$$. In one operation you can choose two indices $$$i$$$ and $$$j$$$ ($$$1 \le i < j \le n$$$) and swap $$$p_i$$$ with $$$p_j$$$.Find the minimum number of operations needed to make the sum $$$p_1 + p_2 + \ldots + p_k$$$ as small as possible.A permutation is an array consisting of $$$n$$$ distinct integers from $$$1$$$ to $$$n$$$ in arbitrary order. For example, $$$[2,3,1,5,4]$$$ is a permutation, but $$$[1,2,2]$$$ is not a permutation ($$$2$$$ appears twice in the array) and $$$[1,3,4]$$$ is also not a permutation ($$$n=3$$$ but there is $$$4$$$ in the array).InputEach test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows.The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le k \le n \le 100$$$).The second line of each test case contains $$$n$$$ integers $$$p_1,p_2,\ldots,p_n$$$ ($$$1 \le p_i \le n$$$). It is guaranteed that the given numbers form a permutation of length $$$n$$$.OutputFor each test case print one integer — the minimum number of operations needed to make the sum $$$p_1 + p_2 + \ldots + p_k$$$ as small as possible.ExampleInput 43 12 3 13 31 2 34 23 4 1 21 11Output 1 0 2 0 NoteIn the first test case, the value of $$$p_1 + p_2 + \ldots + p_k$$$ is initially equal to $$$2$$$, but the smallest possible value is $$$1$$$. You can achieve it by swapping $$$p_1$$$ with $$$p_3$$$, resulting in the permutation $$$[1, 3, 2]$$$.In the second test case, the sum is already as small as possible, so the answer is $$$0$$$.
cf
tst=int(input()) for i in range(tst): num,k=map(int,input().split()) arr=list(map(int,input().split())) c=0 if num==k: c=0 else: for j in range(1,k+1): if arr[j-1]!=j and arr[j-1]>k: c=c+1 print(c)
python
13
0.432624
42
10.32
25
A. Wonderful Permutationtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputGod's Blessing on This PermutationForces!A Random PebbleYou are given a permutation $$$p_1,p_2,\ldots,p_n$$$ of length $$$n$$$ and a positive integer $$$k \le n$$$. In one operation you can choose two indices $$$i$$$ and $$$j$$$ ($$$1 \le i < j \le n$$$) and swap $$$p_i$$$ with $$$p_j$$$.Find the minimum number of operations needed to make the sum $$$p_1 + p_2 + \ldots + p_k$$$ as small as possible.A permutation is an array consisting of $$$n$$$ distinct integers from $$$1$$$ to $$$n$$$ in arbitrary order. For example, $$$[2,3,1,5,4]$$$ is a permutation, but $$$[1,2,2]$$$ is not a permutation ($$$2$$$ appears twice in the array) and $$$[1,3,4]$$$ is also not a permutation ($$$n=3$$$ but there is $$$4$$$ in the array).InputEach test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows.The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le k \le n \le 100$$$).The second line of each test case contains $$$n$$$ integers $$$p_1,p_2,\ldots,p_n$$$ ($$$1 \le p_i \le n$$$). It is guaranteed that the given numbers form a permutation of length $$$n$$$.OutputFor each test case print one integer — the minimum number of operations needed to make the sum $$$p_1 + p_2 + \ldots + p_k$$$ as small as possible.ExampleInput 43 12 3 13 31 2 34 23 4 1 21 11Output 1 0 2 0 NoteIn the first test case, the value of $$$p_1 + p_2 + \ldots + p_k$$$ is initially equal to $$$2$$$, but the smallest possible value is $$$1$$$. You can achieve it by swapping $$$p_1$$$ with $$$p_3$$$, resulting in the permutation $$$[1, 3, 2]$$$.In the second test case, the sum is already as small as possible, so the answer is $$$0$$$.
cf
def main(): t = int(input()) for i in range(t): n, k= map(int, input().split()) p = list(map(int, input().split())) counter = 0 for j in range(k): if p[j] <= k: counter += 1 print(k - counter) main()
python
15
0.391304
43
12.043478
23
A. Wonderful Permutationtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputGod's Blessing on This PermutationForces!A Random PebbleYou are given a permutation $$$p_1,p_2,\ldots,p_n$$$ of length $$$n$$$ and a positive integer $$$k \le n$$$. In one operation you can choose two indices $$$i$$$ and $$$j$$$ ($$$1 \le i < j \le n$$$) and swap $$$p_i$$$ with $$$p_j$$$.Find the minimum number of operations needed to make the sum $$$p_1 + p_2 + \ldots + p_k$$$ as small as possible.A permutation is an array consisting of $$$n$$$ distinct integers from $$$1$$$ to $$$n$$$ in arbitrary order. For example, $$$[2,3,1,5,4]$$$ is a permutation, but $$$[1,2,2]$$$ is not a permutation ($$$2$$$ appears twice in the array) and $$$[1,3,4]$$$ is also not a permutation ($$$n=3$$$ but there is $$$4$$$ in the array).InputEach test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows.The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le k \le n \le 100$$$).The second line of each test case contains $$$n$$$ integers $$$p_1,p_2,\ldots,p_n$$$ ($$$1 \le p_i \le n$$$). It is guaranteed that the given numbers form a permutation of length $$$n$$$.OutputFor each test case print one integer — the minimum number of operations needed to make the sum $$$p_1 + p_2 + \ldots + p_k$$$ as small as possible.ExampleInput 43 12 3 13 31 2 34 23 4 1 21 11Output 1 0 2 0 NoteIn the first test case, the value of $$$p_1 + p_2 + \ldots + p_k$$$ is initially equal to $$$2$$$, but the smallest possible value is $$$1$$$. You can achieve it by swapping $$$p_1$$$ with $$$p_3$$$, resulting in the permutation $$$[1, 3, 2]$$$.In the second test case, the sum is already as small as possible, so the answer is $$$0$$$.
cf
import java.util.*; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Main { public static void main(String[] args) { try { FastScanner sc = new FastScanner(); int T = Integer.parseInt(sc.next()); while (T-- > 0) { int n = Integer.parseInt(sc.next()); int k = Integer.parseInt(sc.next()); int arr[] = sc.readArray(n); boolean new_arr[] = new boolean[n+1]; Arrays.fill(new_arr, false); int ans = 0; for(int i=0;i<k;i++){ new_arr[arr[i]] = true; } for(int i=1;i<=k;i++){ if(new_arr[i] == false) ans++; } System.out.println(ans); } } catch (Exception e) { // } } static void printArr(int arr[]) { for (int i = 0; i < arr.length; i++) { System.out.print(arr[i] + " "); } System.out.println(); } static class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } long[] readLongArray(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } long nextLong() { return Long.parseLong(next()); } } }
java
16
0.417323
81
13.693878
147
A. Wonderful Permutationtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputGod's Blessing on This PermutationForces!A Random PebbleYou are given a permutation $$$p_1,p_2,\ldots,p_n$$$ of length $$$n$$$ and a positive integer $$$k \le n$$$. In one operation you can choose two indices $$$i$$$ and $$$j$$$ ($$$1 \le i < j \le n$$$) and swap $$$p_i$$$ with $$$p_j$$$.Find the minimum number of operations needed to make the sum $$$p_1 + p_2 + \ldots + p_k$$$ as small as possible.A permutation is an array consisting of $$$n$$$ distinct integers from $$$1$$$ to $$$n$$$ in arbitrary order. For example, $$$[2,3,1,5,4]$$$ is a permutation, but $$$[1,2,2]$$$ is not a permutation ($$$2$$$ appears twice in the array) and $$$[1,3,4]$$$ is also not a permutation ($$$n=3$$$ but there is $$$4$$$ in the array).InputEach test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows.The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le k \le n \le 100$$$).The second line of each test case contains $$$n$$$ integers $$$p_1,p_2,\ldots,p_n$$$ ($$$1 \le p_i \le n$$$). It is guaranteed that the given numbers form a permutation of length $$$n$$$.OutputFor each test case print one integer — the minimum number of operations needed to make the sum $$$p_1 + p_2 + \ldots + p_k$$$ as small as possible.ExampleInput 43 12 3 13 31 2 34 23 4 1 21 11Output 1 0 2 0 NoteIn the first test case, the value of $$$p_1 + p_2 + \ldots + p_k$$$ is initially equal to $$$2$$$, but the smallest possible value is $$$1$$$. You can achieve it by swapping $$$p_1$$$ with $$$p_3$$$, resulting in the permutation $$$[1, 3, 2]$$$.In the second test case, the sum is already as small as possible, so the answer is $$$0$$$.
cf
def solve(n,k,p): res = 0 s = set(p[:k]) for i in range(1,k+1): if i not in s: res +=1 return res t = int(input()) for i in range(t): n,k = map(int,input().split()) p =[int(x) for x in input().split()] print(solve(n,k,p))
python
11
0.439189
40
6.789474
38
A. Wonderful Permutationtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputGod's Blessing on This PermutationForces!A Random PebbleYou are given a permutation $$$p_1,p_2,\ldots,p_n$$$ of length $$$n$$$ and a positive integer $$$k \le n$$$. In one operation you can choose two indices $$$i$$$ and $$$j$$$ ($$$1 \le i < j \le n$$$) and swap $$$p_i$$$ with $$$p_j$$$.Find the minimum number of operations needed to make the sum $$$p_1 + p_2 + \ldots + p_k$$$ as small as possible.A permutation is an array consisting of $$$n$$$ distinct integers from $$$1$$$ to $$$n$$$ in arbitrary order. For example, $$$[2,3,1,5,4]$$$ is a permutation, but $$$[1,2,2]$$$ is not a permutation ($$$2$$$ appears twice in the array) and $$$[1,3,4]$$$ is also not a permutation ($$$n=3$$$ but there is $$$4$$$ in the array).InputEach test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows.The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le k \le n \le 100$$$).The second line of each test case contains $$$n$$$ integers $$$p_1,p_2,\ldots,p_n$$$ ($$$1 \le p_i \le n$$$). It is guaranteed that the given numbers form a permutation of length $$$n$$$.OutputFor each test case print one integer — the minimum number of operations needed to make the sum $$$p_1 + p_2 + \ldots + p_k$$$ as small as possible.ExampleInput 43 12 3 13 31 2 34 23 4 1 21 11Output 1 0 2 0 NoteIn the first test case, the value of $$$p_1 + p_2 + \ldots + p_k$$$ is initially equal to $$$2$$$, but the smallest possible value is $$$1$$$. You can achieve it by swapping $$$p_1$$$ with $$$p_3$$$, resulting in the permutation $$$[1, 3, 2]$$$.In the second test case, the sum is already as small as possible, so the answer is $$$0$$$.
cf
/*package whatever //do not write package name here */ import java.util.Scanner; import java.util.Arrays; public class code{ public static boolean is_there(int[] arr , int k1){ for(int i = 0;i<arr.length;i++){ if(arr[i]==k1){ return true; } } return false; } public static int[] copy(int[] arr){ int n1 = arr.length; int[] arr1 = new int[n1]; for(int i = 0;i<n1;i++){ arr1[i] = arr[i]; } return arr1; } public static void main(String args[]){ Scanner s = new Scanner(System.in); int t = s.nextInt(); while(t-->0){ int n = s.nextInt(); int k = s.nextInt(); int[] arr = new int[n]; for(int i = 0;i<n;i++){ arr[i] = s.nextInt(); } int[] arr1 = copy(arr); Arrays.sort(arr1); int[] arr2 = new int[k]; for(int t1 = 0;t1<k;t1++){ arr2[t1] = arr1[t1]; } if(n==k){ System.out.println(0); }else{ int count = 0; for(int j = 0;j<k;j++){ if(is_there(arr2,arr[j])==false){ count++; } } System.out.println(count); } } } }
java
17
0.384668
55
13.475248
101
A. Wonderful Permutationtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputGod's Blessing on This PermutationForces!A Random PebbleYou are given a permutation $$$p_1,p_2,\ldots,p_n$$$ of length $$$n$$$ and a positive integer $$$k \le n$$$. In one operation you can choose two indices $$$i$$$ and $$$j$$$ ($$$1 \le i < j \le n$$$) and swap $$$p_i$$$ with $$$p_j$$$.Find the minimum number of operations needed to make the sum $$$p_1 + p_2 + \ldots + p_k$$$ as small as possible.A permutation is an array consisting of $$$n$$$ distinct integers from $$$1$$$ to $$$n$$$ in arbitrary order. For example, $$$[2,3,1,5,4]$$$ is a permutation, but $$$[1,2,2]$$$ is not a permutation ($$$2$$$ appears twice in the array) and $$$[1,3,4]$$$ is also not a permutation ($$$n=3$$$ but there is $$$4$$$ in the array).InputEach test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows.The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le k \le n \le 100$$$).The second line of each test case contains $$$n$$$ integers $$$p_1,p_2,\ldots,p_n$$$ ($$$1 \le p_i \le n$$$). It is guaranteed that the given numbers form a permutation of length $$$n$$$.OutputFor each test case print one integer — the minimum number of operations needed to make the sum $$$p_1 + p_2 + \ldots + p_k$$$ as small as possible.ExampleInput 43 12 3 13 31 2 34 23 4 1 21 11Output 1 0 2 0 NoteIn the first test case, the value of $$$p_1 + p_2 + \ldots + p_k$$$ is initially equal to $$$2$$$, but the smallest possible value is $$$1$$$. You can achieve it by swapping $$$p_1$$$ with $$$p_3$$$, resulting in the permutation $$$[1, 3, 2]$$$.In the second test case, the sum is already as small as possible, so the answer is $$$0$$$.
cf
#include<bits/stdc++.h> using namespace std; const int maxn = 2e5 + 10; int a[maxn]; void solve() { int n, k,cnt=0; cin >> n >> k; for(int i=0;i<n;i++) cin >> a[i]; for(int i=0;i<k;i++) if (a[i] > k)cnt++; printf("%d\n", cnt); } int main() { int t; cin >> t; while (t--)solve(); }
c++
9
0.491961
26
7
39
A. Wonderful Permutationtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputGod's Blessing on This PermutationForces!A Random PebbleYou are given a permutation $$$p_1,p_2,\ldots,p_n$$$ of length $$$n$$$ and a positive integer $$$k \le n$$$. In one operation you can choose two indices $$$i$$$ and $$$j$$$ ($$$1 \le i < j \le n$$$) and swap $$$p_i$$$ with $$$p_j$$$.Find the minimum number of operations needed to make the sum $$$p_1 + p_2 + \ldots + p_k$$$ as small as possible.A permutation is an array consisting of $$$n$$$ distinct integers from $$$1$$$ to $$$n$$$ in arbitrary order. For example, $$$[2,3,1,5,4]$$$ is a permutation, but $$$[1,2,2]$$$ is not a permutation ($$$2$$$ appears twice in the array) and $$$[1,3,4]$$$ is also not a permutation ($$$n=3$$$ but there is $$$4$$$ in the array).InputEach test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows.The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le k \le n \le 100$$$).The second line of each test case contains $$$n$$$ integers $$$p_1,p_2,\ldots,p_n$$$ ($$$1 \le p_i \le n$$$). It is guaranteed that the given numbers form a permutation of length $$$n$$$.OutputFor each test case print one integer — the minimum number of operations needed to make the sum $$$p_1 + p_2 + \ldots + p_k$$$ as small as possible.ExampleInput 43 12 3 13 31 2 34 23 4 1 21 11Output 1 0 2 0 NoteIn the first test case, the value of $$$p_1 + p_2 + \ldots + p_k$$$ is initially equal to $$$2$$$, but the smallest possible value is $$$1$$$. You can achieve it by swapping $$$p_1$$$ with $$$p_3$$$, resulting in the permutation $$$[1, 3, 2]$$$.In the second test case, the sum is already as small as possible, so the answer is $$$0$$$.
cf
import java.util.*; public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); int test = input.nextInt(); while(test > 0) { int n = input.nextInt(); int k = input.nextInt(); int[] a = new int[n]; for(int i = 0; i < n; i++) { a[i] = input.nextInt(); } int cnt = 0; for(int i = 0; i < k; i++) { if(a[i] > k) cnt++; } System.out.println(cnt); test--; } } }
java
13
0.421147
45
11.422222
45
A. Wonderful Permutationtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputGod's Blessing on This PermutationForces!A Random PebbleYou are given a permutation $$$p_1,p_2,\ldots,p_n$$$ of length $$$n$$$ and a positive integer $$$k \le n$$$. In one operation you can choose two indices $$$i$$$ and $$$j$$$ ($$$1 \le i < j \le n$$$) and swap $$$p_i$$$ with $$$p_j$$$.Find the minimum number of operations needed to make the sum $$$p_1 + p_2 + \ldots + p_k$$$ as small as possible.A permutation is an array consisting of $$$n$$$ distinct integers from $$$1$$$ to $$$n$$$ in arbitrary order. For example, $$$[2,3,1,5,4]$$$ is a permutation, but $$$[1,2,2]$$$ is not a permutation ($$$2$$$ appears twice in the array) and $$$[1,3,4]$$$ is also not a permutation ($$$n=3$$$ but there is $$$4$$$ in the array).InputEach test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows.The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le k \le n \le 100$$$).The second line of each test case contains $$$n$$$ integers $$$p_1,p_2,\ldots,p_n$$$ ($$$1 \le p_i \le n$$$). It is guaranteed that the given numbers form a permutation of length $$$n$$$.OutputFor each test case print one integer — the minimum number of operations needed to make the sum $$$p_1 + p_2 + \ldots + p_k$$$ as small as possible.ExampleInput 43 12 3 13 31 2 34 23 4 1 21 11Output 1 0 2 0 NoteIn the first test case, the value of $$$p_1 + p_2 + \ldots + p_k$$$ is initially equal to $$$2$$$, but the smallest possible value is $$$1$$$. You can achieve it by swapping $$$p_1$$$ with $$$p_3$$$, resulting in the permutation $$$[1, 3, 2]$$$.In the second test case, the sum is already as small as possible, so the answer is $$$0$$$.
cf
import java.io.*; import java.util.*; public class R813A { public static void main(String[] args) { JS scan = new JS(); int cases = scan.nextInt(); while(cases-->0){ int n = scan.nextInt(), k = scan.nextInt(); int[] arr = new int[n]; int[] pos = new int[n]; int ans = 0; for(int i =0;i<n;i++){ arr[i] = scan.nextInt()-1; //where is this number in the array? pos[arr[i]] = i; if(arr[i]>=k)continue; if(i>=k)ans++; } System.out.println(ans); } } static class JS { public int BS = 1 << 16; public char NC = (char) 0; byte[] buf = new byte[BS]; int bId = 0, size = 0; char c = NC; double num = 1; BufferedInputStream in; public JS() { in = new BufferedInputStream(System.in, BS); } public JS(String s) throws FileNotFoundException { in = new BufferedInputStream(new FileInputStream(new File(s)), BS); } public char nextChar() { while (bId == size) { try { size = in.read(buf); } catch (Exception e) { return NC; } if (size == -1) return NC; bId = 0; } return (char) buf[bId++]; } public int nextInt() { return (int) nextLong(); } public long nextLong() { num = 1; boolean neg = false; if (c == NC) c = nextChar(); for (; (c < '0' || c > '9'); c = nextChar()) { if (c == '-') neg = true; } long res = 0; for (; c >= '0' && c <= '9'; c = nextChar()) { res = (res << 3) + (res << 1) + c - '0'; num *= 10; } return neg ? -res : res; } public double nextDouble() { double cur = nextLong(); return c != '.' ? cur : cur + nextLong() / num; } public String next() { StringBuilder res = new StringBuilder(); while (c <= 32) c = nextChar(); while (c > 32) { res.append(c); c = nextChar(); } return res.toString(); } public String nextLine() { StringBuilder res = new StringBuilder(); while (c <= 32) c = nextChar(); while (c != '\n') { res.append(c); c = nextChar(); } return res.toString(); } public boolean hasNext() { if (c > 32) return true; while (true) { c = nextChar(); if (c == NC) return false; else if (c > 32) return true; } } } }
java
16
0.376049
79
13.476636
214
A. Wonderful Permutationtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputGod's Blessing on This PermutationForces!A Random PebbleYou are given a permutation $$$p_1,p_2,\ldots,p_n$$$ of length $$$n$$$ and a positive integer $$$k \le n$$$. In one operation you can choose two indices $$$i$$$ and $$$j$$$ ($$$1 \le i < j \le n$$$) and swap $$$p_i$$$ with $$$p_j$$$.Find the minimum number of operations needed to make the sum $$$p_1 + p_2 + \ldots + p_k$$$ as small as possible.A permutation is an array consisting of $$$n$$$ distinct integers from $$$1$$$ to $$$n$$$ in arbitrary order. For example, $$$[2,3,1,5,4]$$$ is a permutation, but $$$[1,2,2]$$$ is not a permutation ($$$2$$$ appears twice in the array) and $$$[1,3,4]$$$ is also not a permutation ($$$n=3$$$ but there is $$$4$$$ in the array).InputEach test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows.The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le k \le n \le 100$$$).The second line of each test case contains $$$n$$$ integers $$$p_1,p_2,\ldots,p_n$$$ ($$$1 \le p_i \le n$$$). It is guaranteed that the given numbers form a permutation of length $$$n$$$.OutputFor each test case print one integer — the minimum number of operations needed to make the sum $$$p_1 + p_2 + \ldots + p_k$$$ as small as possible.ExampleInput 43 12 3 13 31 2 34 23 4 1 21 11Output 1 0 2 0 NoteIn the first test case, the value of $$$p_1 + p_2 + \ldots + p_k$$$ is initially equal to $$$2$$$, but the smallest possible value is $$$1$$$. You can achieve it by swapping $$$p_1$$$ with $$$p_3$$$, resulting in the permutation $$$[1, 3, 2]$$$.In the second test case, the sum is already as small as possible, so the answer is $$$0$$$.
cf
#include<bits/stdc++.h> using namespace std; //#typedef ios_base::sync_with_stdio(false);cin.tie(NULL); Fast I/O typedef long long ll; #define forf(i,a,n) for(ll i = a; i < n; i++) #define forb(i,a,n) for(ll i = a; i >= n; i--) #define pb push_back #define all(v) (v).begin(),(v).end() #define mp make_pair #define f first #define s second #define endl '\n' const ll prime = 1000000007; int main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); ll t=1; cin>>t; while(t--){ ll n,x,y,z,k,l,m,r; string str,str1,str2; cin>>n>>k; vector<ll>v; for (ll i=0;i<n;i++){ cin>>x; v.pb(x); } ll swapcount=0; for (ll i=0;i < k;i++){ if (v[i] > k){ swapcount++; } } cout<<swapcount<<endl; } return 0; } /*BT Waale Edge Cases All Elements are equal, Also remember that in this case maximum of the array == minimum of the array Some Elements are equal Case of single input No Input Are elements in some sorted order? Are all elements +ve? Is negative number a possibility? Lookout for if elements are real numbers or integers Lookout for if all numbers are given to be distinct What if the new variable you have created, eg counter or something else...it remains zero (basically unmodified) You always tend to write code assuming that the new variable created will be always modified...! */
c++
12
0.624476
112
13.752577
97
A. Wonderful Permutationtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputGod's Blessing on This PermutationForces!A Random PebbleYou are given a permutation $$$p_1,p_2,\ldots,p_n$$$ of length $$$n$$$ and a positive integer $$$k \le n$$$. In one operation you can choose two indices $$$i$$$ and $$$j$$$ ($$$1 \le i < j \le n$$$) and swap $$$p_i$$$ with $$$p_j$$$.Find the minimum number of operations needed to make the sum $$$p_1 + p_2 + \ldots + p_k$$$ as small as possible.A permutation is an array consisting of $$$n$$$ distinct integers from $$$1$$$ to $$$n$$$ in arbitrary order. For example, $$$[2,3,1,5,4]$$$ is a permutation, but $$$[1,2,2]$$$ is not a permutation ($$$2$$$ appears twice in the array) and $$$[1,3,4]$$$ is also not a permutation ($$$n=3$$$ but there is $$$4$$$ in the array).InputEach test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows.The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le k \le n \le 100$$$).The second line of each test case contains $$$n$$$ integers $$$p_1,p_2,\ldots,p_n$$$ ($$$1 \le p_i \le n$$$). It is guaranteed that the given numbers form a permutation of length $$$n$$$.OutputFor each test case print one integer — the minimum number of operations needed to make the sum $$$p_1 + p_2 + \ldots + p_k$$$ as small as possible.ExampleInput 43 12 3 13 31 2 34 23 4 1 21 11Output 1 0 2 0 NoteIn the first test case, the value of $$$p_1 + p_2 + \ldots + p_k$$$ is initially equal to $$$2$$$, but the smallest possible value is $$$1$$$. You can achieve it by swapping $$$p_1$$$ with $$$p_3$$$, resulting in the permutation $$$[1, 3, 2]$$$.In the second test case, the sum is already as small as possible, so the answer is $$$0$$$.
cf
import java.util.*; public class sortZero { public static void main(String[] swami){ Scanner sc=new Scanner(System.in); int t=sc.nextInt(); while(t-->0){ int n=sc.nextInt(); int[] a=new int[n]; a[0]=sc.nextInt(); boolean issorted=true; HashMap<Integer,Integer> map=new HashMap<Integer,Integer>(); map.put(a[0],1); for(int i=1;i<n;i++){ a[i]=sc.nextInt(); if(a[i]<a[i-1]){ issorted=false; } if(!map.containsKey(a[i])){ map.put(a[i],1); } else{ map.put(a[i],map.get(a[i])+1); } } if(issorted){ System.out.println(0); continue; } int ct=0; for(int i=n-1;i>0;i--){ int j=i; int count=0; while(j>-1 && a[i]==a[j]){ count++; j--; } if(map.get(a[i])==count){ ct++; } else{ break; } if(a[i]<a[j]){ break; } i=j+1; } System.out.println(map.size()-ct); } } }
java
18
0.319695
72
24.75
56
C. Sort Zerotime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputAn array is sorted if it has no inversionsA Young BoyYou are given an array of $$$n$$$ positive integers $$$a_1,a_2,\ldots,a_n$$$. In one operation you do the following: Choose any integer $$$x$$$. For all $$$i$$$ such that $$$a_i = x$$$, do $$$a_i := 0$$$ (assign $$$0$$$ to $$$a_i$$$). Find the minimum number of operations required to sort the array in non-decreasing order.InputEach test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows.The first line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$).The second line of each test case contains $$$n$$$ positive integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_i \le n$$$).It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.OutputFor each test case print one integer — the minimum number of operations required to sort the array in non-decreasing order.ExampleInput 533 3 241 3 1 354 1 5 3 242 4 1 211Output 1 2 4 3 0 NoteIn the first test case, you can choose $$$x = 3$$$ for the operation, the resulting array is $$$[0, 0, 2]$$$.In the second test case, you can choose $$$x = 1$$$ for the first operation and $$$x = 3$$$ for the second operation, the resulting array is $$$[0, 0, 0, 0]$$$.
cf
#include<bits/stdc++.h> using namespace std; int t,n,a[100005]; int main(){ cin>>t; while(t--){ set<int>st; cin>>n;int ans=0; for(int i=1;i<=n;i++){ cin>>a[i]; if(i==1) continue; if(a[i]!=a[i-1]) st.insert(a[i-1]); if(a[i]<a[i-1]) ans=st.size(); if(st.count(a[i])) ans=st.size(); } cout<<ans<<"\n"; } return 0; }
c++
14
0.450633
24
6.764706
51
C. Sort Zerotime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputAn array is sorted if it has no inversionsA Young BoyYou are given an array of $$$n$$$ positive integers $$$a_1,a_2,\ldots,a_n$$$. In one operation you do the following: Choose any integer $$$x$$$. For all $$$i$$$ such that $$$a_i = x$$$, do $$$a_i := 0$$$ (assign $$$0$$$ to $$$a_i$$$). Find the minimum number of operations required to sort the array in non-decreasing order.InputEach test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows.The first line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$).The second line of each test case contains $$$n$$$ positive integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_i \le n$$$).It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.OutputFor each test case print one integer — the minimum number of operations required to sort the array in non-decreasing order.ExampleInput 533 3 241 3 1 354 1 5 3 242 4 1 211Output 1 2 4 3 0 NoteIn the first test case, you can choose $$$x = 3$$$ for the operation, the resulting array is $$$[0, 0, 2]$$$.In the second test case, you can choose $$$x = 1$$$ for the first operation and $$$x = 3$$$ for the second operation, the resulting array is $$$[0, 0, 0, 0]$$$.
cf
#include <cstdio> #include <vector> #include <set> int main(){ long t; scanf("%ld", &t); while(t--){ long n; scanf("%ld", &n); std::vector<long> a(n); for(long p = 0; p < n; p++){scanf("%ld", &a[p]);} std::set<long> s; long cur(a.back()); for(long p = n - 2; p >= 0; p--){ if(a[p] > cur){s.insert(a[p]);} else{cur = a[p];} } bool flag(false); for(long p = n - 1; p >= 0; p--){ if(flag){s.insert(a[p]);} else if(s.count(a[p])){flag = true;} } printf("%ld\n", s.size()); } }
c++
15
0.387247
81
11.150943
53
C. Sort Zerotime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputAn array is sorted if it has no inversionsA Young BoyYou are given an array of $$$n$$$ positive integers $$$a_1,a_2,\ldots,a_n$$$. In one operation you do the following: Choose any integer $$$x$$$. For all $$$i$$$ such that $$$a_i = x$$$, do $$$a_i := 0$$$ (assign $$$0$$$ to $$$a_i$$$). Find the minimum number of operations required to sort the array in non-decreasing order.InputEach test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows.The first line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$).The second line of each test case contains $$$n$$$ positive integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_i \le n$$$).It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.OutputFor each test case print one integer — the minimum number of operations required to sort the array in non-decreasing order.ExampleInput 533 3 241 3 1 354 1 5 3 242 4 1 211Output 1 2 4 3 0 NoteIn the first test case, you can choose $$$x = 3$$$ for the operation, the resulting array is $$$[0, 0, 2]$$$.In the second test case, you can choose $$$x = 1$$$ for the first operation and $$$x = 3$$$ for the second operation, the resulting array is $$$[0, 0, 0, 0]$$$.
cf
#include <bits/stdc++.h> using namespace std; #define pb push_back #define ub upper_bound #define lb lower_bound #define ll long long #define ld long double #define pii pair<ll, ll> typedef __int128 lll; typedef vector<ll> vi; typedef vector<vi> vvi; typedef vector<pii> vpi; typedef set<ll> si; typedef map<ll, ll> mii; #define all(value) value.begin(), value.end() const int M = 1e9 + 7; //.................................................................. void solve() { ll n; cin >> n; vi v(n); for (int i = 0; i < n; i++) { cin >> v[i]; } if(n==1) { cout<<"0"<<endl; return; } si s; ll ind = -1; for (int i = n - 2; i >= 0; i--) { if (v[i] > v[i + 1]) { ind = i; break; } } for (int i = 0; i <= ind; i++) { s.insert(v[i]); } // cout<<"ss"<<" "<<s.size()<<endl; bool check=false; for (int i = n-1; i >ind; i--) { ll curr=v[i]; if(check) { s.insert(v[i]); } if (s.count(curr) && check==false) { check =true; } } cout<<s.size()<<endl; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif ll t = 1; cin >> t; while (t--) { solve(); } }
c++
12
0.486943
68
7.194969
159
C. Sort Zerotime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputAn array is sorted if it has no inversionsA Young BoyYou are given an array of $$$n$$$ positive integers $$$a_1,a_2,\ldots,a_n$$$. In one operation you do the following: Choose any integer $$$x$$$. For all $$$i$$$ such that $$$a_i = x$$$, do $$$a_i := 0$$$ (assign $$$0$$$ to $$$a_i$$$). Find the minimum number of operations required to sort the array in non-decreasing order.InputEach test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows.The first line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$).The second line of each test case contains $$$n$$$ positive integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_i \le n$$$).It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.OutputFor each test case print one integer — the minimum number of operations required to sort the array in non-decreasing order.ExampleInput 533 3 241 3 1 354 1 5 3 242 4 1 211Output 1 2 4 3 0 NoteIn the first test case, you can choose $$$x = 3$$$ for the operation, the resulting array is $$$[0, 0, 2]$$$.In the second test case, you can choose $$$x = 1$$$ for the first operation and $$$x = 3$$$ for the second operation, the resulting array is $$$[0, 0, 0, 0]$$$.
cf
for _ in range(int(input().strip())): n=int(input().strip()) # blank=input().strip() # n,x,c=map(int,input().strip().split()) # s=(input().strip()) a=list(map(int,input().strip().split())) # s=list(input().strip()) l = len(a)-1 while l>0 and a[l-1] <= a[l]: l -= 1 see=set(a[:l]) id=l for i in range(n-1,l-1,-1): if a[i] in see: id=i break print(len(set(a[:id])))
python
15
0.447537
44
13.181818
33
C. Sort Zerotime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputAn array is sorted if it has no inversionsA Young BoyYou are given an array of $$$n$$$ positive integers $$$a_1,a_2,\ldots,a_n$$$. In one operation you do the following: Choose any integer $$$x$$$. For all $$$i$$$ such that $$$a_i = x$$$, do $$$a_i := 0$$$ (assign $$$0$$$ to $$$a_i$$$). Find the minimum number of operations required to sort the array in non-decreasing order.InputEach test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows.The first line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$).The second line of each test case contains $$$n$$$ positive integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_i \le n$$$).It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.OutputFor each test case print one integer — the minimum number of operations required to sort the array in non-decreasing order.ExampleInput 533 3 241 3 1 354 1 5 3 242 4 1 211Output 1 2 4 3 0 NoteIn the first test case, you can choose $$$x = 3$$$ for the operation, the resulting array is $$$[0, 0, 2]$$$.In the second test case, you can choose $$$x = 1$$$ for the first operation and $$$x = 3$$$ for the second operation, the resulting array is $$$[0, 0, 0, 0]$$$.
cf
import math for _ in range(int(input())): n = int(input()) arr = list(map(int,input().split())) if(arr==sorted(arr)): print(0) else: i = n-1 while(arr[i-1]<=arr[i]): i-=1 x = set(arr[:i]) r = -1 for i in range(n-1,-1,-1): if(arr[i] in x): r = i break x = set(arr[:r+1]) if(0 in x): print(len(x)-1) else: print(len(x))
python
14
0.360396
40
11.341463
41
C. Sort Zerotime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputAn array is sorted if it has no inversionsA Young BoyYou are given an array of $$$n$$$ positive integers $$$a_1,a_2,\ldots,a_n$$$. In one operation you do the following: Choose any integer $$$x$$$. For all $$$i$$$ such that $$$a_i = x$$$, do $$$a_i := 0$$$ (assign $$$0$$$ to $$$a_i$$$). Find the minimum number of operations required to sort the array in non-decreasing order.InputEach test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows.The first line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$).The second line of each test case contains $$$n$$$ positive integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_i \le n$$$).It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.OutputFor each test case print one integer — the minimum number of operations required to sort the array in non-decreasing order.ExampleInput 533 3 241 3 1 354 1 5 3 242 4 1 211Output 1 2 4 3 0 NoteIn the first test case, you can choose $$$x = 3$$$ for the operation, the resulting array is $$$[0, 0, 2]$$$.In the second test case, you can choose $$$x = 1$$$ for the first operation and $$$x = 3$$$ for the second operation, the resulting array is $$$[0, 0, 0, 0]$$$.
cf
#Khushal Sindhav #Indian Institute Of Technology, Jodhpur # 18 Dec 2022 def exe(): for i in range(n-1,0,-1): if lst[i]>=lst[i-1]: pass else: index=i break else: return 0 d={} for i in lst: d[i]=0 for i in range(index): d[lst[i]]=1 for i in range(index,n): if d[lst[i]]==1: index=i return len(set(lst[:index])) for _ in range(int(input())): n=int(input()) lst=list(map(int,input().split())) print(exe())
python
13
0.465487
40
10.098039
51
C. Sort Zerotime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputAn array is sorted if it has no inversionsA Young BoyYou are given an array of $$$n$$$ positive integers $$$a_1,a_2,\ldots,a_n$$$. In one operation you do the following: Choose any integer $$$x$$$. For all $$$i$$$ such that $$$a_i = x$$$, do $$$a_i := 0$$$ (assign $$$0$$$ to $$$a_i$$$). Find the minimum number of operations required to sort the array in non-decreasing order.InputEach test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows.The first line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$).The second line of each test case contains $$$n$$$ positive integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_i \le n$$$).It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.OutputFor each test case print one integer — the minimum number of operations required to sort the array in non-decreasing order.ExampleInput 533 3 241 3 1 354 1 5 3 242 4 1 211Output 1 2 4 3 0 NoteIn the first test case, you can choose $$$x = 3$$$ for the operation, the resulting array is $$$[0, 0, 2]$$$.In the second test case, you can choose $$$x = 1$$$ for the first operation and $$$x = 3$$$ for the second operation, the resulting array is $$$[0, 0, 0, 0]$$$.
cf
#include<bits/stdc++.h> using namespace std; #define ll long long #define all(x) (x).begin(),(x).end() #define ull unsigned long long #define ld long double #define pb push_back #define fi first #define se second #define umap unordered_map #define lb lower_bound #define ub upper_bound #define nl "\n" const ll N= 998244353; void solve(){ int n; cin>>n; int ans=0,pre=0; vector<int>a(n); map<int,int>mp; for(auto &x:a) cin>>x; for(int i=0;i<n;i++) mp[a[i]]=a[i]; for(int i=0;i<n-1;i++){ if(mp[a[i]]>mp[a[i+1]]){ int j=i; while(j>=pre) mp[a[j--]]=0; pre=i; } } for(auto x:mp) ans+=(x.se==0); cout<<ans; } int main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cout<<fixed<<setprecision(15); ll T=1; cin>>T; while(T--){ solve(); cout<<nl; } }
c++
15
0.533188
40
9.574713
87
C. Sort Zerotime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputAn array is sorted if it has no inversionsA Young BoyYou are given an array of $$$n$$$ positive integers $$$a_1,a_2,\ldots,a_n$$$. In one operation you do the following: Choose any integer $$$x$$$. For all $$$i$$$ such that $$$a_i = x$$$, do $$$a_i := 0$$$ (assign $$$0$$$ to $$$a_i$$$). Find the minimum number of operations required to sort the array in non-decreasing order.InputEach test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows.The first line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$).The second line of each test case contains $$$n$$$ positive integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_i \le n$$$).It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.OutputFor each test case print one integer — the minimum number of operations required to sort the array in non-decreasing order.ExampleInput 533 3 241 3 1 354 1 5 3 242 4 1 211Output 1 2 4 3 0 NoteIn the first test case, you can choose $$$x = 3$$$ for the operation, the resulting array is $$$[0, 0, 2]$$$.In the second test case, you can choose $$$x = 1$$$ for the first operation and $$$x = 3$$$ for the second operation, the resulting array is $$$[0, 0, 0, 0]$$$.
cf
import java.util.*; public class CF816{ static Scanner in = new Scanner(System.in); static void solve(){ int n = in.nextInt(); int[] a= new int[n + 1]; for(int i = 1; i <= n; i++){ a[i] = in.nextInt(); } int[] suffixMin = new int[n + 1]; suffixMin[n] = a[n]; for(int i = n - 1; i >= 1; i--){ suffixMin[i] = Math.min(suffixMin[i + 1], a[i]); } boolean[] erased = new boolean[n + 1]; int res = 0; for(int i = 1; i <= n; i++){ if(erased[a[i]]){ a[i] = 0; continue; } if(suffixMin[i] < a[i]){ erased[a[i]] = true; a[i] = 0; res++; } } boolean sorted = true; for(int i = 1; i <= n - 1; i++){ if(a[i] > a[i + 1]){ sorted = false; break; } } if(sorted){ System.out.println(res); } else{ Set<Integer> toDelete = new HashSet<>(); for(int i = n; i >= 1; i--){ if(a[i] == 0){ for(int j = i; j >= 1; j--){ toDelete.add(a[j]); } break; } } System.out.println(res + toDelete.size() - 1); } } public static void main(String[] args) { int t = in.nextInt(); while(t > 0){ solve(); t--; } } }
java
17
0.320261
60
12.691057
123
C. Sort Zerotime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputAn array is sorted if it has no inversionsA Young BoyYou are given an array of $$$n$$$ positive integers $$$a_1,a_2,\ldots,a_n$$$. In one operation you do the following: Choose any integer $$$x$$$. For all $$$i$$$ such that $$$a_i = x$$$, do $$$a_i := 0$$$ (assign $$$0$$$ to $$$a_i$$$). Find the minimum number of operations required to sort the array in non-decreasing order.InputEach test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows.The first line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$).The second line of each test case contains $$$n$$$ positive integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_i \le n$$$).It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.OutputFor each test case print one integer — the minimum number of operations required to sort the array in non-decreasing order.ExampleInput 533 3 241 3 1 354 1 5 3 242 4 1 211Output 1 2 4 3 0 NoteIn the first test case, you can choose $$$x = 3$$$ for the operation, the resulting array is $$$[0, 0, 2]$$$.In the second test case, you can choose $$$x = 1$$$ for the first operation and $$$x = 3$$$ for the second operation, the resulting array is $$$[0, 0, 0, 0]$$$.
cf
#include <bits/stdc++.h> using namespace std; #pragma GCC optimize("Ofast") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native") static const auto fast = []() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); return 0; } (); ; #define mod 1e9+7 #define imod ((int)mod) typedef long long ll; typedef unsigned long long ull; int main() { long long int t; cin>>t; for(int i=0;i<t;i++){ long long int n; cin>>n; int a[n]; int count=0; unordered_map<int,int> mp; for(int i=0;i<n;i++){ cin>>a[i]; } int ind; for(int j=n-1;j>0;j--){ if(a[j]<a[j-1]){ ind =j-1; for(int k=ind;k>=0;k--){ if(mp[a[k]]==0){ count++; } mp[a[k]]++; } break; } } for(int i=n-1;i>ind;i--){ if(mp[a[i]]!=0){ for(int k=i-1;k>ind;k--){ if(mp[a[k]]==0){ count++; } mp[a[k]]++; } break; } } cout<<count<<endl; } }
c++
18
0.415606
82
9.26087
115
C. Sort Zerotime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputAn array is sorted if it has no inversionsA Young BoyYou are given an array of $$$n$$$ positive integers $$$a_1,a_2,\ldots,a_n$$$. In one operation you do the following: Choose any integer $$$x$$$. For all $$$i$$$ such that $$$a_i = x$$$, do $$$a_i := 0$$$ (assign $$$0$$$ to $$$a_i$$$). Find the minimum number of operations required to sort the array in non-decreasing order.InputEach test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows.The first line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$).The second line of each test case contains $$$n$$$ positive integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_i \le n$$$).It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.OutputFor each test case print one integer — the minimum number of operations required to sort the array in non-decreasing order.ExampleInput 533 3 241 3 1 354 1 5 3 242 4 1 211Output 1 2 4 3 0 NoteIn the first test case, you can choose $$$x = 3$$$ for the operation, the resulting array is $$$[0, 0, 2]$$$.In the second test case, you can choose $$$x = 1$$$ for the first operation and $$$x = 3$$$ for the second operation, the resulting array is $$$[0, 0, 0, 0]$$$.
cf
N = int(input()) for _ in range(N): input() arr = list(map(int, input().split())) if len(arr) <= 1: print(0) continue seen = {arr[-1]: len(arr) - 1} ans = 0 invalid = False i = len(arr) - 2 while i >= 0: index = i if arr[i] > arr[i + 1]: invalid = True while i >= 0: if arr[i] in seen: index = max(index, seen[arr[i]]) i -= 1 else: if arr[i] not in seen: seen[arr[i]] = i i -= 1 dist = set() if invalid: for i in range(index + 1): dist.add(arr[i]) print(len(dist))
python
17
0.375516
52
10.555556
63
C. Sort Zerotime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputAn array is sorted if it has no inversionsA Young BoyYou are given an array of $$$n$$$ positive integers $$$a_1,a_2,\ldots,a_n$$$. In one operation you do the following: Choose any integer $$$x$$$. For all $$$i$$$ such that $$$a_i = x$$$, do $$$a_i := 0$$$ (assign $$$0$$$ to $$$a_i$$$). Find the minimum number of operations required to sort the array in non-decreasing order.InputEach test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows.The first line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$).The second line of each test case contains $$$n$$$ positive integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_i \le n$$$).It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.OutputFor each test case print one integer — the minimum number of operations required to sort the array in non-decreasing order.ExampleInput 533 3 241 3 1 354 1 5 3 242 4 1 211Output 1 2 4 3 0 NoteIn the first test case, you can choose $$$x = 3$$$ for the operation, the resulting array is $$$[0, 0, 2]$$$.In the second test case, you can choose $$$x = 1$$$ for the first operation and $$$x = 3$$$ for the second operation, the resulting array is $$$[0, 0, 0, 0]$$$.
cf
for i in range(int(input())): n=int(input()) p=list(map(int,input().split())) b=set() c=0 for j in range(1,len(p)): if p[j]<p[j-1] or p[j] in b: for k in p[c:j]: b.add(k) c=j print(len(b))
python
13
0.390071
36
12.47619
21
C. Sort Zerotime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputAn array is sorted if it has no inversionsA Young BoyYou are given an array of $$$n$$$ positive integers $$$a_1,a_2,\ldots,a_n$$$. In one operation you do the following: Choose any integer $$$x$$$. For all $$$i$$$ such that $$$a_i = x$$$, do $$$a_i := 0$$$ (assign $$$0$$$ to $$$a_i$$$). Find the minimum number of operations required to sort the array in non-decreasing order.InputEach test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows.The first line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$).The second line of each test case contains $$$n$$$ positive integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_i \le n$$$).It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.OutputFor each test case print one integer — the minimum number of operations required to sort the array in non-decreasing order.ExampleInput 533 3 241 3 1 354 1 5 3 242 4 1 211Output 1 2 4 3 0 NoteIn the first test case, you can choose $$$x = 3$$$ for the operation, the resulting array is $$$[0, 0, 2]$$$.In the second test case, you can choose $$$x = 1$$$ for the first operation and $$$x = 3$$$ for the second operation, the resulting array is $$$[0, 0, 0, 0]$$$.
cf
/*package whatever //do not write package name here */ import java.io.*; import java.net.ConnectException; import java.util.*; public class codeforces { static Scanner sc=new Scanner(System.in); static PrintWriter out=new PrintWriter(System.out); static void solve(){ int n=sc.nextInt(); int[] arr=new int[n]; for(int i=0;i<n;i++){ arr[i]=sc.nextInt(); } Set<Integer> set=new HashSet<>(); int previous_added=0; int last=arr[0]; for(int i=0;i<n;i++){ if(set.contains(arr[i])){ for(int j=i-1;j>=previous_added;j--){ set.add(arr[j]); } previous_added=i-1; }else if(arr[i]<last){ for(int j=i-1;j>=previous_added;j--){ set.add(arr[j]); } previous_added=i-1; } last=arr[i]; } System.out.println(set.size()); } public static void main(String[] args) { int t=sc.nextInt(); while(t-->0){ solve(); } } }
java
16
0.449117
55
13
85
C. Sort Zerotime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputAn array is sorted if it has no inversionsA Young BoyYou are given an array of $$$n$$$ positive integers $$$a_1,a_2,\ldots,a_n$$$. In one operation you do the following: Choose any integer $$$x$$$. For all $$$i$$$ such that $$$a_i = x$$$, do $$$a_i := 0$$$ (assign $$$0$$$ to $$$a_i$$$). Find the minimum number of operations required to sort the array in non-decreasing order.InputEach test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows.The first line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$).The second line of each test case contains $$$n$$$ positive integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_i \le n$$$).It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.OutputFor each test case print one integer — the minimum number of operations required to sort the array in non-decreasing order.ExampleInput 533 3 241 3 1 354 1 5 3 242 4 1 211Output 1 2 4 3 0 NoteIn the first test case, you can choose $$$x = 3$$$ for the operation, the resulting array is $$$[0, 0, 2]$$$.In the second test case, you can choose $$$x = 1$$$ for the first operation and $$$x = 3$$$ for the second operation, the resulting array is $$$[0, 0, 0, 0]$$$.
cf
#include<bits/stdc++.h> using namespace std; int a[100010],f[100010]; int main() { int t; cin>>t; while(t--) { int n,temp=-1; cin>>n; map<int,int> mp; for(int i=0;i<n;i++) { cin>>a[i]; f[a[i]]=0; mp[a[i]]=max(mp[a[i]],i); } if(n==1){cout<<0<<'\n';continue; } for(int i=n-1;i>=1;i--) { if(a[i]<a[i-1]) { temp=i-1;break; } } if(temp==-1){cout<<0<<'\n';continue; } int maxx=-1,ans=0; for(int i=0;i<=temp;i++) { maxx=max(maxx,mp[a[i]]); } for(int i=0;i<=maxx;i++) { if(a[i]&&f[a[i]]==0) { f[a[i]]=1; ans++; } } cout<<ans<<'\n'; } return 0; }
c++
15
0.416914
38
6.326087
92
C. Sort Zerotime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputAn array is sorted if it has no inversionsA Young BoyYou are given an array of $$$n$$$ positive integers $$$a_1,a_2,\ldots,a_n$$$. In one operation you do the following: Choose any integer $$$x$$$. For all $$$i$$$ such that $$$a_i = x$$$, do $$$a_i := 0$$$ (assign $$$0$$$ to $$$a_i$$$). Find the minimum number of operations required to sort the array in non-decreasing order.InputEach test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows.The first line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$).The second line of each test case contains $$$n$$$ positive integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_i \le n$$$).It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.OutputFor each test case print one integer — the minimum number of operations required to sort the array in non-decreasing order.ExampleInput 533 3 241 3 1 354 1 5 3 242 4 1 211Output 1 2 4 3 0 NoteIn the first test case, you can choose $$$x = 3$$$ for the operation, the resulting array is $$$[0, 0, 2]$$$.In the second test case, you can choose $$$x = 1$$$ for the first operation and $$$x = 3$$$ for the second operation, the resulting array is $$$[0, 0, 0, 0]$$$.
cf
#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ Created on Sat Jan 28 01:29:46 2023 @author: manisarthak """ import sys input = lambda: sys.stdin.readline().rstrip() def solve (): [n] = list(map(int, input().split())) arr = list(map(int, input().split())) # n = 4 # arr = [2, 4, 1, 2] # n = 5 # arr = [4, 1, 5, 3, 2] prev = 10**10 d = dict() for i in range(n-1, -1, -1): if arr[i] in d : pass d[arr[i]].append(i) else : d[arr[i]] = [i] if arr[i] <= prev : prev = arr[i] # if arr[i] in d : # pass # d[arr[i]].append(i) # else : # d[arr[i]] = [i] else : maxi = i for j in range(i, -1, -1): if arr[j] in d : maxi = max(maxi, d[arr[j]][0]) s = set() # print(maxi) for k in range(maxi+1): s.add(arr[k]) print(len(s)) return print(0) # solve() for _ in range(int(input())): solve ()
python
19
0.344992
50
9.579832
119
C. Sort Zerotime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputAn array is sorted if it has no inversionsA Young BoyYou are given an array of $$$n$$$ positive integers $$$a_1,a_2,\ldots,a_n$$$. In one operation you do the following: Choose any integer $$$x$$$. For all $$$i$$$ such that $$$a_i = x$$$, do $$$a_i := 0$$$ (assign $$$0$$$ to $$$a_i$$$). Find the minimum number of operations required to sort the array in non-decreasing order.InputEach test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows.The first line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$).The second line of each test case contains $$$n$$$ positive integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_i \le n$$$).It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.OutputFor each test case print one integer — the minimum number of operations required to sort the array in non-decreasing order.ExampleInput 533 3 241 3 1 354 1 5 3 242 4 1 211Output 1 2 4 3 0 NoteIn the first test case, you can choose $$$x = 3$$$ for the operation, the resulting array is $$$[0, 0, 2]$$$.In the second test case, you can choose $$$x = 1$$$ for the first operation and $$$x = 3$$$ for the second operation, the resulting array is $$$[0, 0, 0, 0]$$$.
cf
#include <bits/stdc++.h> using namespace std; typedef pair<int,int> ii; typedef vector<int> vi; typedef vector<ii> vii; typedef long long ll; typedef unsigned long long ull; void solve() { int n; cin >> n; int arr[n+1]={0}; set<int>st; int freq[n+1]={0}; for(int i = 1 ; i <= n ; i++){ cin >> arr[i]; st.insert(arr[i]); freq[arr[i]]++; } ll ans = st.size(); if(st.size()==1) { cout << 0 << endl; return; } for(int i = n ; i > 0 ; i--){ if(arr[i-1] > arr[i]){ if(freq[arr[i]] == 1) ans--; break; } else if(arr[i-1] < arr[i]){ if(freq[arr[i]] > 1) break; ans--; } else freq[arr[i]]--; } cout << ans << endl; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int t; cin>>t; while(t--) solve(); return 0; }
c++
16
0.42116
40
8.742574
101
C. Sort Zerotime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputAn array is sorted if it has no inversionsA Young BoyYou are given an array of $$$n$$$ positive integers $$$a_1,a_2,\ldots,a_n$$$. In one operation you do the following: Choose any integer $$$x$$$. For all $$$i$$$ such that $$$a_i = x$$$, do $$$a_i := 0$$$ (assign $$$0$$$ to $$$a_i$$$). Find the minimum number of operations required to sort the array in non-decreasing order.InputEach test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows.The first line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$).The second line of each test case contains $$$n$$$ positive integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_i \le n$$$).It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.OutputFor each test case print one integer — the minimum number of operations required to sort the array in non-decreasing order.ExampleInput 533 3 241 3 1 354 1 5 3 242 4 1 211Output 1 2 4 3 0 NoteIn the first test case, you can choose $$$x = 3$$$ for the operation, the resulting array is $$$[0, 0, 2]$$$.In the second test case, you can choose $$$x = 1$$$ for the first operation and $$$x = 3$$$ for the second operation, the resulting array is $$$[0, 0, 0, 0]$$$.
cf
import java.io.*; import java.util.*; public class R813C { public static void main(String[] args) { JS scan = new JS(); int cases = scan.nextInt(); while(cases-->0){ int n = scan.nextInt(); int[] arr =new int[n]; boolean[] bad = new boolean[n]; int min = n*5; for(int i = 0;i<n;i++){ arr[i] = scan.nextInt()-1; } for(int i =n-1;i>=0;i--){ min = Math.min(min,arr[i]); if(arr[i] >min ){ bad[arr[i]] = true; } } boolean goody = true; for(int i = n-1;i>=0;i--){ if(bad[arr[i]])goody = false; if(!goody)bad[arr[i]] = true; } int ans =0; for(int i = 0;i<n;i++){ if(bad[i]){ ans++; } } System.out.println(ans); } } static class JS { public int BS = 1 << 16; public char NC = (char) 0; byte[] buf = new byte[BS]; int bId = 0, size = 0; char c = NC; double num = 1; BufferedInputStream in; public JS() { in = new BufferedInputStream(System.in, BS); } public JS(String s) throws FileNotFoundException { in = new BufferedInputStream(new FileInputStream(new File(s)), BS); } public char nextChar() { while (bId == size) { try { size = in.read(buf); } catch (Exception e) { return NC; } if (size == -1) return NC; bId = 0; } return (char) buf[bId++]; } public int nextInt() { return (int) nextLong(); } public long nextLong() { num = 1; boolean neg = false; if (c == NC) c = nextChar(); for (; (c < '0' || c > '9'); c = nextChar()) { if (c == '-') neg = true; } long res = 0; for (; c >= '0' && c <= '9'; c = nextChar()) { res = (res << 3) + (res << 1) + c - '0'; num *= 10; } return neg ? -res : res; } public double nextDouble() { double cur = nextLong(); return c != '.' ? cur : cur + nextLong() / num; } public String next() { StringBuilder res = new StringBuilder(); while (c <= 32) c = nextChar(); while (c > 32) { res.append(c); c = nextChar(); } return res.toString(); } public String nextLine() { StringBuilder res = new StringBuilder(); while (c <= 32) c = nextChar(); while (c != '\n') { res.append(c); c = nextChar(); } return res.toString(); } public boolean hasNext() { if (c > 32) return true; while (true) { c = nextChar(); if (c == NC) return false; else if (c > 32) return true; } } } }
java
16
0.360116
79
13.416667
240
C. Sort Zerotime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputAn array is sorted if it has no inversionsA Young BoyYou are given an array of $$$n$$$ positive integers $$$a_1,a_2,\ldots,a_n$$$. In one operation you do the following: Choose any integer $$$x$$$. For all $$$i$$$ such that $$$a_i = x$$$, do $$$a_i := 0$$$ (assign $$$0$$$ to $$$a_i$$$). Find the minimum number of operations required to sort the array in non-decreasing order.InputEach test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows.The first line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$).The second line of each test case contains $$$n$$$ positive integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_i \le n$$$).It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.OutputFor each test case print one integer — the minimum number of operations required to sort the array in non-decreasing order.ExampleInput 533 3 241 3 1 354 1 5 3 242 4 1 211Output 1 2 4 3 0 NoteIn the first test case, you can choose $$$x = 3$$$ for the operation, the resulting array is $$$[0, 0, 2]$$$.In the second test case, you can choose $$$x = 1$$$ for the first operation and $$$x = 3$$$ for the second operation, the resulting array is $$$[0, 0, 0, 0]$$$.
cf
for t in range(int(input())): n = int(input()) arr = list(map(int, input().split())) minn=arr[-1] rem_arr = set() for i in range(n-1,-1,-1): if arr[i]<=minn: minn=arr[i] else: minn=0 rem_arr.add(arr[i]) flag=False for i in range(n-1,-1,-1): if arr[i] in rem_arr: flag=True if flag: rem_arr.add(arr[i]) print(len(rem_arr))
python
13
0.437229
41
12.228571
35
C. Sort Zerotime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputAn array is sorted if it has no inversionsA Young BoyYou are given an array of $$$n$$$ positive integers $$$a_1,a_2,\ldots,a_n$$$. In one operation you do the following: Choose any integer $$$x$$$. For all $$$i$$$ such that $$$a_i = x$$$, do $$$a_i := 0$$$ (assign $$$0$$$ to $$$a_i$$$). Find the minimum number of operations required to sort the array in non-decreasing order.InputEach test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows.The first line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$).The second line of each test case contains $$$n$$$ positive integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_i \le n$$$).It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.OutputFor each test case print one integer — the minimum number of operations required to sort the array in non-decreasing order.ExampleInput 533 3 241 3 1 354 1 5 3 242 4 1 211Output 1 2 4 3 0 NoteIn the first test case, you can choose $$$x = 3$$$ for the operation, the resulting array is $$$[0, 0, 2]$$$.In the second test case, you can choose $$$x = 1$$$ for the first operation and $$$x = 3$$$ for the second operation, the resulting array is $$$[0, 0, 0, 0]$$$.
cf
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.HashSet; import java.util.Set; //https://codeforces.com/problemset/problem/1712/C public class C1712 { static BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); public static void main(String[] args) throws IOException { int n = Integer.parseInt(in.readLine().trim()); while (n-- > 0) { int len = Integer.parseInt(in.readLine().trim()); String[] str = in.readLine().split(" "); int[] arr = new int[len]; int maxLen = 1; for (int i = 0; i < len; i++) { arr[i] = Integer.parseInt(str[i]); } for (int i = len - 1; i > 0; i--) { if (arr[i] >= arr[i - 1]) { ++maxLen; } else if (arr[i] < arr[i - 1]) { i = -100; } } Set<Integer> arrSet = new HashSet<>(); for (int i = 0; i < len - maxLen; i++) { arrSet.add(arr[i]); } for (int i = len - 1; i >= len - maxLen; i--) { if (arrSet.contains(arr[i])) { for (int j = len - maxLen; j < i; ++j) { arrSet.add(arr[j]); } i = -100; } } System.out.println(arrSet.size()); } } }
java
17
0.441907
84
30.680851
47
C. Sort Zerotime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputAn array is sorted if it has no inversionsA Young BoyYou are given an array of $$$n$$$ positive integers $$$a_1,a_2,\ldots,a_n$$$. In one operation you do the following: Choose any integer $$$x$$$. For all $$$i$$$ such that $$$a_i = x$$$, do $$$a_i := 0$$$ (assign $$$0$$$ to $$$a_i$$$). Find the minimum number of operations required to sort the array in non-decreasing order.InputEach test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows.The first line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$).The second line of each test case contains $$$n$$$ positive integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_i \le n$$$).It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.OutputFor each test case print one integer — the minimum number of operations required to sort the array in non-decreasing order.ExampleInput 533 3 241 3 1 354 1 5 3 242 4 1 211Output 1 2 4 3 0 NoteIn the first test case, you can choose $$$x = 3$$$ for the operation, the resulting array is $$$[0, 0, 2]$$$.In the second test case, you can choose $$$x = 1$$$ for the first operation and $$$x = 3$$$ for the second operation, the resulting array is $$$[0, 0, 0, 0]$$$.
cf
from sys import stdin from math import log,floor,ceil,gcd from collections import defaultdict as dd #from bisect import bisect_left as bl,bisect_right as br,insort as ins #from itertools import groupby as gb #from heapq import heapify as hpf, heappush as hpush, heappop as hpop #from collections import deque #from itertools import accumulate as prefix inp = lambda: int(stdin.readline()) ra = lambda typ : list(map(typ, stdin.readline().split())) rv = lambda typ : map(typ, stdin.readline().split()) #rss = lambda: stdin.readline().rstrip() #rs = lambda: stdin.readline() #mod = 1000000007 def main(): for _ in range(inp()): n = inp() arr = ra(int) pos = dd(list) for i in range(n): pos[arr[i]].append(i+1) pos_ = None for i in range(n-1,0,-1): if arr[i-1] > arr[i]: pos_ = i+1 break if pos_ == None: print(0) continue x = set(arr[:pos_-1]) max_pos = 0 for i in x: max_pos = max(max_pos,pos[i][-1]) print(len(set(arr[:max_pos]))) main()
python
14
0.539354
70
25.844444
45
C. Sort Zerotime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputAn array is sorted if it has no inversionsA Young BoyYou are given an array of $$$n$$$ positive integers $$$a_1,a_2,\ldots,a_n$$$. In one operation you do the following: Choose any integer $$$x$$$. For all $$$i$$$ such that $$$a_i = x$$$, do $$$a_i := 0$$$ (assign $$$0$$$ to $$$a_i$$$). Find the minimum number of operations required to sort the array in non-decreasing order.InputEach test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows.The first line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$).The second line of each test case contains $$$n$$$ positive integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_i \le n$$$).It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.OutputFor each test case print one integer — the minimum number of operations required to sort the array in non-decreasing order.ExampleInput 533 3 241 3 1 354 1 5 3 242 4 1 211Output 1 2 4 3 0 NoteIn the first test case, you can choose $$$x = 3$$$ for the operation, the resulting array is $$$[0, 0, 2]$$$.In the second test case, you can choose $$$x = 1$$$ for the first operation and $$$x = 3$$$ for the second operation, the resulting array is $$$[0, 0, 0, 0]$$$.
cf
from collections import deque def updateZeros(prev): global zerod, toZero, count, q if prev in toZero: for val in toZero[prev]: if val not in zerod: zerod.add(val) q.append(val) count += 1 t = int(input()) for test in range(t): n = int(input()) nums = list(map(int, input().split())) endPos = len(nums) - 1 zerod = set() toZero = {} q = deque() count = 0 for pos in range(endPos, 0, -1): prev = nums[pos-1] cur = nums[pos] if prev in zerod: prev = 0 if cur in zerod: cur = 0 if cur < prev: zerod.add(prev) q.append(prev) while len(q) != 0: updateZeros(q.popleft()) updateZeros(prev) count += 1 else: if cur not in toZero: toZero[cur] = [prev] else: toZero[cur].append(prev) print(count)
python
15
0.442216
42
11.768293
82
C. Sort Zerotime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputAn array is sorted if it has no inversionsA Young BoyYou are given an array of $$$n$$$ positive integers $$$a_1,a_2,\ldots,a_n$$$. In one operation you do the following: Choose any integer $$$x$$$. For all $$$i$$$ such that $$$a_i = x$$$, do $$$a_i := 0$$$ (assign $$$0$$$ to $$$a_i$$$). Find the minimum number of operations required to sort the array in non-decreasing order.InputEach test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows.The first line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$).The second line of each test case contains $$$n$$$ positive integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_i \le n$$$).It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.OutputFor each test case print one integer — the minimum number of operations required to sort the array in non-decreasing order.ExampleInput 533 3 241 3 1 354 1 5 3 242 4 1 211Output 1 2 4 3 0 NoteIn the first test case, you can choose $$$x = 3$$$ for the operation, the resulting array is $$$[0, 0, 2]$$$.In the second test case, you can choose $$$x = 1$$$ for the first operation and $$$x = 3$$$ for the second operation, the resulting array is $$$[0, 0, 0, 0]$$$.
cf
#include<bits/stdc++.h> #define endl '\n' #define all(v) v.begin(), v.end() #define pb push_back #define ppb pop_back #define vl vector<long long> #define ff first #define ss second #define vi vector<int> #define set_bits(a) __builtin_popcountll(a) #define pll pair<ll,ll> #define cin(v) {int x; cin>>x; v.push_back(x);} #define display(a) { for (int i=0;i<a.size();i++) cout<<a[i]<<" "; cout<<endl; } #define sum(a) ( accumulate ((a).begin(), (a).end(), 0ll)) using namespace std; using ll = long long; const int N=1e7+10; void solve(){ int n;cin>>n; vi a; unordered_map<int,int> m,next_index,prev_index; for(int i=0;i<n;i++){ int x;cin>>x; int p=a.size(); if(i!=0&&a[p-1]==x){ continue; } m[x]++; a.pb(x); } n=a.size(); for(int i=0;i<n;i++){ next_index[a[i]]=i; } int k=-1; for(int i=n-2;i>=0;i--){ if(a[i]>a[i+1]){ k=i;break; } }//cout<<k<<endl;return; if(k==-1){ cout<<0<<endl;return; } for(int i=k;i<n;i++){ if(m[a[i]]!=1){ k=i; } } int c=0; unordered_map<int,int> ct; for(int i=0;i<=k;i++){ if(ct[a[i]]==0){ c++; } ct[a[i]]++; }cout<<c<<endl; } int main(){ //freopen("output.txt","w",stdout); int t; cin>>t; while(t--){ solve(); } }
c++
12
0.452092
80
10.150376
133
C. Sort Zerotime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputAn array is sorted if it has no inversionsA Young BoyYou are given an array of $$$n$$$ positive integers $$$a_1,a_2,\ldots,a_n$$$. In one operation you do the following: Choose any integer $$$x$$$. For all $$$i$$$ such that $$$a_i = x$$$, do $$$a_i := 0$$$ (assign $$$0$$$ to $$$a_i$$$). Find the minimum number of operations required to sort the array in non-decreasing order.InputEach test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows.The first line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$).The second line of each test case contains $$$n$$$ positive integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_i \le n$$$).It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.OutputFor each test case print one integer — the minimum number of operations required to sort the array in non-decreasing order.ExampleInput 533 3 241 3 1 354 1 5 3 242 4 1 211Output 1 2 4 3 0 NoteIn the first test case, you can choose $$$x = 3$$$ for the operation, the resulting array is $$$[0, 0, 2]$$$.In the second test case, you can choose $$$x = 1$$$ for the first operation and $$$x = 3$$$ for the second operation, the resulting array is $$$[0, 0, 0, 0]$$$.
cf
#include<bits/stdc++.h> using namespace std; #define ll long long int #define endl "\n" #define all(v) v.begin(), v.end() #define vll vector<long long int> #define vi vector<int> #define vs vector<string> #define vii vector<pair<int,int>> #define pb push_back #define mp map<string, int> #define ff first #define ss second #define print(v) for(auto & i : (v)) {cout << i << " " ;} cout << endl ; #define mod 1000000007 #define ispowoftwo(n) (!(n & (n-1))) #define YES cout << "YES" << "\n" #define NO cout << "NO" << "\n" #define str(n) to_string(n) #define lower(c) (char)(c | ' ') #define upper(c) (char)(c & '_') void solve() { int n ; cin >> n; vi v(n); for(auto & i : v) cin >> i ; if(n == 1) {cout << 0 << endl; return ; } int blk = -1 ; for (int i = n-1 ; i >= 1 ; i--) { if (v[i] < v[i-1]) { blk = i-1 ; break ; } } // cout << "blk " << blk << endl ; vi hsh(100005 , 0 ); for (int i = 0; i <= blk; ++i) { hsh[v[i]]++ ; } int finalblk = blk ; for (int i = blk+1; i < n; ++i) { if (hsh[v[i]] != 0) { finalblk = i ; } } for (int i = blk + 1 ; i <= finalblk; ++i) { hsh[v[i]]++ ; } int ct = 0 ; for (int i = 1; i < hsh.size(); ++i) { if (hsh[i] != 0) { ct++ ; } } cout << ct << endl ; // cout << "finalblk" << finalblk << endl ; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); ll t = 1 ; cin >> t ; while(t--) solve(); // for (int tc = 1; tc <= t; tc++) { // cout << "Case #" << tc << ": "; // solve(); // } return 0 ; }
c++
12
0.401904
77
8.140097
207
C. Sort Zerotime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputAn array is sorted if it has no inversionsA Young BoyYou are given an array of $$$n$$$ positive integers $$$a_1,a_2,\ldots,a_n$$$. In one operation you do the following: Choose any integer $$$x$$$. For all $$$i$$$ such that $$$a_i = x$$$, do $$$a_i := 0$$$ (assign $$$0$$$ to $$$a_i$$$). Find the minimum number of operations required to sort the array in non-decreasing order.InputEach test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows.The first line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$).The second line of each test case contains $$$n$$$ positive integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_i \le n$$$).It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.OutputFor each test case print one integer — the minimum number of operations required to sort the array in non-decreasing order.ExampleInput 533 3 241 3 1 354 1 5 3 242 4 1 211Output 1 2 4 3 0 NoteIn the first test case, you can choose $$$x = 3$$$ for the operation, the resulting array is $$$[0, 0, 2]$$$.In the second test case, you can choose $$$x = 1$$$ for the first operation and $$$x = 3$$$ for the second operation, the resulting array is $$$[0, 0, 0, 0]$$$.
cf
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #include <functional> using namespace std; using namespace __gnu_pbds; #define ll long long #define ld long double #define Fast ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); #define pb push_back #define vll std::vector<ll>v #define max3(a,b,c) max(a,max(b,c)) #define min3(a,b,c) min(a,min(b,c)) #define lcm(a,b) (a*b)/(__gcd(a,b)) const ll MOD = 1e9 + 7; const ll INF = 1e18; const ll NEG_INF = (-1)*1e18; const ll MAX = 1e5 + 5; bool isPrime(ll x){ if(x==0 || x==1) return false; else if(x==2 || x==3) return true; else if(x%2==0 || x%3==0) return false; else{ for(ll i=5; i<=sqrt(x); i+=6){ if(x%i==0 || x%(i+2)==0) return false; } return true; } } bool isPowerOfTwo(int n){ if (n == 0) return false; return (ceil(log2(n)) == floor(log2(n))); } void myTask(){ int n; cin>>n; vector<int>v(n); for(int i=0; i<n; i++){ cin>>v[i]; } int j=n, k; vector<int>ans; for(int i=n-2; i>=0; i--){ if(v[i]>v[i+1]){ j = i; break; } } if(j==n){ cout<<0<<endl; } else{ for(int i=0; i<=j; i++){ ans.pb(v[i]); } sort(ans.begin(),ans.end()); k = j; vector<int>::iterator it; for(int i=n-1; i>j; i--){ it = find(ans.begin(), ans.end(), v[i]); if(it!=ans.end()){ k = i; break; } } set<int>tmp; for(int i=0; i<=k; i++){ tmp.insert(v[i]); } cout<<tmp.size()<<endl; } } int main(){ Fast ll T=1; cin>>T; while(T--){ myTask(); } return 0; }
c++
19
0.385375
77
11.185792
183
C. Sort Zerotime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputAn array is sorted if it has no inversionsA Young BoyYou are given an array of $$$n$$$ positive integers $$$a_1,a_2,\ldots,a_n$$$. In one operation you do the following: Choose any integer $$$x$$$. For all $$$i$$$ such that $$$a_i = x$$$, do $$$a_i := 0$$$ (assign $$$0$$$ to $$$a_i$$$). Find the minimum number of operations required to sort the array in non-decreasing order.InputEach test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows.The first line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$).The second line of each test case contains $$$n$$$ positive integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_i \le n$$$).It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.OutputFor each test case print one integer — the minimum number of operations required to sort the array in non-decreasing order.ExampleInput 533 3 241 3 1 354 1 5 3 242 4 1 211Output 1 2 4 3 0 NoteIn the first test case, you can choose $$$x = 3$$$ for the operation, the resulting array is $$$[0, 0, 2]$$$.In the second test case, you can choose $$$x = 1$$$ for the first operation and $$$x = 3$$$ for the second operation, the resulting array is $$$[0, 0, 0, 0]$$$.
cf
import java.io.DataOutput; import java.io.PrintWriter; import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); // System.out.println(sc.nextByte()); PrintWriter pw = new PrintWriter(System.out); // napolean(sc,pw); // paint(sc,pw); // tean(sc,pw); // ugu(sc,pw); // System.out.println(gcd(80,3)); occurence(sc, pw); // for(int j=0;j<8;j++){ // String s=sc.next(); // for(int k=0;k<8;k++){ // // System.out.println(s.charAt(k)); // } // } pw.close(); } private static void occurence(Scanner scanner, PrintWriter printWriter) { int testcase = scanner.nextInt(); for (int i = 0; i < testcase; i++) { HashMap<Integer, Integer> map = new HashMap<>(); int size = scanner.nextInt(); int[] arr = new int[size]; for (int j = 0; j < size; j++) { int m = scanner.nextInt(); arr[j]=m; map.put(m,j); } int num=-1; for(int j=size-2;j>=0;j--){ int first=arr[j+1]; int last=arr[j]; if (last>first){ num=last; break; } } if (num==-1){ System.out.println(0); continue; } // System.out.println(num); int lastind=map.get(num); for (int j=lastind;j>=0;j--){ int m=arr[j]; int ls=map.get(m); if (ls>lastind)lastind=ls; } // System.out.println(lastind); map.clear(); for(int j=lastind;j>=0;j--){ map.put(arr[j],1); } System.out.println(map.size()); } }}
java
13
0.392417
77
14.87218
133
C. Sort Zerotime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputAn array is sorted if it has no inversionsA Young BoyYou are given an array of $$$n$$$ positive integers $$$a_1,a_2,\ldots,a_n$$$. In one operation you do the following: Choose any integer $$$x$$$. For all $$$i$$$ such that $$$a_i = x$$$, do $$$a_i := 0$$$ (assign $$$0$$$ to $$$a_i$$$). Find the minimum number of operations required to sort the array in non-decreasing order.InputEach test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows.The first line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$).The second line of each test case contains $$$n$$$ positive integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_i \le n$$$).It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.OutputFor each test case print one integer — the minimum number of operations required to sort the array in non-decreasing order.ExampleInput 533 3 241 3 1 354 1 5 3 242 4 1 211Output 1 2 4 3 0 NoteIn the first test case, you can choose $$$x = 3$$$ for the operation, the resulting array is $$$[0, 0, 2]$$$.In the second test case, you can choose $$$x = 1$$$ for the first operation and $$$x = 3$$$ for the second operation, the resulting array is $$$[0, 0, 0, 0]$$$.
cf
#include<bits/stdc++.h> using namespace std; #define int long long #define dg(x) cout<<#x<<'='<<x<<'\n' const int N=1e5+10; int a[N]; int mp[N]; void solve() { int n; cin>>n; for(int i=1;i<=n;i++) cin>>a[i],mp[i]=0; int ans=0; queue<int>q; q.push(a[1]); for(int i=2;i<=n;i++){ if(mp[a[i]]==1) a[i]=0; if(a[i]<a[i-1]){ while(q.size()){ if(mp[q.front()]==0){ ans++; mp[q.front()]=1; } q.pop(); } } if(a[i]) q.push(a[i]); } cout<<ans<<'\n'; } signed main() { ios::sync_with_stdio(0),cin.tie(0),cout.tie(0); int T=1; cin>>T; while(T--){ solve(); } return 0; }
c++
17
0.462006
48
6.404494
89
C. Sort Zerotime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputAn array is sorted if it has no inversionsA Young BoyYou are given an array of $$$n$$$ positive integers $$$a_1,a_2,\ldots,a_n$$$. In one operation you do the following: Choose any integer $$$x$$$. For all $$$i$$$ such that $$$a_i = x$$$, do $$$a_i := 0$$$ (assign $$$0$$$ to $$$a_i$$$). Find the minimum number of operations required to sort the array in non-decreasing order.InputEach test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows.The first line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$).The second line of each test case contains $$$n$$$ positive integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_i \le n$$$).It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.OutputFor each test case print one integer — the minimum number of operations required to sort the array in non-decreasing order.ExampleInput 533 3 241 3 1 354 1 5 3 242 4 1 211Output 1 2 4 3 0 NoteIn the first test case, you can choose $$$x = 3$$$ for the operation, the resulting array is $$$[0, 0, 2]$$$.In the second test case, you can choose $$$x = 1$$$ for the first operation and $$$x = 3$$$ for the second operation, the resulting array is $$$[0, 0, 0, 0]$$$.
cf
#include <bits/stdc++.h> using namespace std; #define ll long long int int main() { ll t; cin >> t; while (t--) { ll n; cin >> n; vector<ll> v(n); for (int i = 0; i < n; i++) { cin >> v[i]; } vector<ll> a = v; ll index = -1; for (int i = n - 2; i >= 0; i--) { if (a[i] > a[i + 1]) { index = i; break; } } if (index == -1) { cout << 0 << endl; } else { unordered_set<ll> s; for (int i = 0; i <= index; i++) { s.insert(a[i]); a[i] = 0; } for (int i = index + 1; i < n; i++) { if (s.find(a[i]) != s.end()) a[i] = 0; } ll val = -1; for (int i = n - 1; i >= 0; i--) { if (a[i] == 0) { val = i; break; } } unordered_set<ll> res; for (int i = 0; i <= val; i++) { res.insert(v[i]); } cout << res.size() << endl; } } return 0; }
c++
17
0.245
47
10.111111
126
C. Sort Zerotime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputAn array is sorted if it has no inversionsA Young BoyYou are given an array of $$$n$$$ positive integers $$$a_1,a_2,\ldots,a_n$$$. In one operation you do the following: Choose any integer $$$x$$$. For all $$$i$$$ such that $$$a_i = x$$$, do $$$a_i := 0$$$ (assign $$$0$$$ to $$$a_i$$$). Find the minimum number of operations required to sort the array in non-decreasing order.InputEach test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows.The first line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$).The second line of each test case contains $$$n$$$ positive integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_i \le n$$$).It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.OutputFor each test case print one integer — the minimum number of operations required to sort the array in non-decreasing order.ExampleInput 533 3 241 3 1 354 1 5 3 242 4 1 211Output 1 2 4 3 0 NoteIn the first test case, you can choose $$$x = 3$$$ for the operation, the resulting array is $$$[0, 0, 2]$$$.In the second test case, you can choose $$$x = 1$$$ for the first operation and $$$x = 3$$$ for the second operation, the resulting array is $$$[0, 0, 0, 0]$$$.
cf
import java.util.HashSet; import java.util.Scanner; public class SortZero1712C { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int tests = sc.nextInt(); while (tests-->0) { int n = sc.nextInt(); int a[] = new int[n]; for(int i=0;i<n;i++) { a[i]=sc.nextInt(); } int[] ocur = new int[n+1]; int i; for(i=n-1;i>0;i--) { if(a[i-1]>a[i]) break; } for(int j=0;j<i;j++) { ocur[a[j]]++; } int k; for(k=n-1;k>=i;k--) { if(ocur[a[k]]>0) break; } for(int j=i;j<=k;j++) ocur[a[j]]++; HashSet<Integer> set = new HashSet<>(); for(int j=0;j<n;j++) { if(ocur[a[j]]>0) set.add(a[j]); } System.out.println(set.size()); } } }
java
14
0.318374
51
10.141509
106
C. Sort Zerotime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputAn array is sorted if it has no inversionsA Young BoyYou are given an array of $$$n$$$ positive integers $$$a_1,a_2,\ldots,a_n$$$. In one operation you do the following: Choose any integer $$$x$$$. For all $$$i$$$ such that $$$a_i = x$$$, do $$$a_i := 0$$$ (assign $$$0$$$ to $$$a_i$$$). Find the minimum number of operations required to sort the array in non-decreasing order.InputEach test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows.The first line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$).The second line of each test case contains $$$n$$$ positive integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_i \le n$$$).It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.OutputFor each test case print one integer — the minimum number of operations required to sort the array in non-decreasing order.ExampleInput 533 3 241 3 1 354 1 5 3 242 4 1 211Output 1 2 4 3 0 NoteIn the first test case, you can choose $$$x = 3$$$ for the operation, the resulting array is $$$[0, 0, 2]$$$.In the second test case, you can choose $$$x = 1$$$ for the first operation and $$$x = 3$$$ for the second operation, the resulting array is $$$[0, 0, 0, 0]$$$.
cf
#include<bits/stdc++.h> using namespace std; #define endl "\n" #define read(arr) for(auto &x : arr) cin>>x; #define print(arr) for(auto &x: arr) cout<<x<<" "; cout<<endl; #define sortv(arr) sort(arr.begin(), arr.end()) #define sorta(arr) sort(arr.begin(), arr.end()) #define revers(arr) reverse(arr.begin(), arr.end()) #define ll long long void solve(){ int n; cin>>n; int arr[n];read(arr); map<int,set<int>>mp; for(int i=0;i<n;i++){ mp[arr[i]].insert(i); } int x=0; for(int i=n-1;i>0;i--){ if(arr[i-1]>arr[i]){ int newi=-1; x++; for(auto it:mp[arr[i-1]]){ newi=max(it,newi); arr[it]=0; mp[0].insert(it); } mp[arr[i-1]].clear(); i=newi+1; } } cout<<x<<endl; } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); ll int t=1; cin>>t; while(t--){ solve(); } return 0; }
c++
15
0.520742
62
8.453608
97
C. Sort Zerotime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputAn array is sorted if it has no inversionsA Young BoyYou are given an array of $$$n$$$ positive integers $$$a_1,a_2,\ldots,a_n$$$. In one operation you do the following: Choose any integer $$$x$$$. For all $$$i$$$ such that $$$a_i = x$$$, do $$$a_i := 0$$$ (assign $$$0$$$ to $$$a_i$$$). Find the minimum number of operations required to sort the array in non-decreasing order.InputEach test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows.The first line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$).The second line of each test case contains $$$n$$$ positive integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_i \le n$$$).It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.OutputFor each test case print one integer — the minimum number of operations required to sort the array in non-decreasing order.ExampleInput 533 3 241 3 1 354 1 5 3 242 4 1 211Output 1 2 4 3 0 NoteIn the first test case, you can choose $$$x = 3$$$ for the operation, the resulting array is $$$[0, 0, 2]$$$.In the second test case, you can choose $$$x = 1$$$ for the first operation and $$$x = 3$$$ for the second operation, the resulting array is $$$[0, 0, 0, 0]$$$.
cf
from collections import Counter, defaultdict, deque import os import sys # from math import gcd, ceil, sqrt # from bisect import bisect_left, bisect_right # import math, bisect, heapq, random # from functools import lru_cache, reduce, cmp_to_key # from itertools import accumulate, combinations, permutations from io import BytesIO, IOBase inf = float('inf') mod = 10**9 + 7 # region fastio BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") print = lambda *args, **kwargs: sys.stdout.write(" ".join(map(str, args)) + " " if kwargs.get("end") == " " else " ".join(map(str, args)) + " " if kwargs.get("end") == " " else " ".join(map(str, args)) + "\n") ints = lambda: list(map(int, input().split())) #------------------- fast io --------------------# def solve(): i = int(input()) - 1 L = ints() D = Counter(L) while i > 0: if i > 0 and L[i] == L[i - 1]: while i > 0 and L[i] == L[i - 1]: D[L[i]] -= 1 i -= 1 if D[L[i]] != 1: break elif i > 0 and L[i] > L[i - 1] and D[L[i]] == 1: while i > 0 and L[i] > L[i - 1] and D[L[i]] == 1: D.pop(L[i]) i -= 1 else: break print(len(D) - (D[L[i]] == 1)) for _ in range(int(input())): solve()
python
17
0.53504
209
17.214724
163
C. Sort Zerotime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputAn array is sorted if it has no inversionsA Young BoyYou are given an array of $$$n$$$ positive integers $$$a_1,a_2,\ldots,a_n$$$. In one operation you do the following: Choose any integer $$$x$$$. For all $$$i$$$ such that $$$a_i = x$$$, do $$$a_i := 0$$$ (assign $$$0$$$ to $$$a_i$$$). Find the minimum number of operations required to sort the array in non-decreasing order.InputEach test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows.The first line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$).The second line of each test case contains $$$n$$$ positive integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_i \le n$$$).It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.OutputFor each test case print one integer — the minimum number of operations required to sort the array in non-decreasing order.ExampleInput 533 3 241 3 1 354 1 5 3 242 4 1 211Output 1 2 4 3 0 NoteIn the first test case, you can choose $$$x = 3$$$ for the operation, the resulting array is $$$[0, 0, 2]$$$.In the second test case, you can choose $$$x = 1$$$ for the first operation and $$$x = 3$$$ for the second operation, the resulting array is $$$[0, 0, 0, 0]$$$.
cf
from sys import stdin, stdout input = stdin.readline from collections import defaultdict, Counter, deque for _ in range(int(input())): n = int(stdin.readline()) l = list(map(int, stdin.readline().split())) switch = set() for i in range(n-1): # O(n**2) if not l[i] in switch: if l[i+1] < l[i]: switch.add(l[i]) prevMax = set() for i in range(n): # O(n) if l[i] in switch: for n in prevMax: switch.add(n) prevMax = set() else: prevMax.add(l[i]) print(len(switch))
python
13
0.482704
51
10.581818
55
C. Sort Zerotime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputAn array is sorted if it has no inversionsA Young BoyYou are given an array of $$$n$$$ positive integers $$$a_1,a_2,\ldots,a_n$$$. In one operation you do the following: Choose any integer $$$x$$$. For all $$$i$$$ such that $$$a_i = x$$$, do $$$a_i := 0$$$ (assign $$$0$$$ to $$$a_i$$$). Find the minimum number of operations required to sort the array in non-decreasing order.InputEach test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows.The first line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$).The second line of each test case contains $$$n$$$ positive integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_i \le n$$$).It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.OutputFor each test case print one integer — the minimum number of operations required to sort the array in non-decreasing order.ExampleInput 533 3 241 3 1 354 1 5 3 242 4 1 211Output 1 2 4 3 0 NoteIn the first test case, you can choose $$$x = 3$$$ for the operation, the resulting array is $$$[0, 0, 2]$$$.In the second test case, you can choose $$$x = 1$$$ for the first operation and $$$x = 3$$$ for the second operation, the resulting array is $$$[0, 0, 0, 0]$$$.
cf
import io, os input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline def sorts(a, k, n): s = set() lens = 0 b = a.copy() for i in range(n): if a[i] in s: b[i] = 0 elif lens<k: s.add(a[i]) b[i] = 0 lens += 1 c = b.copy() b.sort() if b == c: return True return False t = int(input()) for tidx in range(t): n = int(input()) a = [int(x) for x in input().split()] kmin = 0 kmax = n while kmin != kmax: kmid = (kmin+kmax)//2 if sorts(a, kmid, n): kmax = kmid else: kmin = kmid + 1 print(kmin)
python
12
0.433432
60
19.484848
33
C. Sort Zerotime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputAn array is sorted if it has no inversionsA Young BoyYou are given an array of $$$n$$$ positive integers $$$a_1,a_2,\ldots,a_n$$$. In one operation you do the following: Choose any integer $$$x$$$. For all $$$i$$$ such that $$$a_i = x$$$, do $$$a_i := 0$$$ (assign $$$0$$$ to $$$a_i$$$). Find the minimum number of operations required to sort the array in non-decreasing order.InputEach test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows.The first line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$).The second line of each test case contains $$$n$$$ positive integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_i \le n$$$).It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.OutputFor each test case print one integer — the minimum number of operations required to sort the array in non-decreasing order.ExampleInput 533 3 241 3 1 354 1 5 3 242 4 1 211Output 1 2 4 3 0 NoteIn the first test case, you can choose $$$x = 3$$$ for the operation, the resulting array is $$$[0, 0, 2]$$$.In the second test case, you can choose $$$x = 1$$$ for the first operation and $$$x = 3$$$ for the second operation, the resulting array is $$$[0, 0, 0, 0]$$$.
cf
for _ in range(int(input())): n = int(input()) arr_list = list(map(int, input().split())) s = set() f = 0 ans = 0 for i in range(1,n): if arr_list[i] in s or arr_list[i] < arr_list[i-1]: for j in arr_list[f:i]: s.add(j) f=i print(len(s))
python
13
0.42042
59
10.1
30
C. Sort Zerotime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputAn array is sorted if it has no inversionsA Young BoyYou are given an array of $$$n$$$ positive integers $$$a_1,a_2,\ldots,a_n$$$. In one operation you do the following: Choose any integer $$$x$$$. For all $$$i$$$ such that $$$a_i = x$$$, do $$$a_i := 0$$$ (assign $$$0$$$ to $$$a_i$$$). Find the minimum number of operations required to sort the array in non-decreasing order.InputEach test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows.The first line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$).The second line of each test case contains $$$n$$$ positive integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_i \le n$$$).It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.OutputFor each test case print one integer — the minimum number of operations required to sort the array in non-decreasing order.ExampleInput 533 3 241 3 1 354 1 5 3 242 4 1 211Output 1 2 4 3 0 NoteIn the first test case, you can choose $$$x = 3$$$ for the operation, the resulting array is $$$[0, 0, 2]$$$.In the second test case, you can choose $$$x = 1$$$ for the first operation and $$$x = 3$$$ for the second operation, the resulting array is $$$[0, 0, 0, 0]$$$.
cf
t = int(input()) for i in range(t): len_1 = int(input()) lst1 = list(map(int,input().split())) dct1 = {} lstcount = [] count = 0 for j in range(len_1): dct1[j+1] = [] for k in range(len_1): if dct1[lst1[k]] == []: count += 1 dct1[lst1[k]].append(k) lstcount.append(count) g = 1 ans = 0 store = 0 while g < len_1: if lst1[g-1] > lst1[g]: for h in dct1[lst1[g-1]]: lst1[h] = 0 for f in range(store,h): if lst1[f]!=0: for t in dct1[lst1[f]]: lst1[t] = 0 store = h ans = lstcount[store] g += 1 print(ans)
python
16
0.383526
43
10.969231
65
C. Sort Zerotime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputAn array is sorted if it has no inversionsA Young BoyYou are given an array of $$$n$$$ positive integers $$$a_1,a_2,\ldots,a_n$$$. In one operation you do the following: Choose any integer $$$x$$$. For all $$$i$$$ such that $$$a_i = x$$$, do $$$a_i := 0$$$ (assign $$$0$$$ to $$$a_i$$$). Find the minimum number of operations required to sort the array in non-decreasing order.InputEach test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows.The first line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$).The second line of each test case contains $$$n$$$ positive integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_i \le n$$$).It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.OutputFor each test case print one integer — the minimum number of operations required to sort the array in non-decreasing order.ExampleInput 533 3 241 3 1 354 1 5 3 242 4 1 211Output 1 2 4 3 0 NoteIn the first test case, you can choose $$$x = 3$$$ for the operation, the resulting array is $$$[0, 0, 2]$$$.In the second test case, you can choose $$$x = 1$$$ for the first operation and $$$x = 3$$$ for the second operation, the resulting array is $$$[0, 0, 0, 0]$$$.
cf
// Author: Aaron He // Created: 11 January 2023 (Wednesday) #include <bits/stdc++.h> using namespace std; // Based on editorial solution int main() { ios_base::sync_with_stdio(0); cin.tie(0); int t; cin >> t; while (t--) { int n, k; cin >> n >> k; vector<int> a(n); for (int i = 0; i < n; i++) { cin >> a[i]; } const int mx = 1e9; int l = *min_element(a.begin(), a.end()), r = mx; while (l < r) { int m = (l + r + 1)/2; int ops = k; vector<int> b = a; for (int i = 0; i < n; i++) { if (2 * b[i] < m) { b[i] = mx; ops--; } } bool good = false; if (ops == 0) { for (int i = 0; i < n - 1; i++) { if (min(b[i], b[i + 1]) >= m) { good = true; } } } else if (ops == 1) { for (int i = 0; i < n; i++) { if (b[i] >= m) { good = true; } } } else if (ops >= 2) { good = true; } if (good) { l = m; } else { r = m - 1; } } cout << l << '\n'; } }
c++
19
0.389798
51
8.114035
114
D. Empty Graphtime limit per test1.5 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard output — Do you have a wish?  — I want people to stop gifting each other arrays.O_o and Another Young BoyAn array of $$$n$$$ positive integers $$$a_1,a_2,\ldots,a_n$$$ fell down on you from the skies, along with a positive integer $$$k \le n$$$.You can apply the following operation at most $$$k$$$ times: Choose an index $$$1 \le i \le n$$$ and an integer $$$1 \le x \le 10^9$$$. Then do $$$a_i := x$$$ (assign $$$x$$$ to $$$a_i$$$). Then build a complete undirected weighted graph with $$$n$$$ vertices numbered with integers from $$$1$$$ to $$$n$$$, where edge $$$(l, r)$$$ ($$$1 \le l < r \le n$$$) has weight $$$\min(a_{l},a_{l+1},\ldots,a_{r})$$$.You have to find the maximum possible diameter of the resulting graph after performing at most $$$k$$$ operations.The diameter of a graph is equal to $$$\max\limits_{1 \le u < v \le n}{\operatorname{d}(u, v)}$$$, where $$$\operatorname{d}(u, v)$$$ is the length of the shortest path between vertex $$$u$$$ and vertex $$$v$$$.InputEach test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows.The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$2 \le n \le 10^5$$$, $$$1 \le k \le n$$$).The second line of each test case contains $$$n$$$ positive integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_i \le 10^9$$$).It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.OutputFor each test case print one integer — the maximum possible diameter of the graph after performing at most $$$k$$$ operations.ExampleInput 63 12 4 13 21 9 843 110 2 63 2179 17 10000000002 15 92 24 2Output 4 168 10 1000000000 9 1000000000 NoteIn the first test case, one of the optimal arrays is $$$[2,4,5]$$$.The graph built on this array: $$$\operatorname{d}(1, 2) = \operatorname{d}(1, 3) = 2$$$ and $$$\operatorname{d}(2, 3) = 4$$$, so the diameter is equal to $$$\max(2,2,4) = 4$$$.
cf
import sys; input = sys.stdin.readline def solve(): n, k = map(int, input().split()) a = list(map(int, input().split())) # 이분 탐색 start = 0; end = 1000000000 result = 0 while start <= end: mid = (start + end) // 2 arr = a[:] # 배열 복사 K = k # 남은 작업 수 # 임의로 정한 mid에 따라 배열을 조작 # 남은 작업 수에 따라 분기가 나뉜다. # mid = 2 * min(a1, ..., an) for i in range(n): if arr[i] < mid / 2: # mid / 2보다 작으면 1000000000으로 교체 작업 arr[i] = 1000000000 K -= 1 # 잘 모르겠다면 배열과 s, m, e, K의 변화를 살펴보자 # print(arr) # print(start ,mid ,end) # print(K) # print() # 남은 작업 수가 음수이면 이 mid는 불가능하다. if K < 0: end = mid - 1 # 남은 작업 수가 0이면 그래프의 직경을 계산하여 비교 elif not K: MAX = 0 # 직경 for i in range(n - 1): MAX = max(MAX, min(arr[i], arr[i + 1])) if MAX < mid: # 직경보다 mid가 크면 불가능하다. end = mid - 1 else: # 직경보다 mid가 같거나 작으면 가능하다. result = mid start = mid + 1 # 남은 작업 수가 1이면 # 최대한 mid가 답이 될 수 있도록 # arr의 최댓값에 작업을 해야 한다. elif K == 1: if max(arr) < mid: # 최댓값보다 mid가 크면 불가능하다. end = mid - 1 else: # 최댓값보다 mid가 같거나 작으면 가능하다. result = mid start = mid + 1 # 남은 작업 수가 2 이상이면 # 가능한 mid else: result = mid start = mid + 1 print(result) for _ in range(int(input())): solve()
python
18
0.402644
67
12.110236
127
D. Empty Graphtime limit per test1.5 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard output — Do you have a wish?  — I want people to stop gifting each other arrays.O_o and Another Young BoyAn array of $$$n$$$ positive integers $$$a_1,a_2,\ldots,a_n$$$ fell down on you from the skies, along with a positive integer $$$k \le n$$$.You can apply the following operation at most $$$k$$$ times: Choose an index $$$1 \le i \le n$$$ and an integer $$$1 \le x \le 10^9$$$. Then do $$$a_i := x$$$ (assign $$$x$$$ to $$$a_i$$$). Then build a complete undirected weighted graph with $$$n$$$ vertices numbered with integers from $$$1$$$ to $$$n$$$, where edge $$$(l, r)$$$ ($$$1 \le l < r \le n$$$) has weight $$$\min(a_{l},a_{l+1},\ldots,a_{r})$$$.You have to find the maximum possible diameter of the resulting graph after performing at most $$$k$$$ operations.The diameter of a graph is equal to $$$\max\limits_{1 \le u < v \le n}{\operatorname{d}(u, v)}$$$, where $$$\operatorname{d}(u, v)$$$ is the length of the shortest path between vertex $$$u$$$ and vertex $$$v$$$.InputEach test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows.The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$2 \le n \le 10^5$$$, $$$1 \le k \le n$$$).The second line of each test case contains $$$n$$$ positive integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_i \le 10^9$$$).It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.OutputFor each test case print one integer — the maximum possible diameter of the graph after performing at most $$$k$$$ operations.ExampleInput 63 12 4 13 21 9 843 110 2 63 2179 17 10000000002 15 92 24 2Output 4 168 10 1000000000 9 1000000000 NoteIn the first test case, one of the optimal arrays is $$$[2,4,5]$$$.The graph built on this array: $$$\operatorname{d}(1, 2) = \operatorname{d}(1, 3) = 2$$$ and $$$\operatorname{d}(2, 3) = 4$$$, so the diameter is equal to $$$\max(2,2,4) = 4$$$.
cf
import sys from array import array input = lambda: sys.stdin.readline().rstrip("\r\n") inp = lambda dtype: [dtype(x) for x in input().split()] debug = lambda *x: print(*x, file=sys.stderr) ceil1 = lambda a, b: (a + b - 1) // b out, tests = [], int(input()) def solve(a): mi = ans = min(a) for i in range(n - 1): cur = min(a[i:i + 2]) if k_: cur = 10 ** 9 if k_ > 1 else max(a[i:i + 2]) ans = max(ans, min(cur, 2 * mi)) return ans for _ in range(tests): n, k = inp(int) a = array('i', inp(int)) b = array('i', a[:]) ans, sor = 0, sorted(range(n), key=a.__getitem__)[:k] for i in sor: a[i] = 10 ** 9 k_ = 0 for i in range(3): ans = max(max(solve(a), solve(a[::-1])), ans) if k_ == k: break lst = b[sor[-1]] while sor: if b[sor[-1]] != lst: break k_ += 1 a[sor[-1]] = b[sor[-1]] sor.pop() out.append(ans) print('\n'.join(map(str, out)))
python
16
0.457447
59
11.309524
84
D. Empty Graphtime limit per test1.5 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard output — Do you have a wish?  — I want people to stop gifting each other arrays.O_o and Another Young BoyAn array of $$$n$$$ positive integers $$$a_1,a_2,\ldots,a_n$$$ fell down on you from the skies, along with a positive integer $$$k \le n$$$.You can apply the following operation at most $$$k$$$ times: Choose an index $$$1 \le i \le n$$$ and an integer $$$1 \le x \le 10^9$$$. Then do $$$a_i := x$$$ (assign $$$x$$$ to $$$a_i$$$). Then build a complete undirected weighted graph with $$$n$$$ vertices numbered with integers from $$$1$$$ to $$$n$$$, where edge $$$(l, r)$$$ ($$$1 \le l < r \le n$$$) has weight $$$\min(a_{l},a_{l+1},\ldots,a_{r})$$$.You have to find the maximum possible diameter of the resulting graph after performing at most $$$k$$$ operations.The diameter of a graph is equal to $$$\max\limits_{1 \le u < v \le n}{\operatorname{d}(u, v)}$$$, where $$$\operatorname{d}(u, v)$$$ is the length of the shortest path between vertex $$$u$$$ and vertex $$$v$$$.InputEach test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows.The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$2 \le n \le 10^5$$$, $$$1 \le k \le n$$$).The second line of each test case contains $$$n$$$ positive integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_i \le 10^9$$$).It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.OutputFor each test case print one integer — the maximum possible diameter of the graph after performing at most $$$k$$$ operations.ExampleInput 63 12 4 13 21 9 843 110 2 63 2179 17 10000000002 15 92 24 2Output 4 168 10 1000000000 9 1000000000 NoteIn the first test case, one of the optimal arrays is $$$[2,4,5]$$$.The graph built on this array: $$$\operatorname{d}(1, 2) = \operatorname{d}(1, 3) = 2$$$ and $$$\operatorname{d}(2, 3) = 4$$$, so the diameter is equal to $$$\max(2,2,4) = 4$$$.
cf
// LUOGU_RID: 99112796 #include <bits/stdc++.h> // #include <iostream> #define IOS std::ios::sync_with_stdio(false);std::cin.tie(0);std::cout.tie(0); using namespace std; #define INF 0x3f3f3f3f #define endl '\n' #define int long long using pll = std::pair<int, int>; const int g = 1000000000; void solv() { int n, k; cin >> n >> k; vector<int>a(n + 8); // auto gr = [&](int x, int y) ->bool {return a[x] < a[y];}; priority_queue<pair<int, int>, vector<pll>, greater<pll>> qu; for (int i = 1;i <= n;++i) { cin >> a[i]; qu.emplace(a[i], i);//// } if (k == n) { cout << g << '\n'; return; } for (int i = 1;i < k;++i) { a[qu.top().second] = g; qu.pop(); } int ans = 0, ans1 = 0, mx1, mx2, mx; for (int i = 1;i < n;i++) ans1 = max(ans1, min(a[i], a[i + 1])); int ans2 = g; int ans3 = g; for (int i = 1;i <= n;i++) { if (a[i] < ans2) { ans3 = ans2; ans2 = a[i]; } else if (a[i] < ans3) ans3 = a[i]; } ans2 *= 2; ans3 *= 2; for (int i = 1;i <= n;i++) { int u = a[i]; a[i] = g; mx1 = a[i - 1];mx2 = a[i + 1]; mx = max(ans1, max(mx1, mx2)); if (u * 2 == ans2) ans = max(ans, min(mx, ans3)); else ans = max(ans, min(mx, ans2)); a[i] = u; } cout << ans << '\n'; } signed main() { // #ifdef LOCAL // freopen("in.txt","r",stdin); // freopen("out.txt","w",stdout); // #endif // IOS; int T = 1; cin >> T; while (T--) { solv(); } return 0; }
c++
13
0.422939
78
11.137681
138
D. Empty Graphtime limit per test1.5 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard output — Do you have a wish?  — I want people to stop gifting each other arrays.O_o and Another Young BoyAn array of $$$n$$$ positive integers $$$a_1,a_2,\ldots,a_n$$$ fell down on you from the skies, along with a positive integer $$$k \le n$$$.You can apply the following operation at most $$$k$$$ times: Choose an index $$$1 \le i \le n$$$ and an integer $$$1 \le x \le 10^9$$$. Then do $$$a_i := x$$$ (assign $$$x$$$ to $$$a_i$$$). Then build a complete undirected weighted graph with $$$n$$$ vertices numbered with integers from $$$1$$$ to $$$n$$$, where edge $$$(l, r)$$$ ($$$1 \le l < r \le n$$$) has weight $$$\min(a_{l},a_{l+1},\ldots,a_{r})$$$.You have to find the maximum possible diameter of the resulting graph after performing at most $$$k$$$ operations.The diameter of a graph is equal to $$$\max\limits_{1 \le u < v \le n}{\operatorname{d}(u, v)}$$$, where $$$\operatorname{d}(u, v)$$$ is the length of the shortest path between vertex $$$u$$$ and vertex $$$v$$$.InputEach test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows.The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$2 \le n \le 10^5$$$, $$$1 \le k \le n$$$).The second line of each test case contains $$$n$$$ positive integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_i \le 10^9$$$).It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.OutputFor each test case print one integer — the maximum possible diameter of the graph after performing at most $$$k$$$ operations.ExampleInput 63 12 4 13 21 9 843 110 2 63 2179 17 10000000002 15 92 24 2Output 4 168 10 1000000000 9 1000000000 NoteIn the first test case, one of the optimal arrays is $$$[2,4,5]$$$.The graph built on this array: $$$\operatorname{d}(1, 2) = \operatorname{d}(1, 3) = 2$$$ and $$$\operatorname{d}(2, 3) = 4$$$, so the diameter is equal to $$$\max(2,2,4) = 4$$$.
cf
for _ in range(int(input())): n, k = map(int, input().split()) arr = list(map(int, input().split())) if n == k: print(10 ** 9) continue sa = sorted([[v, i] for i, v in enumerate(arr)]) lo, hi = min(arr), 10 ** 9 while lo < hi: mid = (lo + hi + 1) // 2 cnt = k ar = list(arr) for i in range(k): if 2 * sa[i][0] < mid: ar[sa[i][1]] = 10 ** 9 cnt -= 1 else: break if 2 * min(ar) < mid: hi = mid - 1 elif cnt == 1: if max(ar) < mid: hi = mid - 1 else: lo = mid elif cnt >= 2: lo = mid else: flag = False for i in range(1, n): if min(ar[i], ar[i - 1]) >= mid: flag = True break if flag: lo = mid else: hi = mid - 1 print(hi)
python
16
0.324144
52
13.04
75
D. Empty Graphtime limit per test1.5 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard output — Do you have a wish?  — I want people to stop gifting each other arrays.O_o and Another Young BoyAn array of $$$n$$$ positive integers $$$a_1,a_2,\ldots,a_n$$$ fell down on you from the skies, along with a positive integer $$$k \le n$$$.You can apply the following operation at most $$$k$$$ times: Choose an index $$$1 \le i \le n$$$ and an integer $$$1 \le x \le 10^9$$$. Then do $$$a_i := x$$$ (assign $$$x$$$ to $$$a_i$$$). Then build a complete undirected weighted graph with $$$n$$$ vertices numbered with integers from $$$1$$$ to $$$n$$$, where edge $$$(l, r)$$$ ($$$1 \le l < r \le n$$$) has weight $$$\min(a_{l},a_{l+1},\ldots,a_{r})$$$.You have to find the maximum possible diameter of the resulting graph after performing at most $$$k$$$ operations.The diameter of a graph is equal to $$$\max\limits_{1 \le u < v \le n}{\operatorname{d}(u, v)}$$$, where $$$\operatorname{d}(u, v)$$$ is the length of the shortest path between vertex $$$u$$$ and vertex $$$v$$$.InputEach test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows.The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$2 \le n \le 10^5$$$, $$$1 \le k \le n$$$).The second line of each test case contains $$$n$$$ positive integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_i \le 10^9$$$).It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.OutputFor each test case print one integer — the maximum possible diameter of the graph after performing at most $$$k$$$ operations.ExampleInput 63 12 4 13 21 9 843 110 2 63 2179 17 10000000002 15 92 24 2Output 4 168 10 1000000000 9 1000000000 NoteIn the first test case, one of the optimal arrays is $$$[2,4,5]$$$.The graph built on this array: $$$\operatorname{d}(1, 2) = \operatorname{d}(1, 3) = 2$$$ and $$$\operatorname{d}(2, 3) = 4$$$, so the diameter is equal to $$$\max(2,2,4) = 4$$$.
cf
t = int(input()) for test in range(t): n, k = map(int, input().split()) nums = list(map(int, input().split())) numSorted = list(enumerate(nums)) numSorted.sort(key = lambda x: x[1]) ptr = 0 # print(numSorted) # print(nums) # changed = set() flag = False if k == len(nums): print(10**9) continue if k > 1: while k > 0: nums[numSorted[ptr][0]] = 10**9 k -= 1 ptr += 1 traversals = [min(nums[i], nums[i+1]) for i in range(len(nums)-1)] tempMax = max(traversals) print(max(min(tempMax, 2*numSorted[ptr][1]), min(10**9, 2*numSorted[ptr-1][1]))) else: opt1 = min(numSorted[-1][1], 2*numSorted[0][1]) #make largest traversal nums[numSorted[ptr][0]] = 10**9 #convert smallest num traversals = [min(nums[i], nums[i+1]) for i in range(len(nums)-1)] tempMax = max(traversals) opt2 = min(tempMax, 2*numSorted[1][1]) print(max(opt1, opt2)) continue # if tempMax >= 2*numSorted[ptr][1]: # print(2*numSorted[ptr][1]) # else: # if tempMax >= 2*numSorted[ptr-1][1] or tempMax == 10**9: # print(tempMax) # else: # print(2*numSorted[ptr-1][1]) # changed.add
python
16
0.489485
88
14.322222
90
D. Empty Graphtime limit per test1.5 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard output — Do you have a wish?  — I want people to stop gifting each other arrays.O_o and Another Young BoyAn array of $$$n$$$ positive integers $$$a_1,a_2,\ldots,a_n$$$ fell down on you from the skies, along with a positive integer $$$k \le n$$$.You can apply the following operation at most $$$k$$$ times: Choose an index $$$1 \le i \le n$$$ and an integer $$$1 \le x \le 10^9$$$. Then do $$$a_i := x$$$ (assign $$$x$$$ to $$$a_i$$$). Then build a complete undirected weighted graph with $$$n$$$ vertices numbered with integers from $$$1$$$ to $$$n$$$, where edge $$$(l, r)$$$ ($$$1 \le l < r \le n$$$) has weight $$$\min(a_{l},a_{l+1},\ldots,a_{r})$$$.You have to find the maximum possible diameter of the resulting graph after performing at most $$$k$$$ operations.The diameter of a graph is equal to $$$\max\limits_{1 \le u < v \le n}{\operatorname{d}(u, v)}$$$, where $$$\operatorname{d}(u, v)$$$ is the length of the shortest path between vertex $$$u$$$ and vertex $$$v$$$.InputEach test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows.The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$2 \le n \le 10^5$$$, $$$1 \le k \le n$$$).The second line of each test case contains $$$n$$$ positive integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_i \le 10^9$$$).It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.OutputFor each test case print one integer — the maximum possible diameter of the graph after performing at most $$$k$$$ operations.ExampleInput 63 12 4 13 21 9 843 110 2 63 2179 17 10000000002 15 92 24 2Output 4 168 10 1000000000 9 1000000000 NoteIn the first test case, one of the optimal arrays is $$$[2,4,5]$$$.The graph built on this array: $$$\operatorname{d}(1, 2) = \operatorname{d}(1, 3) = 2$$$ and $$$\operatorname{d}(2, 3) = 4$$$, so the diameter is equal to $$$\max(2,2,4) = 4$$$.
cf
import sys input = sys.stdin.readline def solve(): n, k = map(int, input().split()) arr = list(map(int, input().split())) if n == k: return 10 ** 9 lo, hi = min(arr), 10 ** 9 sa = sorted([[v, i] for i, v in enumerate(arr)]) while lo < hi: mid = (lo + hi + 1) // 2 if sa[k][0] >= mid: lo = mid continue cnt = k ar = list(arr) for i in range(k): if 2 * sa[i][0] < mid: cnt -= 1 ar[sa[i][1]] = 10**9 else: break mi = min(ar) if 2 * mi < mid: hi = mid - 1 elif cnt >= 2: lo = mid elif cnt == 1: if max(ar) >= mid: lo = mid else: hi = mid - 1 else: f = True for i in range(1, n): if min(ar[i], ar[i-1]) >= mid: f = False break if f: hi = mid - 1 else: lo = mid return hi for _ in range(int(input())): print(solve())
python
16
0.334448
52
11.090909
99
D. Empty Graphtime limit per test1.5 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard output — Do you have a wish?  — I want people to stop gifting each other arrays.O_o and Another Young BoyAn array of $$$n$$$ positive integers $$$a_1,a_2,\ldots,a_n$$$ fell down on you from the skies, along with a positive integer $$$k \le n$$$.You can apply the following operation at most $$$k$$$ times: Choose an index $$$1 \le i \le n$$$ and an integer $$$1 \le x \le 10^9$$$. Then do $$$a_i := x$$$ (assign $$$x$$$ to $$$a_i$$$). Then build a complete undirected weighted graph with $$$n$$$ vertices numbered with integers from $$$1$$$ to $$$n$$$, where edge $$$(l, r)$$$ ($$$1 \le l < r \le n$$$) has weight $$$\min(a_{l},a_{l+1},\ldots,a_{r})$$$.You have to find the maximum possible diameter of the resulting graph after performing at most $$$k$$$ operations.The diameter of a graph is equal to $$$\max\limits_{1 \le u < v \le n}{\operatorname{d}(u, v)}$$$, where $$$\operatorname{d}(u, v)$$$ is the length of the shortest path between vertex $$$u$$$ and vertex $$$v$$$.InputEach test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows.The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$2 \le n \le 10^5$$$, $$$1 \le k \le n$$$).The second line of each test case contains $$$n$$$ positive integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_i \le 10^9$$$).It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.OutputFor each test case print one integer — the maximum possible diameter of the graph after performing at most $$$k$$$ operations.ExampleInput 63 12 4 13 21 9 843 110 2 63 2179 17 10000000002 15 92 24 2Output 4 168 10 1000000000 9 1000000000 NoteIn the first test case, one of the optimal arrays is $$$[2,4,5]$$$.The graph built on this array: $$$\operatorname{d}(1, 2) = \operatorname{d}(1, 3) = 2$$$ and $$$\operatorname{d}(2, 3) = 4$$$, so the diameter is equal to $$$\max(2,2,4) = 4$$$.
cf
#include <bits/stdc++.h> using namespace std; #define int long long #define INF 1e9 #define endl '\n' const int N = 2e5 + 10; int n,m,q,k; int a[N]; string s; typedef pair<int, int> PII; struct Node { int id, w; bool operator < (const Node &t) const { return w < t.w; } }p[N]; void solve() { cin >> n >> k; int res , res1 = INF, res2 = INF; for(int i = 1 ; i <= n ; i ++ ) cin >> a[i], p[i] = {i, a[i]}; sort(p + 1, p + n + 1); for(int i = 1 ; i <= k - 1 ; i ++ ) { p[i].w = INF; a[p[i].id] = INF; } sort(p + 1, p + n + 1); // 加k-1次,最大值在最小值*2和最大值的最小值之间产生 res1 = min(res1, p[1].w * 2); res1 = min(res1, p[n].w); // 加k次,最大值在min(a[i], a[i + 1])的最大值和最小值*2之间产生 p[1].w = INF; a[p[1].id] = INF; sort(p + 1, p + n + 1); res2 = min(res2, p[1].w * 2); // int temp = -INF; for(int i = 1 ; i <= n ; i ++ ) a[p[i].id] = p[i].w; for(int i = 1 ; i < n ; i ++ ) temp = max(temp, min(a[i], a[i + 1])); res2 = min(temp, res2); res = max(res2, res1); cout << res << endl; } signed main() { int t = 1; cin >> t; while(t--) { solve(); } }
c++
13
0.417591
66
8.790698
129
D. Empty Graphtime limit per test1.5 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard output — Do you have a wish?  — I want people to stop gifting each other arrays.O_o and Another Young BoyAn array of $$$n$$$ positive integers $$$a_1,a_2,\ldots,a_n$$$ fell down on you from the skies, along with a positive integer $$$k \le n$$$.You can apply the following operation at most $$$k$$$ times: Choose an index $$$1 \le i \le n$$$ and an integer $$$1 \le x \le 10^9$$$. Then do $$$a_i := x$$$ (assign $$$x$$$ to $$$a_i$$$). Then build a complete undirected weighted graph with $$$n$$$ vertices numbered with integers from $$$1$$$ to $$$n$$$, where edge $$$(l, r)$$$ ($$$1 \le l < r \le n$$$) has weight $$$\min(a_{l},a_{l+1},\ldots,a_{r})$$$.You have to find the maximum possible diameter of the resulting graph after performing at most $$$k$$$ operations.The diameter of a graph is equal to $$$\max\limits_{1 \le u < v \le n}{\operatorname{d}(u, v)}$$$, where $$$\operatorname{d}(u, v)$$$ is the length of the shortest path between vertex $$$u$$$ and vertex $$$v$$$.InputEach test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows.The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$2 \le n \le 10^5$$$, $$$1 \le k \le n$$$).The second line of each test case contains $$$n$$$ positive integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_i \le 10^9$$$).It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.OutputFor each test case print one integer — the maximum possible diameter of the graph after performing at most $$$k$$$ operations.ExampleInput 63 12 4 13 21 9 843 110 2 63 2179 17 10000000002 15 92 24 2Output 4 168 10 1000000000 9 1000000000 NoteIn the first test case, one of the optimal arrays is $$$[2,4,5]$$$.The graph built on this array: $$$\operatorname{d}(1, 2) = \operatorname{d}(1, 3) = 2$$$ and $$$\operatorname{d}(2, 3) = 4$$$, so the diameter is equal to $$$\max(2,2,4) = 4$$$.
cf
import java.io.*; import java.util.*; public class d { public static void main(String[] args) throws Exception { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); int nc = Integer.parseInt(in.readLine()); for (int cn = 0; cn < nc; cn++) { StringTokenizer tokenizer = new StringTokenizer(in.readLine()); int n = Integer.parseInt(tokenizer.nextToken()); int k = Integer.parseInt(tokenizer.nextToken()); tokenizer = new StringTokenizer(in.readLine()); ArrayList<Integer> a = new ArrayList<>(); for (int i = 0; i < n; i++) { int v = Integer.parseInt(tokenizer.nextToken()); a.add(v); } int low = 0; int high = 1000000000; while (low < high) { int mid = low + (high - low + 1) / 2; if (test(a, mid) <= k) { low = mid; } else { high = mid - 1; } } System.out.println(low); } in.close(); } public static int test(ArrayList<Integer> a, int val) { ArrayList<Integer> arr = cp(a); int cnt = 0; for (int i = 0; i < arr.size(); i++) { if (arr.get(i) * 2 < val) { arr.set(i, 1000000000); cnt++; } } for (int i = 0; i + 1 < arr.size(); i++) { if (Math.min(arr.get(i), arr.get(i + 1)) >= val) { return cnt; } } for (int i = 0; i + 1 < arr.size(); i++) { if (Math.min(arr.get(i), arr.get(i + 1)) < val && Math.max(arr.get(i), arr.get(i + 1)) >= val) { if (arr.get(i) < arr.get(i + 1)) { arr.set(i, 1000000000); cnt++; } else { arr.set(i + 1, 1000000000); cnt++; } return cnt; } } for (int i = 0; i + 1 < arr.size(); i++) { if (Math.max(arr.get(i), arr.get(i + 1)) < val) { arr.set(i, 1000000000); cnt++; arr.set(i + 1, 1000000000); cnt++; return cnt; } } return cnt; } public static ArrayList<Integer> cp(ArrayList<Integer> arr) { ArrayList<Integer> a = new ArrayList<>(); for (int i = 0; i < arr.size(); i++) { a.add(arr.get(i)); } return a; } }
java
16
0.401493
108
16.076433
157
D. Empty Graphtime limit per test1.5 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard output — Do you have a wish?  — I want people to stop gifting each other arrays.O_o and Another Young BoyAn array of $$$n$$$ positive integers $$$a_1,a_2,\ldots,a_n$$$ fell down on you from the skies, along with a positive integer $$$k \le n$$$.You can apply the following operation at most $$$k$$$ times: Choose an index $$$1 \le i \le n$$$ and an integer $$$1 \le x \le 10^9$$$. Then do $$$a_i := x$$$ (assign $$$x$$$ to $$$a_i$$$). Then build a complete undirected weighted graph with $$$n$$$ vertices numbered with integers from $$$1$$$ to $$$n$$$, where edge $$$(l, r)$$$ ($$$1 \le l < r \le n$$$) has weight $$$\min(a_{l},a_{l+1},\ldots,a_{r})$$$.You have to find the maximum possible diameter of the resulting graph after performing at most $$$k$$$ operations.The diameter of a graph is equal to $$$\max\limits_{1 \le u < v \le n}{\operatorname{d}(u, v)}$$$, where $$$\operatorname{d}(u, v)$$$ is the length of the shortest path between vertex $$$u$$$ and vertex $$$v$$$.InputEach test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows.The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$2 \le n \le 10^5$$$, $$$1 \le k \le n$$$).The second line of each test case contains $$$n$$$ positive integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_i \le 10^9$$$).It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.OutputFor each test case print one integer — the maximum possible diameter of the graph after performing at most $$$k$$$ operations.ExampleInput 63 12 4 13 21 9 843 110 2 63 2179 17 10000000002 15 92 24 2Output 4 168 10 1000000000 9 1000000000 NoteIn the first test case, one of the optimal arrays is $$$[2,4,5]$$$.The graph built on this array: $$$\operatorname{d}(1, 2) = \operatorname{d}(1, 3) = 2$$$ and $$$\operatorname{d}(2, 3) = 4$$$, so the diameter is equal to $$$\max(2,2,4) = 4$$$.
cf
#include<bits/stdc++.h> using namespace std; typedef long long ll; #define endl '\n' const int maxn=1e5+11; const int Maxx=5000+11; const int mod=19260817; int t; int n,k; ll a[maxn]; int vis[maxn]; void solve() { cin>>n>>k; for(int i=1;i<=n;++i) cin>>a[i]; priority_queue<pair<ll,int>,vector<pair<ll,int>>,greater<pair<ll,int>>>pq; for(int i=1;i<=n;++i) pq.emplace(a[i],i); for(int i=1;i<=n;++i) vis[i]=0; for(int i=1;i<=k-1;++i) { auto it=pq.top(); a[it.second]=int(1e9); vis[it.second]=1; pq.pop(); } set<pair<ll,int>>st; for(int i=1;i<=n;++i) st.emplace(a[i],i); ll mx=0; for(int i=1;i<=n-1;++i) mx=max(mx,min(a[i],a[i+1])); ll ans=0; for(int i=1;i<=n;++i) { if(vis[i]) continue; ll tmpmx=mx; if(i-1>=1) tmpmx=max(tmpmx,min(a[i-1],ll(1e9))); if(i+1<=n) tmpmx=max(tmpmx,min(a[i+1],ll(1e9))); st.erase(make_pair(a[i],i)); ll Mn=2ll*(*st.begin()).first; ans=max(ans,min(tmpmx,Mn)); st.emplace(make_pair(a[i],i)); } cout<<ans<<endl; } int main() { //scanf("%d",&t); //ios::sync_with_stdio(false);cin.tie(0); ios::sync_with_stdio(false);cin.tie(0);cin>>t;while(t--) solve(); return 0; }
c++
15
0.492604
79
12.39604
101
D. Empty Graphtime limit per test1.5 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard output — Do you have a wish?  — I want people to stop gifting each other arrays.O_o and Another Young BoyAn array of $$$n$$$ positive integers $$$a_1,a_2,\ldots,a_n$$$ fell down on you from the skies, along with a positive integer $$$k \le n$$$.You can apply the following operation at most $$$k$$$ times: Choose an index $$$1 \le i \le n$$$ and an integer $$$1 \le x \le 10^9$$$. Then do $$$a_i := x$$$ (assign $$$x$$$ to $$$a_i$$$). Then build a complete undirected weighted graph with $$$n$$$ vertices numbered with integers from $$$1$$$ to $$$n$$$, where edge $$$(l, r)$$$ ($$$1 \le l < r \le n$$$) has weight $$$\min(a_{l},a_{l+1},\ldots,a_{r})$$$.You have to find the maximum possible diameter of the resulting graph after performing at most $$$k$$$ operations.The diameter of a graph is equal to $$$\max\limits_{1 \le u < v \le n}{\operatorname{d}(u, v)}$$$, where $$$\operatorname{d}(u, v)$$$ is the length of the shortest path between vertex $$$u$$$ and vertex $$$v$$$.InputEach test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows.The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$2 \le n \le 10^5$$$, $$$1 \le k \le n$$$).The second line of each test case contains $$$n$$$ positive integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_i \le 10^9$$$).It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.OutputFor each test case print one integer — the maximum possible diameter of the graph after performing at most $$$k$$$ operations.ExampleInput 63 12 4 13 21 9 843 110 2 63 2179 17 10000000002 15 92 24 2Output 4 168 10 1000000000 9 1000000000 NoteIn the first test case, one of the optimal arrays is $$$[2,4,5]$$$.The graph built on this array: $$$\operatorname{d}(1, 2) = \operatorname{d}(1, 3) = 2$$$ and $$$\operatorname{d}(2, 3) = 4$$$, so the diameter is equal to $$$\max(2,2,4) = 4$$$.
cf
for _ in range(int(input())): n,k=map(int,input().split()) a=list(map(int,input().split())) if k==n: print(10**9) continue b=[] for i in range(n): b.append((a[i],i)) b.sort() i=0 while k>1: a[b[i][1]]=10**9 i+=1 k-=1 ans=0 j=i for i in range(1,n): x,y=a[i-1],a[i] z=b[j][0]*2 if i-1<=b[j][1]<=i: z=b[j+1][0]*2 z1 = b[j + 1][0] * 2 ans=max(ans,min(max(x,y),z),min(min(x,y),z1)) print(ans)
python
13
0.381038
53
10.428571
49
D. Empty Graphtime limit per test1.5 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard output — Do you have a wish?  — I want people to stop gifting each other arrays.O_o and Another Young BoyAn array of $$$n$$$ positive integers $$$a_1,a_2,\ldots,a_n$$$ fell down on you from the skies, along with a positive integer $$$k \le n$$$.You can apply the following operation at most $$$k$$$ times: Choose an index $$$1 \le i \le n$$$ and an integer $$$1 \le x \le 10^9$$$. Then do $$$a_i := x$$$ (assign $$$x$$$ to $$$a_i$$$). Then build a complete undirected weighted graph with $$$n$$$ vertices numbered with integers from $$$1$$$ to $$$n$$$, where edge $$$(l, r)$$$ ($$$1 \le l < r \le n$$$) has weight $$$\min(a_{l},a_{l+1},\ldots,a_{r})$$$.You have to find the maximum possible diameter of the resulting graph after performing at most $$$k$$$ operations.The diameter of a graph is equal to $$$\max\limits_{1 \le u < v \le n}{\operatorname{d}(u, v)}$$$, where $$$\operatorname{d}(u, v)$$$ is the length of the shortest path between vertex $$$u$$$ and vertex $$$v$$$.InputEach test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows.The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$2 \le n \le 10^5$$$, $$$1 \le k \le n$$$).The second line of each test case contains $$$n$$$ positive integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_i \le 10^9$$$).It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.OutputFor each test case print one integer — the maximum possible diameter of the graph after performing at most $$$k$$$ operations.ExampleInput 63 12 4 13 21 9 843 110 2 63 2179 17 10000000002 15 92 24 2Output 4 168 10 1000000000 9 1000000000 NoteIn the first test case, one of the optimal arrays is $$$[2,4,5]$$$.The graph built on this array: $$$\operatorname{d}(1, 2) = \operatorname{d}(1, 3) = 2$$$ and $$$\operatorname{d}(2, 3) = 4$$$, so the diameter is equal to $$$\max(2,2,4) = 4$$$.
cf
import java.io.*; import java.util.*; public class EmptyGraph { public static void main(String[] args) throws IOException { // BufferedReader in = new BufferedReader(new FileReader("EmptyGraph.in")); // PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("EmptyGraph.out"))); BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); PrintWriter out = new PrintWriter(System.out); int T = Integer.parseInt(in.readLine()); for (int t = 0; t < T; t++) { StringTokenizer st1 = new StringTokenizer(in.readLine()); StringTokenizer st2 = new StringTokenizer(in.readLine()); int N = Integer.parseInt(st1.nextToken()); int K = Integer.parseInt(st1.nextToken()); int[] a = new int[N]; for (int i = 0; i < N; i++) { a[i] = Integer.parseInt(st2.nextToken()); } int lo = 0; int hi = 1000000000; while (lo < hi) { int mid = (lo + hi + 1) / 2; int k = 0; int[] v = new int[N]; for (int i = 0; i < N; i++) { if (a[i] * 2 < mid) { v[i] = 1000000000; k++; } else { v[i] = a[i]; } } int maxmin = 0; int maxmax = 0; for (int i = 0; i < N - 1; i++) { maxmin = Math.max(maxmin, Math.min(v[i], v[i + 1])); maxmax = Math.max(maxmax, Math.max(v[i], v[i + 1])); } if (maxmin < mid) k++; if (maxmax < mid) k++; if (k <= K) { lo = mid; } else { hi = mid - 1; } } out.println(lo); } out.close(); in.close(); } }
java
19
0.547257
93
24.233333
60
D. Empty Graphtime limit per test1.5 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard output — Do you have a wish?  — I want people to stop gifting each other arrays.O_o and Another Young BoyAn array of $$$n$$$ positive integers $$$a_1,a_2,\ldots,a_n$$$ fell down on you from the skies, along with a positive integer $$$k \le n$$$.You can apply the following operation at most $$$k$$$ times: Choose an index $$$1 \le i \le n$$$ and an integer $$$1 \le x \le 10^9$$$. Then do $$$a_i := x$$$ (assign $$$x$$$ to $$$a_i$$$). Then build a complete undirected weighted graph with $$$n$$$ vertices numbered with integers from $$$1$$$ to $$$n$$$, where edge $$$(l, r)$$$ ($$$1 \le l < r \le n$$$) has weight $$$\min(a_{l},a_{l+1},\ldots,a_{r})$$$.You have to find the maximum possible diameter of the resulting graph after performing at most $$$k$$$ operations.The diameter of a graph is equal to $$$\max\limits_{1 \le u < v \le n}{\operatorname{d}(u, v)}$$$, where $$$\operatorname{d}(u, v)$$$ is the length of the shortest path between vertex $$$u$$$ and vertex $$$v$$$.InputEach test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows.The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$2 \le n \le 10^5$$$, $$$1 \le k \le n$$$).The second line of each test case contains $$$n$$$ positive integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_i \le 10^9$$$).It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.OutputFor each test case print one integer — the maximum possible diameter of the graph after performing at most $$$k$$$ operations.ExampleInput 63 12 4 13 21 9 843 110 2 63 2179 17 10000000002 15 92 24 2Output 4 168 10 1000000000 9 1000000000 NoteIn the first test case, one of the optimal arrays is $$$[2,4,5]$$$.The graph built on this array: $$$\operatorname{d}(1, 2) = \operatorname{d}(1, 3) = 2$$$ and $$$\operatorname{d}(2, 3) = 4$$$, so the diameter is equal to $$$\max(2,2,4) = 4$$$.
cf
/// What are you doing now? Just go f*cking code now dude? #include <bits/stdc++.h> #pragma GCC optimize("Ofast") #define TASK "codin" //#define int long long using namespace std; typedef long long ll; typedef long double ld; typedef unsigned long long llu; #define IO ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0); #define fi first #define se second #define mp make_pair #define pb push_back #define is insert #define eb emplace_back #define FOR(x,a,b) for (ll x=a;x<=b;x++) #define FOD(x,a,b) for (ll x=a;x>=b;x--) #define FER(x,a,b) for (ll x=a;x<b;x++) #define FED(x,a,b) for (ll x=a;x>b;x--) #define EL "\n" #define ALL(v) v.begin(),v.end() #define vi vector<ll> #define vii vector<pii> #define pii pair<int,int> ///---------- TEMPLATE ABOUT BIT ----------/// ll getbit(ll val, ll num){ return ((val >> (num)) & 1LL); } ll offbit(ll val, ll num){ return ((val ^ (1LL << (num - 1)))); } ll setbit(ll k, ll s) { return (k &~ (1 << s)); } ///---------- TEMPLATE ABOUT MATH ----------/// ll lcm(ll a, ll b){ return a * b/__gcd(a, b); } ll bmul(ll a, ll b, ll mod){ if(b == 0){return 0;} if(b == 1){return a;} ll t = bmul(a, b/2, mod);t = (t + t)%mod; if(b%2 == 1){t = (t + a) % mod;}return t; } ll bpow(ll n, ll m, ll mod){ ll res = 1; while (m) { if (m & 1) res = res * n % mod; n = n * n % mod; m >>= 1; } return res; } ///----------------------------------------/// const int S = 5e5 + 5, M = 2e3 + 4; const ll mod = 1e9 + 7, hashmod = 1e9 + 9, inf = 1e18; const int base = 311, BLOCK_SIZE = 420; const int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1}; const int dxx[8] = {-1, -1, 0, 1, 1, 1, 0, -1}, dyy[8]={0, 1, 1, 1, 0, -1, -1, -1}; ///------ The main code starts here ------/// int n, k, a[S], b[S]; bool check(int x){ FOR(i, 1, n){ b[i] = a[i]; } int cnt = 0; FOR(i, 1, n){ if(2 * b[i] < x){ b[i] = 1e9; cnt++; } if(cnt > k){ return false; } } cnt = k - cnt; if(cnt >= 2){ return true; } if(cnt == 0){ FOR(i, 1, n - 1){ if(b[i] >= x && b[i + 1] >= x){ return true; } } return false; } int tmp = 0; FOR(i, 1, n){ tmp = max(tmp, b[i]); } return (tmp >= x); } void solve(){ cin >> n >> k; FOR(i, 1, n){ cin >> a[i]; } int le = 1, ri = 1e9, ans = 0; while(le <= ri){ int mi = (le + ri) / 2; if(check(mi)){ ans = mi; le = mi + 1; } else{ ri = mi - 1; } } cout << ans << EL; } signed main(){ IO if(fopen(TASK".inp","r")){ freopen(TASK".inp","r",stdin); freopen(TASK".out","w",stdout); } int t = 1; cin >> t; while(t--){ solve(); } }
c++
13
0.426548
83
9.765957
282
D. Empty Graphtime limit per test1.5 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard output — Do you have a wish?  — I want people to stop gifting each other arrays.O_o and Another Young BoyAn array of $$$n$$$ positive integers $$$a_1,a_2,\ldots,a_n$$$ fell down on you from the skies, along with a positive integer $$$k \le n$$$.You can apply the following operation at most $$$k$$$ times: Choose an index $$$1 \le i \le n$$$ and an integer $$$1 \le x \le 10^9$$$. Then do $$$a_i := x$$$ (assign $$$x$$$ to $$$a_i$$$). Then build a complete undirected weighted graph with $$$n$$$ vertices numbered with integers from $$$1$$$ to $$$n$$$, where edge $$$(l, r)$$$ ($$$1 \le l < r \le n$$$) has weight $$$\min(a_{l},a_{l+1},\ldots,a_{r})$$$.You have to find the maximum possible diameter of the resulting graph after performing at most $$$k$$$ operations.The diameter of a graph is equal to $$$\max\limits_{1 \le u < v \le n}{\operatorname{d}(u, v)}$$$, where $$$\operatorname{d}(u, v)$$$ is the length of the shortest path between vertex $$$u$$$ and vertex $$$v$$$.InputEach test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows.The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$2 \le n \le 10^5$$$, $$$1 \le k \le n$$$).The second line of each test case contains $$$n$$$ positive integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_i \le 10^9$$$).It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.OutputFor each test case print one integer — the maximum possible diameter of the graph after performing at most $$$k$$$ operations.ExampleInput 63 12 4 13 21 9 843 110 2 63 2179 17 10000000002 15 92 24 2Output 4 168 10 1000000000 9 1000000000 NoteIn the first test case, one of the optimal arrays is $$$[2,4,5]$$$.The graph built on this array: $$$\operatorname{d}(1, 2) = \operatorname{d}(1, 3) = 2$$$ and $$$\operatorname{d}(2, 3) = 4$$$, so the diameter is equal to $$$\max(2,2,4) = 4$$$.
cf
#include <bits/stdc++.h> using namespace std; using LL = long long; #define endl '\n' using db = double; template <class T> using max_heap = priority_queue<T>; template <class T> using min_heap = priority_queue<T, vector<T>, greater<>>; void solve() { int n, m; cin >> n >> m; vector<int> a(n + 1); for (int i = 1; i <= n; ++i) cin >> a[i]; auto check = [&](int x) -> bool { // cerr << "mid = " << x << endl; int s = 0; for (int i = 1; i <= n; ++i) s += (a[i] < (x + 1) / 2); bool ok = 0; for (int i = 1; i + 1 <= n; ++i) { int d = m - s; int v1 = (a[i] >= x || a[i] < (x + 1) / 2); if (v1 == 0 && d > 0) { v1 = 1; d--; } int v2 = (a[i + 1] >= x || a[i + 1] < (x + 1) / 2); if (v2 == 0 && d > 0) { v2 = 1; d--; } ok |= (v1 & v2); } // cerr << "s = " << s << endl; // cerr << "ok = " << ok << endl; return (s <= m & ok); }; int l = 1, r = 1e9; while (l < r) { int mid = (l + r + 1) / 2; if (check(mid)) l = mid; else r = mid - 1; } cout << l << endl; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int T; cin >> T; while (T--) solve(); return 0; }
c++
17
0.324499
63
10.136691
139
D. Empty Graphtime limit per test1.5 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard output — Do you have a wish?  — I want people to stop gifting each other arrays.O_o and Another Young BoyAn array of $$$n$$$ positive integers $$$a_1,a_2,\ldots,a_n$$$ fell down on you from the skies, along with a positive integer $$$k \le n$$$.You can apply the following operation at most $$$k$$$ times: Choose an index $$$1 \le i \le n$$$ and an integer $$$1 \le x \le 10^9$$$. Then do $$$a_i := x$$$ (assign $$$x$$$ to $$$a_i$$$). Then build a complete undirected weighted graph with $$$n$$$ vertices numbered with integers from $$$1$$$ to $$$n$$$, where edge $$$(l, r)$$$ ($$$1 \le l < r \le n$$$) has weight $$$\min(a_{l},a_{l+1},\ldots,a_{r})$$$.You have to find the maximum possible diameter of the resulting graph after performing at most $$$k$$$ operations.The diameter of a graph is equal to $$$\max\limits_{1 \le u < v \le n}{\operatorname{d}(u, v)}$$$, where $$$\operatorname{d}(u, v)$$$ is the length of the shortest path between vertex $$$u$$$ and vertex $$$v$$$.InputEach test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows.The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$2 \le n \le 10^5$$$, $$$1 \le k \le n$$$).The second line of each test case contains $$$n$$$ positive integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_i \le 10^9$$$).It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.OutputFor each test case print one integer — the maximum possible diameter of the graph after performing at most $$$k$$$ operations.ExampleInput 63 12 4 13 21 9 843 110 2 63 2179 17 10000000002 15 92 24 2Output 4 168 10 1000000000 9 1000000000 NoteIn the first test case, one of the optimal arrays is $$$[2,4,5]$$$.The graph built on this array: $$$\operatorname{d}(1, 2) = \operatorname{d}(1, 3) = 2$$$ and $$$\operatorname{d}(2, 3) = 4$$$, so the diameter is equal to $$$\max(2,2,4) = 4$$$.
cf
import sys input=sys.stdin.readline max_value=10**9 def check(target): y=x.copy() ans=0 for i in range(n): if y[i]<(target+1)//2: ans+=1 y[i]=max_value best_value=-1 for i in range(n-1): best_value=max(best_value,min(y[i],y[i+1])) if best_value<target: if max(y)>=target: ans+=1 else: ans+=2 return ans<=k for _ in range(int(input())): n,k=map(int,input().split()) x=list(map(int,input().split())) low,high=0,10**9 while low<=high: mid=(low+high)>>1 if check(mid): low=mid+1 else: high=mid-1 print(high)
python
13
0.471831
51
10.833333
60
D. Empty Graphtime limit per test1.5 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard output — Do you have a wish?  — I want people to stop gifting each other arrays.O_o and Another Young BoyAn array of $$$n$$$ positive integers $$$a_1,a_2,\ldots,a_n$$$ fell down on you from the skies, along with a positive integer $$$k \le n$$$.You can apply the following operation at most $$$k$$$ times: Choose an index $$$1 \le i \le n$$$ and an integer $$$1 \le x \le 10^9$$$. Then do $$$a_i := x$$$ (assign $$$x$$$ to $$$a_i$$$). Then build a complete undirected weighted graph with $$$n$$$ vertices numbered with integers from $$$1$$$ to $$$n$$$, where edge $$$(l, r)$$$ ($$$1 \le l < r \le n$$$) has weight $$$\min(a_{l},a_{l+1},\ldots,a_{r})$$$.You have to find the maximum possible diameter of the resulting graph after performing at most $$$k$$$ operations.The diameter of a graph is equal to $$$\max\limits_{1 \le u < v \le n}{\operatorname{d}(u, v)}$$$, where $$$\operatorname{d}(u, v)$$$ is the length of the shortest path between vertex $$$u$$$ and vertex $$$v$$$.InputEach test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows.The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$2 \le n \le 10^5$$$, $$$1 \le k \le n$$$).The second line of each test case contains $$$n$$$ positive integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_i \le 10^9$$$).It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.OutputFor each test case print one integer — the maximum possible diameter of the graph after performing at most $$$k$$$ operations.ExampleInput 63 12 4 13 21 9 843 110 2 63 2179 17 10000000002 15 92 24 2Output 4 168 10 1000000000 9 1000000000 NoteIn the first test case, one of the optimal arrays is $$$[2,4,5]$$$.The graph built on this array: $$$\operatorname{d}(1, 2) = \operatorname{d}(1, 3) = 2$$$ and $$$\operatorname{d}(2, 3) = 4$$$, so the diameter is equal to $$$\max(2,2,4) = 4$$$.
cf
#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; template <class T> using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; template <class K, class V> using omap = tree<K, V, less<K>, rb_tree_tag, tree_order_statistics_node_update>; void __print(int x) {cout << x;} void __print(long long x) {cout << x;} void __print(double x) {cout << x;} void __print(long double x) {cout << x;} void __print(char x) {cout << '\'' << x << '\'';} void __print(const char *x) {cout << '\"' << x << '\"';} void __print(const string &x) {cout << '\"' << x << '\"';} void __print(bool x) {cout << (x ? "True" : "False");} template<typename T, typename V> void __print(const pair<T, V>&x) {cout << '{'; __print(x.first); cout << ','; __print(x.second); cout << '}';} template<typename T> void __print(const T &x) {int f = 0; cout << '{'; for (auto &i : x)cout << (f++ ? "," : ""), __print(i); cout << "}";} void _print() {cout << "]\n";} template <typename T, typename... V> void _print(T t, V... v) {__print(t); if (sizeof...(v)) cout << ", "; _print(v...);} #define debug(x...) cout << "[" << #x << "] = ["; _print(x) #define odrkey order_of_key #define fbodr find_by_order #define sq(a) ((a)*(a)) #define ull unsigned long long #define all(x) (x).begin(),(x).end() #define rall(x) (x).rbegin(),(x).rend() #define pi 3.1415926536 #define nwl cout <<"\n"; #define ff first #define ss second #define pb push_back #define pf push_front #define eps 0.000001 typedef long long ll; const int mod = 998244353; ll bigmod(ll a, ll b) { ll res = 1; while (b) { if (b % 2)res = (res * a) % mod; a = (a * a) % mod; b >>= 1; } return res; } ll ncr(ll n, ll r) { if (r > n - r) r = n - r;// O(r) ll up = 1, down = 1; for (ll i = 0; i < r; i++) { up = (up * (n - i)) % mod; down = (down * (r - i)) % mod; } return (up * bigmod(down, mod - 2)) % mod; } ll lcm(ll a, ll b) { return (a * b) / __gcd(a, b); } int dx[4] = {1, -1, 0, 0}; int dy[4] = {0, 0, 1, -1}; const int N = 2e5 + 10; int main() { ios::sync_with_stdio(0); cin.tie(0); int t; cin>>t; while(t--){ int n,k; cin>>n>>k; int ar[n]; oset<pair<int,int>>st; for(int i=0;i<n;i++){ cin>>ar[i]; st.insert({ar[i],i}); } int mx=1e9; int ans=0; for(int i=1;i<n;i++){ pair<int,int> p={ar[i-1],i-1}; pair<int,int> q={ar[i],i}; st.erase(p); st.erase(q); int tm=min(ar[i],ar[i-1]); { // 0 0 if((int)st.size()<=k){ ans=max(ans,tm); } else{ int tmm=(*st.find_by_order(k)).ff; ans=max(ans,min(tm,2*tmm)); } } { // 0 1 // 1 0 tm=max(ar[i-1],ar[i]); if((int)st.size()<=k-1){ ans=max(ans,tm); } else{ int tmm=(*st.find_by_order(k-1)).ff; ans=max(ans,min(tm,2*tmm)); } } if(k>1){ //1 1 tm=mx; if((int)st.size()<=k-2){ ans=max(ans,tm); } else{ int tmm=(*st.find_by_order(k-2)).ff; ans=max(ans,min(tm,2*tmm)); } } st.insert(p); st.insert(q); } cout<<ans<<'\n'; } }
c++
20
0.472843
118
12.555118
254
D. Empty Graphtime limit per test1.5 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard output — Do you have a wish?  — I want people to stop gifting each other arrays.O_o and Another Young BoyAn array of $$$n$$$ positive integers $$$a_1,a_2,\ldots,a_n$$$ fell down on you from the skies, along with a positive integer $$$k \le n$$$.You can apply the following operation at most $$$k$$$ times: Choose an index $$$1 \le i \le n$$$ and an integer $$$1 \le x \le 10^9$$$. Then do $$$a_i := x$$$ (assign $$$x$$$ to $$$a_i$$$). Then build a complete undirected weighted graph with $$$n$$$ vertices numbered with integers from $$$1$$$ to $$$n$$$, where edge $$$(l, r)$$$ ($$$1 \le l < r \le n$$$) has weight $$$\min(a_{l},a_{l+1},\ldots,a_{r})$$$.You have to find the maximum possible diameter of the resulting graph after performing at most $$$k$$$ operations.The diameter of a graph is equal to $$$\max\limits_{1 \le u < v \le n}{\operatorname{d}(u, v)}$$$, where $$$\operatorname{d}(u, v)$$$ is the length of the shortest path between vertex $$$u$$$ and vertex $$$v$$$.InputEach test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows.The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$2 \le n \le 10^5$$$, $$$1 \le k \le n$$$).The second line of each test case contains $$$n$$$ positive integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_i \le 10^9$$$).It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.OutputFor each test case print one integer — the maximum possible diameter of the graph after performing at most $$$k$$$ operations.ExampleInput 63 12 4 13 21 9 843 110 2 63 2179 17 10000000002 15 92 24 2Output 4 168 10 1000000000 9 1000000000 NoteIn the first test case, one of the optimal arrays is $$$[2,4,5]$$$.The graph built on this array: $$$\operatorname{d}(1, 2) = \operatorname{d}(1, 3) = 2$$$ and $$$\operatorname{d}(2, 3) = 4$$$, so the diameter is equal to $$$\max(2,2,4) = 4$$$.
cf
/* the length must be min(d(i,i+1),2*m) (m is the shortest length of the graph) as we know this, so the goal is to make d(i,i+1) or 2*m great if the answer is d(i,i+1) then we just need to make a[i] equals to ANS if the answer is 2*m ,then we need to maximize it, so we could just make */ #include <bits/stdc++.h> using namespace std; using ll=long long; const int MOD=1e9+7; using pii=pair<int,int>; const int ANS=1e9; void solve() { int n,k;cin>>n>>k; vector<pii>v(n); vector<int>a(n); for(int i=0;i<n;++i) { cin>>a[i]; v[i]={a[i],i}; } sort(v.begin(),v.end());// first sort, get the lowest k-1 elements for(int i=0;i<k-1;++i) { v[i].first=a[v[i].second]=ANS; } sort(v.begin(),v.end());//second sort int ans1=min(2*v[0].first,v[n-1].first);//first calculate (whether the i in in the minimum k elements) v[0].first=ANS,a[v[0].second]=ANS; sort(v.begin(),v.end()); int ans2=-1; for(int i=1;i<n;++i) { ans2=max(ans2,min(a[i],a[i-1])); } ans2=min(ans2,2*v[0].first); cout<<max(ans1,ans2)<<'\n'; return; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); #ifdef LOCAL freopen("/Users/xiangyanxin/code/Algorithom/in.txt","r",stdin); freopen("/Users/xiangyanxin/code/Algorithom/out.txt","w",stdout); #endif int T; cin>>T; while(T--) { solve(); } return 0; }
c++
14
0.584572
104
11.738739
111
D. Empty Graphtime limit per test1.5 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard output — Do you have a wish?  — I want people to stop gifting each other arrays.O_o and Another Young BoyAn array of $$$n$$$ positive integers $$$a_1,a_2,\ldots,a_n$$$ fell down on you from the skies, along with a positive integer $$$k \le n$$$.You can apply the following operation at most $$$k$$$ times: Choose an index $$$1 \le i \le n$$$ and an integer $$$1 \le x \le 10^9$$$. Then do $$$a_i := x$$$ (assign $$$x$$$ to $$$a_i$$$). Then build a complete undirected weighted graph with $$$n$$$ vertices numbered with integers from $$$1$$$ to $$$n$$$, where edge $$$(l, r)$$$ ($$$1 \le l < r \le n$$$) has weight $$$\min(a_{l},a_{l+1},\ldots,a_{r})$$$.You have to find the maximum possible diameter of the resulting graph after performing at most $$$k$$$ operations.The diameter of a graph is equal to $$$\max\limits_{1 \le u < v \le n}{\operatorname{d}(u, v)}$$$, where $$$\operatorname{d}(u, v)$$$ is the length of the shortest path between vertex $$$u$$$ and vertex $$$v$$$.InputEach test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows.The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$2 \le n \le 10^5$$$, $$$1 \le k \le n$$$).The second line of each test case contains $$$n$$$ positive integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_i \le 10^9$$$).It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.OutputFor each test case print one integer — the maximum possible diameter of the graph after performing at most $$$k$$$ operations.ExampleInput 63 12 4 13 21 9 843 110 2 63 2179 17 10000000002 15 92 24 2Output 4 168 10 1000000000 9 1000000000 NoteIn the first test case, one of the optimal arrays is $$$[2,4,5]$$$.The graph built on this array: $$$\operatorname{d}(1, 2) = \operatorname{d}(1, 3) = 2$$$ and $$$\operatorname{d}(2, 3) = 4$$$, so the diameter is equal to $$$\max(2,2,4) = 4$$$.
cf
import sys; input = sys.stdin.readline def solve(): n, k = map(int, input().split()) a = list(map(int, input().split())) # 이분 탐색 start = 0; end = 1000000000 result = 0 while start <= end: mid = (start + end) // 2 arr = a[:] # 배열 복사 K = k # 남은 작업 수 # 임의로 정한 직경에 따라 배열을 조작 # 남은 작업 수에 따라 분기가 나뉜다. # mid = min(max(min(ai, ai+1)) (1 <= i < n), 2 * min(a1, ..., an)) # 2 * min(a1, ..., an) 부터 만족시켜보자. for i in range(n): if arr[i] < mid / 2: # mid / 2보다 작으면 1000000000으로 교체 작업 arr[i] = 1000000000 K -= 1 # 남은 작업 수가 음수이면 이 mid는 불가능한 직경이다. if K < 0: end = mid - 1 # 남은 작업 수가 0이면 max(min(ai, ai+1)) (1 <= i < n)을 계산하여 비교 elif not K: MAX = 0 for i in range(n - 1): MAX = max(MAX, min(arr[i], arr[i + 1])) if MAX < mid: # max보다 mid가 크면 불가능한 직경이다. end = mid - 1 else: # max보다 mid가 같거나 작으면 가능한 직경이다. result = mid start = mid + 1 # 남은 작업 수가 1이면 # 최대한 mid가 직경이 될 수 있도록 # arr의 최댓값 인근에 작업을 해야 한다. elif K == 1: if max(arr) < mid: # 최댓값보다 mid가 크면 불가능한 직경이다. end = mid - 1 else: # 최댓값보다 mid가 같거나 작으면 가능힌 직경이다. result = mid start = mid + 1 # 남은 작업 수가 2 이상이면 # 인접한 아무 두 원소를 최대로 올려버리면 무조건 mid는 가능한 직경이다. else: result = mid start = mid + 1 print(result) for _ in range(int(input())): solve()
python
18
0.411939
74
13.470085
117
D. Empty Graphtime limit per test1.5 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard output — Do you have a wish?  — I want people to stop gifting each other arrays.O_o and Another Young BoyAn array of $$$n$$$ positive integers $$$a_1,a_2,\ldots,a_n$$$ fell down on you from the skies, along with a positive integer $$$k \le n$$$.You can apply the following operation at most $$$k$$$ times: Choose an index $$$1 \le i \le n$$$ and an integer $$$1 \le x \le 10^9$$$. Then do $$$a_i := x$$$ (assign $$$x$$$ to $$$a_i$$$). Then build a complete undirected weighted graph with $$$n$$$ vertices numbered with integers from $$$1$$$ to $$$n$$$, where edge $$$(l, r)$$$ ($$$1 \le l < r \le n$$$) has weight $$$\min(a_{l},a_{l+1},\ldots,a_{r})$$$.You have to find the maximum possible diameter of the resulting graph after performing at most $$$k$$$ operations.The diameter of a graph is equal to $$$\max\limits_{1 \le u < v \le n}{\operatorname{d}(u, v)}$$$, where $$$\operatorname{d}(u, v)$$$ is the length of the shortest path between vertex $$$u$$$ and vertex $$$v$$$.InputEach test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows.The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$2 \le n \le 10^5$$$, $$$1 \le k \le n$$$).The second line of each test case contains $$$n$$$ positive integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_i \le 10^9$$$).It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.OutputFor each test case print one integer — the maximum possible diameter of the graph after performing at most $$$k$$$ operations.ExampleInput 63 12 4 13 21 9 843 110 2 63 2179 17 10000000002 15 92 24 2Output 4 168 10 1000000000 9 1000000000 NoteIn the first test case, one of the optimal arrays is $$$[2,4,5]$$$.The graph built on this array: $$$\operatorname{d}(1, 2) = \operatorname{d}(1, 3) = 2$$$ and $$$\operatorname{d}(2, 3) = 4$$$, so the diameter is equal to $$$\max(2,2,4) = 4$$$.
cf
#include<bits/stdc++.h> using namespace std; int mxm=1000000000; int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); int t; cin >> t; while(t>0){ t--; int n,k; cin >> n >> k; vector<int> a(n); for(auto &nx : a){cin >> nx;} int l=0,r=mxm; while(l<=r){ // max{ min(global_min*2,min(a[i],a[i+1])) } int te=(l+r)/2,cop=0; vector<int> ca=a; int must=(te-1)/2; for(auto &nx : ca){ if(nx<=must){ cop++; nx=mxm; } } if(cop>k){r=te-1;continue;} int rsmx=0; for(int i=1;i<n;i++){rsmx=max(rsmx,min(ca[i-1],ca[i]));} if(rsmx>=te){l=te+1;continue;} cop+=2; for(int i=0;i<n;i++){ if(i!=0 && min(ca[i-1],mxm)>=te){cop--;break;} if(i!=(n-1) && min(mxm,ca[i+1])>=te){cop--;break;} } if(cop>k){r=te-1;}else{l=te+1;} } cout << r << "\n"; } return 0; }
c++
18
0.426817
62
9.619565
92
D. Empty Graphtime limit per test1.5 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard output — Do you have a wish?  — I want people to stop gifting each other arrays.O_o and Another Young BoyAn array of $$$n$$$ positive integers $$$a_1,a_2,\ldots,a_n$$$ fell down on you from the skies, along with a positive integer $$$k \le n$$$.You can apply the following operation at most $$$k$$$ times: Choose an index $$$1 \le i \le n$$$ and an integer $$$1 \le x \le 10^9$$$. Then do $$$a_i := x$$$ (assign $$$x$$$ to $$$a_i$$$). Then build a complete undirected weighted graph with $$$n$$$ vertices numbered with integers from $$$1$$$ to $$$n$$$, where edge $$$(l, r)$$$ ($$$1 \le l < r \le n$$$) has weight $$$\min(a_{l},a_{l+1},\ldots,a_{r})$$$.You have to find the maximum possible diameter of the resulting graph after performing at most $$$k$$$ operations.The diameter of a graph is equal to $$$\max\limits_{1 \le u < v \le n}{\operatorname{d}(u, v)}$$$, where $$$\operatorname{d}(u, v)$$$ is the length of the shortest path between vertex $$$u$$$ and vertex $$$v$$$.InputEach test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows.The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$2 \le n \le 10^5$$$, $$$1 \le k \le n$$$).The second line of each test case contains $$$n$$$ positive integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_i \le 10^9$$$).It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.OutputFor each test case print one integer — the maximum possible diameter of the graph after performing at most $$$k$$$ operations.ExampleInput 63 12 4 13 21 9 843 110 2 63 2179 17 10000000002 15 92 24 2Output 4 168 10 1000000000 9 1000000000 NoteIn the first test case, one of the optimal arrays is $$$[2,4,5]$$$.The graph built on this array: $$$\operatorname{d}(1, 2) = \operatorname{d}(1, 3) = 2$$$ and $$$\operatorname{d}(2, 3) = 4$$$, so the diameter is equal to $$$\max(2,2,4) = 4$$$.
cf
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <random> using namespace std; using namespace __gnu_pbds; typedef long long ll; typedef unsigned long long ull; typedef tree<int, null_type, less<>, rb_tree_tag, tree_order_statistics_node_update> ordered_set; #define AboTaha_on_da_code ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); #define X first #define Y second const int dx[8]={0, 0, 1, -1, 1, -1, -1, 1}, dy[8]={1, -1, 0, 0, 1, -1, 1, -1}; const int M = 1e9+7, M2 = 998244353; const double EPS = 1e-8; void burn(int tc) { int n, k; cin >> n >> k; vector <int> a(n); for (auto &i : a) cin >> i; int st = 1, en = 1e9; while(st <= en) { int mi = (st+en)/2; int kk = k; auto aa = a; for (auto &i : aa) { if (2*i < mi) i = 1e9, kk--; } int inc = 2; for (int i = 0; i+1 < n; i++) { inc = min(inc, (aa[i] < mi)+(aa[i+1] < mi)); } if (kk-inc < 0) en = mi-1; else st = mi+1; } cout << st-1; } int main() { AboTaha_on_da_code // freopen("zeros.in", "r", stdin); // freopen("Aout.txt", "w", stdout); int T = 1; cin >> T; for (int i = 1; i <= T; i++) { // cout << "Case " << i << ": "; burn(i); cout << '\n'; } return 0; }
c++
17
0.474733
97
11.442478
113
D. Empty Graphtime limit per test1.5 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard output — Do you have a wish?  — I want people to stop gifting each other arrays.O_o and Another Young BoyAn array of $$$n$$$ positive integers $$$a_1,a_2,\ldots,a_n$$$ fell down on you from the skies, along with a positive integer $$$k \le n$$$.You can apply the following operation at most $$$k$$$ times: Choose an index $$$1 \le i \le n$$$ and an integer $$$1 \le x \le 10^9$$$. Then do $$$a_i := x$$$ (assign $$$x$$$ to $$$a_i$$$). Then build a complete undirected weighted graph with $$$n$$$ vertices numbered with integers from $$$1$$$ to $$$n$$$, where edge $$$(l, r)$$$ ($$$1 \le l < r \le n$$$) has weight $$$\min(a_{l},a_{l+1},\ldots,a_{r})$$$.You have to find the maximum possible diameter of the resulting graph after performing at most $$$k$$$ operations.The diameter of a graph is equal to $$$\max\limits_{1 \le u < v \le n}{\operatorname{d}(u, v)}$$$, where $$$\operatorname{d}(u, v)$$$ is the length of the shortest path between vertex $$$u$$$ and vertex $$$v$$$.InputEach test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows.The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$2 \le n \le 10^5$$$, $$$1 \le k \le n$$$).The second line of each test case contains $$$n$$$ positive integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_i \le 10^9$$$).It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.OutputFor each test case print one integer — the maximum possible diameter of the graph after performing at most $$$k$$$ operations.ExampleInput 63 12 4 13 21 9 843 110 2 63 2179 17 10000000002 15 92 24 2Output 4 168 10 1000000000 9 1000000000 NoteIn the first test case, one of the optimal arrays is $$$[2,4,5]$$$.The graph built on this array: $$$\operatorname{d}(1, 2) = \operatorname{d}(1, 3) = 2$$$ and $$$\operatorname{d}(2, 3) = 4$$$, so the diameter is equal to $$$\max(2,2,4) = 4$$$.
cf
//check editorial #include <bits/stdc++.h> using namespace std; #ifdef LOCAL #include "algo/debug.h" #else #define debug(...) 42 #endif int main() { ios::sync_with_stdio(false); cin.tie(0); int tt; cin >> tt; while (tt--) { int n, k; cin >> n >> k; vector<int> a(n); for (int i = 0; i < n; i++) { cin >> a[i]; } vector<int> b = a; sort(b.begin(), b.end()); int low = 0, high = (int) 1e9; while (low < high) { int mid = (low + high + 1) >> 1; bool found = false; for (int i = 0; i < n - 1; i++) { int cnt = 0; if (a[i] < mid) { cnt += 1; } if (a[i + 1] < mid) { cnt += 1; } int val = (mid + 1) / 2; cnt += (int) (lower_bound(b.begin(), b.end(), val) - b.begin()); if (a[i] < val) { cnt -= 1; } if (a[i + 1] < val) { cnt -= 1; } if (cnt <= k) { found = true; break; } } if (found) { low = mid; } else { high = mid - 1; } } cout << low << '\n'; } return 0; }
c++
18
0.363411
72
8.901639
122
D. Empty Graphtime limit per test1.5 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard output — Do you have a wish?  — I want people to stop gifting each other arrays.O_o and Another Young BoyAn array of $$$n$$$ positive integers $$$a_1,a_2,\ldots,a_n$$$ fell down on you from the skies, along with a positive integer $$$k \le n$$$.You can apply the following operation at most $$$k$$$ times: Choose an index $$$1 \le i \le n$$$ and an integer $$$1 \le x \le 10^9$$$. Then do $$$a_i := x$$$ (assign $$$x$$$ to $$$a_i$$$). Then build a complete undirected weighted graph with $$$n$$$ vertices numbered with integers from $$$1$$$ to $$$n$$$, where edge $$$(l, r)$$$ ($$$1 \le l < r \le n$$$) has weight $$$\min(a_{l},a_{l+1},\ldots,a_{r})$$$.You have to find the maximum possible diameter of the resulting graph after performing at most $$$k$$$ operations.The diameter of a graph is equal to $$$\max\limits_{1 \le u < v \le n}{\operatorname{d}(u, v)}$$$, where $$$\operatorname{d}(u, v)$$$ is the length of the shortest path between vertex $$$u$$$ and vertex $$$v$$$.InputEach test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows.The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$2 \le n \le 10^5$$$, $$$1 \le k \le n$$$).The second line of each test case contains $$$n$$$ positive integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_i \le 10^9$$$).It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.OutputFor each test case print one integer — the maximum possible diameter of the graph after performing at most $$$k$$$ operations.ExampleInput 63 12 4 13 21 9 843 110 2 63 2179 17 10000000002 15 92 24 2Output 4 168 10 1000000000 9 1000000000 NoteIn the first test case, one of the optimal arrays is $$$[2,4,5]$$$.The graph built on this array: $$$\operatorname{d}(1, 2) = \operatorname{d}(1, 3) = 2$$$ and $$$\operatorname{d}(2, 3) = 4$$$, so the diameter is equal to $$$\max(2,2,4) = 4$$$.
cf
#include <bits/stdc++.h> using namespace std; #define fastio cin.tie(0)->sync_with_stdio(0) using ll = long long; using pii = pair<int, int>; using pll = pair<ll, ll>; void solve() { int n, k; cin >> n >> k; vector<int> a(n); for(int i = 0; i < n; i++) { cin >> a[i]; } int s = 0, e = (int)1e9; int ans; while(s <= e) { int mid = (s + e) / 2; vector<int> b = a; int cnt = 0; for(int i = 0; i < n; i++) { if(b[i] * 2 < mid) { b[i] = 1e9; cnt++; } } bool two = false, one = false; for(int i = 0; i < n; i++) { if(b[i] >= mid) { one = true; } if(i < n - 1 && min(b[i], b[i + 1]) >= mid) { two = true; } } cnt += !two + !one; if(cnt <= k) { ans = mid; s = mid + 1; } else e = mid - 1; } cout << ans << '\n'; } int main() { fastio; int tc; cin >> tc; while(tc--) { solve(); } return 0; }
c++
16
0.337339
57
10.104762
105
D. Empty Graphtime limit per test1.5 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard output — Do you have a wish?  — I want people to stop gifting each other arrays.O_o and Another Young BoyAn array of $$$n$$$ positive integers $$$a_1,a_2,\ldots,a_n$$$ fell down on you from the skies, along with a positive integer $$$k \le n$$$.You can apply the following operation at most $$$k$$$ times: Choose an index $$$1 \le i \le n$$$ and an integer $$$1 \le x \le 10^9$$$. Then do $$$a_i := x$$$ (assign $$$x$$$ to $$$a_i$$$). Then build a complete undirected weighted graph with $$$n$$$ vertices numbered with integers from $$$1$$$ to $$$n$$$, where edge $$$(l, r)$$$ ($$$1 \le l < r \le n$$$) has weight $$$\min(a_{l},a_{l+1},\ldots,a_{r})$$$.You have to find the maximum possible diameter of the resulting graph after performing at most $$$k$$$ operations.The diameter of a graph is equal to $$$\max\limits_{1 \le u < v \le n}{\operatorname{d}(u, v)}$$$, where $$$\operatorname{d}(u, v)$$$ is the length of the shortest path between vertex $$$u$$$ and vertex $$$v$$$.InputEach test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows.The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$2 \le n \le 10^5$$$, $$$1 \le k \le n$$$).The second line of each test case contains $$$n$$$ positive integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_i \le 10^9$$$).It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.OutputFor each test case print one integer — the maximum possible diameter of the graph after performing at most $$$k$$$ operations.ExampleInput 63 12 4 13 21 9 843 110 2 63 2179 17 10000000002 15 92 24 2Output 4 168 10 1000000000 9 1000000000 NoteIn the first test case, one of the optimal arrays is $$$[2,4,5]$$$.The graph built on this array: $$$\operatorname{d}(1, 2) = \operatorname{d}(1, 3) = 2$$$ and $$$\operatorname{d}(2, 3) = 4$$$, so the diameter is equal to $$$\max(2,2,4) = 4$$$.
cf
import random import sys sys.setrecursionlimit(1000000000) from collections import defaultdict, deque from functools import lru_cache from bisect import bisect_left, bisect_right, insort_right, insort_left import heapq from itertools import accumulate # list(map(int, input().strip().split(' '))) # int(input().strip()) def solve(n, nums, k): M = 10 ** 9 Max = max(nums) def check(t): nums1 = list(nums) tmp = 0 for i in range(n): if nums1[i] * 2 < t: nums1[i] = M tmp += 1 if tmp > k: return False if k - tmp >= 2: return True if k - tmp == 1: return max(nums1) >= t return max(min(nums1[i], nums1[i + 1]) for i in range(n - 1)) >= t lo = 0 hi = M while lo<hi: p = (lo+hi)//2 if not check(p): hi = p-1 else: if lo==p: break lo=p if check(hi): lo=hi return lo if __name__ == '__main__': for _ in range(int(input())): n, k = list(map(int, input().strip().split(' '))) res = solve(n, list(map(int, input().strip().split(' '))), k) print(res)
python
19
0.470817
74
10.681818
110
D. Empty Graphtime limit per test1.5 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard output — Do you have a wish?  — I want people to stop gifting each other arrays.O_o and Another Young BoyAn array of $$$n$$$ positive integers $$$a_1,a_2,\ldots,a_n$$$ fell down on you from the skies, along with a positive integer $$$k \le n$$$.You can apply the following operation at most $$$k$$$ times: Choose an index $$$1 \le i \le n$$$ and an integer $$$1 \le x \le 10^9$$$. Then do $$$a_i := x$$$ (assign $$$x$$$ to $$$a_i$$$). Then build a complete undirected weighted graph with $$$n$$$ vertices numbered with integers from $$$1$$$ to $$$n$$$, where edge $$$(l, r)$$$ ($$$1 \le l < r \le n$$$) has weight $$$\min(a_{l},a_{l+1},\ldots,a_{r})$$$.You have to find the maximum possible diameter of the resulting graph after performing at most $$$k$$$ operations.The diameter of a graph is equal to $$$\max\limits_{1 \le u < v \le n}{\operatorname{d}(u, v)}$$$, where $$$\operatorname{d}(u, v)$$$ is the length of the shortest path between vertex $$$u$$$ and vertex $$$v$$$.InputEach test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows.The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$2 \le n \le 10^5$$$, $$$1 \le k \le n$$$).The second line of each test case contains $$$n$$$ positive integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_i \le 10^9$$$).It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.OutputFor each test case print one integer — the maximum possible diameter of the graph after performing at most $$$k$$$ operations.ExampleInput 63 12 4 13 21 9 843 110 2 63 2179 17 10000000002 15 92 24 2Output 4 168 10 1000000000 9 1000000000 NoteIn the first test case, one of the optimal arrays is $$$[2,4,5]$$$.The graph built on this array: $$$\operatorname{d}(1, 2) = \operatorname{d}(1, 3) = 2$$$ and $$$\operatorname{d}(2, 3) = 4$$$, so the diameter is equal to $$$\max(2,2,4) = 4$$$.
cf
/* 6 3 1 2 4 1 3 2 1 9 84 3 1 10 2 6 3 2 179 17 1000000000 2 1 5 9 2 2 4 2 1 5 2 3 1 5 4 4 expect 8 1 2 3 4 5 6 */ import java.util.*; import java.io.*; public class Main{ public static int n; public static int k; //# of operations that you can do public static int mx; //max of all elements in the array public static int[] arr; public static void main(String[] args) throws IOException{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringBuilder ret = new StringBuilder(); int nc = Integer.parseInt(br.readLine()); for(int cc = 0; cc < nc; cc++){ mx = 0; StringTokenizer st = new StringTokenizer(br.readLine()); n = Integer.parseInt(st.nextToken()); k = Integer.parseInt(st.nextToken()); arr = new int[n]; StringTokenizer ns = new StringTokenizer(br.readLine()); for(int a = 0; a < n; a++){ arr[a] = Integer.parseInt(ns.nextToken()); mx = Math.max(mx, arr[a]); } //d(u, v) = min(min(au...vu), 2 * min(a1...an)) int min = 0; int max = 1000000001; //will take about 30 iterations for(int iter = 0; iter < 31; iter++){ int mid = (min + max)/2; if(valid(mid)) min = mid; else max = mid; } //System.out.println(); ret.append(min + "\n"); } System.out.println(ret.toString()); br.close(); } public static boolean valid(int diameter){ int left = k; //set all elements, if k == 0 then calculate the diameter by taking the max of all adjacent elements, if k == 1 then return true iff the maximum element in the array is greater than or equal to diameter, else return true //System.out.println(diameter + " check"); for(int a = 0; a < n; a++){ if(arr[a]*2 < diameter) left--; } //System.out.println("left is " + left); if(left < 0) return false; for(int a = 1; a < n; a++){ if((arr[a] >= diameter || arr[a]*2 < diameter) && (arr[a-1] >= diameter || arr[a-1]*2 < diameter)) return true; } //System.out.println("still not there"); if(left == 0) return false; else if(left == 1){ if(k > 1) return true; //just go next to this number return mx >= diameter; } return true; } }
java
16
0.555229
224
12.160221
181
D. Empty Graphtime limit per test1.5 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard output — Do you have a wish?  — I want people to stop gifting each other arrays.O_o and Another Young BoyAn array of $$$n$$$ positive integers $$$a_1,a_2,\ldots,a_n$$$ fell down on you from the skies, along with a positive integer $$$k \le n$$$.You can apply the following operation at most $$$k$$$ times: Choose an index $$$1 \le i \le n$$$ and an integer $$$1 \le x \le 10^9$$$. Then do $$$a_i := x$$$ (assign $$$x$$$ to $$$a_i$$$). Then build a complete undirected weighted graph with $$$n$$$ vertices numbered with integers from $$$1$$$ to $$$n$$$, where edge $$$(l, r)$$$ ($$$1 \le l < r \le n$$$) has weight $$$\min(a_{l},a_{l+1},\ldots,a_{r})$$$.You have to find the maximum possible diameter of the resulting graph after performing at most $$$k$$$ operations.The diameter of a graph is equal to $$$\max\limits_{1 \le u < v \le n}{\operatorname{d}(u, v)}$$$, where $$$\operatorname{d}(u, v)$$$ is the length of the shortest path between vertex $$$u$$$ and vertex $$$v$$$.InputEach test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows.The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$2 \le n \le 10^5$$$, $$$1 \le k \le n$$$).The second line of each test case contains $$$n$$$ positive integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_i \le 10^9$$$).It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.OutputFor each test case print one integer — the maximum possible diameter of the graph after performing at most $$$k$$$ operations.ExampleInput 63 12 4 13 21 9 843 110 2 63 2179 17 10000000002 15 92 24 2Output 4 168 10 1000000000 9 1000000000 NoteIn the first test case, one of the optimal arrays is $$$[2,4,5]$$$.The graph built on this array: $$$\operatorname{d}(1, 2) = \operatorname{d}(1, 3) = 2$$$ and $$$\operatorname{d}(2, 3) = 4$$$, so the diameter is equal to $$$\max(2,2,4) = 4$$$.
cf
#include <bits/stdc++.h> using namespace std; #define pb push_back #define fs first #define sc second #define sz(x) (int)(x.size()) typedef long long ll; typedef pair<int, int> pii; typedef pair<int, ll> pil; typedef pair<ll, ll> pll; typedef priority_queue <ll, vector<ll>, greater<ll>> pqmin; const int INF = 2e9; const int MOD = 1e9+7; const int p=1e5+5; int a[p]; bool flag[p]; int compute(int mid,int n){ for(int i=1;i<=n;i++){ flag[i]=false; } int tot=0; for(int i=1;i<=n;i++){ if(2*a[i]<mid){ flag[i]=true; tot++; } } int mnm=2; for(int i=1;i<=n;i++){ int cnt=0; if(i>1){ if(!flag[i-1] && a[i-1]<mid){ cnt++; } if(!flag[i] && a[i]<mid){ cnt++; } mnm=min(mnm,cnt); } cnt=0; if(i<n){ if(!flag[i+1] && a[i+1]<mid){ cnt++; } if(!flag[i] && a[i]<mid){ cnt++; } mnm=min(mnm,cnt); } } return mnm+tot; } void solve(){ int n,k; cin>>n>>k; for(int i=1;i<=n;i++){ cin>>a[i]; } int l=1; int r=1e9; int ans=1; while(l<=r){ int mid=(l+r)/2; int res=compute(mid,n); if(res<=k){ l=mid+1; ans=mid; }else{ r=mid-1; } } cout<<ans<<endl; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t; cin>>t; while(t--){ solve(); } cin.get(); return 0; }
c++
14
0.391055
59
8.178947
190
D. Empty Graphtime limit per test1.5 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard output — Do you have a wish?  — I want people to stop gifting each other arrays.O_o and Another Young BoyAn array of $$$n$$$ positive integers $$$a_1,a_2,\ldots,a_n$$$ fell down on you from the skies, along with a positive integer $$$k \le n$$$.You can apply the following operation at most $$$k$$$ times: Choose an index $$$1 \le i \le n$$$ and an integer $$$1 \le x \le 10^9$$$. Then do $$$a_i := x$$$ (assign $$$x$$$ to $$$a_i$$$). Then build a complete undirected weighted graph with $$$n$$$ vertices numbered with integers from $$$1$$$ to $$$n$$$, where edge $$$(l, r)$$$ ($$$1 \le l < r \le n$$$) has weight $$$\min(a_{l},a_{l+1},\ldots,a_{r})$$$.You have to find the maximum possible diameter of the resulting graph after performing at most $$$k$$$ operations.The diameter of a graph is equal to $$$\max\limits_{1 \le u < v \le n}{\operatorname{d}(u, v)}$$$, where $$$\operatorname{d}(u, v)$$$ is the length of the shortest path between vertex $$$u$$$ and vertex $$$v$$$.InputEach test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows.The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$2 \le n \le 10^5$$$, $$$1 \le k \le n$$$).The second line of each test case contains $$$n$$$ positive integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_i \le 10^9$$$).It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.OutputFor each test case print one integer — the maximum possible diameter of the graph after performing at most $$$k$$$ operations.ExampleInput 63 12 4 13 21 9 843 110 2 63 2179 17 10000000002 15 92 24 2Output 4 168 10 1000000000 9 1000000000 NoteIn the first test case, one of the optimal arrays is $$$[2,4,5]$$$.The graph built on this array: $$$\operatorname{d}(1, 2) = \operatorname{d}(1, 3) = 2$$$ and $$$\operatorname{d}(2, 3) = 4$$$, so the diameter is equal to $$$\max(2,2,4) = 4$$$.
cf
/* * * @UtkarshAgarwal */ import java.util.Arrays; import java.util.OptionalInt; import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int t = scanner.nextInt(); while(t-- > 0){ int n = scanner.nextInt(); int k = scanner.nextInt(); int[] arr = new int[n]; for(int i = 0 ; i < n ; i++) arr[i] = scanner.nextInt(); int ans = solve(arr, k); System.out.println(ans); } } private static int solve(int[] arr, int k) { int l = 1, h = 1000000000; int ans = Integer.MIN_VALUE; while(l <= h){ int mid = l + (h - l) / 2; if(check(mid, arr.clone(), k)){ l = mid + 1; ans = Math.max(ans, mid); }else{ h = mid - 1; } } return ans; } private static boolean check(int mid, int[] arr, int k) { int max = 1000000000; for(int i = 0 ; i < arr.length ; i++){ if(2 * arr[i] < mid){ arr[i] = max; k--; } } if(k < 0) return false; else if(k == 0){ for(int i = 0 ; i < arr.length - 1 ; i++) if(Math.min(arr[i], arr[i + 1]) >= mid) return true; return false; } else if(k == 1){ return Arrays.stream(arr).max().getAsInt() >= mid; }else return true; } }
java
16
0.403697
62
11.9
130
D. Empty Graphtime limit per test1.5 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard output — Do you have a wish?  — I want people to stop gifting each other arrays.O_o and Another Young BoyAn array of $$$n$$$ positive integers $$$a_1,a_2,\ldots,a_n$$$ fell down on you from the skies, along with a positive integer $$$k \le n$$$.You can apply the following operation at most $$$k$$$ times: Choose an index $$$1 \le i \le n$$$ and an integer $$$1 \le x \le 10^9$$$. Then do $$$a_i := x$$$ (assign $$$x$$$ to $$$a_i$$$). Then build a complete undirected weighted graph with $$$n$$$ vertices numbered with integers from $$$1$$$ to $$$n$$$, where edge $$$(l, r)$$$ ($$$1 \le l < r \le n$$$) has weight $$$\min(a_{l},a_{l+1},\ldots,a_{r})$$$.You have to find the maximum possible diameter of the resulting graph after performing at most $$$k$$$ operations.The diameter of a graph is equal to $$$\max\limits_{1 \le u < v \le n}{\operatorname{d}(u, v)}$$$, where $$$\operatorname{d}(u, v)$$$ is the length of the shortest path between vertex $$$u$$$ and vertex $$$v$$$.InputEach test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). Description of the test cases follows.The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$2 \le n \le 10^5$$$, $$$1 \le k \le n$$$).The second line of each test case contains $$$n$$$ positive integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_i \le 10^9$$$).It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.OutputFor each test case print one integer — the maximum possible diameter of the graph after performing at most $$$k$$$ operations.ExampleInput 63 12 4 13 21 9 843 110 2 63 2179 17 10000000002 15 92 24 2Output 4 168 10 1000000000 9 1000000000 NoteIn the first test case, one of the optimal arrays is $$$[2,4,5]$$$.The graph built on this array: $$$\operatorname{d}(1, 2) = \operatorname{d}(1, 3) = 2$$$ and $$$\operatorname{d}(2, 3) = 4$$$, so the diameter is equal to $$$\max(2,2,4) = 4$$$.
cf