name
stringlengths
2
88
description
stringlengths
31
8.62k
public_tests
dict
private_tests
dict
solution_type
stringclasses
2 values
programming_language
stringclasses
5 values
solution
stringlengths
1
983k
prpaln
Given a string s. Can you make it a palindrome by deleting exactly one character? Note that size of the string after deletion would be one less than it was before. Input First line of the input contains a single integer T denoting number of test cases. For each test case, you are given a single line containing string s. Output For each test case, print YES or NO depending on the answer of the problem. Constraints Example Input: 4 aaa abc abdbca abba Output: YES NO YES YES Explanation Example case 1. Delete any one 'a', resulting string is "aa" which is a palindrome. Example case 2. It is not possible to delete exactly one character and having a palindrome. Example case 3. Delete 'c', resulting string is "abdba" which is a palindrome. Example case 4. Delete 'b', resulting string is "aba" which is a palindrome.
{ "input": [ "4\naaa\nabc\nabdbca\nabba" ], "output": [ "YES\nNO\nYES\nYES\n" ] }
{ "input": [], "output": [] }
CORRECT
python2
def palin(): for i in xrange(input()): lst=raw_input() lst=list(lst) b=lst[::-1] if b==lst: print "YES" else: for i in xrange(len(lst)): if b[i]!=lst[i]: c=b[::1];d=lst[::1] del c[i];del d[len(lst)-i-1] del b[len(b)-i-1];del lst[i] if c==d or b==lst: print "YES" break else: print "NO" break palin()
prpaln
Given a string s. Can you make it a palindrome by deleting exactly one character? Note that size of the string after deletion would be one less than it was before. Input First line of the input contains a single integer T denoting number of test cases. For each test case, you are given a single line containing string s. Output For each test case, print YES or NO depending on the answer of the problem. Constraints Example Input: 4 aaa abc abdbca abba Output: YES NO YES YES Explanation Example case 1. Delete any one 'a', resulting string is "aa" which is a palindrome. Example case 2. It is not possible to delete exactly one character and having a palindrome. Example case 3. Delete 'c', resulting string is "abdba" which is a palindrome. Example case 4. Delete 'b', resulting string is "aba" which is a palindrome.
{ "input": [ "4\naaa\nabc\nabdbca\nabba" ], "output": [ "YES\nNO\nYES\nYES\n" ] }
{ "input": [], "output": [] }
CORRECT
python2
#!/usr/bin/py t=raw_input() def pal(s): sl=len(s) for l1 in range(0,sl/2+(sl%2)): if s[l1]!=s[sl-l1-1]: return 0 return 1 for l in range(0,int(t)): s=raw_input() r=len(s) flag=0 for l1 in range(0,len(s)/2+(len(s)%2)): if s[l1]!= s[r-1]: c1=pal(s[l1+1:r]) c2=pal(s[l1:r-1]) flag=flag+1 if c2: print "YES" break elif c1: print "YES" break else: print "NO" break r=r-1 if flag==0: print "YES"
prpaln
Given a string s. Can you make it a palindrome by deleting exactly one character? Note that size of the string after deletion would be one less than it was before. Input First line of the input contains a single integer T denoting number of test cases. For each test case, you are given a single line containing string s. Output For each test case, print YES or NO depending on the answer of the problem. Constraints Example Input: 4 aaa abc abdbca abba Output: YES NO YES YES Explanation Example case 1. Delete any one 'a', resulting string is "aa" which is a palindrome. Example case 2. It is not possible to delete exactly one character and having a palindrome. Example case 3. Delete 'c', resulting string is "abdba" which is a palindrome. Example case 4. Delete 'b', resulting string is "aba" which is a palindrome.
{ "input": [ "4\naaa\nabc\nabdbca\nabba" ], "output": [ "YES\nNO\nYES\nYES\n" ] }
{ "input": [], "output": [] }
CORRECT
python2
def isPal(s): l=len(s) for x in xrange(l/2): if s[x]!=s[-1-x]: return False return True def isPos(): s=raw_input() n=len(s) for i in xrange(n/2): if s[i]!=s[n-1-i]: if isPal(s[i:n-1-i]) or isPal(s[i+1:n-i]): return "YES" else: return "NO" return "YES" def solve(): t=input() for x in xrange(t): print isPos() solve()
prpaln
Given a string s. Can you make it a palindrome by deleting exactly one character? Note that size of the string after deletion would be one less than it was before. Input First line of the input contains a single integer T denoting number of test cases. For each test case, you are given a single line containing string s. Output For each test case, print YES or NO depending on the answer of the problem. Constraints Example Input: 4 aaa abc abdbca abba Output: YES NO YES YES Explanation Example case 1. Delete any one 'a', resulting string is "aa" which is a palindrome. Example case 2. It is not possible to delete exactly one character and having a palindrome. Example case 3. Delete 'c', resulting string is "abdba" which is a palindrome. Example case 4. Delete 'b', resulting string is "aba" which is a palindrome.
{ "input": [ "4\naaa\nabc\nabdbca\nabba" ], "output": [ "YES\nNO\nYES\nYES\n" ] }
{ "input": [], "output": [] }
CORRECT
python2
t=int(raw_input()) while t>0: s=raw_input() length=len(s) i=0 j=length-1 counter=0 while(i<j): if(s[i]!=s[j]): counter+=1 i+=1 else: i+=1 j-=1 if(counter<=1): print "YES" else: i=0 j=length-1 counter=0 while(i<j): if(s[i]!=s[j]): counter+=1 j-=1 else: i+=1 j-=1 if(counter<=1): print "YES" else: print "NO" t-=1
prpaln
Given a string s. Can you make it a palindrome by deleting exactly one character? Note that size of the string after deletion would be one less than it was before. Input First line of the input contains a single integer T denoting number of test cases. For each test case, you are given a single line containing string s. Output For each test case, print YES or NO depending on the answer of the problem. Constraints Example Input: 4 aaa abc abdbca abba Output: YES NO YES YES Explanation Example case 1. Delete any one 'a', resulting string is "aa" which is a palindrome. Example case 2. It is not possible to delete exactly one character and having a palindrome. Example case 3. Delete 'c', resulting string is "abdba" which is a palindrome. Example case 4. Delete 'b', resulting string is "aba" which is a palindrome.
{ "input": [ "4\naaa\nabc\nabdbca\nabba" ], "output": [ "YES\nNO\nYES\nYES\n" ] }
{ "input": [], "output": [] }
CORRECT
python2
def check(s,x): n=len(s) if (s==s[::-1]): return 1 else: if (x==0): for i in range(n): if (s[i]!=s[n-i-1]): t=s[:i]+s[i+1:] u=s[:n-i-1]+s[n-i:] return (check(t,1) or check(u,1)) else: return 0 for t in range(int(raw_input())): s=raw_input() if (check(s,0)): print "YES" else: print "NO"
prpaln
Given a string s. Can you make it a palindrome by deleting exactly one character? Note that size of the string after deletion would be one less than it was before. Input First line of the input contains a single integer T denoting number of test cases. For each test case, you are given a single line containing string s. Output For each test case, print YES or NO depending on the answer of the problem. Constraints Example Input: 4 aaa abc abdbca abba Output: YES NO YES YES Explanation Example case 1. Delete any one 'a', resulting string is "aa" which is a palindrome. Example case 2. It is not possible to delete exactly one character and having a palindrome. Example case 3. Delete 'c', resulting string is "abdba" which is a palindrome. Example case 4. Delete 'b', resulting string is "aba" which is a palindrome.
{ "input": [ "4\naaa\nabc\nabdbca\nabba" ], "output": [ "YES\nNO\nYES\nYES\n" ] }
{ "input": [], "output": [] }
CORRECT
python2
t=input() while t>0: t-=1 s=raw_input() i,j=0,len(s)-1 lim=0 while i<j: if s[i]!=s[j]: lim+=1 if s[i]==s[j-1]: k,l=i,j-1 flag=0 while k<l: if s[k]!=s[l]: flag=1 break k+=1 l-=1 if(flag==0): break if s[i+1]==s[j]: k,l=i+1,j flag=0 while k<l: if s[k]!=s[l]: flag=1 break k+=1 l-=1 if(flag==0): break lim=2 break i+=1 j-=1 if lim>1: break if lim>1: print 'NO' else: print 'YES'
prpaln
Given a string s. Can you make it a palindrome by deleting exactly one character? Note that size of the string after deletion would be one less than it was before. Input First line of the input contains a single integer T denoting number of test cases. For each test case, you are given a single line containing string s. Output For each test case, print YES or NO depending on the answer of the problem. Constraints Example Input: 4 aaa abc abdbca abba Output: YES NO YES YES Explanation Example case 1. Delete any one 'a', resulting string is "aa" which is a palindrome. Example case 2. It is not possible to delete exactly one character and having a palindrome. Example case 3. Delete 'c', resulting string is "abdba" which is a palindrome. Example case 4. Delete 'b', resulting string is "aba" which is a palindrome.
{ "input": [ "4\naaa\nabc\nabdbca\nabba" ], "output": [ "YES\nNO\nYES\nYES\n" ] }
{ "input": [], "output": [] }
CORRECT
python2
def check_palindrome(s): #s = list(s) l = len(s) j = l-1 for i in range(l/2): if s[i] != s[j]: return False j = j-1 return True #if (__name__ == "__main__"): if(True): t = raw_input() t = int(t) for i in range(t): s = raw_input() l = len(s) y = 0 j = l-1 cnt = 0 for k in range(l/2): if(s[k] == s[j]): y = 1 j = j-1 else: list1 = list(s) list1.pop(k) str1 = ''.join(list1) list1 = list(s) list1.pop(j) str2 = ''.join(list1) if(check_palindrome(str1)or check_palindrome(str2)): y = 1 break; else: y = 0 break; if(y == 1): print 'YES' else: print 'NO'
prpaln
Given a string s. Can you make it a palindrome by deleting exactly one character? Note that size of the string after deletion would be one less than it was before. Input First line of the input contains a single integer T denoting number of test cases. For each test case, you are given a single line containing string s. Output For each test case, print YES or NO depending on the answer of the problem. Constraints Example Input: 4 aaa abc abdbca abba Output: YES NO YES YES Explanation Example case 1. Delete any one 'a', resulting string is "aa" which is a palindrome. Example case 2. It is not possible to delete exactly one character and having a palindrome. Example case 3. Delete 'c', resulting string is "abdba" which is a palindrome. Example case 4. Delete 'b', resulting string is "aba" which is a palindrome.
{ "input": [ "4\naaa\nabc\nabdbca\nabba" ], "output": [ "YES\nNO\nYES\nYES\n" ] }
{ "input": [], "output": [] }
CORRECT
python2
t = input() while(t>0): t = t-1 s = raw_input() length = len(s) if(length==2): print "YES" continue freq = [0]*26 for x in (s): pos=ord(x) freq[pos-97] = freq[pos-97]+1 evenfreq=0 oddfreq=0 diffcharfreq=0 for x in range(0,26): if(freq[x]==0): continue; if(freq[x]%2==0): evenfreq=evenfreq+1 if(freq[x]%2==1): oddfreq=oddfreq+1 if(freq[x]>0): diffcharfreq = diffcharfreq+1 if(diffcharfreq==1): print "YES" continue if(diffcharfreq==length): print "NO" continue if(oddfreq>=3): print "NO" continue flag=0 i=0 j=length-1 si=0 li=0; while(i<=j): if(s[i]==s[j]): i=i+1 j=j-1 else: flag=1 si=i li=j break sic=si lic=li siflag=0 liflag=0 if(flag==1): #si wala deleted si=si+1 while(si<=li): if(s[si]!=s[li]): siflag=1 break else: si=si+1 li=li-1 if(siflag==0): print "YES" continue #lic wala deleted lic = lic -1 # print "lic = "+lic # print "sic = "+sic while(sic<=lic): if(s[sic]!=s[lic]): liflag=1 break else: sic=sic+1 lic=lic-1 if(liflag==0): print "YES" continue if(siflag==1): print "NO" continue if(liflag==1): print "NO" continue else: pflag=0 if(flag==0): if(length%2==1): pi=0 pj=length-1 while(pi<pj): if(s[pi]==s[pj]): pi=pi+1 pj=pj-1 else: pflag=1 if(pflag==0): print "YES" continue if(length%2==0): t1=length/2 t2=(length-2)/2 if(s[t1]==s[t2]): print "YES" continue
prpaln
Given a string s. Can you make it a palindrome by deleting exactly one character? Note that size of the string after deletion would be one less than it was before. Input First line of the input contains a single integer T denoting number of test cases. For each test case, you are given a single line containing string s. Output For each test case, print YES or NO depending on the answer of the problem. Constraints Example Input: 4 aaa abc abdbca abba Output: YES NO YES YES Explanation Example case 1. Delete any one 'a', resulting string is "aa" which is a palindrome. Example case 2. It is not possible to delete exactly one character and having a palindrome. Example case 3. Delete 'c', resulting string is "abdba" which is a palindrome. Example case 4. Delete 'b', resulting string is "aba" which is a palindrome.
{ "input": [ "4\naaa\nabc\nabdbca\nabba" ], "output": [ "YES\nNO\nYES\nYES\n" ] }
{ "input": [], "output": [] }
CORRECT
python2
def check_palin(val, i, j): flag = False while i < j: if val[i] != val[j]: flag = True break i += 1 j -= 1 if flag: return (i, j) return (-1, -1) def process(): val = raw_input() i, j = check_palin(val, 0, len(val)-1) if i == -1 or j == -1: print "YES" return ii, jj = check_palin(val, i+1, j) if ii == -1 or jj == -1: print "YES" return ii, jj = check_palin(val, i, j-1) if ii == -1 or jj == -1: print "YES" return print "NO" def main(): T = input() for i in xrange(T): process() if __name__ == '__main__': main()
prpaln
Given a string s. Can you make it a palindrome by deleting exactly one character? Note that size of the string after deletion would be one less than it was before. Input First line of the input contains a single integer T denoting number of test cases. For each test case, you are given a single line containing string s. Output For each test case, print YES or NO depending on the answer of the problem. Constraints Example Input: 4 aaa abc abdbca abba Output: YES NO YES YES Explanation Example case 1. Delete any one 'a', resulting string is "aa" which is a palindrome. Example case 2. It is not possible to delete exactly one character and having a palindrome. Example case 3. Delete 'c', resulting string is "abdba" which is a palindrome. Example case 4. Delete 'b', resulting string is "aba" which is a palindrome.
{ "input": [ "4\naaa\nabc\nabdbca\nabba" ], "output": [ "YES\nNO\nYES\nYES\n" ] }
{ "input": [], "output": [] }
CORRECT
python2
for _ in range(int(raw_input())): s=list(raw_input()) s1c=list(s) s2c=list(s) n=len(s) flag=0 for i in range(n/2): if s[i]!=s[n-1-i]: del s1c[i] if s1c==s1c[::-1]: print "YES" flag=1 break del s2c[n-1-i] if s2c==s2c[::-1]: print "YES" flag=1 break print "NO" flag=1 break if flag==0: print "YES"
prpaln
Given a string s. Can you make it a palindrome by deleting exactly one character? Note that size of the string after deletion would be one less than it was before. Input First line of the input contains a single integer T denoting number of test cases. For each test case, you are given a single line containing string s. Output For each test case, print YES or NO depending on the answer of the problem. Constraints Example Input: 4 aaa abc abdbca abba Output: YES NO YES YES Explanation Example case 1. Delete any one 'a', resulting string is "aa" which is a palindrome. Example case 2. It is not possible to delete exactly one character and having a palindrome. Example case 3. Delete 'c', resulting string is "abdba" which is a palindrome. Example case 4. Delete 'b', resulting string is "aba" which is a palindrome.
{ "input": [ "4\naaa\nabc\nabdbca\nabba" ], "output": [ "YES\nNO\nYES\nYES\n" ] }
{ "input": [], "output": [] }
CORRECT
python2
import sys for __ in range(input()) : a=list(raw_input()) answered=False s=[i for i in reversed(a)] for i in range(len(s)) : if s[i]!=a[i] : s.pop(i) a.pop(i) #print s,a if s==s[::-1] or a==a[::-1] : print "YES" answered=True break else : print "NO" answered=True break if not answered : print "YES"
tf01
An established group of scientists are working on finding solution to NP hard problems. They claim Subset Sum as an NP-hard problem. The problem is to determine whether there exists a subset of a given set S whose sum is a given number K. You are a computer engineer and you claim to solve this problem given that all numbers in the set are non-negative. Given a set S of size N of non-negative integers, find whether there exists a subset whose sum is K. Input First line of input contains T, the number of test cases. T test cases follow. Each test case contains 2 lines. First line contains two integers N and K. Next line contains N space separated non-negative integers (each less than 100000). 0 < T < 1000 0 < N < 1000 0 < K < 1000 Output Output T lines, one for each test case. Every line should be either 0 or 1 depending on whether such a subset exists or not. Example Input: 2 5 10 3 4 6 1 9 3 2 1 3 4 Output: 1 0
{ "input": [ "2\n5 10\n3 4 6 1 9\n3 2\n1 3 4" ], "output": [ "1\n0" ] }
{ "input": [], "output": [] }
CORRECT
python2
import sys for __ in range(input()) : n , k = map(int,sys.stdin.readline().split()) lists = map(int,sys.stdin.readline().split()) dp = [0]*(k+1) dp[0]=1 for i in lists : for j in range(k-i,-1,-1) : if dp[k] : break if dp[j] : dp[j+i] = 1 print dp[k]
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int MAXN = 300000; map<int, int> mapa; map<pair<int, int>, vector<int>> pos; vector<int> g[MAXN]; int ptr[MAXN]; int used[MAXN]; void euler(int v, vector<int> &res) { used[v] = true; for (; ptr[v] < (int)(g[v]).size();) { ++ptr[v]; int u = g[v][ptr[v] - 1]; euler(u, res); res.push_back(u); } } int main() { ios_base::sync_with_stdio(false); cin.tie(0); int n, s; cin >> n >> s; int k = 0; vector<int> a(n); for (int i = 0; i < n; ++i) { cin >> a[i]; } vector<int> b = a; sort((b).begin(), (b).end()); int m = 0; for (int i = 0; i < n; ++i) { if (a[i] == b[i]) { continue; } ++m; if (!mapa.count(b[i])) { mapa[b[i]] = k++; } } if (m > s) { cout << -1 << endl; return 0; } for (int i = 0; i < n; ++i) { if (a[i] == b[i]) { continue; } a[i] = mapa[a[i]]; b[i] = mapa[b[i]]; g[b[i]].push_back(a[i]); pos[{b[i], a[i]}].push_back(i); } vector<vector<int>> cycles; for (int i = 0; i < k; ++i) { if (!used[i]) { vector<int> arr; euler(i, arr); reverse((arr).begin(), (arr).end()); cycles.push_back({}); for (int i = 0; i < (int)(arr).size(); ++i) { int j = (i + 1) % (int)(arr).size(); cycles.back().push_back(pos[{arr[i], arr[j]}].back()); pos[{arr[i], arr[j]}].pop_back(); } } } vector<vector<int>> res; if (s - m > 1 && (int)(cycles).size() > 1) { int len = min((int)(cycles).size(), s - m); res.push_back({}); vector<int> newcycle; for (int i = (int)(cycles).size() - len; i < (int)(cycles).size(); ++i) { res.back().push_back(cycles[i].back()); for (int j : cycles[i]) { newcycle.push_back(j); } } reverse((res.back()).begin(), (res.back()).end()); for (int i = 0; i < len; ++i) { cycles.pop_back(); } cycles.push_back(newcycle); } for (int i = 0; i < (int)(cycles).size(); ++i) { res.push_back(cycles[i]); } cout << (int)(res).size() << endl; for (int i = 0; i < (int)(res).size(); ++i) { cout << (int)(res[i]).size() << endl; for (int j : res[i]) { cout << j + 1 << " "; } cout << endl; } }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int n, k, tot, x, ans, a[1000000], b[1000000], f[1000000], ed[1000000], nt[1000000], nm[1000000], ss[1000000]; map<int, int> mp, rw; bool v[1000000]; int gf(int x) { if (f[x] == x) return x; f[x] = gf(f[x]); return f[x]; } int main() { scanf("%d %d", &n, &k); for (int i = (1); i <= (n); i++) { scanf("%d", &a[i]); b[i] = a[i]; } sort(b + 1, b + n + 1); for (int i = (1); i <= (n); i++) if (b[i] != b[i - 1]) mp[b[i]] = i; for (int i = (1); i <= (n); i++) if (a[i] != b[i]) k--; else v[i] = 1; for (int i = (1); i <= (n); i++) for (; v[mp[b[i]]]; mp[b[i]]++) { if (b[mp[b[i]]] != b[i]) { mp[b[i]] = 0; break; } } if (k < 0) { printf("-1"); return 0; } for (int i = (1); i <= (n); i++) if (!v[i]) { tot++; for (x = i; x; x = mp[a[x]]) { for (mp[b[x]]++; v[mp[b[x]]]; mp[b[x]]++) { if (b[mp[b[x]]] != b[x]) { mp[b[x]] = 0; break; } } if (b[mp[b[x]]] != b[x]) mp[b[x]] = 0; v[x] = 1; if (ed[tot]) { nt[ed[tot]] = x; f[x] = f[ed[tot]]; } else f[x] = x; ed[tot] = x; nm[f[x]]++; } nt[ed[tot]] = f[ed[tot]]; } for (int i = (1); i <= (n); i++) if (a[i] != b[i]) { if (rw[a[i]]) { if (gf(i) != gf(rw[a[i]])) { nm[f[rw[a[i]]]] += nm[f[i]]; f[f[i]] = f[rw[a[i]]]; swap(nt[i], nt[rw[a[i]]]); } } else rw[a[i]] = i; } tot = 0; for (int i = (1); i <= (n); i++) if ((a[i] != b[i]) && (gf(i) == i)) ss[++tot] = i; if ((tot > 2) && (k > 2)) { ans = tot - ((tot) < (k) ? (tot) : (k)) + 1; x = nt[ss[ans]]; for (int i = (ans + 1); i <= (tot); i++) { nt[ss[i - 1]] = nt[ss[i]]; nm[ss[ans]] += nm[ss[i]]; } nt[ss[tot]] = x; } else ans = tot; if (ans < tot) printf("%d\n", ans + 1); else printf("%d\n", ans); for (int i = (1); i <= (ans); i++) { printf("%d\n", nm[ss[i]]); for (x = ss[i]; nt[x] != ss[i]; x = nt[x]) printf("%d ", x); printf("%d\n", x); } if (ans < tot) { printf("%d\n", tot - ans + 1); for (int i = (tot); i >= (ans); i--) printf("%d ", nt[ss[i]]); } }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; template <class BidirIt> BidirIt prev(BidirIt it, typename iterator_traits<BidirIt>::difference_type n = 1) { advance(it, -n); return it; } template <class ForwardIt> ForwardIt next(ForwardIt it, typename iterator_traits<ForwardIt>::difference_type n = 1) { advance(it, n); return it; } const double EPS = 1e-9; const double PI = 3.141592653589793238462; template <typename T> inline T sq(T a) { return a * a; } const int MAXN = 4e5 + 5; int ar[MAXN], sor[MAXN]; map<int, int> dummy; bool visit[MAXN], proc[MAXN]; int nxt[MAXN]; vector<int> gr[MAXN]; vector<int> cur, tour[MAXN]; vector<vector<int> > cycles; void addEdge(int u, int v) { gr[u].push_back(v); } void dfs(int u) { visit[u] = true; cur.push_back(u); while (nxt[u] < (int)gr[u].size()) { int v = gr[u][nxt[u]]; nxt[u]++; dfs(v); } if (cur.size() > 0) { if (!tour[u].empty()) assert(false); tour[u] = cur; cur.clear(); } } void getcycle(int u, vector<int> &vec) { proc[u] = true; for (auto it : tour[u]) { vec.push_back(it); if (!proc[it]) getcycle(it, vec); } } int main() { int n, s; scanf("%d %d", &n, &s); for (int i = 1; i <= n; i++) { scanf("%d", &ar[i]); sor[i] = ar[i]; } sort(sor + 1, sor + n + 1); for (int i = 1; i <= n; i++) { if (ar[i] != sor[i]) dummy[ar[i]] = 0; } int cnt = 0; for (auto &it : dummy) it.second = n + (++cnt); for (int i = 1; i <= n; i++) { if (ar[i] == sor[i]) continue; addEdge(dummy[sor[i]], i); addEdge(i, dummy[ar[i]]); } for (int i = 1; i <= n + cnt; i++) { if ((i > n || ar[i] != sor[i]) && !visit[i]) { dfs(i); vector<int> vec; getcycle(i, vec); vector<int> res; for (auto it : vec) if (it <= n) res.push_back(it); res.pop_back(); cycles.push_back(res); s -= (int)res.size(); } } for (int i = 1; i <= n + cnt; i++) if (i > n || ar[i] != sor[i]) assert(proc[i]); if (s < 0) { puts("-1"); return 0; } int pos = 0; if (s > 1 && cycles.size() > 1) { printf("%d\n", max(2, (int)cycles.size() - s + 2)); int sum = 0; while (s > 0 && pos < (int)cycles.size()) { s--; sum += (int)cycles[pos].size(); pos++; } printf("%d\n", sum); vector<int> vec; for (int i = 0; i < pos; i++) { for (auto it : cycles[i]) printf("%d ", it); vec.push_back(cycles[i][0]); } puts(""); reverse((vec).begin(), (vec).end()); printf("%d\n", (int)vec.size()); for (auto it : vec) printf("%d ", it); puts(""); } else { printf("%d\n", (int)cycles.size()); } for (int i = pos; i < (int)cycles.size(); i++) { printf("%d\n", (int)cycles[i].size()); for (auto it : cycles[i]) printf("%d ", it); puts(""); } return 0; }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
CORRECT
java
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.Arrays; import java.io.IOException; import java.util.Random; import java.util.ArrayList; import java.io.UncheckedIOException; import java.util.List; import java.io.Closeable; import java.io.Writer; import java.io.OutputStreamWriter; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) throws Exception { Thread thread = new Thread(null, new TaskAdapter(), "", 1 << 27); thread.start(); thread.join(); } static class TaskAdapter implements Runnable { @Override public void run() { InputStream inputStream = System.in; OutputStream outputStream = System.out; FastInput in = new FastInput(inputStream); FastOutput out = new FastOutput(outputStream); ECycleSort solver = new ECycleSort(); solver.solve(1, in, out); out.close(); } } static class ECycleSort { int n; int[] a; public void solve(int testNumber, FastInput in, FastOutput out) { n = in.readInt(); int s = in.readInt(); a = new int[n]; for (int i = 0; i < n; i++) { a[i] = in.readInt(); } int[] b = a.clone(); Randomized.shuffle(b); Arrays.sort(b); int[] same = new int[n]; int sum = 0; for (int i = 0; i < n; i++) { if (a[i] == b[i]) { same[i] = 1; } } for (int x : same) { sum += x; } if (n - sum > s) { out.println(-1); return; } IntegerList permList = new IntegerList(n); for (int i = 0; i < n; i++) { if (same[i] == 0) { permList.add(i); } } int[] perm = permList.toArray(); CompareUtils.quickSort(perm, (x, y) -> Integer.compare(a[x], a[y]), 0, perm.length); DSU dsu = new DSU(n); for (int i = 0; i < perm.length; i++) { int from = perm[i]; int to = permList.get(i); dsu.merge(from, to); } for (int i = 1; i < perm.length; i++) { if (a[perm[i]] != a[perm[i - 1]]) { continue; } if (dsu.find(perm[i]) == dsu.find(perm[i - 1])) { continue; } dsu.merge(perm[i], perm[i - 1]); SequenceUtils.swap(perm, i, i - 1); } IntegerList first = new IntegerList(); if (perm.length > 0) { int remain = s - (n - sum) - 1; first.add(perm[0]); for (int i = 1; remain > 0 && i < perm.length; i++) { if (dsu.find(perm[i - 1]) != dsu.find(perm[i])) { remain--; first.add(perm[i]); dsu.merge(perm[i - 1], perm[i]); } } int last = a[first.get(0)]; for (int i = 1; i < first.size(); i++) { int y = first.get(i); int tmp = a[y]; a[y] = last; last = tmp; } a[first.get(0)] = last; //System.err.println(Arrays.toString(a)); } List<IntegerList> circles = new ArrayList<>(); if (first.size() > 1) { circles.add(first); } circles.addAll(solve()); out.println(circles.size()); for (IntegerList list : circles) { out.println(list.size()); for (int i = 0; i < list.size(); i++) { out.append(list.get(i) + 1).append(' '); } out.println(); } } public List<IntegerList> solve() { int[] b = a.clone(); Randomized.shuffle(b); Arrays.sort(b); int[] same = new int[n]; for (int i = 0; i < n; i++) { if (a[i] == b[i]) { same[i] = 1; } } IntegerList permList = new IntegerList(n); for (int i = 0; i < n; i++) { if (same[i] == 0) { permList.add(i); } } int[] perm = permList.toArray(); CompareUtils.quickSort(perm, (x, y) -> Integer.compare(a[x], a[y]), 0, perm.length); DSU dsu = new DSU(n); for (int i = 0; i < perm.length; i++) { int from = perm[i]; int to = permList.get(i); dsu.merge(from, to); } for (int i = 1; i < perm.length; i++) { if (a[perm[i]] != a[perm[i - 1]]) { continue; } if (dsu.find(perm[i]) == dsu.find(perm[i - 1])) { continue; } dsu.merge(perm[i], perm[i - 1]); SequenceUtils.swap(perm, i, i - 1); } int[] index = new int[n]; for (int i = 0; i < n; i++) { if (same[i] == 1) { index[i] = i; } } for (int i = 0; i < perm.length; i++) { index[perm[i]] = permList.get(i); } PermutationUtils.PowerPermutation pp = new PermutationUtils.PowerPermutation(index); List<IntegerList> circles = pp.extractCircles(2); return circles; } } static class Randomized { private static Random random = new Random(0); public static void shuffle(int[] data) { shuffle(data, 0, data.length - 1); } public static void shuffle(int[] data, int from, int to) { to--; for (int i = from; i <= to; i++) { int s = nextInt(i, to); int tmp = data[i]; data[i] = data[s]; data[s] = tmp; } } public static int nextInt(int l, int r) { return random.nextInt(r - l + 1) + l; } } static class SequenceUtils { public static void swap(int[] data, int i, int j) { int tmp = data[i]; data[i] = data[j]; data[j] = tmp; } public static boolean equal(int[] a, int al, int ar, int[] b, int bl, int br) { if ((ar - al) != (br - bl)) { return false; } for (int i = al, j = bl; i <= ar; i++, j++) { if (a[i] != b[j]) { return false; } } return true; } } static class FastInput { private final InputStream is; private byte[] buf = new byte[1 << 20]; private int bufLen; private int bufOffset; private int next; public FastInput(InputStream is) { this.is = is; } private int read() { while (bufLen == bufOffset) { bufOffset = 0; try { bufLen = is.read(buf); } catch (IOException e) { bufLen = -1; } if (bufLen == -1) { return -1; } } return buf[bufOffset++]; } public void skipBlank() { while (next >= 0 && next <= 32) { next = read(); } } public int readInt() { int sign = 1; skipBlank(); if (next == '+' || next == '-') { sign = next == '+' ? 1 : -1; next = read(); } int val = 0; if (sign == 1) { while (next >= '0' && next <= '9') { val = val * 10 + next - '0'; next = read(); } } else { while (next >= '0' && next <= '9') { val = val * 10 - next + '0'; next = read(); } } return val; } } static class FastOutput implements AutoCloseable, Closeable, Appendable { private StringBuilder cache = new StringBuilder(10 << 20); private final Writer os; public FastOutput append(CharSequence csq) { cache.append(csq); return this; } public FastOutput append(CharSequence csq, int start, int end) { cache.append(csq, start, end); return this; } public FastOutput(Writer os) { this.os = os; } public FastOutput(OutputStream os) { this(new OutputStreamWriter(os)); } public FastOutput append(char c) { cache.append(c); return this; } public FastOutput append(int c) { cache.append(c); return this; } public FastOutput println(int c) { cache.append(c); println(); return this; } public FastOutput println() { cache.append(System.lineSeparator()); return this; } public FastOutput flush() { try { os.append(cache); os.flush(); cache.setLength(0); } catch (IOException e) { throw new UncheckedIOException(e); } return this; } public void close() { flush(); try { os.close(); } catch (IOException e) { throw new UncheckedIOException(e); } } public String toString() { return cache.toString(); } } static class IntegerList implements Cloneable { private int size; private int cap; private int[] data; private static final int[] EMPTY = new int[0]; public IntegerList(int cap) { this.cap = cap; if (cap == 0) { data = EMPTY; } else { data = new int[cap]; } } public IntegerList(IntegerList list) { this.size = list.size; this.cap = list.cap; this.data = Arrays.copyOf(list.data, size); } public IntegerList() { this(0); } public void ensureSpace(int req) { if (req > cap) { while (cap < req) { cap = Math.max(cap + 10, 2 * cap); } data = Arrays.copyOf(data, cap); } } private void checkRange(int i) { if (i < 0 || i >= size) { throw new ArrayIndexOutOfBoundsException(); } } public int get(int i) { checkRange(i); return data[i]; } public void add(int x) { ensureSpace(size + 1); data[size++] = x; } public void addAll(int[] x, int offset, int len) { ensureSpace(size + len); System.arraycopy(x, offset, data, size, len); size += len; } public void addAll(IntegerList list) { addAll(list.data, 0, list.size); } public int size() { return size; } public int[] toArray() { return Arrays.copyOf(data, size); } public String toString() { return Arrays.toString(toArray()); } public boolean equals(Object obj) { if (!(obj instanceof IntegerList)) { return false; } IntegerList other = (IntegerList) obj; return SequenceUtils.equal(data, 0, size - 1, other.data, 0, other.size - 1); } public int hashCode() { int h = 1; for (int i = 0; i < size; i++) { h = h * 31 + Integer.hashCode(data[i]); } return h; } public IntegerList clone() { IntegerList ans = new IntegerList(size); ans.addAll(this); return ans; } } static class DSU { int[] p; int[] rank; public DSU(int n) { p = new int[n]; rank = new int[n]; reset(); } public void reset() { for (int i = 0; i < p.length; i++) { p[i] = i; rank[i] = 0; } } public int find(int a) { return p[a] == p[p[a]] ? p[a] : (p[a] = find(p[a])); } public void merge(int a, int b) { a = find(a); b = find(b); if (a == b) { return; } if (rank[a] == rank[b]) { rank[a]++; } if (rank[a] > rank[b]) { p[b] = a; } else { p[a] = b; } } } static interface IntComparator { public int compare(int a, int b); } static class PermutationUtils { private static final long[] PERMUTATION_CNT = new long[21]; static { PERMUTATION_CNT[0] = 1; for (int i = 1; i <= 20; i++) { PERMUTATION_CNT[i] = PERMUTATION_CNT[i - 1] * i; } } public static class PowerPermutation { int[] g; int[] idx; int[] l; int[] r; int n; public List<IntegerList> extractCircles(int threshold) { List<IntegerList> ans = new ArrayList<>(n); for (int i = 0; i < n; i = r[i] + 1) { int size = r[i] - l[i] + 1; if (size < threshold) { continue; } IntegerList list = new IntegerList(r[i] - l[i] + 1); for (int j = l[i]; j <= r[i]; j++) { list.add(g[j]); } ans.add(list); } return ans; } public PowerPermutation(int[] p) { this(p, p.length); } public PowerPermutation(int[] p, int len) { n = len; boolean[] visit = new boolean[n]; g = new int[n]; l = new int[n]; r = new int[n]; idx = new int[n]; int wpos = 0; for (int i = 0; i < n; i++) { int val = p[i]; if (visit[val]) { continue; } visit[val] = true; g[wpos] = val; l[wpos] = wpos; idx[val] = wpos; wpos++; while (true) { int x = p[g[wpos - 1]]; if (visit[x]) { break; } visit[x] = true; g[wpos] = x; l[wpos] = l[wpos - 1]; idx[x] = wpos; wpos++; } for (int j = l[wpos - 1]; j < wpos; j++) { r[j] = wpos - 1; } } } public int apply(int x, int p) { int i = idx[x]; int dist = DigitUtils.mod((i - l[i]) + p, r[i] - l[i] + 1); return g[dist + l[i]]; } public String toString() { StringBuilder builder = new StringBuilder(); for (int i = 0; i < n; i++) { builder.append(apply(i, 1)).append(' '); } return builder.toString(); } } } static class CompareUtils { private static final int THRESHOLD = 4; private CompareUtils() { } public static <T> void insertSort(int[] data, IntComparator cmp, int l, int r) { for (int i = l + 1; i <= r; i++) { int j = i; int val = data[i]; while (j > l && cmp.compare(data[j - 1], val) > 0) { data[j] = data[j - 1]; j--; } data[j] = val; } } public static void quickSort(int[] data, IntComparator cmp, int f, int t) { if (t - f <= THRESHOLD) { insertSort(data, cmp, f, t - 1); return; } SequenceUtils.swap(data, f, Randomized.nextInt(f, t - 1)); int l = f; int r = t; int m = l + 1; while (m < r) { int c = cmp.compare(data[m], data[l]); if (c == 0) { m++; } else if (c < 0) { SequenceUtils.swap(data, l, m); l++; m++; } else { SequenceUtils.swap(data, m, --r); } } quickSort(data, cmp, f, l); quickSort(data, cmp, m, t); } } static class DigitUtils { private DigitUtils() { } public static int mod(int x, int mod) { x %= mod; if (x < 0) { x += mod; } return x; } } }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); } const int MAXN = 200000; int n, lim; int a[MAXN]; int b[MAXN]; bool isfixed[MAXN]; pair<int, int> o[MAXN]; int no; int dst[MAXN]; int cyc[MAXN], ncyc; int par[MAXN], sz[MAXN]; int find(int a) { if (par[a] == a) return a; return par[a] = find(par[a]); } bool unite(int a, int b) { a = find(a), b = find(b); if (a == b) return false; if (sz[a] < sz[b]) swap(a, b); par[b] = a, sz[a] += sz[b]; return true; } bool done[MAXN]; vector<vector<int>> ans; bool solve() { for (int i = (0); i < (n); ++i) b[i] = a[i]; sort(b, b + n); int nfixed = 0; for (int i = (0); i < (n); ++i) { isfixed[i] = a[i] == b[i]; if (isfixed[i]) ++nfixed; } if (n - nfixed > lim) return false; no = 0; for (int i = (0); i < (n); ++i) if (!isfixed[i]) o[no++] = make_pair(a[i], i); sort(o, o + no); for (int i = (0); i < (n); ++i) dst[i] = isfixed[i] ? i : -1; { int at = 0; for (int i = (0); i < (no); ++i) { while (at < n && isfixed[at]) ++at; dst[o[i].second] = at++; } } for (int i = (0); i < (n); ++i) cyc[i] = -1; ncyc = 0; for (int i = (0); i < (n); ++i) if (cyc[i] == -1 && dst[i] != i) { int at = i; while (cyc[at] == -1) { cyc[at] = ncyc; at = dst[at]; } ++ncyc; } int expect = ncyc; for (int i = (0); i < (ncyc); ++i) par[i] = i, sz[i] = 1; for (int l = 0, r = l; l < no; l = r) { while (r < no && o[l].first == o[r].first) ++r; int cur = o[l].second; for (int i = (l + 1); i < (r); ++i) { int oth = o[i].second; if (unite(cyc[cur], cyc[oth])) { swap(dst[cur], dst[oth]); --expect; } } } for (int i = (0); i < (n); ++i) done[i] = false; ans.clear(); for (int i = (0); i < (n); ++i) if (!done[i] && dst[i] != i) { ans.push_back(vector<int>()); int at = i; while (!done[at]) { done[at] = true; ans.back().push_back(at); at = dst[at]; } } assert(((int)(ans).size()) == expect); int rem = lim - (n - nfixed); if (((int)(ans).size()) >= 3 && rem >= 3) { int cnt = min(rem, ((int)(ans).size())); vector<int> fst(cnt); for (int i = (0); i < (cnt); ++i) fst[i] = ans[i][0]; reverse(fst.begin(), fst.end()); vector<int> snd; for (int i = (0); i < (cnt); ++i) { snd.push_back(ans[i][0]); int j = (i + 1) % cnt; for (int k = (1); k < (((int)(ans[j]).size())); ++k) snd.push_back(ans[j][k]); } vector<vector<int>> nans; nans.push_back(fst); nans.push_back(snd); for (int i = (cnt); i < (((int)(ans).size())); ++i) nans.push_back(ans[i]); ans = nans; } return true; } bool verify(vector<vector<int>> ans) { vector<int> cur(n); for (int i = (0); i < (n); ++i) cur[i] = a[i]; for (int i = (0); i < (((int)(ans).size())); ++i) { int val = cur[ans[i][0]]; for (int j = (1); j < (((int)(ans[i]).size())); ++j) swap(val, cur[ans[i][j]]); cur[ans[i][0]] = val; } for (int i = (1); i < (((int)(cur).size())); ++i) if (cur[i] < cur[i - 1]) return false; return true; } void run() { scanf("%d%d", &n, &lim); for (int i = (0); i < (n); ++i) scanf("%d", &a[i]); if (!solve()) { printf("-1\n"); return; } printf("%d\n", ((int)(ans).size())); for (int i = (0); i < (((int)(ans).size())); ++i) { printf("%d\n", ((int)(ans[i]).size())); for (int j = (0); j < (((int)(ans[i]).size())); ++j) { if (j != 0) printf(" "); printf("%d", ans[i][j] + 1); } puts(""); } assert(verify(ans)); } int main() { run(); return 0; }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int n, s, a[200005], b[200005], c[200005], vis[200005], wa, cir, prn[200005], to[200005], nex[200005], beg[200005], val[200005], e; bool acs[200005]; void insert(int x, int y, int z) { to[++e] = y; nex[e] = beg[x]; beg[x] = e; val[e] = z; } vector<int> tek[200005]; void dfs(int now) { vis[now] = cir; for (int &i = beg[now]; i; i = nex[i]) { if (!acs[i]) { int tmp = i; acs[i] = true; dfs(to[i]); tek[cir].push_back(val[tmp]); } } } int main() { scanf("%d %d", &n, &s); for (int i = 1; i <= n; ++i) scanf("%d", &a[i]), b[i] = a[i]; sort(b + 1, b + 1 + n); int len = unique(b + 1, b + 1 + n) - b - 1; for (int i = 1; i <= n; ++i) c[i] = a[i] = lower_bound(b + 1, b + 1 + len, a[i]) - b; sort(c + 1, c + 1 + n); for (int i = 1; i <= n; ++i) if (c[i] != a[i]) ++wa, insert(a[i], c[i], i); if (wa > s) return puts("-1") & 0; for (int i = 1; i <= n; ++i) if (!vis[i] && beg[i]) ++cir, dfs(i); if (cir <= 1 || s - wa <= 1) { printf("%d\n", cir); for (int i = 1; i <= cir; ++i) { printf("%d\n", int(tek[i].size())); for (unsigned int j = 0; j < tek[i].size(); ++j) printf("%d ", tek[i][j]); puts(""); } } else { printf("%d\n", cir - min(cir, s - wa) + 2); for (int i = s - wa + 1; i <= cir; ++i) { printf("%d\n", int(tek[i].size())); for (unsigned int j = 0; j < tek[i].size(); ++j) printf("%d ", tek[i][j]); puts(""); } if (s != wa) { int p1 = 0, p2 = 0; for (int i = min(s - wa, cir); i; --i) p1 += int(tek[i].size()), prn[++p2] = tek[i][0]; printf("%d\n", p1); for (int i = 1; i <= min(s - wa, cir); ++i) for (unsigned int j = 0; j < tek[i].size(); ++j) printf("%d ", tek[i][j]); puts(""); printf("%d\n", p2); for (int i = 1; i <= p2; ++i) printf("%d ", prn[i]); } } return 0; }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 20; int a[maxn], q[maxn], num[maxn], par[maxn]; bool visited[maxn]; vector<int> ind[maxn], cycle[maxn]; int f(int v) { return (par[v] == -1) ? v : par[v] = f(par[v]); } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); memset(par, -1, sizeof par); int n, s; cin >> n >> s; vector<int> tmp; for (int i = 0; i < n; i++) cin >> a[i], tmp.push_back(a[i]); sort(tmp.begin(), tmp.end()); tmp.resize(unique(tmp.begin(), tmp.end()) - tmp.begin()); for (int i = 0; i < n; i++) a[i] = lower_bound(tmp.begin(), tmp.end(), a[i]) - tmp.begin(); for (int i = 0; i < n; i++) ind[a[i]].push_back(i); int t = 0; for (int i = 0; i < (int)tmp.size(); i++) { int sz = ind[i].size(); vector<int> tmp2 = ind[i]; for (auto &x : ind[i]) if (t <= x && x < t + sz) { a[x] = x; visited[x] = 1; x = 1e9; } sort(ind[i].begin(), ind[i].end()); while (!ind[i].empty() && ind[i].back() > n) ind[i].pop_back(); tmp2 = ind[i]; for (int j = t; j < t + sz; j++) if (!visited[j]) a[tmp2.back()] = j, tmp2.pop_back(); t += sz; } t = 0; memset(par, -1, sizeof par); for (int i = 0; i < n; i++) if (!visited[i]) { while (!visited[i]) { visited[i] = 1; cycle[t].push_back(i); i = a[i]; } for (int j = 1; j < (int)cycle[t].size(); j++) par[cycle[t][j]] = cycle[t][0]; t++; } for (int i = 0; i < (int)tmp.size(); i++) { if (ind[i].empty()) continue; int marja = ind[i].back(); ind[i].pop_back(); for (auto shit : ind[i]) { if (f(shit) == f(marja)) continue; par[f(shit)] = f(marja); swap(a[shit], a[marja]); } } memset(visited, 0, sizeof visited); for (int i = 0; i < t; i++) cycle[i].clear(); t = 0; int T = 0; for (int i = 0; i < n; i++) if (!visited[i]) { int sz = 0; while (!visited[i]) { sz++; cycle[t].push_back(i); visited[i] = 1; i = a[i]; } if (sz != 1) t++; else cycle[t].clear(); } if (!t) return cout << 0 << endl, 0; if (t == 1) { if (s < (int)cycle[0].size()) return cout << -1 << endl, 0; cout << 1 << endl; cout << cycle[0].size() << endl; for (auto x : cycle[0]) cout << x + 1 << " "; cout << endl; return 0; } for (int i = 0; i < t; i++) T += (int)cycle[i].size(); if (s >= T + t) { cout << 2 << endl; cout << T << endl; for (int i = 0; i < t; i++) for (auto x : cycle[i]) cout << x + 1 << " "; cout << endl; cout << t << endl; for (int i = t - 1; i >= 0; i--) cout << cycle[i][0] + 1 << " "; cout << endl; return 0; } if (s < T) return cout << -1 << endl, 0; s -= T; if (s < 2) { cout << t << endl; for (int i = 0; i < t; i++) { cout << cycle[i].size() << endl; for (auto x : cycle[i]) cout << x + 1 << " "; cout << endl; } return 0; } cout << 2 + (t - s) << endl; for (int i = s; i < t; i++) { cout << cycle[i].size() << endl; for (auto x : cycle[i]) cout << x + 1 << " "; cout << endl; } t = s; T = 0; for (int i = 0; i < t; i++) T += (int)cycle[i].size(); cout << T << endl; for (int i = 0; i < t; i++) for (auto x : cycle[i]) cout << x + 1 << " "; cout << endl; cout << t << endl; for (int i = t - 1; i >= 0; i--) cout << cycle[i][0] + 1 << " "; cout << endl; }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 5; int n, S; int a[N], p[N], q[N]; vector<vector<int>> cir; int uf[N]; int find(int x) { return (uf[x] == x) ? (x) : (uf[x] = find(uf[x])); } void adjust() { for (int i = 1; i <= n; i++) { uf[i] = i; } pair<int, int>* b = new pair<int, int>[(n + 1)]; for (int i = 1; i <= n; i++) b[i] = pair<int, int>(a[i], i); sort(b + 1, b + n + 1); for (int i = 1, j = 1; i <= n; i++, i = j) { while (j <= n && b[i].first == b[j].first) j++; for (int k = i, x; k < j; k++) { x = b[k].second; if (x >= i && x < j) { q[x] = x; } } for (int k = i, cur = i; k < j; k++) { if (!q[k]) { while (cur < j && q[b[cur].second] == b[cur].second) cur++; q[k] = b[cur++].second; } } } for (int i = 1; i <= n; i++) { p[q[i]] = i; } for (int i = 1; i <= n; i++) { uf[find(i)] = find(p[i]); } for (int i = 1, j = 1, ls; i <= n; i = j) { while (j <= n && b[i].first == b[j].first) j++; for (ls = i; ls < j && q[ls] == ls; ls++) ; for (int k = ls + 1; k < j; k++) { if (q[k] != k && find(q[k]) != find(q[ls])) { uf[find(q[k])] = find(ls); uf[find(q[ls])] = find(k); swap(p[q[ls]], p[q[k]]); swap(q[ls], q[k]); ls = k; } } } } bool vis[N]; int find_circle(int x) { if (p[x] == x) { return 0; } cir.push_back(vector<int>()); do { vis[x] = true; cir.back().push_back(x); x = p[x]; } while (!vis[x]); return cir.back().size(); } int main() { scanf("%d%d", &n, &S); for (int i = 1; i <= n; i++) { scanf("%d", a + i); } adjust(); int t = 0, m = 0; for (int i = 1, x; i <= n; i++) { if (!vis[i]) { x = find_circle(i); t += x; m += (x != 0); } } if (t > S) { puts("-1"); return 0; } if (m <= 2 || t + 2 >= S) { printf("%d\n", m); for (int i = 0; i < m; i++) { printf("%d\n", (signed)cir[i].size()); for (auto& e : cir[i]) { printf("%d ", e); } putchar('\n'); } } else { int c = min(m, S - t); printf("%d\n", 2 + m - c); int sum = 0; for (int i = 0; i < c; i++) sum += cir[i].size(); printf("%d\n", sum); for (int i = 0; i < c; i++) { for (auto& e : cir[i]) { printf("%d ", e); } } putchar('\n'); printf("%d\n%d", c, cir[0][0]); for (int i = c - 1; i; i--) printf(" %d", cir[i][0]); putchar('\n'); for (int i = c; i < m; i++) { printf("%d\n", (signed)cir[i].size()); for (auto& e : cir[i]) { printf("%d ", e); } putchar('\n'); } } return 0; }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 100; int n, limit; int nxt[N], lab[N], flag[N]; pair<int, int> a[N]; vector<int> ans1, ans2; vector<vector<int> > ans3; vector<pair<int, int> > v; int root(int u) { if (lab[u] < 0) return u; return lab[u] = root(lab[u]); } void join(int u, int v) { int l1 = root(u), l2 = root(v); if (l1 == l2) return; if (lab[l1] > lab[l2]) swap(l1, l2); lab[l1] += lab[l2]; lab[l2] = l1; } void show(vector<int> &s) { cout << s.size() << '\n'; for (auto &x : s) cout << x << ' '; cout << '\n'; } int main() { ios::sync_with_stdio(0); cin >> n >> limit; memset(lab, -1, sizeof lab); for (int i = 1; i <= n; ++i) { cin >> a[i].first; a[i].second = i; } sort(a + 1, a + n + 1); for (int i = 1; i <= n; ++i) { int j1 = i, j2 = i; while (j2 < n && a[j2 + 1].first == a[j2].first) j2++; for (; i <= j2; ++i) { while (j1 <= a[i].second && a[i].second <= j2 && a[i].second != i) swap(a[i], a[a[i].second]); } i = j2; } for (int i = 1; i <= n; ++i) if (a[i].second != i) { nxt[a[i].second] = i; join(a[i].second, i); v.push_back(a[i]); } for (int i = 0; i < v.size(); ++i) { while (i + 1 < v.size() && v[i + 1].first == v[i].first) { i++; int idx1 = v[i].second, idx2 = v[i - 1].second; if (root(idx1) == root(idx2)) continue; join(idx1, idx2); swap(nxt[idx1], nxt[idx2]); } } int sum = 0, tmp = 0; for (int i = 1; i <= n; ++i) if (a[i].second != i && !flag[root(a[i].second)]) { flag[root(a[i].second)] = true; tmp++; sum += abs(lab[root(a[i].second)]); } if (limit < sum) { cout << -1; return 0; } int addmx = limit - sum; int cnt = 0; memset(flag, 0, sizeof flag); for (int i = 1; i <= n; ++i) if (a[i].second != i && !flag[root(a[i].second)]) { flag[root(a[i].second)] = true; cnt++; if (cnt <= addmx) { ans2.push_back(a[i].second); int cur = a[i].second; do { ans1.push_back(cur); cur = nxt[cur]; } while (cur != a[i].second); } else { vector<int> cycle; int cur = a[i].second; do { cycle.push_back(cur); cur = nxt[cur]; } while (cur != a[i].second); ans3.push_back(cycle); } } if (ans1.size()) ans3.push_back(ans1); reverse(ans2.begin(), ans2.end()); if (ans2.size() > 1) ans3.push_back(ans2); cout << ans3.size() << '\n'; for (auto &s : ans3) show(s); return 0; }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int MAXN = (int)2e5 + 3; const int infint = (int)1e9 + 3; const long long inf = (long long)1e17; int n, s, a[MAXN], idx[MAXN], final[MAXN], par[MAXN], visited[MAXN]; set<int> id[MAXN], num[MAXN]; vector<int> cyc[MAXN]; bool cmp(int u, int v) { return a[u] < a[v]; } int get(int u) { return par[u] < 0 ? u : par[u] = get(par[u]); } void merge(int u, int v) { if ((u = get(u)) == (v = get(v))) return; if (par[u] > par[v]) swap(u, v); par[u] += par[v]; par[u] = v; return; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n >> s; for (int i = 1; i <= n; i++) cin >> a[i]; for (int i = 1; i <= n; i++) idx[i] = i; sort(idx + 1, idx + n + 1, cmp); int sz = -1; for (int i = 1; i <= n; i++) { if (a[idx[i]] != a[idx[i - 1]]) sz++; id[sz].insert(i); num[sz].insert(idx[i]); } sz++; for (int i = 0; i < sz; i++) { vector<int> to_del; for (auto u : id[i]) if (num[i].find(u) != num[i].end()) to_del.push_back(u); for (auto u : to_del) final[u] = u, num[i].erase(u), id[i].erase(u); } memset(par, -1, sizeof par); for (int i = 0; i < sz; i++) { vector<int> curid, curnum; for (auto u : id[i]) curid.push_back(u); for (auto u : num[i]) curnum.push_back(u); for (int j = 0; j < curnum.size(); j++) final[curnum[j]] = curid[j], merge(curid[j], curnum[j]); } for (int i = 0; i < sz; i++) { vector<int> curnum; for (auto u : num[i]) curnum.push_back(u); for (int j = 1; j < curnum.size(); j++) if (get(curnum[j]) != get(curnum[j - 1])) swap(final[curnum[j]], final[curnum[j - 1]]), merge(curnum[j], curnum[j - 1]); } sz = -1; for (int i = 1; i <= n; i++) if (final[i] != i && !visited[i]) { sz++; int iter = i; while (!visited[iter]) { cyc[sz].push_back(iter); visited[iter] = 1; iter = final[iter]; } } sz++; int sum_of_cyc = 0; for (int i = 0; i < sz; i++) sum_of_cyc += cyc[i].size(); s -= sum_of_cyc; if (s < 0) return cout << -1, 0; if (sz <= 2 || s <= 2) { cout << sz << "\n"; for (int i = 0; i < sz; i++) { cout << cyc[i].size() << "\n"; for (auto u : cyc[i]) cout << u << " "; cout << "\n"; } return 0; } int merge_cyc = min(s, sz); cout << sz - merge_cyc + 2 << "\n"; int s0 = 0; for (int i = 0; i < merge_cyc; i++) s0 += cyc[i].size(); cout << s0 << "\n"; for (int i = 0; i < merge_cyc; i++) for (auto u : cyc[i]) cout << u << " "; cout << "\n"; cout << merge_cyc << "\n"; cout << cyc[0][0] << " "; for (int i = merge_cyc - 1; i >= 1; i--) cout << cyc[i][0] << " "; cout << "\n"; for (int i = merge_cyc; i < sz; i++) { cout << cyc[i].size() << "\n"; for (auto u : cyc[i]) cout << u << " "; cout << "\n"; } }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; template <typename T> void read(T &t) { t = 0; char ch = getchar(); int f = 1; while ('0' > ch || ch > '9') { if (ch == '-') f = -1; ch = getchar(); } do { (t *= 10) += ch - '0'; ch = getchar(); } while ('0' <= ch && ch <= '9'); t *= f; } const int maxn = (2e5) + 10; int n, s, a[maxn], b[maxn]; vector<vector<int> > ans; map<int, vector<int> > g; void dfs(int u) { while (!g[u].empty()) { int v = g[u].back(); g[u].pop_back(); dfs(a[v]); ans.back().push_back(v); } g.erase(u); } int main() { read(n); read(s); for (int i = 1; i <= n; i++) { read(a[i]); b[i] = a[i]; } sort(b + 1, b + n + 1); for (int i = 1; i <= n; i++) { if (a[i] != b[i]) g[b[i]].push_back(i); } while (!g.empty()) { ans.push_back(vector<int>()); dfs((*g.begin()).first); reverse(ans.back().begin(), ans.back().end()); s -= ans.back().size(); } if (s < 0) { printf("-1\n"); return 0; } int tmp = (int)ans.size(), b = min(s, tmp); if (b >= 3) tmp -= b - 2; printf("%d\n", tmp); if (b >= 3) { tmp = 0; for (int i = 0; i < b; i++) tmp += ans[i].size(); printf("%d\n", tmp); for (int i = 0; i < b; i++) for (int j = 0; j < ans[i].size(); j++) printf("%d ", ans[i][j]); printf("\n"); printf("%d\n", b); for (int i = b - 1; i >= 0; i--) printf("%d ", ans[i][0]); printf("\n"); } else b = 0; for (int i = b; i < ans.size(); i++) { printf("%d\n", ans[i].size()); for (int j = 0; j < ans[i].size(); j++) printf("%d ", ans[i][j]); printf("\n"); } return 0; }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int N = 200000; int n, m; int a[N + 1]; vector<int> nums; void discrete() { sort(nums.begin(), nums.end()); nums.resize(unique(nums.begin(), nums.end()) - nums.begin()); for (int i = 1; i <= n; i++) a[i] = lower_bound(nums.begin(), nums.end(), a[i]) - nums.begin(); } int val_to[N], pos_to[N + 1]; vector<pair<int, int> > nei[N + 1]; vector<bool> ban[N + 1]; vector<int> pa; vector<vector<int> > ans; bool vis[N + 1]; int now[N + 1]; void dfs(int x) { vis[x] = true; for (int &i = now[x]; i < nei[x].size(); i++) if (!ban[x][i]) { int y = nei[x][i].first, y0 = nei[x][i].second; ban[x][i] = true; dfs(y); pa.push_back(y0); } } int main() { cin >> n >> m; for (int i = 1; i <= n; i++) scanf("%d", a + i), nums.push_back(a[i]); discrete(); nums.clear(); for (int i = 1; i <= n; i++) nums.push_back(a[i]); sort(nums.begin(), nums.end()); for (int i = 0; i < n; i++) { if (!val_to[nums[i]]) val_to[nums[i]] = i + 1; pos_to[i + 1] = val_to[nums[i]]; } int cnt = 0; for (int i = 1; i <= n; i++) if (pos_to[i] != val_to[a[i]]) cnt++, nei[pos_to[i]].push_back(make_pair(val_to[a[i]], i)), ban[pos_to[i]].push_back(false); if (cnt > m) return puts("-1"), 0; for (int i = 1; i <= n; i++) if (!vis[i] && nei[i].size()) pa.clear(), dfs(i), ans.push_back(pa); if (min(int(ans.size()), m - cnt) > 1) { pa.clear(); for (int i = 0; i + 1 < min(int(ans.size()), m - cnt); i++) swap(a[ans[i][0]], a[ans[i + 1][0]]); for (int i = 0; i < min(int(ans.size()), m - cnt); i++) pa.push_back(ans[i][0]); ans.clear(); ans.push_back(pa); } else ans.clear(); for (int i = 1; i <= n; i++) nei[i].clear(), ban[i].clear(), vis[i] = now[i] = 0; for (int i = 1; i <= n; i++) if (pos_to[i] != val_to[a[i]]) nei[pos_to[i]].push_back(make_pair(val_to[a[i]], i)), ban[pos_to[i]].push_back(false); for (int i = 1; i <= n; i++) if (!vis[i] && nei[i].size()) pa.clear(), dfs(i), ans.push_back(pa); cout << ans.size() << "\n"; for (int i = 0; i < ans.size(); i++) { printf("%d\n", int(ans[i].size())); for (int j = ans[i].size() - 1; ~j; j--) printf("%d ", ans[i][j]); puts(""); } return 0; }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; struct ln { int val; ln* next; }; vector<pair<int, int> >* nl; vector<pair<int, int> > ar; int* roi; bool* btdt; ln* fe(ln* ath, int cp) { ln* sv = NULL; ln* ov = NULL; for (int i = roi[cp]; i < nl[cp].size(); i = roi[cp]) { int np = nl[cp][i].first; int ind = nl[cp][i].second; if (btdt[ind]) { continue; } roi[cp]++; btdt[ind] = 1; ln* cn = new ln(); cn->val = ind; cn->next = NULL; ln* vas = fe(cn, np); if (sv == NULL) { sv = cn; ov = vas; continue; } ath->next = cn; ath = vas; } if (sv != NULL) { ath->next = sv; ath = ov; } return ath; } int main() { cin.sync_with_stdio(0); cout.sync_with_stdio(0); int n, s; cin >> n >> s; int S = s; nl = new vector<pair<int, int> >[n]; btdt = new bool[n]; roi = new int[n]; for (int i = 0; i < n; i++) { roi[i] = 0; vector<pair<int, int> > vpii; nl[i] = vpii; btdt[i] = 0; int a; cin >> a; ar.push_back(make_pair(a, i)); } vector<pair<int, int> > br = ar; sort(br.begin(), br.end()); unordered_map<int, int> nct; int cn = -1; int an = -1; for (int i = 0; i < n; i++) { if (br[i].first != an) { cn++; an = br[i].first; nct[an] = cn; } br[i].first = cn; } for (int i = 0; i < n; i++) { ar[i].first = nct[ar[i].first]; } for (int i = 0; i < n; i++) { if (br[i].first == ar[i].first) { continue; } s--; nl[br[i].first].push_back(make_pair(ar[i].first, ar[i].second)); } if (s < 0) { cout << -1 << "\n"; return 0; } vector<ln*> atc; for (int i = 0; i < n; i++) { ln* tn = new ln(); tn->val = -1; tn->next = NULL; ln* on = fe(tn, i); if (on != tn) { atc.push_back(tn->next); } } int noc = atc.size(); int hmg = min(s, noc); vector<vector<int> > vas; if (hmg == 1) { hmg--; } if (hmg > 0) { if (S == 198000) { } vector<int> fv; vector<int> sv; for (int i = hmg - 1; i >= 0; i--) { fv.push_back(atc[i]->val); } for (int i = 0; i < hmg; i++) { int oi = i - 1; if (oi < 0) { oi += hmg; } sv.push_back(atc[oi]->val); ln* cn = atc[i]->next; while (cn != NULL) { sv.push_back(cn->val); cn = cn->next; } } vas.push_back(fv); vas.push_back(sv); } for (int i = hmg; i < atc.size(); i++) { vector<int> sv; ln* cn = atc[i]; while (cn != NULL) { sv.push_back(cn->val); cn = cn->next; } vas.push_back(sv); } cout << vas.size() << "\n"; for (int i = 0; i < vas.size(); i++) { cout << vas[i].size() << "\n"; for (int j = 0; j < vas[i].size(); j++) { if (j > 0) { cout << " "; } cout << (vas[i][j] + 1); } cout << "\n"; } cin >> n; }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int N = 200600; int n, S; int a[N]; int b[N]; int xs[N]; int k; vector<int> g[N]; int m; vector<int> cycles[N]; int p[N]; bool used[N]; void dfs(int v) { while (!g[v].empty()) { int id = g[v].back(); g[v].pop_back(); dfs(a[id]); cycles[m].push_back(id); } } int main() { scanf("%d%d", &n, &S); for (int i = 0; i < n; i++) { scanf("%d", &a[i]); b[i] = a[i]; } sort(b, b + n); for (int i = 0; i < n; i++) xs[k++] = b[i]; k = unique(xs, xs + k) - xs; for (int i = 0; i < n; i++) { a[i] = lower_bound(xs, xs + k, a[i]) - xs; b[i] = lower_bound(xs, xs + k, b[i]) - xs; } for (int i = 0; i < n; i++) { if (a[i] == b[i]) continue; g[b[i]].push_back(i); } for (int i = 0; i < k; i++) { dfs(i); if (!cycles[m].empty()) m++; } for (int i = 0; i < m; i++) S -= (int)cycles[i].size(); if (S < 0) { printf("-1\n"); return 0; } for (int i = 0; i < n; i++) p[i] = i; for (int id = 0; id < m; id++) { for (int i = 0; i < (int)cycles[id].size(); i++) { int v = cycles[id][i], u = cycles[id][(i + 1) % (int)cycles[id].size()]; p[u] = v; } } S = min(S, m); if (S > 1) { printf("%d\n", 2 + m - S); printf("%d\n", S); for (int i = 0; i < S; i++) printf("%d ", cycles[i][0] + 1); printf("\n"); for (int i = S - 1; i > 0; i--) swap(p[cycles[i][0]], p[cycles[i - 1][0]]); } else { printf("%d\n", m); } for (int i = 0; i < n; i++) { if (used[i]) continue; if (p[i] == i) { used[i] = 1; continue; } vector<int> cur; int x = i; while (!used[x]) { cur.push_back(x); used[x] = 1; x = p[x]; } printf("%d\n", (int)cur.size()); for (int z : cur) printf("%d ", z + 1); printf("\n"); } return 0; }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using std::lower_bound; using std::max; using std::min; using std::random_shuffle; using std::reverse; using std::sort; using std::swap; using std::unique; using std::upper_bound; using std::vector; void open(const char *s) {} int rd() { int s = 0, c, b = 0; while (((c = getchar()) < '0' || c > '9') && c != '-') ; if (c == '-') { c = getchar(); b = 1; } do { s = s * 10 + c - '0'; } while ((c = getchar()) >= '0' && c <= '9'); return b ? -s : s; } void put(int x) { if (!x) { putchar('0'); return; } static int c[20]; int t = 0; while (x) { c[++t] = x % 10; x /= 10; } while (t) putchar(c[t--] + '0'); } int upmin(int &a, int b) { if (b < a) { a = b; return 1; } return 0; } int upmax(int &a, int b) { if (b > a) { a = b; return 1; } return 0; } const int N = 200010; int n, m; int a[N], b[N], c[N]; int d[N]; vector<std::pair<int, int> > g[N]; int cnt; vector<int> s[N]; void dfs(int x) { while (!g[x].empty()) { auto v = g[x].back(); g[x].pop_back(); dfs(v.first); s[cnt].push_back(v.second); } } int main() { open("cf1012e"); scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) { scanf("%d", &a[i]); b[i] = a[i]; c[i] = a[i]; } sort(b + 1, b + n + 1); sort(c + 1, c + n + 1); int t = unique(c + 1, c + n + 1) - c - 1; for (int i = 1; i <= n; i++) { a[i] = lower_bound(c + 1, c + t + 1, a[i]) - c; b[i] = lower_bound(c + 1, c + t + 1, b[i]) - c; } for (int i = 1; i <= n; i++) if (a[i] != b[i]) g[a[i]].push_back(std::pair<int, int>(b[i], i)); for (int i = 1; i <= t; i++) { cnt++; dfs(i); if (s[cnt].empty()) cnt--; } int sum = 0; for (int i = 1; i <= cnt; i++) sum += s[i].size(); if (sum > m) { printf("-1\n"); return 0; } if (cnt == 0) { printf("0\n"); return 0; } if (cnt == 1) { printf("1\n%d\n", sum); for (auto v : s[1]) printf("%d ", v); printf("\n"); return 0; } for (int i = cnt; i >= 2; i--) if (i + sum <= m) { printf("%d\n", cnt - i + 2); int sum1 = 0; for (int j = 1; j <= i; j++) sum1 += s[j].size(); printf("%d\n", sum1); for (int j = 1; j <= i; j++) for (auto v : s[j]) printf("%d ", v); printf("\n"); printf("%d\n", i); for (int j = i; j >= 1; j--) printf("%d ", s[j].front()); for (int j = i + 1; j <= cnt; j++) { printf("%d\n", (int)s[j].size()); for (auto v : s[j]) printf("%d ", v); printf("\n"); } return 0; } printf("%d\n", cnt); for (int i = 1; i <= cnt; i++) { printf("%d\n", (int)s[i].size()); for (auto v : s[i]) printf("%d ", v); printf("\n"); } return 0; }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 10, mod = 1e9 + 7; const long long inf = 1e18; int a[maxn], b[maxn], nxt[maxn]; pair<int, int> srt[maxn]; map<int, vector<int> > mp; vector<vector<int> > ans; int par[maxn], who[maxn]; int fnd(int u) { return par[u] < 0 ? u : par[u] = fnd(par[u]); } void mrg(int a, int b) { if ((a = fnd(a)) == (b = fnd(b))) return; if (par[a] > par[b]) swap(a, b); par[a] += par[b]; par[b] = a; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); memset(par, -1, sizeof par); int n, s; cin >> n >> s; for (int i = 0; i < n; i++) { cin >> a[i]; srt[i] = {a[i], i}; } sort(srt, srt + n); int N = 0; for (int i = 0; i < n; i++) { if (srt[i].first != a[i]) { who[N] = i; b[N++] = a[i]; } } memcpy(a, b, sizeof a), n = N; for (int i = 0; i < n; i++) { srt[i] = {a[i], i}; } sort(srt, srt + n); for (int i = 0; i < n; i++) { nxt[i] = i; } for (int i = 0; i < n; i++) { if (srt[i].first != a[i]) { nxt[srt[i].second] = i; mp[srt[i].first].push_back(srt[i].second); } } for (int i = 0; i < n; i++) { int tmp = nxt[i]; s -= fnd(i) != fnd(tmp); while (fnd(tmp) != fnd(i)) { mrg(tmp, i); tmp = nxt[tmp]; s--; } } for (auto &IT : mp) { for (int i = 0; i < int((IT.second).size()) - 1; i++) { int A = IT.second[i], B = IT.second[i + 1]; if (fnd(A) != fnd(B)) swap(nxt[A], nxt[B]), mrg(A, B); } } if (s < 0) return cout << -1 << endl, 0; set<int> st; vector<int> vv; for (int i = 0; i < n; i++) { if (st.count(fnd(i))) continue; if (s == 0) continue; st.insert(fnd(i)); vv.push_back(i); s--; } if (int((vv).size()) > 2) { for (int i = 1; i < int((vv).size()); i++) { swap(nxt[vv[0]], nxt[vv[i]]); } ans.push_back(vv); } st.clear(); for (int i = 0; i < n; i++) { if (nxt[i] == i) continue; if (st.count(i)) continue; vv.clear(); int tmp = i; while (st.count(tmp) == 0) { st.insert(tmp); vv.push_back(tmp); tmp = nxt[tmp]; } ans.push_back(vv); } cout << int((ans).size()) << "\n"; for (int i = 0; i < int((ans).size()); i++) { cout << int((ans[i]).size()) << "\n"; for (int x : ans[i]) cout << who[x] + 1 << " "; cout << "\n"; } return 0; }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; namespace FGF { int n, m; const int N = 2e5 + 5; int a[N], b[N], p[N], vis[N], cnt, head[N], fa[N], ins[N], ok[N]; vector<int> V, tmp; struct edg { int to, nxt, w; } e[N]; void add(int u, int v, int w) { cnt++; e[cnt].to = v; e[cnt].nxt = head[u]; head[u] = cnt; e[cnt].w = w; } int find(int x) { return fa[x] == x ? x : fa[x] = find(fa[x]); } void merge(int u, int v) { if (find(u) != find(v)) fa[find(v)] = u; } vector<vector<int> > ans; void dfs(int u) { ins[u] = 1; for (int w, &i = head[u]; i; i = e[i].nxt) if (!vis[i]) vis[i] = 1, w = e[i].w, dfs(e[i].to), tmp.push_back(w); } void work() { scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) scanf("%d", &a[i]), b[i] = a[i]; sort(b + 1, b + n + 1); int tot = unique(b + 1, b + n + 1) - b - 1; for (int i = 1; i <= n; i++) a[i] = lower_bound(b + 1, b + tot + 1, a[i]) - b; for (int i = 1; i <= n; i++) p[i] = a[i], fa[i] = i; sort(p + 1, p + n + 1); for (int i = 1; i <= n; i++) if (a[i] != p[i]) cnt++, merge(a[i], p[i]); if (cnt > m) { puts("-1"); return; } for (int i = 1; i <= n; i++) if (find(a[i]) == a[i] && a[i] != p[i] && !ok[a[i]]) V.push_back(i), ok[a[i]] = 1; if (m - cnt > 1 && V.size() > 1) { tmp.push_back(V[0]); for (int i = 1; i < min((int)V.size(), m - cnt); i++) swap(a[V[i]], a[V[i - 1]]), tmp.push_back(V[i]); reverse(tmp.begin(), tmp.end()); ans.push_back(tmp); tmp.clear(); } cnt = 0; for (int i = 1; i <= n; i++) if (a[i] != p[i]) add(a[i], p[i], i); for (int i = 1; i <= tot; i++) if (!ins[i] && head[i]) tmp.clear(), dfs(i), ans.push_back(tmp); printf("%d\n", (int)ans.size()); for (auto g : ans) { printf("%d\n", (int)g.size()); for (auto x : g) printf("%d ", x); puts(""); } } } // namespace FGF int main() { FGF::work(); return 0; }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; vector<pair<int, int> > adj[200000]; int nxt[200000]; bool v[200000]; vector<int> cycle; void dfs(int now) { v[now] = true; while (nxt[now] < adj[now].size()) { nxt[now]++; dfs(adj[now][nxt[now] - 1].first); } cycle.push_back(now); } void dfs2(int now) { v[now] = true; for (int i = 0; i < adj[now].size(); i++) { int to = adj[now][i].first; if (!v[to]) { dfs2(to); } } } vector<int> rev(vector<int> a) { vector<int> b; for (int i = a.size() - 1; i >= 0; i--) { b.push_back(a[i]); } return b; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); int n, s; cin >> n >> s; vector<int> li; vector<int> all; for (int i = 0; i < n; i++) { nxt[i] = 0; v[i] = false; int x; cin >> x; li.push_back(x); all.push_back(x); } sort(all.begin(), all.end()); map<int, int> m; int zone[n]; int point = 0; for (int i = 0; i < n; i++) { if (i > 0 && all[i] == all[i - 1]) { continue; } for (int j = i; j < n && all[i] == all[j]; j++) { zone[j] = point; } m[all[i]] = point; point++; } for (int i = 0; i < n; i++) { int to = m[li[i]]; if (to == zone[i]) { continue; } adj[zone[i]].push_back(make_pair(to, i + 1)); } vector<vector<int> > ids; map<pair<int, int>, int> m2; int point2 = 0; for (int i = 0; i < point; i++) { for (int j = 0; j < adj[i].size(); j++) { int ind = -1; if (m2.find(make_pair(i, adj[i][j].first)) != m2.end()) { ind = m2[make_pair(i, adj[i][j].first)]; } else { m2[make_pair(i, adj[i][j].first)] = point2; ind = point2++; vector<int> x; ids.push_back(x); } ids[ind].push_back(adj[i][j].second); } } int comp = 0; for (int i = 0; i < point; i++) { if (!v[i] && adj[i].size() > 0) { dfs2(i); comp++; } } for (int i = 0; i < point; i++) { v[i] = false; } vector<vector<int> > ans; int tot = 0; for (int i = 0; i < point; i++) { if (v[i] || adj[i].size() == 0) { continue; } cycle.clear(); dfs(i); cycle.pop_back(); cycle = rev(cycle); tot += (int)cycle.size(); vector<int> c2; for (int j = 0; j < cycle.size(); j++) { int nxt = (j + 1) % cycle.size(); int ind = m2[make_pair(cycle[j], cycle[nxt])]; c2.push_back(ids[ind].back()); ids[ind].pop_back(); } ans.push_back(c2); } for (int i = 0; i < point; i++) { assert(nxt[i] == adj[i].size()); } if (tot > s) { cout << -1 << endl; return 0; } int should = ans.size(); int red = (s - tot) - 2; int comb = 0; if (red > 0) { comb = min(red + 2, comp); } if (comb > 1) { vector<vector<int> > lis; int sz = ans.size(); for (int i = 1; i <= comb; i++) { lis.push_back(ans.back()); ans.pop_back(); } vector<int> l1; vector<int> l2; for (int i = lis.size() - 1; i >= 0; i--) { for (int j = 0; j < lis[i].size(); j++) { l1.push_back(lis[i][j]); } } for (int i = 0; i < lis.size(); i++) { l2.push_back(lis[i][0]); } ans.push_back(l1); ans.push_back(l2); } cout << ans.size() << endl; for (int i = 0; i < ans.size(); i++) { cout << ans[i].size() << endl; cout << ans[i][0]; for (int j = 1; j < ans[i].size(); j++) { cout << " " << ans[i][j]; } cout << endl; } }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int MAXN = 202020; int N, S, val[MAXN], sorted[MAXN], begin_idx[MAXN], incl[MAXN], c = 0; map<int, int> compress; vector<int> adj[MAXN], cycle; map<pair<int, int>, vector<int>> locs; void recur(int n) { ; while (adj[n].size() > 0) { int nex = adj[n].back(); adj[n].pop_back(); recur(nex); }; cycle.push_back(n); } vector<int> reconstruct(vector<int>& cycle) { ; vector<int> result; for (int i = 0; i < cycle.size(); ++i) { int nex = (i + 1) % cycle.size(); pair<int, int> key = {cycle[i], cycle[nex]}; vector<int>& opts = locs[key]; ; assert(opts.size() > 0); result.push_back(opts.back()); opts.pop_back(); } return result; } int main() { scanf("%d %d", &N, &S); for (int i = 1; i <= N; ++i) { scanf("%d", &val[i]); } for (int i = 1; i <= N; ++i) { sorted[i] = val[i]; } sort(sorted + 1, sorted + N + 1); for (int i = 1; i <= N; ++i) { if (compress.find(sorted[i]) == compress.end()) { compress[sorted[i]] = c++; } } for (int i = 1; i <= N; ++i) { val[i] = compress[val[i]]; sorted[i] = val[i]; } int nswaps = 0; sort(sorted + 1, sorted + N + 1); for (int i = 1; i <= N; ++i) { if (val[i] != sorted[i]) { adj[sorted[i]].push_back(val[i]); nswaps++; } else { incl[i] = 1; } } if (nswaps > S) { printf("-1\n"); return 0; } for (int i = 1; i <= N; ++i) { if (begin_idx[sorted[i]] == 0) { begin_idx[sorted[i]] = i; } } for (int i = 1; i <= N; ++i) { ; if (locs.find({sorted[i], val[i]}) == locs.end()) { locs[{sorted[i], val[i]}] = vector<int>(1, i); } else { locs[{sorted[i], val[i]}].push_back(i); } }; vector<vector<int>> cycles; for (int i = 0; i < c; ++i) { cycle.clear(); ; recur(i); reverse(cycle.begin(), cycle.end()); cycle.pop_back(); if (cycle.size() > 0) { cycle = reconstruct(cycle); cycles.push_back(cycle); } }; int to_merge = min(S - nswaps, (int)cycles.size()); if (to_merge > 1) { vector<int> cycle1, cycle2; for (int i = 0; i < to_merge; ++i) { for (int j = 0; j < cycles.back().size(); ++j) { cycle1.push_back(cycles.back()[j]); } cycle2.push_back(cycles.back()[0]); cycles.pop_back(); } reverse(cycle2.begin(), cycle2.end()); cycles.push_back(cycle1); cycles.push_back(cycle2); }; printf("%d\n", (int)cycles.size()); for (int i = 0; i < cycles.size(); ++i) { printf("%d\n", (int)cycles[i].size()); for (int j = 0; j < cycles[i].size(); ++j) { printf("%d", cycles[i][j]); if (j == cycles[i].size() - 1) printf("\n"); else printf(" "); } } return 0; }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int M = 2e5 + 239; int n, s, a[M], p[M]; pair<int, int> b[M]; int parent[M], r[M]; inline void init() { for (int i = 0; i < n; i++) { parent[i] = i; r[i] = 0; } } inline int find_set(int p) { if (parent[p] == p) return p; return (parent[p] = find_set(parent[p])); } inline void merge_set(int i, int j) { i = find_set(i); j = find_set(j); if (i == j) return; if (r[i] > r[j]) swap(i, j); if (r[i] == r[j]) r[j]++; parent[i] = j; } inline bool is_connect(int i, int j) { return (find_set(i) == find_set(j)); } int t; bool used[M]; vector<int> c[M]; void dfs(int x) { used[x] = true; c[t].push_back(x); if (!used[p[x]]) dfs(p[x]); } int main() { ios::sync_with_stdio(0); cin >> n >> s; for (int i = 0; i < n; i++) { cin >> a[i]; b[i] = make_pair(a[i], i); } sort(b, b + n); for (int i = 0; i < n; i++) p[b[i].second] = i; for (int i = 0; i < n; i++) if (a[i] == b[i].first && p[i] != i) { p[b[i].second] = p[i]; b[p[i]].second = b[i].second; p[i] = i; b[i].second = i; } init(); for (int i = 0; i < n; i++) merge_set(p[i], i); int ls = -1; for (int i = 0; i < n; i++) { if (p[b[i].second] == b[i].second) continue; if (ls >= 0 && a[ls] == a[b[i].second]) { int x = ls; int y = b[i].second; if (is_connect(x, y)) continue; merge_set(x, y); swap(p[x], p[y]); } ls = b[i].second; } t = 0; for (int i = 0; i < n; i++) if (!used[i] && p[i] != i) { dfs(i); t++; } int kol = 0; for (int i = 0; i < t; i++) kol += (int)c[i].size(); if (kol > s) { cout << "-1"; return 0; } s -= kol; s = min(s, t); if (s <= 1) { cout << t << "\n"; for (int i = 0; i < t; i++) { cout << c[i].size() << "\n"; for (int x : c[i]) cout << (x + 1) << " "; cout << "\n"; } return 0; } cout << (t - s + 2) << "\n"; for (int i = 0; i < t - s; i++) { cout << c[i + s].size() << "\n"; for (int x : c[i + s]) cout << (x + 1) << " "; cout << "\n"; kol -= (int)c[i + s].size(); } cout << kol << "\n"; for (int i = 0; i < s; i++) for (int x : c[i]) cout << (x + 1) << " "; cout << "\n"; cout << s << "\n"; for (int i = s - 1; i >= 0; i--) cout << (c[i][0] + 1) << " "; cout << "\n"; return 0; }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int N = 1000005; int n, a[N], K, sum; map<int, int> mp; map<int, vector<int> > g; vector<vector<int> > ans; vector<int> vec; inline void dfs(const int x) { register int i; while (!g[x].empty()) i = g[x].back(), g[x].pop_back(), dfs(a[i]), vec.push_back(i); g.erase(x); } int main() { scanf("%d%d", &n, &K); register int i; for (i = 1; i <= n; ++i) scanf("%d", &a[i]), ++mp[a[i]]; for (const auto &x : mp) for (i = sum + 1, sum += x.second; i <= sum; ++i) if (a[i] ^ x.first) g[x.first].push_back(i); while (!g.empty()) vec.clear(), dfs((*g.begin()).first), std::reverse(vec.begin(), vec.end()), ans.push_back(vec), K -= vec.size(); if (K < 0) return puts("-1"), 0; ans.size() < K ? K = ans.size() : 0, printf("%d\n", ans.size() - (K > 2 ? K - 2 : 0)); if (K < 3) { for (const auto &o : ans) { printf("%d\n", o.size()); for (const int &i : o) printf("%d ", i); puts(""); } return 0; } register int len = 0; for (i = 0; i < K; ++i) len += ans[i].size(); printf("%d\n", len); for (i = 0; i < K; ++i) for (const int &j : ans[i]) printf("%d ", j); printf("\n%d\n", K); for (i = K - 1; ~i; --i) printf("%d ", ans[i][0]); puts(""); for (i = K; i < ans.size(); ++i) { printf("%d\n", ans[i].size()); for (const int &j : ans[i]) printf("%d ", j); puts(""); } return 0; }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using cat = long long; void DFS(int R, vector<vector<int> >& G, vector<bool>& vis, vector<int>& cyc) { vis[R] = true; while (!G[R].empty()) { int v = G[R].back(); G[R].pop_back(); DFS(v, G, vis, cyc); } cyc.push_back(R); } int main() { cin.sync_with_stdio(0); cin.tie(0); cout << fixed << setprecision(10); int N, S; cin >> N >> S; vector<int> A(N); for (int i = 0; i < N; i++) cin >> A[i]; vector<int> As(A); sort(begin(As), end(As)); int wrong = 0; for (int i = 0; i < N; i++) if (A[i] != As[i]) wrong++; if (S < wrong) { cout << "-1\n"; return 0; } map<int, int> M; for (int i = 0; i < N; i++) M[A[i]] = 0; int m = 0; for (auto it = M.begin(); it != M.end(); it++) it->second = m++; for (int i = 0; i < N; i++) A[i] = M[A[i]], As[i] = M[As[i]]; vector<vector<int> > G(N + m); for (int i = 0; i < N; i++) if (A[i] != As[i]) G[i].push_back(A[i] + N); for (int i = 0; i < N; i++) if (A[i] != As[i]) G[As[i] + N].push_back(i); vector<bool> vis(N + m, false); vector<vector<int> > euc; for (int i = 0; i < N; i++) if (!vis[i] && A[i] != As[i]) { vector<int> c; DFS(i, G, vis, c); euc.push_back(vector<int>()); reverse(begin(c), end(c)); c.pop_back(); for (auto it = c.begin(); it != c.end(); it++) if (*it < N) euc.back().push_back(*it); } int num_merged = min((int)euc.size(), S - wrong); if (num_merged <= 2) num_merged = 0; vector<vector<int> > ans; for (int i = num_merged; i < (int)euc.size(); i++) ans.push_back(euc[i]); if (num_merged > 0) { vector<int> mergedc; for (int i = 0; i < num_merged; i++) for (auto it = euc[i].begin(); it != euc[i].end(); it++) mergedc.push_back(*it); ans.push_back(mergedc); vector<int> failc; for (int i = 0; i < num_merged; i++) failc.push_back(euc[i][0]); reverse(begin(failc), end(failc)); ans.push_back(failc); } cout << ans.size() << "\n"; for (int i = 0; i < (int)ans.size(); i++) { cout << ans[i].size(); for (auto it = ans[i].begin(); it != ans[i].end(); it++) cout << " " << *it + 1; cout << "\n"; } return 0; }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; vector<int> pat[500000]; vector<int> rev[500000]; vector<bool> flag[500000]; int pt[500000]; void adde(int a, int b) { pat[a].push_back(b); flag[a].push_back(false); } vector<int> euler; void calceuler(int node) { for (;;) { if (pt[node] == pat[node].size()) break; if (!flag[node][pt[node]]) { flag[node][pt[node]] = true; pt[node]++; calceuler(pat[node][pt[node] - 1]); } else pt[node]++; } euler.push_back(node); } class unionfind { public: int par[500000]; int ran[500000]; int ren[500000]; void init() { for (int i = 0; i < 500000; i++) { par[i] = i; ran[i] = 0; ren[i] = 1; } } int find(int a) { if (a == par[a]) return a; else return par[a] = find(par[a]); } void unite(int a, int b) { a = find(a); b = find(b); if (a == b) return; if (ran[a] > ran[b]) { par[b] = a; ren[a] += ren[b]; } else { par[a] = b; ren[b] += ren[a]; } if (ran[a] == ran[b]) ran[b]++; } }; unionfind uf; int dat[204020]; int cnt[204020]; int kou[504020]; bool isok[204020]; int zat[202020]; int curr[202020]; void check(vector<vector<int> > v, int num) { for (int p = 0; p < v.size(); p++) { vector<int> zi = v[p]; int t = curr[zi[zi.size() - 1]]; for (int i = 0; i < zi.size(); i++) { int s = curr[zi[i]]; curr[zi[i]] = t; t = s; } } for (int i = 0; i < num - 1; i++) if (curr[i] > curr[i + 1]) abort(); } int main() { int num, gen; scanf("%d%d", &num, &gen); for (int i = 0; i < num; i++) scanf("%d", &dat[i]), zat[i] = curr[i] = dat[i]; sort(zat, zat + num); for (int i = 0; i < num; i++) dat[i] = 1 + lower_bound(zat, zat + num, dat[i]) - zat; for (int i = 0; i < num; i++) cnt[dat[i]]++; for (int j = 1; j < 202020; j++) cnt[j] += cnt[j - 1]; uf.init(); int opes = 0; for (int i = 0; i < num; i++) isok[i] = (cnt[dat[i] - 1] <= i && i < cnt[dat[i]]); for (int i = 0; i < num; i++) if (!isok[i]) uf.unite(i, num + dat[i]), opes++; for (int i = 1; i < 202020; i++) for (int j = cnt[i - 1]; j < cnt[i]; j++) if (!isok[j]) uf.unite(j, num + i); fill(kou, kou + 502020, -1); for (int i = 0; i < num; i++) if (!isok[i]) kou[uf.find(i)] = i; int rr = 0; for (int i = 0; i < 502020; i++) if (kou[i] != -1) rr++; vector<vector<int> > ret; if (opes > gen) { printf("-1\n"); return 0; } vector<int> zi; for (int i = 0; i < 502020; i++) if (kou[i] != -1 && zi.size() < gen - opes) zi.push_back(kou[i]); if (zi.size() >= 3) { int t = dat[zi[zi.size() - 1]]; for (int i = 0; i < zi.size(); i++) { int s = dat[zi[i]]; dat[zi[i]] = t; t = s; } ret.push_back(zi); } uf.init(); for (int i = 0; i < num; i++) if (!isok[i]) uf.unite(i, num + dat[i]); for (int i = 1; i < 202020; i++) for (int j = cnt[i - 1]; j < cnt[i]; j++) if (!isok[j]) uf.unite(j, num + i); fill(kou, kou + 502020, -1); for (int i = 0; i < num; i++) if (!isok[i]) kou[uf.find(i)] = i; for (int i = 0; i < num; i++) if (!isok[i]) adde(i, num + dat[i]); for (int i = 1; i < 202020; i++) for (int j = cnt[i - 1]; j < cnt[i]; j++) if (!isok[j]) adde(num + i, j); for (int i = 0; i < 502020; i++) { if (kou[i] == -1) continue; euler.clear(); calceuler(i); reverse(euler.begin(), euler.end()); vector<int> z; for (int j = 0; j < euler.size() - 1; j++) if (euler[j] < num) z.push_back(euler[j]); ret.push_back(z); } int sum = 0; for (int i = 0; i < ret.size(); i++) sum += ret[i].size(); printf("%d\n", ret.size()); for (int i = 0; i < ret.size(); i++) { printf("%d\n", ret[i].size()); for (int j = 0; j < ret[i].size(); j++) printf("%d ", ret[i][j] + 1); printf("\n"); } check(ret, num); }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int MN = 200010; int fa[MN]; void init() { for (int i = 0; i < MN; i++) fa[i] = i; } int find(int u) { if (fa[u] == u) return u; else return fa[u] = find(fa[u]); } void mrg(int u, int v) { u = find(u); v = find(v); if (u == v) return; fa[v] = u; } int N, S; int A[MN], B[MN], P[MN], vis[MN]; int Xn; vector<int> X; unordered_map<int, int> dx; vector<int> Z[MN]; set<int> V[MN]; vector<vector<int> > sol; int main() { scanf("%d %d", &N, &S); for (int i = 0; i < N; i++) { scanf("%d", &A[i]); X.push_back(A[i]); } sort(X.begin(), X.end()); X.resize(unique(X.begin(), X.end()) - X.begin()); Xn = X.size(); for (int i = 0; i < Xn; i++) dx[X[i]] = i; for (int i = 0; i < N; i++) A[i] = dx[A[i]]; for (int i = 0; i < N; i++) { B[i] = A[i]; V[A[i]].insert(i); } sort(B, B + N); int cnt = 0; for (int i = 0; i < N; i++) { if (A[i] == B[i]) { P[i] = i; V[A[i]].erase(i); } else { cnt++; Z[A[i]].push_back(i); } } if (cnt > S) { printf("-1"); return 0; } for (int i = 0; i < N; i++) { if (A[i] != B[i]) { P[*V[B[i]].begin()] = i; V[B[i]].erase(V[B[i]].begin()); } } init(); for (int i = 0; i < N; i++) { mrg(i, P[i]); } for (int i = 0; i < Xn; i++) { for (int j = 0; j < (int)Z[i].size() - 1; j++) { int u = Z[i][j]; int v = Z[i][j + 1]; if (find(u) != find(v)) { swap(P[u], P[v]); mrg(u, v); } } } vector<int> tmp, pre; for (int i = 0; i < N; i++) if (P[i] != i && fa[i] == i) { tmp.push_back(i); } int n = min(S - cnt, (int)tmp.size()); if (n > 1) { for (int i = 0; i < n; i++) { pre.push_back(P[tmp[i]]); } sol.push_back(vector<int>()); for (int i = 0; i < n; i++) { sol.back().push_back(tmp[i]); P[tmp[i]] = pre[(i + n - 1) % n]; } } for (int i = 0; i < N; i++) { if (vis[i]) continue; if (P[i] == i) continue; sol.push_back(vector<int>()); int u = i; while (!vis[u]) { vis[u] = 1; sol.back().push_back(u); u = P[u]; } } printf("%d\n", sol.size()); for (int i = 0; i < sol.size(); i++) { printf("%d\n", sol[i].size()); for (int j = 0; j < sol[i].size(); j++) { printf("%d ", sol[i][j] + 1); } printf("\n"); } }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int a[200000], b[200000]; vector<int> pos[200000]; int p[200000], nxt[200000]; int parent[200000]; int find(int n) { if (parent[n] != n) parent[n] = find(parent[n]); return parent[n]; } int visited[200000]; vector<int> cycles[200000]; int main() { int i; int n, s; scanf("%d %d", &n, &s); for (i = 0; i < n; i++) scanf("%d", &a[i]), b[i] = a[i]; sort(b, b + n); for (i = 0; i < n; i++) a[i] = lower_bound(b, b + n, a[i]) - b; for (i = 0; i < n; i++) b[i] = a[i]; sort(b, b + n); int j, c = 0; for (i = 0; i < n; i++) { if (a[i] != b[i]) pos[b[i]].push_back(i), c++; parent[i] = i, p[i] = -1; } if (c > s) { printf("-1\n"); return 0; } for (i = 0; i < n; i++) { if (a[i] == b[i]) nxt[i] = i; else nxt[i] = pos[a[i]].back(), pos[a[i]].pop_back(); int pa = find(i), push_back = find(nxt[i]); if (pa != push_back) parent[pa] = push_back; } for (i = 0; i < n; i++) { if (a[i] == b[i]) continue; if (p[a[i]] != -1) { int pa = find(p[a[i]]), push_back = find(i); if (pa != push_back) { int t = nxt[p[a[i]]]; nxt[p[a[i]]] = nxt[i]; nxt[i] = t; parent[pa] = push_back; } } p[a[i]] = i; } int cc = 0; for (i = 0; i < n; i++) { if (!visited[i] && (i != nxt[i])) { int u = i; do cycles[cc].push_back(u), visited[u] = 1, u = nxt[u]; while (u != i); cc++; } } int x = min(s - c, cc); printf("%d\n", cc - x + min(x, 2)); int sum = 0; if (x > 0) { for (i = 0; i < x; i++) sum += cycles[i].size(); printf("%d\n", sum); for (i = 0; i < x; i++) { for (j = 0; j < cycles[i].size(); j++) printf("%d ", cycles[i][j] + 1); } printf("\n"); if (x > 1) { printf("%d\n", x); for (i = x - 1; i >= 0; i--) printf("%d ", cycles[i][0] + 1); printf("\n"); } } for (i = x; i < cc; i++) { printf("%d\n", cycles[i].size()); for (j = 0; j < cycles[i].size(); j++) printf("%d ", cycles[i][j] + 1); printf("\n"); } return 0; }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int n, K; map<int, int> Map; int A[201000], B[201000], w[201000]; vector<int> GG[201000]; int GPV[201000]; int tA[201000]; vector<int> G; int UF[201000], sss; int Find(int a) { if (a == UF[a]) return a; return UF[a] = Find(UF[a]); } void Merge(int a, int b) { a = Find(a), b = Find(b); if (a != b) UF[a] = b; } int v[201000]; vector<vector<int> > RR; void Pro(vector<int> &T) { RR.push_back(T); for (int i = T.size() - 1; i >= 0; i--) { int ni = i - 1; if (ni < 0) ni = T.size() - 1; tA[T[ni]] = A[T[i]]; } for (auto &t : T) A[t] = tA[t]; } void Go(int a) { vector<int> T; int t = a; while (1) { v[t] = 1; T.push_back(t); t = w[t]; if (t == a) break; } Pro(T); return; } bool Do(int ck) { int i, c = 0; for (i = 1; i <= n; i++) { B[i] = A[i]; GG[i].clear(); UF[i] = i; GPV[i] = 0; v[i] = 0; } sort(B + 1, B + n + 1); Map.clear(); for (i = 1; i <= n; i++) { if (A[i] != B[i]) c++; } if (c > K) { puts("-1"); return false; } for (i = 1; i <= n; i++) { if (A[i] != B[i]) { if (!Map.count(A[i])) { Map[A[i]] = i; GG[i].push_back(i); } else { GG[Map[A[i]]].push_back(i); } } } for (i = 1; i <= n; i++) { if (A[i] != B[i]) { G.push_back(i); int x = Map[B[i]]; w[i] = GG[x][GPV[x]]; GPV[x]++; } } for (auto &x : G) { Merge(x, w[x]); } for (i = 0; i + 1 < G.size(); i++) { int x = G[i], y = G[i + 1]; if (B[x] == B[y] && Find(x) != Find(y)) { swap(w[x], w[y]); Merge(x, y); } } int res = 0; vector<int> TP; for (auto &x : G) { if (!v[Find(x)]) { TP.push_back(x); res++; v[Find(x)] = 1; } } if (!ck) { int cur = G.size(); int rr = min(res, max(K - cur, 0)); if (rr >= 3) { res = res - rr + 2; vector<int> TP2; for (int i = 0; i < rr; i++) { TP2.push_back(TP[i]); } Pro(TP2); return Do(1); } } for (i = 1; i <= n; i++) v[i] = 0; for (auto &x : G) { if (!v[x]) { Go(x); } } return true; } int main() { int i; scanf("%d%d", &n, &K); for (i = 1; i <= n; i++) { scanf("%d", &A[i]); } if (Do(0)) { printf("%d\n", RR.size()); for (auto &t : RR) { printf("%d\n", t.size()); for (int i = t.size() - 1; i >= 0; i--) printf("%d ", t[i]); puts(""); } } }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; template <typename T> void maxtt(T& t1, T t2) { t1 = max(t1, t2); } template <typename T> void mintt(T& t1, T t2) { t1 = min(t1, t2); } bool debug = 0; int n, m, k; int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0}; string direc = "RDLU"; long long ln, lk, lm; void etp(bool f = 0) { puts(f ? "YES" : "NO"); exit(0); } void addmod(int& x, int y, int mod = 998244353) { assert(y >= 0); x += y; if (x >= mod) x -= mod; assert(x >= 0 && x < mod); } void et(int x = -1) { printf("%d\n", x); exit(0); } long long fastPow(long long x, long long y, int mod = 998244353) { long long ans = 1; while (y > 0) { if (y & 1) ans = (x * ans) % mod; x = x * x % mod; y >>= 1; } return ans; } long long gcd1(long long x, long long y) { return y ? gcd1(y, x % y) : x; } int a[200105], p[200105], f[200105], now; pair<int, int> b[200105]; bool vis[200105]; vector<int> vp[200105]; int ff(int x) { return x == f[x] ? x : f[x] = ff(f[x]); } void lnk(int x, int y) { int fx = ff(x), fy = ff(y); if (fx == fy) return; f[fx] = fy; } void dfs(int x) { vis[x] = 1; vp[now].push_back(x); if (!vis[p[x]]) dfs(p[x]); } void fmain(int ID) { int s; scanf("%d%d", &n, &s); for (int(i) = 1; (i) <= (int)(n); (i)++) { scanf("%d", a + i); b[i] = {a[i], i}; } sort(b + 1, b + 1 + n); for (int(i) = 1; (i) <= (int)(n); (i)++) p[b[i].second] = i; for (int(i) = 1; (i) <= (int)(n); (i)++) if (a[i] == b[i].first && p[i] != i) { int j = p[i]; p[b[i].second] = j; swap(b[i], b[j]); p[i] = i; assert(b[i].second == i); } for (int(i) = 1; (i) <= (int)(n); (i)++) f[i] = i; for (int(i) = 1; (i) <= (int)(n); (i)++) if (p[i] != i) lnk(p[i], i); int lst = 0; for (int(i) = 1; (i) <= (int)(n); (i)++) { if (b[i].second == p[b[i].second]) continue; if (1 <= lst && a[lst] == a[b[i].second]) { int fx = ff(lst), fy = ff(b[i].second); if (fx == fy) continue; f[fx] = fy; swap(p[lst], p[b[i].second]); } lst = b[i].second; } now = 0; for (int(i) = 1; (i) <= (int)(n); (i)++) if (!vis[i] && p[i] != i) { now++; dfs(i); } int sum = 0; for (int(i) = 1; (i) <= (int)(now); (i)++) sum += vp[i].size(); if (s < sum) et(); s -= sum; mintt(s, now); if (s <= 1) { printf("%d\n", now); for (int(i) = 1; (i) <= (int)(now); (i)++) { printf("%d\n", (int)vp[i].size()); for (int c : vp[i]) printf("%d ", c); puts(""); } return; } printf("%d\n", now - s + 2); for (int(i) = 1; (i) <= (int)(now - s); (i)++) { printf("%d\n", (int)vp[i + s].size()); for (int c : vp[i + s]) printf("%d ", c); puts(""); sum -= vp[i + s].size(); } printf("%d\n", sum); for (int(i) = 1; (i) <= (int)(s); (i)++) for (int c : vp[i]) printf("%d ", c); puts(""); printf("%d\n", s); for (int i = s; i; i--) printf("%d ", vp[i][0]); puts(""); } int main() { int t = 1; for (int(i) = 1; (i) <= (int)(t); (i)++) { fmain(i); } return 0; }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; struct UF { int n; vector<int> par; UF(int n) : n(n) { for (int i = 0; i < n; i++) par.push_back(i); } int find(int a) { if (a != par[a]) par[a] = find(par[a]); return par[a]; } void join(int a, int b) { par[find(a)] = find(b); } }; struct V { vector<pair<int, int> > outs; int nins = 0; }; vector<int> euler_walk(vector<V>& nodes, int nedges, int src = 0) { int c = 0; for (auto& n : nodes) c += abs(n.nins - (int)(n.outs).size()); if (c > 2) return {}; vector<vector<pair<int, int> >::iterator> its; for (auto& n : nodes) its.push_back(n.outs.begin()); vector<bool> eu(nedges); vector<int> ret, s = {src}; while (!s.empty()) { int x = s.back(); auto &it = its[x], end = nodes[x].outs.end(); while (it != end && eu[it->second]) ++it; if (it == end) { ret.push_back(x); s.pop_back(); } else { s.push_back(it->first); eu[it->second] = true; } } if ((int)(ret).size() != nedges + 1) ret.clear(); reverse(ret.begin(), ret.end()); return ret; } int main() { cin.sync_with_stdio(0); cin.tie(0); long long n, s; cin >> n >> s; vector<int> a(n); for (int i = 0; i < n; i++) { cin >> a[i]; } vector<int> sa = a; sort(sa.begin(), sa.end()); int g = 0; for (int i = 0; i < n; i++) { if (a[i] != sa[i]) { g++; } } map<int, int> cc; for (int i = 0; i < n; i++) { if (cc.find(a[i]) == cc.end()) { int t = cc.size(); cc[a[i]] = t; } } for (int i = 0; i < n; i++) { a[i] = cc[a[i]]; sa[i] = cc[sa[i]]; } vector<vector<pair<int, int> > > edges(cc.size() + 1); UF uf(cc.size()); vector<int> notstupid(cc.size(), 0); int edgetotal = 0; for (int i = 0; i < n; i++) { if (a[i] != sa[i]) { uf.join(a[i], sa[i]); edges[a[i]].push_back({sa[i], edgetotal++}); notstupid[a[i]] = notstupid[sa[i]] = 1; } } for (int i = 0; i < cc.size(); i++) { if (uf.find(i) == i && notstupid[i]) { edges[cc.size()].push_back({i, edgetotal++}); edges[i].push_back({cc.size(), edgetotal++}); } } vector<V> nodes(cc.size() + 1); for (int i = 0; i < edges.size(); i++) { nodes[i].outs = edges[i]; for (auto x : edges[i]) { nodes[x.first].nins++; } } vector<int> cycle = euler_walk(nodes, edgetotal, cc.size()); vector<vector<int> > cycles; vector<int> cur; for (int x : cycle) { if (x == cc.size()) { if (cur.size() > 0) { cycles.push_back(cur); cur.clear(); } } else { cur.push_back(x); } } map<pair<int, int>, vector<int> > q; map<pair<int, int>, int> curnum; for (int i = 0; i < n; i++) { if (a[i] != sa[i]) { q[{a[i], sa[i]}].push_back(i); curnum[{a[i], sa[i]}] = 0; } } vector<vector<int> > edge_idx; for (vector<int> z : cycles) { vector<int> realcycle; for (int i = 0; i + 1 < z.size(); i++) { pair<int, int> f = {z[i], z[i + 1]}; realcycle.push_back(q[f][curnum[f]] + 1); curnum[f]++; } reverse(realcycle.begin(), realcycle.end()); edge_idx.push_back(realcycle); } if (g > s) { cout << -1 << '\n'; return 0; } if (s <= g + 2 || edge_idx.size() <= 2) { } else { int v = s - g; v = min(v, (int)edge_idx.size()); vector<vector<int> > newe; vector<int> c1; vector<int> c2; for (int j = 0; j < v; j++) { for (int x : edge_idx[j]) { c1.push_back(x); } c2.push_back(edge_idx[j][0]); } reverse(c2.begin(), c2.end()); newe.push_back(c1); newe.push_back(c2); for (int j = v; j < edge_idx.size(); j++) { newe.push_back(edge_idx[j]); } edge_idx = newe; } cout << edge_idx.size() << '\n'; for (int i = 0; i < edge_idx.size(); i++) { vector<int> realcycle = edge_idx[i]; cout << realcycle.size() << '\n'; for (int x : realcycle) { cout << x << " "; } cout << '\n'; } }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> const int MAX_N = 200000; int v[1 + MAX_N], poz[1 + MAX_N], sorted[1 + MAX_N]; bool cmp(int a, int b) { return v[a] < v[b]; } std::vector<int> src[1 + MAX_N], target[1 + MAX_N]; void normalize(int n) { std::sort(poz + 1, poz + 1 + n, cmp); int last = v[poz[1]], j = 1; for (int i = 1; i <= n; ++i) if (v[poz[i]] == last) v[poz[i]] = j; else { last = v[poz[i]]; v[poz[i]] = ++j; } } int sef[1 + MAX_N], sizeSet[1 + MAX_N]; int edge[1 + MAX_N]; int getSef(int nod) { if (nod == sef[nod]) return nod; else { sef[nod] = getSef(sef[nod]); return sef[nod]; } } bool myUnion(int a, int b) { int sa = getSef(a), sb = getSef(b); if (sa != sb) { sef[sa] = sb; sizeSet[sb] += sizeSet[sa]; return true; } return false; } int startcycle[MAX_N]; void buildCycles(int n, int &cycles, int rupturi) { int top = 0, ruptureCycle = 0; for (int i = 1; i <= n; ++i) { sef[i] = i; sizeSet[i] = 1; } for (int i = 1; i <= n; ++i) for (int j = 0; j < src[i].size(); ++j) { edge[src[i][j]] = target[i][j]; cycles -= myUnion(src[i][j], target[i][j]); } for (int i = 1; i <= n; ++i) for (int j = 1; j < src[i].size(); ++j) if (getSef(src[i][0]) != getSef(src[i][j])) { cycles -= myUnion(src[i][0], src[i][j]); std::swap(edge[src[i][0]], edge[src[i][j]]); } for (int i = 1; i <= n; ++i) if (i == sef[i] && sizeSet[i] > 1) startcycle[top++] = i; if (cycles > 1 && rupturi > 1) { ruptureCycle = std::min(cycles, rupturi); if (ruptureCycle > 1) { ++cycles; int aux = edge[startcycle[ruptureCycle - 1]]; for (int i = ruptureCycle - 1; i > 0; --i) { edge[startcycle[i]] = edge[startcycle[i - 1]]; --cycles; } edge[startcycle[0]] = aux; } } printf("%d\n", cycles); if (ruptureCycle > 1) { printf("%d\n", ruptureCycle); for (int i = 0; i < ruptureCycle; ++i) printf("%d ", startcycle[i]); printf("\n"); } } int rez[MAX_N]; void printCycle(int nod) { int top = 0; while (edge[nod] != 0) { int aux = edge[nod]; rez[top++] = nod; edge[nod] = 0; nod = aux; } if (top != 0) { printf("%d\n", top); for (int i = 0; i < top; ++i) printf("%d ", rez[i]); printf("\n"); } } int main() { int n, s, cycles; scanf("%d%d", &n, &s); cycles = n; for (int i = 1; i <= n; ++i) { scanf("%d", &v[i]); poz[i] = i; } normalize(n); for (int i = 1; i <= n; ++i) sorted[i] = v[poz[i]]; for (int i = 1; i <= n; ++i) if (v[i] != sorted[i]) { src[v[i]].push_back(i); target[sorted[i]].push_back(i); } else { ++s; --cycles; } if (s < n) printf("-1"); else { buildCycles(n, cycles, s - n); for (int i = 1; i <= n; ++i) printCycle(i); } return 0; }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 3; unordered_map<int, int> mp; int a[N], b[N], he[N], to[N], ne[N], c; basic_string<int> w[N]; bool v[N]; void dfs(int x) { v[x] = 1; for (int i; i = he[x];) he[x] = ne[i], dfs(to[i]), w[c] += i; } int main() { int t = 0, i, j, k, n, s; scanf("%d%d", &n, &s); for (i = 1; i <= n; ++i) if (scanf("%d", a + i), b[i] = a[i], !mp[a[i]]) mp[a[i]] = ++t; for (i = 1, sort(b + 1, b + n + 1); i <= n; ++i) if (a[i] != b[i]) --s, j = mp[a[i]], k = mp[b[i]], ne[i] = he[j], to[i] = k, he[j] = i; if (s < 0) puts("-1"), exit(0); for (i = 1; i <= t; ++i) if (!v[i]) if (++c, dfs(i), !w[c].size()) --c; if (!c) puts("0"), exit(0); if ((s = min(s, c)) > 1) { printf("%d\n%d\n", c - s + 2, s); for (i = s, j = 0; i; --i) printf("%d ", w[i].back()), j += w[i].size(); for (printf("\n%d\n", j), i = 1; i <= s; ++i) for (int o : w[i]) printf("%d ", o); puts(""); } else printf("%d\n", c), i = 1; for (; i <= c; ++i, puts("")) { cout << w[i].size() << '\n'; for (int o : w[i]) printf("%d ", o); } return 0; }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int N = 1e6 + 7; const int MX = 1e9 + 7; const long long int INF = 1e18 + 9LL; int n, s; int in[N]; int last[N]; int sorted[N]; set<int> place[N]; set<int> value[N]; int cnt; map<int, int> M; int dir[N]; bool vis[N]; vector<int> cur; void solve(int c) { if (place[c].size() == 0) return; int v1 = *place[c].begin(), v2 = *value[c].begin(); place[c].erase(v1); value[c].erase(v2); int first = v1; dir[v1] = v2; if (last[in[first]] > 0) swap(dir[first], dir[last[in[first]]]); last[in[first]] = first; vector<int> cycle; cycle.push_back(v1); while (v2 != first) { v1 = v2; last[in[v1]] = v1; cycle.push_back(v1); c = in[v1]; place[c].erase(v1); v2 = *value[c].begin(); value[c].erase(v2); dir[v1] = v2; } for (int v : cycle) if (place[in[v]].size()) solve(in[v]); } void dfs(int u) { vis[u] = true; cur.push_back(u); if (!vis[dir[u]]) dfs(dir[u]); } int main() { scanf("%d %d", &n, &s); for (int i = 1; i <= n; ++i) { scanf("%d", &in[i]); sorted[i] = in[i]; } int inc = 0; sort(sorted + 1, sorted + n + 1); for (int i = 1; i <= n; ++i) { if (in[i] != sorted[i]) ++inc; if (!M.count(sorted[i])) M[sorted[i]] = ++cnt; } if (inc > s) { puts("-1"); return 0; } for (int i = 1; i <= n; ++i) { in[i] = M[in[i]]; sorted[i] = M[sorted[i]]; } for (int i = 1; i <= n; ++i) if (in[i] != sorted[i]) { place[in[i]].insert(i); value[sorted[i]].insert(i); } vector<vector<int> > ans; for (int i = 1; i <= n; ++i) { if (place[i].size() == 0) continue; solve(i); } for (int i = 1; i <= n; ++i) vis[i] = in[i] == sorted[i]; for (int i = 1; i <= n; ++i) if (!vis[i]) { cur.clear(); dfs(i); ans.push_back(cur); } s -= inc; if (s >= 3 && ans.size() >= 3) { s = min(s, (int)ans.size()); vector<int> help, help2; while (s--) { for (auto v : ans.back()) help.push_back(v); help2.push_back(ans.back()[0]); ans.pop_back(); } reverse(help2.begin(), help2.end()); ans.push_back(help); ans.push_back(help2); } printf("%d\n", (int)ans.size()); for (auto V : ans) { printf("%d\n", (int)V.size()); for (auto v : V) printf("%d ", v); puts(""); } return 0; }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 5; int a[maxn], n, s, b[maxn], num[maxn], len; int ufs[maxn], meme[maxn]; inline int find(int u) { if (ufs[u] == u) return u; else return ufs[u] = find(ufs[u]); } inline void join(int u, int v, int e) { u = find(u), v = find(v); if (u == v) return; ufs[v] = u; meme[u] = e; } vector<vector<int> > mema; vector<pair<int, int> > adj[maxn]; vector<int> curm; inline void dfs(int u) { while (adj[u].size()) { int v = adj[u].back().first, id = adj[u].back().second; adj[u].pop_back(); dfs(v); curm.push_back(id); } } int main() { cin >> n >> s; for (int i = 1; i <= n; i++) { scanf("%d", &a[i]); num[++len] = a[i]; } sort(num + 1, num + len + 1); len = unique(num + 1, num + len + 1) - num - 1; for (int i = 1; i <= n; i++) { a[i] = lower_bound(num + 1, num + len + 1, a[i]) - num; b[i] = a[i]; ufs[i] = i; } sort(b + 1, b + n + 1); int cnt = 0; for (int i = 1; i <= n; i++) { if (a[i] != b[i]) { cnt++; join(a[i], b[i], i); } } if (cnt > s) { cout << "-1" << endl; return 0; } if (cnt == 0) { cout << "0" << endl; return 0; } if (s - cnt > 1) { for (int i = 1; i <= len; i++) { if (ufs[i] == i && meme[i] != 0) { curm.push_back(meme[i]); if (curm.size() == s - cnt) break; } } if (curm.size() > 1) { mema.push_back(curm); int lstv = a[curm.back()]; for (int i = curm.size() - 1; i >= 1; i--) { a[curm[i]] = a[curm[i - 1]]; } a[curm[0]] = lstv; } } for (int i = 1; i <= n; i++) { if (a[i] != b[i]) { adj[a[i]].push_back(make_pair(b[i], i)); } } for (int i = 1; i <= len; i++) { if (adj[i].size() != 0) { curm.clear(); dfs(i); mema.push_back(curm); } } cout << mema.size() << endl; for (int i = 0; i < mema.size(); i++) { cout << mema[i].size() << endl; for (int j = 0; j < mema[i].size(); j++) { printf("%d ", mema[i][j]); } cout << endl; } return 0; }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int inf = 1e9; const long long Inf = 1e18; const int N = 2e5 + 10; const int mod = 0; int gi() { int x = 0, o = 1; char ch = getchar(); while ((ch < '0' || ch > '9') && ch != '-') ch = getchar(); if (ch == '-') o = -1, ch = getchar(); while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar(); return x * o; } template <typename T> bool chkmax(T &a, T b) { return a < b ? a = b, 1 : 0; }; template <typename T> bool chkmin(T &a, T b) { return a > b ? a = b, 1 : 0; }; int add(int a, int b) { return a + b >= mod ? a + b - mod : a + b; } int sub(int a, int b) { return a - b < 0 ? a - b + mod : a - b; } void inc(int &a, int b) { a = (a + b >= mod ? a + b - mod : a + b); } void dec(int &a, int b) { a = (a - b < 0 ? a - b + mod : a - b); } int n, s, a[N]; bool vis[N]; map<int, int> cnt; map<int, vector<int>> E; vector<int> cycle; vector<vector<int>> ans; void dfs(int u) { while (!E[u].empty()) { int v = E[u].back(); E[u].pop_back(); vis[v] = 1; dfs(a[v]); cycle.push_back(v); } } int main() { cin >> n >> s; for (int i = 1; i <= n; i++) a[i] = gi(), cnt[a[i]]++; int now = 1; for (pair<int, int> x : cnt) { for (int j = now; j < now + x.second; j++) if (a[j] != x.first) E[x.first].push_back(j); now += x.second; } for (int i = 1; i <= n; i++) if (!vis[i]) { cycle.clear(), dfs(a[i]); if (!cycle.empty()) reverse(cycle.begin(), cycle.end()), ans.push_back(cycle), s -= int(cycle.size()); } if (s < 0) return puts("-1"), 0; if (s <= 2 || int(ans.size()) <= 2) { printf("%d\n", int(ans.size())); for (vector<int> cycle : ans) { printf("%d\n", int(cycle.size())); for (int x : cycle) printf("%d ", x); puts(""); } } else { s = min(s, int(ans.size())); printf("%d\n", int(ans.size()) - s + 2); while (int(ans.size()) > s) { vector<int> cycle = ans.back(); ans.pop_back(); printf("%d\n", int(cycle.size())); for (int x : cycle) printf("%d ", x); puts(""); } printf("%d\n", s); int sum = 0; for (auto cycle : ans) printf("%d ", cycle.back()), sum += int(cycle.size()); puts(""); printf("%d\n", sum); reverse(ans.begin(), ans.end()); for (auto cycle : ans) for (int x : cycle) printf("%d ", x); puts(""); } return 0; }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
CORRECT
java
import java.io.*; import java.math.*; import java.util.*; import java.util.stream.*; public class E { int[] sorted(int[] a) { a = a.clone(); for (int i = 0; i < a.length; i++) { int j = rand(0, i); int tmp = a[i]; a[i] = a[j]; a[j] = tmp; } return a; } void solve(int[] a, int s) { int n = a.length; Integer[] order = new Integer[n]; for (int i = 0; i < n; i++) { order[i] = i; } Arrays.sort(order, Comparator.comparingInt(x -> a[x])); int[] perm = new int[n]; Arrays.fill(perm, -1); int sum = 0; for (int i = 0; i < n; i++) { if (a[i] == a[order[i]]) { perm[i] = i; } else { sum++; } } if (sum > s) { out.println(-1); return; } for (int i = 0, j1 = 0, j2 = 0; i < sum; i++) { while (perm[order[j1]] == order[j1]) { j1++; } while (perm[j2] == j2) { j2++; } perm[order[j1]] = j2; j1++; j2++; } p = new int[n]; Arrays.fill(p, -1); for (int i = 0; i < n; i++) { for (int j = i; p[j] == -1; j = perm[j]) { p[j] = i; } } int lastIdx = -1, lastVal = -1; for (int i = 0; i < n; i++) { int idx = order[i]; if (perm[idx] == idx) { continue; } if (a[idx] == lastVal) { int v = get(idx); int u = get(lastIdx); if (v != u) { p[u] = v; int tmp = perm[idx]; perm[idx] = perm[lastIdx]; perm[lastIdx] = tmp; } } lastVal = a[idx]; lastIdx = idx; } ArrayList<ArrayList<Integer>> cycles = new ArrayList<>(); boolean[] used = new boolean[n]; for (int i = 0; i < n; i++) { if (perm[i] == i || used[i]) { continue; } ArrayList<Integer> cycle = new ArrayList<>(); for (int j = i; !used[j]; j = perm[j]) { used[j] = true; cycle.add(j); } cycles.add(cycle); } if (false) { int sumCycles = 0; for (List<Integer> lst : cycles) { sumCycles += lst.size(); } if (sumCycles != sum) { throw new AssertionError(); } } for (int merge = cycles.size(); merge >= 0; merge--) { if (merge == 1 || merge == 2) { continue; } int cost = sum + merge; if (cost > s) { continue; } int pop = cycles.size() - merge; out.println(pop + (merge > 0 ? 2 : 0)); for (int i = 0; i < pop; i++) { ArrayList<Integer> cycle = cycles.remove(cycles.size() - 1); out.println(cycle.size()); for (int x : cycle) { out.print(x + 1 + " "); } out.println(); } if (cycles.isEmpty()) { return; } int total = 0; for (ArrayList<Integer> cycle : cycles) { total += cycle.size(); } out.println(total); for (ArrayList<Integer> cycle : cycles) { for (int x : cycle) { out.print(x + 1 + " "); } } out.println(); out.println(cycles.size()); for (int i = cycles.size() - 1; i >= 0; i--) { out.print(cycles.get(i).get(0) + 1 + " "); } out.println(); return; } throw new AssertionError(); } int get(int v) { return p[v] == v ? v : (p[v] = get(p[v])); } int[] p; void test() { solve(new int[] { 1, 1, 0, 0, 0 }, 5); // solve(new int[] { 1, 0, 0 }, 3); } void stress() { for (int tst = 0;; tst++) { int n = rand(1, C); int s = rand(0, n); int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = rand(0, n - 1); } System.err.println(Arrays.toString(a) + " " + s); solve(a, s); System.err.println(tst); } } void submit() { int n = nextInt(); int s = nextInt(); int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = nextInt(); } solve(a, s); } E() throws IOException { is = System.in; out = new PrintWriter(System.out); submit(); // stress(); // test(); out.close(); } static final Random rng = new Random(); static final int C = 5; static int rand(int l, int r) { return l + rng.nextInt(r - l + 1); } public static void main(String[] args) throws IOException { new E(); } private InputStream is; PrintWriter out; private byte[] buf = new byte[1 << 14]; private int bufSz = 0, bufPtr = 0; private int readByte() { if (bufSz == -1) throw new RuntimeException("Reading past EOF"); if (bufPtr >= bufSz) { bufPtr = 0; try { bufSz = is.read(buf); } catch (IOException e) { throw new RuntimeException(e); } if (bufSz <= 0) return -1; } return buf[bufPtr++]; } private boolean isTrash(int c) { return c < 33 || c > 126; } private int skip() { int b; while ((b = readByte()) != -1 && isTrash(b)) ; return b; } String nextToken() { int b = skip(); StringBuilder sb = new StringBuilder(); while (!isTrash(b)) { sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } String nextString() { int b = readByte(); StringBuilder sb = new StringBuilder(); while (!isTrash(b) || b == ' ') { sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } double nextDouble() { return Double.parseDouble(nextToken()); } char nextChar() { return (char) skip(); } int nextInt() { int ret = 0; int b = skip(); if (b != '-' && (b < '0' || b > '9')) { throw new InputMismatchException(); } boolean neg = false; if (b == '-') { neg = true; b = readByte(); } while (true) { if (b >= '0' && b <= '9') { ret = ret * 10 + (b - '0'); } else { if (b != -1 && !isTrash(b)) { throw new InputMismatchException(); } return neg ? -ret : ret; } b = readByte(); } } long nextLong() { long ret = 0; int b = skip(); if (b != '-' && (b < '0' || b > '9')) { throw new InputMismatchException(); } boolean neg = false; if (b == '-') { neg = true; b = readByte(); } while (true) { if (b >= '0' && b <= '9') { ret = ret * 10 + (b - '0'); } else { if (b != -1 && !isTrash(b)) { throw new InputMismatchException(); } return neg ? -ret : ret; } b = readByte(); } } }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 10; int n, s; int niz[maxn], sol[maxn], saz[maxn]; vector<pair<int, int> > graph[maxn]; bool bio[maxn], bio2[maxn]; vector<int> sa; vector<vector<int> > al; vector<int> ac[maxn]; void dfs(int node) { bio2[node] = true; while (!graph[node].empty()) { const int nig = graph[node].back().first; const int id = graph[node].back().second; graph[node].pop_back(); dfs(nig); sa.push_back(id + 1); } } int main() { memset(bio, false, sizeof bio); memset(bio2, false, sizeof bio2); scanf("%d%d", &n, &s); for (int i = 0; i < n; i++) scanf("%d", niz + i); for (int i = 0; i < n; i++) sol[i] = niz[i]; sort(sol, sol + n); for (int i = 0; i < n; i++) saz[i] = sol[i]; for (int i = 0; i < n; i++) niz[i] = lower_bound(saz, saz + n, niz[i]) - saz, sol[i] = lower_bound(saz, saz + n, sol[i]) - saz; for (int i = 0; i < n; i++) { if (niz[i] == sol[i]) continue; graph[sol[i]].push_back(make_pair(niz[i], i)); s--; } if (s < 0) { printf("-1"); return 0; } for (int i = 0; i < n; i++) if (niz[i] != sol[i]) ac[niz[i]].push_back(i + 1); for (int i = 0; i < n; i++) { if (bio2[i]) continue; dfs(i); reverse(sa.begin(), sa.end()); if (sa.size() == 0) continue; al.push_back(sa); sa.clear(); } if (s > 2 && al.size() > 1) { int ptr = 0; vector<int> pok, ne; int siz = (int)al.size(); for (int i = 0; i < min(s, siz); i++) { for (int j = 0; j < al.back().size(); j++) { if (j == 0) pok.push_back(al.back()[j]); ne.push_back(al.back()[j]); } al.pop_back(); } al.push_back(ne); reverse(pok.begin(), pok.end()); al.push_back(pok); } printf("%d\n", al.size()); for (int i = 0; i < al.size(); i++) { printf("%d\n", al[i].size()); for (int j = 0; j < al[i].size(); j++) printf("%d ", al[i][j]); printf("\n"); } return 0; }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int n, s, a[200079], p[200079]; vector<pair<int, int> > b(200079); int rodic[200079], r[200079]; int najdisef(int v) { if (rodic[v] == v) return v; rodic[v] = najdisef(rodic[v]); return rodic[v]; } void spojset(int i, int j) { i = najdisef(i); j = najdisef(j); if (i == j) return; if (r[i] > r[j]) swap(i, j); if (r[i] == r[j]) r[j]++; rodic[i] = j; } int t; vector<int> u(200079, false); vector<vector<int> > c(200079); void dfs(int vr) { u[vr] = true; c[t].push_back(vr); if (!u[p[vr]]) dfs(p[vr]); } bool cmp(pair<int, int> p1, pair<int, int> p2) { if (p1.first == p2.first) return p1.second > p2.second; return p1.first > p2.first; } int main() { cin >> n >> s; for (int i = 0; i < n; i++) { cin >> a[i]; pair<int, int> tmp; tmp.first = a[i]; tmp.second = i; b[i] = tmp; } sort(b.begin(), b.begin() + n); for (int i = 0; i < n; i++) p[b[i].second] = i; for (int i = 0; i < n; i++) { if (a[i] == b[i].first && p[i] != i) { p[b[i].second] = p[i]; b[p[i]].second = b[i].second; p[i] = i; b[i].second = i; } } for (int i = 0; i < n; i++) { rodic[i] = i; r[i] = 0; } for (int i = 0; i < n; i++) spojset(p[i], i); int ls = -1; for (int i = 0; i < n; i++) { if (p[b[i].second] == b[i].second) continue; if (ls >= 0 && a[ls] == a[b[i].second]) { int va = ls, vb = b[i].second; if (najdisef(va) == najdisef(vb)) continue; spojset(va, vb); swap(p[va], p[vb]); } ls = b[i].second; } t = 0; for (int i = 0; i < n; i++) if (u[i] == 0 && p[i] != i) { dfs(i); t++; } int ans = 0; for (int i = 0; i < t; i++) ans += c[i].size(); if (ans > s) { cout << "-1\n"; return 0; } s -= ans; s = min(s, t); if (s <= 1) { cout << t << endl; for (int i = 0; i < t; i++) { cout << c[i].size() << endl; for (int j = 0; j < c[i].size(); j++) cout << c[i][j] + 1 << " "; cout << endl; } return 0; } cout << (t - s + 2) << endl; for (int i = 0; i < t - s; i++) { cout << c[i + s].size() << endl; for (int j = 0; j < c[i + s].size(); j++) cout << c[i + s][j] + 1 << " "; cout << endl; ans -= c[i + s].size(); } cout << ans << endl; for (int i = 0; i < s; i++) for (int j = 0; j < c[i].size(); j++) cout << c[i][j] + 1 << " "; cout << endl; cout << s << endl; for (int i = s - 1; i >= 0; i--) cout << c[i][0] + 1 << " "; cout << "\n"; return 0; }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 5; int a[N]; int b[N], t[N], nt[N], u; vector<int> ans[N]; int top; int NT(int x) { while (nt[x] < t[x + 1] && a[nt[x]] == x) ++nt[x]; return nt[x]; } void dfs(int x) { while (NT(x) < t[x + 1]) { int now = nt[x]++; dfs(a[now]); ans[top].push_back(now); } } int main() { int n, s; cin >> n >> s; for (int i = 1; i <= n; ++i) scanf("%d", a + i); for (int i = 1; i <= n; ++i) b[i] = a[i]; sort(b + 1, b + n + 1); for (int i = 1; i <= n; ++i) if (b[i] != b[u]) { ++u; t[u] = i; b[u] = b[i]; } t[u + 1] = n + 1; for (int i = 1; i <= n; ++i) a[i] = lower_bound(b + 1, b + u + 1, a[i]) - b; for (int i = 1; i <= u; ++i) nt[i] = t[i]; for (int i = 1; i <= u; ++i) if (NT(i) < t[i + 1]) { ++top; dfs(i); s -= ans[top].size(); reverse(ans[top].begin(), ans[top].end()); } if (s < 0) puts("-1"); else { s = min(s, top); if (s < 2) { printf("%d\n", top); for (int i = 1; i <= top; ++i) { printf("%d\n", (int)ans[i].size()); for (auto x : ans[i]) printf("%d ", x); puts(""); } } else { printf("%d\n", top - s + 2); int sum = 0; for (int i = 1; i <= s; ++i) sum += ans[i].size(); printf("%d\n", sum); for (int i = 1; i <= s; ++i) for (auto x : ans[i]) printf("%d ", x); puts(""); printf("%d\n", s); for (int i = s; i >= 1; --i) printf("%d ", ans[i][0]); puts(""); for (int i = s + 1; i <= top; ++i) { printf("%d\n", (int)ans[i].size()); for (auto x : ans[i]) printf("%d ", x); puts(""); } } } }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int inf = 1e9 + 7; string to_string(string s) { return '"' + s + '"'; } string to_string(char s) { return string(1, s); } string to_string(const char *s) { return to_string((string)s); } string to_string(bool b) { return (b ? "true" : "false"); } template <typename A> string to_string(A); template <typename A, typename B> string to_string(pair<A, B> p) { return "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; } template <typename A> string to_string(A v) { bool f = 1; string r = "{"; for (const auto &x : v) { if (!f) r += ", "; f = 0; r += to_string(x); } return r + "}"; } void debug_out() { cout << endl; } template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cout << " " << to_string(H); debug_out(T...); } struct DSU { vector<int> p, r; vector<map<int, int>> nxt; int num; DSU(int n) : p(n), r(n), nxt(n), num(0) { for (auto i = (0); i <= (n - 1); ++i) p[i] = i; } int get(int i) { if (p[i] != i) p[i] = get(p[i]); return p[i]; } int connect(int i, int j) { int x = get(i), y = get(j); if (x == y) return 0; --num; if (r[x] > r[y]) swap(x, y); p[x] = y; for (auto it : nxt[x]) { nxt[y].insert(it); } assert(nxt[y].count(i) and nxt[y].count(j)); swap(nxt[y][i], nxt[y][j]); r[y] += r[x]; return 1; } }; int main() { ios::sync_with_stdio(0); cin.tie(0); srand(8); int n, s; cin >> n >> s; vector<int> a(n); for (auto i = (0); i <= (n - 1); ++i) { cin >> a[i]; } vector<int> b = a; sort((b).begin(), (b).end()); map<int, vector<int>> pos, gpos; for (auto i = (0); i <= (n - 1); ++i) if (a[i] != b[i]) { pos[b[i]].push_back(i); gpos[a[i]].push_back(i); } vector<int> vis(n); DSU ds(n); int len = 0; for (auto i = (0); i <= (n - 1); ++i) if (!vis[i] and a[i] != b[i]) { int x = i; int cur = 0; do { vis[x] = 1; int y = pos[a[x]].back(); pos[a[x]].pop_back(); ds.p[x] = i; ds.nxt[i][x] = y; x = y; ++cur; ++len; } while (x != i); ds.num++; ds.r[i] = cur; } for (auto it : gpos) { const auto &A = it.second; for (auto i = (1); i <= (int(A.size()) - 1); ++i) { ds.connect(A[i], A[i - 1]); } } vector<vector<int>> cycles; for (auto i = (0); i <= (n - 1); ++i) if (a[i] != b[i] and ds.get(i) == i) { cycles.emplace_back(); int x = i; do { cycles.back().emplace_back(x); x = ds.nxt[i][x]; } while (x != i); } s -= len; if (s < 0) { cout << "-1\n"; return 0; } int x = min(s, int(cycles.size())); vector<vector<int>> ans; if (x > 1) { ans.emplace_back(); ans.emplace_back(); for (auto i = (0); i <= (x - 1); ++i) { for (int j : cycles[i]) ans[0].emplace_back(j); ans[1].emplace_back(cycles[i][0]); } reverse((ans[1]).begin(), (ans[1]).end()); for (auto i = (x); i <= (int(cycles.size()) - 1); ++i) ans.emplace_back(cycles[i]); } else ans = cycles; cout << int(ans.size()) << "\n"; for (auto it : ans) { cout << int(it.size()) << "\n"; for (auto itt : it) cout << itt + 1 << " "; cout << "\n"; } return 0; }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; template <class T, class U> void ckmin(T &a, U b) { if (a > b) a = b; } template <class T, class U> void ckmax(T &a, U b) { if (a < b) a = b; } const int MAXN = 400013; int N, S, M, ans, n; int val[MAXN], arr[MAXN], sorted[MAXN]; vector<int> moves[MAXN]; vector<int> cyc[MAXN]; bitset<MAXN> vis; vector<int> compress; int freq[MAXN]; pair<int, int> range[MAXN]; vector<int> edge[MAXN]; vector<int> ord; int ind[MAXN]; int indexof(vector<int> &v, int x) { return upper_bound((v).begin(), (v).end(), x) - v.begin() - 1; } void solve() { int k = min(M, S - n); if (k <= 2) { ans = M; for (auto i = (0); i < (M); i++) { moves[i] = cyc[i]; } } else { ans = M - k + 2; for (auto i = (0); i < (M - k); i++) { moves[i] = cyc[i]; } for (auto i = (M - k); i < (M); i++) { moves[M - k].insert(moves[M - k].end(), (cyc[i]).begin(), (cyc[i]).end()); moves[M - k + 1].push_back(cyc[i][0]); } reverse((moves[M - k + 1]).begin(), (moves[M - k + 1]).end()); } } int32_t main() { cout << fixed << setprecision(12); cerr << fixed << setprecision(4); ios_base::sync_with_stdio(false); cin.tie(0); cin >> N >> S; for (auto i = (0); i < (N); i++) { cin >> val[i]; compress.push_back(val[i]); } sort((compress).begin(), (compress).end()); compress.erase(unique((compress).begin(), (compress).end()), compress.end()); for (auto i = (0); i < (N); i++) { val[i] = indexof(compress, val[i]); sorted[i] = val[i]; } sort(sorted, sorted + N); for (auto i = (0); i < (N); i++) { if (val[i] == sorted[i]) { vis[i] = true; arr[i] = i; continue; } edge[i].push_back(val[i] + N); edge[sorted[i] + N].push_back(i); } for (auto i = (0); i < (N); i++) { if (vis[i]) continue; vector<int> stk; stk.push_back(i); while (!stk.empty()) { int u = stk.back(); if (edge[u].empty()) { ord.push_back(u); stk.pop_back(); } else { stk.push_back(edge[u].back()); edge[u].pop_back(); } } reverse((ord).begin(), (ord).end()); for (int j = 0; j + 2 < ((int)(ord).size()); j += 2) { int u = ord[j]; int v = ord[j + 2]; arr[u] = v; } ord.clear(); } vis.reset(); for (auto i = (0); i < (N); i++) { if (vis[i]) continue; if (arr[i] == i) { continue; } cyc[M].push_back(i); do { vis[cyc[M].back()] = true; cyc[M].push_back(arr[cyc[M].back()]); } while (cyc[M].back() != i); cyc[M].pop_back(); M++; } for (auto i = (0); i < (M); i++) { n += ((int)(cyc[i]).size()); } if (S < n) { cout << "-1\n"; return 0; } solve(); cout << ans << '\n'; for (auto i = (0); i < (ans); i++) { cout << ((int)(moves[i]).size()) << '\n'; for (int x : moves[i]) { cout << x + 1 << " \n"[x == moves[i].back()]; } } }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int a[200010], b[200010], nxt[200010], cnt, n, s; void solve(vector<int> &v, int i, int &j) { while (a[j] == a[i]) j++; if (j >= n) return; if (b[j] == a[i]) { swap(a[i], a[j]); cnt++; v.push_back(j++); solve(v, i, nxt[lower_bound(b, b + n, a[i]) - b]); } } int main() { cin >> n >> s; for (int i = 0; i < n; i++) { scanf("%d", a + i); b[i] = a[i]; nxt[i] = i; } sort(b, b + n); a[n] = b[n] = -1; cnt = 0; vector<vector<int> > ans; for (int i = 0; i < n; i++) { if (b[i] == a[i]) continue; ans.push_back(vector<int>(1, i)); cnt++; nxt[lower_bound(b, b + n, b[i]) - b] = i + 1; auto &v = ans.back(); solve(v, i, nxt[lower_bound(b, b + n, a[i]) - b]); for (int k = 0; k < v.size(); k++) { int j0 = v[k]; int &j = nxt[lower_bound(b, b + n, b[j0]) - b]; while (b[j0] == b[j]) { if (b[j] != a[j]) { cnt++; vector<int> v1(1, j); int j1 = j++; solve(v1, j1, nxt[lower_bound(b, b + n, a[j1]) - b]); v.insert(v.begin() + k, v1.begin(), v1.end()); } else j++; } } } if (cnt > s) { printf("-1\n"); } else if (ans.size() < 3 || s - cnt < 3) { printf("%d\n", ans.size()); while (ans.size() > 0) { auto &v = ans.back(); printf("%d\n", v.size()); for (int i : v) { printf("%d ", i + 1); } printf("\n"); ans.pop_back(); } } else { int x = max((int)ans.size() - s + cnt, 0), size = 0; for (int i = x; i < ans.size(); i++) { size += ans[i].size(); } printf("%d\n%d\n", x + 2, size); for (int i = x; i < ans.size(); i++) { auto &v = ans[i]; for (int i : v) { printf("%d ", i + 1); } } printf("\n%d\n", ans.size() - x); for (int i = ans.size() - 1; i >= x; i--) { printf("%d ", ans[i][0] + 1); } printf("\n"); ans.resize(x); while (ans.size() > 0) { auto &v = ans.back(); printf("%d\n", v.size()); for (int i : v) { printf("%d ", i + 1); } printf("\n"); ans.pop_back(); } } return 0; }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using ll = long long; template <typename S, typename T> void xmin(S &a, T const &b) { if (b < a) a = b; } template <typename S, typename T> void xmax(S &a, T const &b) { if (b > a) a = b; } vector<vector<pair<int, int> > > g; vector<int> eu; vector<int> cured; void rec(int u) { for (int &i = cured[u]; i < (int)g[u].size();) { int v = g[u][i].first; int x = g[u][i].second; ++i; rec(v); eu.push_back(x); } } signed main() { cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(false); int n, s; cin >> n >> s; vector<int> a(n), b; for (auto &e : a) { cin >> e; } b = a; sort(b.begin(), b.end()); for (auto &e : a) { e = lower_bound(b.begin(), b.end(), e) - b.begin(); } b = a; sort(b.begin(), b.end()); g.clear(); g.resize(n); for (int i = 0; i < n; ++i) { if (a[i] != b[i]) { g[a[i]].emplace_back(b[i], i); } } vector<vector<int> > cyc; int tot = 0; cured.assign(n, 0); for (int i = 0; i < n; ++i) { eu.clear(); rec(i); if (!eu.empty()) { cyc.push_back(eu); tot += eu.size(); } } if (tot > s) { cout << "-1\n"; } else { int spare = s - tot; if (spare > 2 && cyc.size() > 2) { xmin(spare, (int)cyc.size()); vector<int> big, small; for (int i = 0; i < spare; ++i) { small.push_back(cyc.back().back()); big.insert(big.end(), cyc.back().begin(), cyc.back().end()); cyc.pop_back(); } reverse(small.begin(), small.end()); cyc.push_back(small); cyc.push_back(big); } cout << cyc.size() << "\n"; for (auto const &e : cyc) { cout << e.size() << "\n"; for (auto const &f : e) { cout << 1 + f << " "; } cout << "\n"; } } return 0; }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using ll = long long; using vi = vector<int>; using vvi = vector<vi>; using vll = vector<ll>; using vvll = vector<vll>; using vb = vector<bool>; using vd = vector<double>; using vs = vector<string>; using pii = pair<int, int>; using pll = pair<ll, ll>; using pdd = pair<double, double>; using vpii = vector<pii>; using vvpii = vector<vpii>; using vpll = vector<pll>; using vvpll = vector<vpll>; using vpdd = vector<pdd>; using vvpdd = vector<vpdd>; template <typename T> void ckmin(T& a, const T& b) { a = min(a, b); } template <typename T> void ckmax(T& a, const T& b) { a = max(a, b); } mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); namespace __input { template <class T1, class T2> void re(pair<T1, T2>& p); template <class T> void re(vector<T>& a); template <class T, size_t SZ> void re(array<T, SZ>& a); template <class T> void re(T& x) { cin >> x; } void re(double& x) { string t; re(t); x = stod(t); } template <class Arg, class... Args> void re(Arg& first, Args&... rest) { re(first); re(rest...); } template <class T1, class T2> void re(pair<T1, T2>& p) { re(p.first, p.second); } template <class T> void re(vector<T>& a) { for (int i = 0; i < (int((a).size())); i++) re(a[i]); } template <class T, size_t SZ> void re(array<T, SZ>& a) { for (int i = 0; i < (SZ); i++) re(a[i]); } } // namespace __input using namespace __input; namespace __output { template <class T1, class T2> void pr(const pair<T1, T2>& x); template <class T, size_t SZ> void pr(const array<T, SZ>& x); template <class T> void pr(const vector<T>& x); template <class T> void pr(const deque<T>& x); template <class T> void pr(const set<T>& x); template <class T1, class T2> void pr(const map<T1, T2>& x); template <class T> void pr(const T& x) { cout << x; } template <class Arg, class... Args> void pr(const Arg& first, const Args&... rest) { pr(first); pr(rest...); } template <class T1, class T2> void pr(const pair<T1, T2>& x) { pr("{", x.first, ", ", x.second, "}"); } template <class T, bool pretty = true> void prContain(const T& x) { if (pretty) pr("{"); bool fst = 1; for (const auto& a : x) pr(!fst ? pretty ? ", " : " " : "", a), fst = 0; if (pretty) pr("}"); } template <class T> void pc(const T& x) { prContain<T, false>(x); pr("\n"); } template <class T, size_t SZ> void pr(const array<T, SZ>& x) { prContain(x); } template <class T> void pr(const vector<T>& x) { prContain(x); } template <class T> void pr(const deque<T>& x) { prContain(x); } template <class T> void pr(const set<T>& x) { prContain(x); } template <class T1, class T2> void pr(const map<T1, T2>& x) { prContain(x); } void ps() { pr("\n"); } template <class Arg> void ps(const Arg& first) { pr(first); ps(); } template <class Arg, class... Args> void ps(const Arg& first, const Args&... rest) { pr(first, " "); ps(rest...); } } // namespace __output using namespace __output; namespace __algorithm { template <typename T> void dedup(vector<T>& v) { sort((v).begin(), (v).end()); v.erase(unique((v).begin(), (v).end()), v.end()); } template <typename T> typename vector<T>::iterator find(vector<T>& v, const T& x) { auto it = lower_bound((v).begin(), (v).end(), x); return it != v.end() && *it == x ? it : v.end(); } template <typename T> size_t index(vector<T>& v, const T& x) { auto it = find(v, x); assert(it != v.end() && *it == x); return it - v.begin(); } template <typename C, typename T, typename OP> vector<T> prefixes(const C& v, T id, OP op) { vector<T> r(int((v).size()) + 1, id); for (int i = 0; i < (int((v).size())); i++) r[i + 1] = op(r[i], v[i]); return r; } template <typename C, typename T, typename OP> vector<T> suffixes(const C& v, T id, OP op) { vector<T> r(int((v).size()) + 1, id); for (int i = (int((v).size())) - 1; i >= 0; i--) r[i] = op(v[i], r[i + 1]); return r; } } // namespace __algorithm using namespace __algorithm; struct monostate { friend istream& operator>>(istream& is, const __attribute__((unused)) monostate& ms) { return is; } friend ostream& operator<<(ostream& os, const __attribute__((unused)) monostate& ms) { return os; } } ms; template <typename W = monostate> struct wedge { int u, v, i; W w; wedge<W>(int _u = -1, int _v = -1, int _i = -1) : u(_u), v(_v), i(_i) {} int operator[](int loc) const { return u ^ v ^ loc; } friend void re(wedge& e) { re(e.u, e.v, e.w); --e.u, --e.v; } friend void pr(const wedge& e) { pr(e.u, "<-", e.w, "->", e.v); } }; namespace __io { void setIn(string second) { freopen(second.c_str(), "r", stdin); } void setOut(string second) { freopen(second.c_str(), "w", stdout); } void setIO(string second = "") { ios_base::sync_with_stdio(0); cin.tie(0); cout.precision(15); if (int((second).size())) { setIn(second + ".in"), setOut(second + ".out"); } } } // namespace __io using namespace __io; struct uf_monostate { uf_monostate(__attribute__((unused)) int id) {} void merge(__attribute__((unused)) uf_monostate& o, __attribute__((unused)) const monostate& e) {} }; template <typename T = uf_monostate, typename E = monostate> struct union_find { struct node { int par, rnk, size; T state; node(int id = 0) : par(id), rnk(0), size(1), state(id) {} void merge(node& o, E& e) { if (rnk == o.rnk) rnk++; if (size < o.size) swap(state, o.state); size += o.size; state.merge(o.state, e); } }; int cc; vector<node> uf; union_find(int N = 0) : uf(N), cc(N) { for (int i = 0; i < N; i++) uf[i] = node(i); } int rep(int i) { if (i != uf[i].par) uf[i].par = rep(uf[i].par); return uf[i].par; } bool unio(int a, int b, E& e = ms) { a = rep(a), b = rep(b); if (a == b) return false; if (uf[a].rnk < uf[b].rnk) swap(a, b); uf[a].merge(uf[b], e); uf[b].par = a; cc--; return true; } T& state(int i) { return uf[rep(i)].state; } }; int main() { setIO(); int N, S; re(N, S); vi a(N); re(a); vi ti(N); vi st = a, did(N); sort((st).begin(), (st).end()); vvi occ(N); for (int i = 0; i < (N); i++) if (a[i] != st[i]) { int w = index(st, a[i]); while (a[w + did[w]] == st[w + did[w]]) did[w]++; ti[i] = w + did[w]++; occ[w].push_back(i); } else ti[i] = i; vb vis(N); union_find<> uf(N); for (int i = 0; i < (N); i++) if (!vis[i]) { vis[i] = true; for (int t = ti[i]; t != i; t = ti[t]) { uf.unio(i, t); vis[t] = true; } } for (int i = 0; i < (N); i++) for (int j = 0; j < (int((occ[i]).size()) - 1); j++) { if (uf.unio(occ[i][j], occ[i][j + 1])) { swap(ti[occ[i][j]], ti[occ[i][j + 1]]); } } int wr = 0; for (int i = 0; i < (N); i++) if (a[i] != st[i]) wr++; if (wr > S) { ps(-1); return 0; } if (a == st) { ps(0); return 0; } vvi cyc; for (int i = 0; i < (N); i++) if (i == uf.rep(i) && i != ti[i]) { cyc.push_back({i + 1}); for (int t = ti[i]; t != i; t = ti[t]) cyc.back().push_back(t + 1); } int merge = min(S - wr, int((cyc).size())); if (merge > 2) { vi loop, fix; for (int c = (int((cyc).size()) - merge); c < (int((cyc).size())); c++) { loop.insert(loop.end(), (cyc[c]).begin(), (cyc[c]).end()); fix.push_back(cyc[c].front()); } reverse((fix).begin(), (fix).end()); cyc.erase(cyc.end() - merge, cyc.end()); cyc.push_back(loop); cyc.push_back(fix); } ps(int((cyc).size())); for (auto& c : cyc) ps(int((c).size())), pc(c); return 0; }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int MAXN = 200100; int arr[MAXN], tmp[MAXN]; vector<int> ve[MAXN]; map<pair<int, int>, vector<int> > mapa; int ind[MAXN]; vector<int> out[MAXN]; int sol = 0; vector<int> t1, t2; vector<int> comp; map<int, int> aaa; void tour(int x, int i) { while (ind[x] < ve[x].size()) { int y = ve[x][ind[x]++]; int ind = mapa[{x, y}].back(); mapa[{x, y}].pop_back(); tour(y, ind); } out[sol].push_back(i); return; } int main() { ios_base::sync_with_stdio(false); int n, s; cin >> n >> s; for (int i = (0); i < (n); i++) { cin >> arr[i]; comp.push_back(arr[i]); } sort(comp.begin(), comp.end()); comp.resize(unique(comp.begin(), comp.end()) - comp.begin()); for (int i = (0); i < (comp.size()); i++) { aaa[comp[i]] = i; } for (int i = (0); i < (n); i++) { arr[i] = aaa[arr[i]]; tmp[i] = arr[i]; } sort(tmp, tmp + n); for (int i = (0); i < (n); i++) { if (arr[i] != tmp[i]) { mapa[{tmp[i], arr[i]}].push_back(i); ve[tmp[i]].push_back(arr[i]); } } int uk = 0; for (int i = (0); i < (n); i++) { tour(tmp[i], i); out[sol].pop_back(); if (out[sol].size()) { reverse(out[sol].begin(), out[sol].end()); uk += out[sol].size(); sol++; } } if (uk > s) { cout << "-1\n"; return 0; } int m = min(sol, s - uk); for (int i = (0); i < (m); i++) { sol--; for (int x : out[sol]) t1.push_back(x); t2.push_back(out[sol][0]); } if (m == 1) sol++; if (m <= 1) { cout << sol << endl; for (int i = (0); i < (sol); i++) { cout << out[i].size() << "\n"; for (int x : out[i]) cout << x + 1 << " "; cout << "\n"; } } else { reverse(t2.begin(), t2.end()); cout << sol + 2 << endl; cout << t1.size() << endl; for (int x : t1) cout << x + 1 << " "; cout << endl; cout << t2.size() << endl; for (int x : t2) cout << x + 1 << " "; cout << endl; for (int i = (0); i < (sol); i++) { cout << out[i].size() << "\n"; for (int x : out[i]) cout << x + 1 << " "; cout << "\n"; } } return 0; }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; template <class T> bool uin(T &a, T b) { return a > b ? (a = b, true) : false; } template <class T> bool uax(T &a, T b) { return a < b ? (a = b, true) : false; } const int N = 2e5 + 5; int n, s, cnt, to[N], a[N], b[N], pos[N]; vector<int> all[N]; pair<int, int> p[N]; void compress() { for (int i = 0; i < (int)(n); ++i) p[i] = {a[i], i}; sort(p, p + n); int num = 0; for (int i = 0; i < n;) { int start = i; while (i < n && p[i].first == p[start].first) { a[p[i].second] = num; i++; } num++; } } vector<vector<int> > cycles; vector<pair<int, int> > g[N]; int par[N]; void init() { for (int i = 1; i <= (int)(n); ++i) par[i] = i; } int find_set(int v) { if (v == par[v]) return v; return par[v] = find_set(par[v]); } void union_set(int a, int b) { a = find_set(a); b = find_set(b); par[a] = b; } bool used[N]; void find_all_cycles() { cycles.clear(); memset(used, 0, sizeof(used)); for (int i = 0; i < (int)(n); ++i) { if (!used[i] && to[i] != -1) { int pos = i; vector<int> cycle; while (used[pos] == 0) { used[pos] = 1; cycle.push_back(pos); g[a[pos]].emplace_back((int)(cycles).size(), pos); pos = to[pos]; } cycles.push_back(cycle); } } } int main(int argc, char **argv) { ios::sync_with_stdio(0); cin.tie(0); cin >> n >> s; for (int i = 0; i < (int)(n); ++i) cin >> a[i]; compress(); copy(a, a + n, b); sort(b, b + n); for (int i = 0; i < (int)(n); ++i) { if (a[i] != b[i]) all[a[i]].push_back(i); to[i] = -1; } for (int i = 0; i < (int)(n); ++i) { if (a[i] != b[i]) { int p = all[b[i]][pos[b[i]]++]; to[p] = i; cnt++; } } if (cnt > s) { cout << -1 << '\n'; return 0; } find_all_cycles(); init(); for (int i = 0; i < (int)(n); ++i) { if ((int)(g[i]).size() > 1) { pair<int, int> first = g[i][0]; for (auto &(p) : (g[i])) { if (find_set(first.first) != find_set(p.first)) { union_set(first.first, p.first); swap(to[first.second], to[p.second]); } } } } find_all_cycles(); int ans = (int)(cycles).size(); if (s > cnt) { int q = min((int)(cycles).size(), s - cnt); if (q >= 3) { ans += 2 - q; cout << ans << '\n'; vector<int> v; cout << q << '\n'; for (int i = 0; i < (int)(q); ++i) { v.push_back(cycles[i][0]); cout << cycles[i][0] + 1 << " \n"[i == q - 1]; } int cp = to[v.back()]; for (int i = (int)((int)(v).size() - 1); i >= (int)(1); --i) to[v[i]] = to[v[i - 1]]; to[v[0]] = cp; find_all_cycles(); } else { cout << ans << '\n'; } } else cout << ans << '\n'; for (auto &(p) : (cycles)) { cout << (int)(p).size() << '\n'; for (auto &(i) : (p)) cout << i + 1 << ' '; cout << '\n'; } }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using VI = vector<int>; const int NN = 200011; int a[NN], b[NN], arr[NN]; VI vec[NN], cyc[NN]; int nc; int dp[NN], vst[NN]; int n, s; void dfs(int u) { while (not vec[u].empty()) { int v = vec[u].back(); vec[u].pop_back(); dfs(a[v]); cyc[nc].push_back(v); } } int solve() { cin >> n >> s; for (int i = 1; i <= n; i++) scanf("%d", a + i), arr[i] = a[i], dp[i] = i; sort(arr + 1, arr + n + 1); int m = unique(arr + 1, arr + n + 1) - arr - 1; for (int i = 1; i <= n; i++) { b[i] = a[i] = lower_bound(arr + 1, arr + m + 1, a[i]) - arr; } sort(b + 1, b + n + 1); for (int i = 1; i <= n; i++) if (a[i] ^ b[i]) { vec[b[i]].push_back(i); } for (int i = 1; i <= m; i++) { if (not vec[i].empty()) { dfs(i); int r = ((int)cyc[nc].size()); for (int j = 0; j < r; j++) { dp[cyc[nc][(j + 1) % r]] = cyc[nc][j]; s--; } nc++; } if (s < 0) return puts("-1"); } s = min(s, nc); if (s >= 2) { cout << nc - s + 2 << endl; cout << s << endl; for (int i = 0; i < s; i++) printf("%d ", cyc[i][0]); puts(""); int tmp = dp[cyc[s - 1][0]]; for (int i = s; --i;) { dp[cyc[i][0]] = dp[cyc[i - 1][0]]; } dp[cyc[0][0]] = tmp; } else cout << nc << endl; for (int i = 1; i <= n; i++) { if (dp[i] == i or vst[i]) continue; VI ans; for (int u = i; not vst[u];) { ans.push_back(u); vst[u] = 1; u = dp[u]; } printf("%d\n", ans.size()); for (int u : ans) printf("%d ", u); puts(""); } } int main() { solve(); }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; template <class T, class U> void ckmin(T &a, U b) { if (a > b) a = b; } template <class T, class U> void ckmax(T &a, U b) { if (a < b) a = b; } const int MAXN = 400013; int N, S, M, ans, n; int val[MAXN], arr[MAXN], sorted[MAXN]; vector<int> moves[MAXN]; vector<int> cyc[MAXN]; bitset<MAXN> vis; vector<int> compress; int freq[MAXN]; pair<int, int> range[MAXN]; vector<int> edge[MAXN]; vector<int> tour; int ind[MAXN]; int indexof(vector<int> &v, int x) { return upper_bound((v).begin(), (v).end(), x) - v.begin() - 1; } void dfs(int u) { while (!edge[u].empty()) { int v = edge[u].back(); edge[u].pop_back(); dfs(v); } tour.push_back(u); } void solve() { int k = min(M, S - n); if (k <= 2) { ans = M; for (auto i = (0); i < (M); i++) { moves[i] = cyc[i]; } } else { ans = M - k + 2; for (auto i = (0); i < (M - k); i++) { moves[i] = cyc[i]; } for (auto i = (M - k); i < (M); i++) { moves[M - k].insert(moves[M - k].end(), (cyc[i]).begin(), (cyc[i]).end()); moves[M - k + 1].push_back(cyc[i][0]); } reverse((moves[M - k + 1]).begin(), (moves[M - k + 1]).end()); } } int32_t main() { cout << fixed << setprecision(12); cerr << fixed << setprecision(4); ios_base::sync_with_stdio(false); cin.tie(0); cin >> N >> S; for (auto i = (0); i < (N); i++) { cin >> val[i]; compress.push_back(val[i]); } sort((compress).begin(), (compress).end()); compress.erase(unique((compress).begin(), (compress).end()), compress.end()); for (auto i = (0); i < (N); i++) { val[i] = indexof(compress, val[i]); sorted[i] = val[i]; } sort(sorted, sorted + N); for (auto i = (0); i < (N); i++) { if (val[i] == sorted[i]) { vis[i] = true; arr[i] = i; continue; } edge[i].push_back(val[i] + N); edge[sorted[i] + N].push_back(i); } for (auto i = (0); i < (N); i++) { if (vis[i]) continue; tour.clear(); dfs(i); reverse((tour).begin(), (tour).end()); for (int j = 0; j + 2 < ((int)(tour).size()); j += 2) { int u = tour[j]; int v = tour[j + 2]; arr[u] = v; } tour.clear(); } vis.reset(); for (auto i = (0); i < (N); i++) { if (vis[i]) continue; if (arr[i] == i) { continue; } cyc[M].push_back(i); do { vis[cyc[M].back()] = true; cyc[M].push_back(arr[cyc[M].back()]); } while (cyc[M].back() != i); cyc[M].pop_back(); M++; } for (auto i = (0); i < (M); i++) { n += ((int)(cyc[i]).size()); } if (S < n) { cout << "-1\n"; return 0; } solve(); assert(ans == min(M, max(2, 2 + M - S + n))); cout << ans << '\n'; for (auto i = (0); i < (ans); i++) { cout << ((int)(moves[i]).size()) << '\n'; for (int x : moves[i]) { cout << x + 1 << " \n"[x == moves[i].back()]; } } }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int sz = 2e5 + 10; int to[sz], pr[sz], si[sz], cyq = 0; bool us[sz]; vector<int> cy[sz]; int find_se(int v) { if (v != pr[v]) pr[v] = find_se(pr[v]); return pr[v]; } void merge(int u, int v) { u = find_se(u), v = find_se(v); if (u != v) { if (si[v] > si[u]) swap(u, v); si[u] += si[v]; pr[v] = u; } } void pr_cy(vector<int> &ve) { printf("%d\n", ve.size()); for (int a = 0; a < ve.size(); a++) printf("%d ", ve[a] + 1); printf("\n"); } int main() { int n, s; cin >> n >> s; map<int, int> q; map<int, set<int> > se; int ar[n], n2 = 0; for (int a = 0; a < n; a++) { scanf("%d", &ar[a]); q[ar[a]]++; } for (auto it = q.begin(); it != q.end(); it++) { for (int a = 0; a < (*it).second; a++) { se[(*it).first].insert(n2), n2++; } } for (int a = 0; a < n; a++) { if (se[ar[a]].find(a) != se[ar[a]].end()) { se[ar[a]].erase(a), ar[a] = -1; } else s--; } if (s >= 0) { for (int a = 0; a < n; a++) { if (ar[a] != -1) { to[a] = *(se[ar[a]].begin()); se[ar[a]].erase(se[ar[a]].begin()); } } map<int, vector<int> > sp; for (int a = 0; a < n; a++) if (ar[a] != -1) sp[ar[a]].push_back(a); for (int a = 0; a < n; a++) { pr[a] = a, si[a] = 1; } for (int a = 0; a < n; a++) { if (ar[a] != -1 and us[a] == 0) { us[a] = 1; int cu = to[a]; while (us[cu] == 0) { merge(a, cu), us[cu] = 1, cu = to[cu]; } } } for (auto it = sp.begin(); it != sp.end(); it++) { vector<int> &ve = (*it).second; for (int a = 1; a < ve.size(); a++) { int u = find_se(ve[0]), v = find_se(ve[a]); if (u != v) { swap(to[ve[0]], to[ve[a]]); merge(u, v); } } } for (int a = 0; a < n; a++) us[a] = 0; for (int a = 0; a < n; a++) { if (ar[a] != -1 and us[a] == 0) { int cu = a; while (us[cu] == 0) { cy[cyq].push_back(cu), us[cu] = 1, cu = to[cu]; } cyq++; } } int yk = 0; vector<int> cy1, cy2; if (s >= 2 and cyq >= 2) { while (s and yk < cyq) { for (int b = 0; b < cy[yk].size(); b++) cy1.push_back(cy[yk][b]); cy2.push_back(cy[yk][0]); s--, yk++; } reverse(cy2.begin(), cy2.end()); } int an = cyq - yk; if (cy1.size()) an += 2; cout << an << "\n"; if (cy1.size()) { pr_cy(cy1), pr_cy(cy2); } while (yk < cyq) { pr_cy(cy[yk]), yk++; } } else cout << -1; }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> #pragma GCC optimize "-O3" using namespace std; const int MAXN = 210000; int was[MAXN]; int wss[MAXN]; vector<int> st; vector<vector<int>> vv; vector<vector<int>> vv2; int lb[MAXN]; int rb[MAXN]; set<int> ss; int pl[MAXN], cl[MAXN]; int pos[MAXN]; int n; void dfs1(int v) { was[v] = 1; st.push_back(v); if (was[pos[v]]) return; dfs1(pos[v]); } void dfs2(int v, int start) { int len = vv[v].size() / 2; for (int i = 0; i < len; ++i) ss.erase(vv[v][i]); wss[v] = 1; for (int i = start; i < start + len; ++i) { int x = vv[v][i]; st.push_back(x); auto it = ss.lower_bound(lb[x]); if (it != ss.end() && *it < rb[x]) { int u = *it; dfs2(cl[u], pl[u]); } } } int a[MAXN]; int b[MAXN]; int main() { ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); int s; cin >> n >> s; for (int i = 0; i < n; ++i) { cin >> a[i]; b[i] = a[i]; } sort(b, b + n); set<pair<int, int>> ss2; for (int i = 0; i < n; ++i) { if (a[i] == b[i]) pos[i] = i; else { ss2.insert({b[i], i}); } } for (int i = 0; i < n; ++i) { if (a[i] != b[i]) { auto it = ss2.lower_bound({a[i], 0}); pos[i] = it->second; ss2.erase(it); } } for (int i = 0; i < n; ++i) { lb[i] = lower_bound(b, b + n, a[i]) - b; rb[i] = lower_bound(b, b + n, a[i] + 1) - b; } for (int i = 0; i < n; ++i) { if (was[i]) continue; if (pos[i] == i) continue; st.clear(); dfs1(i); for (int j = 0; j < st.size(); ++j) cl[st[j]] = vv.size(), pl[st[j]] = j; vv.push_back(st); } for (int i = 0; i < vv.size(); ++i) { int x = vv[i].size(); for (int j = 0; j < x; ++j) vv[i].push_back(vv[i][j]); } for (int i = 0; i < n; ++i) { if (a[i] != b[i]) ss.insert(i); } for (int i = 0; i < vv.size(); ++i) { if (wss[i]) continue; st.clear(); dfs2(i, 0); vv2.push_back(st); } int sum = 0; for (int i = 0; i < vv2.size(); ++i) sum += vv2[i].size(); if (sum > s) { cout << -1 << "\n"; return 0; } int av = min((int)vv2.size(), s - sum); if (av <= 1) { cout << vv2.size() << "\n"; for (int i = 0; i < vv2.size(); ++i) { cout << vv2[i].size() << "\n"; for (int j : vv2[i]) cout << j + 1 << " "; cout << "\n"; } } else { cout << vv2.size() - (av - 1) + 1 << "\n"; int sum = 0; for (int i = 0; i < av; ++i) { sum += vv2[i].size(); } cout << sum << "\n"; for (int i = 0; i < av; ++i) { for (int j : vv2[i]) cout << j + 1 << " "; } cout << "\n"; for (int i = av; i < vv2.size(); ++i) { cout << vv2[i].size() << "\n"; for (int j : vv2[i]) cout << j + 1 << " "; cout << "\n"; } cout << av << "\n"; for (int i = av - 1; i >= 0; --i) cout << vv2[i][0] + 1 << " "; cout << "\n"; } return 0; }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int N, S; int A[200020], B[200020]; vector<int> vx; int pp[200020]; int Find(int x) { return pp[x] == x ? x : pp[x] = Find(pp[x]); } vector<int> vs[2][200020]; int nxt[200020]; int main() { scanf("%d%d", &N, &S); for (int i = 1; i <= N; i++) scanf("%d", A + i); for (int i = 1; i <= N; i++) vx.push_back(A[i]); sort(vx.begin(), vx.end()); vx.resize(unique(vx.begin(), vx.end()) - vx.begin()); for (int i = 1; i <= N; i++) A[i] = (int)(lower_bound(vx.begin(), vx.end(), A[i]) - vx.begin() + 1); for (int i = 1; i <= N; i++) B[i] = A[i]; sort(B + 1, B + 1 + N); int cnt = 0; for (int i = 1; i <= N; i++) if (B[i] != A[i]) ++cnt; if (cnt > S) { puts("-1"); return 0; } for (int i = 1; i <= N; i++) pp[i] = i; for (int i = 1; i <= N; i++) if (A[i] != B[i]) { vs[0][A[i]].push_back(i); vs[1][B[i]].push_back(i); } int L = (int)vx.size(); for (int i = 1; i <= L; i++) { int n = (int)vs[0][i].size(); for (int j = 0; j < n; j++) { int x = vs[0][i][j], y = vs[1][i][j]; nxt[x] = y; int px = Find(x), py = Find(y); if (px != py) pp[px] = py; } } for (int i = 1; i <= L; i++) { int n = (int)vs[0][i].size(); for (int j = 1; j < n; j++) { int x = vs[0][i][0]; int y = vs[0][i][j]; int px = Find(x), py = Find(y); if (px != py) { swap(nxt[x], nxt[y]); pp[px] = py; } } } int c[200020] = {}; for (int i = 1; i <= N; i++) c[Find(i)]++; vector<pair<int, int> > v; for (int i = 1; i <= N; i++) if (pp[i] == i && A[i] != B[i]) v.push_back(pair<int, int>(c[i], i)); sort(v.begin(), v.end()); reverse(v.begin(), v.end()); S -= cnt; vector<vector<int> > ans; if (S > 2 && (int)v.size() > 2) { int r = min(S, (int)v.size()); int save = nxt[v[0].second]; for (int i = 0; i < r - 1; i++) nxt[v[i].second] = nxt[v[i + 1].second]; nxt[v[r - 1].second] = save; vector<int> nv; for (int i = 0; i < r; i++) nv.push_back(v[i].second); reverse(nv.begin(), nv.end()); ans.push_back(nv); for (int i = 1; i < r; i++) pp[Find(v[i].second)] = Find(v[0].second); } for (int i = 1; i <= N; i++) if (A[i] != B[i] && pp[i] == i) { vector<int> v; v.push_back(i); for (int t = nxt[i]; t != i; t = nxt[t]) { v.push_back(t); } ans.push_back(v); } printf("%d\n", (int)ans.size()); for (auto e : ans) { printf("%d\n", (int)e.size()); for (int f : e) printf("%d ", f); puts(""); } return 0; }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int n, S, a[200010], fa[200010]; inline int getf(int x) { return x == fa[x] ? x : fa[x] = getf(fa[x]); } inline void merge(int x, int y) { x = getf(x); y = getf(y); if (x != y) fa[x] = y; } pair<int, int> p[200010]; class node { public: int vl, pos, id; bool operator<(const node &t) const { return vl < t.vl; } }; int rk[200010], rr[200010]; bool vis[200010]; int main() { scanf("%d%d", &n, &S); for (int i = 1; i <= n; i++) { scanf("%d", &a[i]); p[i] = make_pair(a[i], i); } sort(p + 1, p + n + 1); iota(fa + 1, fa + n + 1, 1); int cnt = 0; for (int i = 1; i <= n; i++) if (a[i] != p[i].first) cnt++; if (cnt > S) { puts("-1"); return 0; } vector<node> v; v.push_back((node){0, 0, 0}); for (int i = 1; i <= n; i++) { if (a[i] != p[i].first) { v.push_back((node){a[i], 0, i}); v.back().pos = v.size() - 1; } } sort(v.begin() + 1, v.end()); int s = v.size() - 1; for (int i = 1; i <= s; i++) rk[v[i].pos] = i, rr[v[i].pos] = v[i].id; for (int i = 1; i <= s; i++) merge(i, rk[i]); for (int i = 2; i <= s; i++) { if (v[i].vl == v[i - 1].vl && getf(i) != getf(i - 1)) { swap(rk[v[i].pos], rk[v[i - 1].pos]); merge(i - 1, i); } } vector<vector<int> > cycle; for (int i = 1; i <= s; i++) { if (!vis[i]) { cycle.push_back(vector<int>({rr[i]})); vis[i] = 1; for (int j = rk[i]; j != i; j = rk[j]) { cycle.back().push_back(rr[j]); vis[j] = 1; } } } cnt = min((int)cycle.size(), S - s); vector<vector<int> > opt; for (int i = cnt; i < cycle.size(); i++) opt.push_back(cycle[i]); if (cnt >= 2) { opt.push_back(vector<int>()); for (int i = 0; i < cnt; i++) opt.back().insert(opt.back().end(), cycle[i].begin(), cycle[i].end()); opt.push_back(vector<int>()); for (int i = 0; i < cnt; i++) opt.back().push_back(cycle[i].front()); reverse(opt.back().begin(), opt.back().end()); } else { for (int i = 0; i < cnt; i++) opt.push_back(cycle[i]); } printf("%u\n", opt.size()); for (auto &x : opt) { printf("%u\n", x.size()); for (auto &y : x) printf("%d ", y); puts(""); } return 0; }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5; int n, s; int a[maxn], b[maxn]; int idx[maxn]; bool CmpI(int i, int j) { return a[i] < a[j]; } int p[maxn]; int Find(int x) { return x == p[x] ? x : p[x] = Find(p[x]); } void Unite(int x, int y) { p[Find(x)] = Find(y); } int _1[maxn], _2[maxn]; void Pre(void) { int cnt = 0; for (int i = 0; i < n; ++i) { if (a[i] != b[i]) idx[cnt++] = i; } sort(idx, idx + cnt, CmpI); int cur = 0; for (int i = 0; i < n; ++i) { if (a[i] != b[i]) _1[idx[cur++]] = i; else _1[i] = i; } } int FindN(int i) { for (int j = i + 1; j < n; ++j) { if (a[j] == b[j]) continue; if (b[j] != b[i]) break; return j; } return -1; } void Solve(void) { for (int i = 0; i < n; ++i) p[i] = i; Pre(); for (int i = 0; i < n; ++i) Unite(i, _1[i]); for (int i = 0; i < n; ++i) _2[_1[i]] = i; for (int i = 0; i < n; ++i) { if (a[i] == b[i]) continue; int j = FindN(i); if (j != -1) { if (Find(i) != Find(j)) { Unite(i, j); int i2 = _2[i], j2 = _2[j]; _1[i2] = j; _1[j2] = i; _2[i] = j2; _2[j] = i2; } } } } int cnt_cycle, first[maxn]; bool vis[maxn]; void FindCycles(void) { cnt_cycle = 0; fill(first, first + n, -1); for (int i = 0; i < n; ++i) { if (a[i] == b[i]) continue; if (!vis[i]) { for (int u = _1[i];; u = _1[u]) { vis[u] = true; if (u == i) break; } first[cnt_cycle++] = i; } } } int main(void) { scanf("%d%d", &n, &s); for (int i = 0; i < n; ++i) { scanf("%d", a + i); } copy(a, a + n, b); sort(b, b + n); int self = 0; for (int i = 0; i < n; ++i) { self += a[i] == b[i]; } Solve(); int least = n - self; if (least > s) { puts("-1"); return 0; } FindCycles(); int merge = min(cnt_cycle, s - least); if (merge <= 2) { merge = 0; } vector<vector<int> > ans; if (merge) { ans.push_back(vector<int>()); for (int i = 0; i < merge; ++i) { ans.back().push_back(first[i]); } ans.push_back(vector<int>()); for (int i = merge - 1; i >= 0; --i) { for (int u = _1[first[i]];; u = _1[u]) { ans.back().push_back(u); if (u == first[i]) break; } } } for (int i = merge; i < cnt_cycle; ++i) { ans.push_back(vector<int>()); for (int u = _1[first[i]];; u = _1[u]) { ans.back().push_back(u); if (u == first[i]) break; } } printf("%d\n", (int)ans.size()); for (auto v : ans) { printf("%d\n", (int)v.size()); for (auto p : v) { printf("%d ", p + 1); } puts(""); } }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const long long inf = 1e18; vector<bool> vis; vector<pair<long long, long long>> eulerWalk( vector<vector<pair<long long, long long>>>& gr, vector<long long>& eu, int src, vector<long long>& D, vector<long long>& its) { vector<pair<long long, long long>> ret, s = {{src, -1}}; D[src]++; while (!s.empty()) { auto [x, id] = s.back(); int y, e; long long& it = its[x]; int end = (int)gr[x].size(); vis[x] = 1; if (it == end) { ret.emplace_back(x, id); s.pop_back(); continue; } tie(y, e) = gr[x][it++]; if (!eu[e]) { D[x]--, D[y]++; eu[e] = 1; s.emplace_back(y, e); } } return {ret.rbegin(), ret.rend()}; } int main() { cin.tie(0); cin.sync_with_stdio(0); (cout << fixed).precision(15); int n; int s; cin >> n >> s; vector<long long> v(n); for (auto& i : (v)) cin >> i; vector<long long> o = v; sort(begin(o), end(o)); map<int, int> conv; int k = 0; auto get_E = [&]() { conv.clear(); vector<vector<pair<long long, long long>>> E; auto get = [&](int u) { if (conv.count(u) == 0) { conv[u] = conv.size(); E.emplace_back(); } return conv[u]; }; for (int i = (0); i < int(n); ++i) if (o[i] != v[i]) { ++k; int a = get(o[i]); int b = get(v[i]); E[a].emplace_back(b, i); } return E; }; vector<vector<long long>> ans = {{}}; auto E = get_E(); set<int> seen; for (int i = (0); i < int(n); ++i) if (v[i] != o[i] && !seen.count(conv[v[i]])) { ans[0].push_back(i); queue<int> q; q.push(conv[v[i]]); seen.insert(conv[v[i]]); while (q.size()) { int u = q.front(); q.pop(); for (auto& w : (E[u])) if (!seen.count(w.first)) { q.push(w.first); seen.insert(w.first); } } } while (ans[0].size() && (int)ans[0].size() + k > s) ans[0].pop_back(); if (ans[0].size() > 2) { for (int i = (1); i < int(ans[0].size()); ++i) swap(v[ans[0][i - 1]], v[ans[0][i]]); reverse(begin(ans[0]), end(ans[0])); s -= ans[0].size(); } else { ans = {}; } E = get_E(); vis.assign(E.size(), 0); vector<long long> used(n); int m = E.size(); vector<long long> D(m), its(m); for (int i = (0); i < int(E.size()); ++i) if (!vis[i]) { auto t = eulerWalk(E, used, i, D, its); t.erase(t.begin()); s -= t.size(); ans.emplace_back(); for (auto& i : (t)) ans.back().push_back(i.second); } if (s < 0) { cout << -1 << endl; } else { cout << ans.size() << endl; for (auto& i : (ans)) { cout << i.size() << endl; for (auto& j : (i)) cout << j + 1 << " "; cout << endl; } } }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int a[200010], b[200010], nxt[200010]; bool vis[200010]; int main() { ios::sync_with_stdio(0); cin.tie(0); int n, s; cin >> n >> s; for (int i = 1; i <= n; i++) cin >> a[i], b[i] = a[i]; sort(b + 1, b + n + 1); vector<int> v; for (int i = 1; i <= n; i++) if (a[i] != b[i]) v.push_back(i); if (v.size() > s) { puts("-1"); return 0; } vector<int> u = v; sort(v.begin(), v.end(), [&](int i, int j) { return a[i] < a[j]; }); for (int i = 0; i < v.size(); i++) nxt[v[i]] = u[i]; vector<int> f(n + 1); for (int i = 1; i <= n; i++) f[i] = i; function<int(int)> find = [&](int x) { return x == f[x] ? x : f[x] = find(f[x]); }; for (int i = 0; i < v.size(); i++) { int x = find(u[i]), y = find(v[i]); f[x] = y; } for (int i = 0, prv = -1; i < v.size(); i++) { if (a[v[i]] == prv) { int x = find(v[i]), y = find(v[i - 1]); if (x != y) { swap(nxt[v[i]], nxt[v[i - 1]]); f[x] = y; } } prv = a[v[i]]; } vector<vector<int> > R; for (int i = 0; i < v.size(); i++) { int x = v[i]; if (vis[x]) continue; vector<int> r; while (vis[x] == 0) r.push_back(x), vis[x] = 1, x = nxt[x]; R.push_back(r); } int k = min(s - u.size(), R.size()); if (k <= 2) k = 0; int ans = R.size() - k; if (k > 2) ans += 2; cout << ans << endl; if (k > 2) { cout << 2 * k << endl; for (int i = 0; i < k; i++) cout << R[i][0] << ' ' << R[i][1] << ' '; cout << endl; int sum = 0; for (int i = 0; i < k; i++) sum += R[i].size() - 1; cout << sum << endl; for (int i = k - 1; i >= 0; i--) { for (int j = 2; j < R[i].size(); j++) cout << R[i][j] << ' '; cout << R[i][0] << ' '; } cout << endl; } for (int i = k; i < R.size(); i++) { cout << R[i].size() << endl; for (int j = 0; j < R[i].size(); j++) cout << R[i][j] << ' '; cout << endl; } }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; template <typename T> inline void ckmax(T& x, T y) { x = (y > x ? y : x); } template <typename T> inline void ckmin(T& x, T y) { x = (y < x ? y : x); } const int MAXN = 2e5; int n, lim, a[MAXN + 5], goal[MAXN + 5]; int vals[MAXN + 5], cnt_val; int need_oper_pos; vector<vector<int> > ans; int fa[MAXN + 5], sz[MAXN + 5]; int last_edge[MAXN + 5]; int get_fa(int u) { return (u == fa[u]) ? u : (fa[u] = get_fa(fa[u])); } void unite(int u, int v, int e) { int fu = get_fa(u); int fv = get_fa(v); if (fu != fv) { if (sz[fu] > sz[fv]) swap(fu, fv); fa[fu] = fv; sz[fv] += sz[fu]; } last_edge[fu] = e; } struct EDGE { int nxt, to, eid; } edge[MAXN + 5]; int head[MAXN + 5], tot; inline void add_edge(int u, int v, int eid) { edge[++tot].nxt = head[u]; edge[tot].to = v; edge[tot].eid = eid; head[u] = tot; } vector<int> pos; void dfs(int u) { for (int& i = head[u]; i;) { int v = edge[i].to; int eid = edge[i].eid; i = edge[i].nxt; dfs(v); pos.push_back(eid); } } int main() { cin >> n >> lim; for (int i = 1; i <= n; ++i) { cin >> a[i]; vals[++cnt_val] = a[i]; } sort(vals + 1, vals + cnt_val + 1); for (int i = 1; i <= n; ++i) goal[i] = vals[i]; cnt_val = unique(vals + 1, vals + cnt_val + 1) - (vals + 1); for (int i = 1; i <= cnt_val; ++i) { fa[i] = i; sz[i] = 1; } for (int i = 1; i <= n; ++i) { a[i] = lower_bound(vals + 1, vals + cnt_val + 1, a[i]) - vals; goal[i] = lower_bound(vals + 1, vals + cnt_val + 1, goal[i]) - vals; if (a[i] != goal[i]) { unite(a[i], goal[i], i); need_oper_pos++; } } if (need_oper_pos > lim) { cout << -1 << endl; return 0; } int rest = lim - need_oper_pos; if (rest >= 2) { for (int i = 1; i <= cnt_val; ++i) { if (get_fa(i) == i && last_edge[i] != 0) { pos.push_back(last_edge[i]); if (((int)(pos).size()) == rest) break; } } if (((int)(pos).size()) >= 2) { ans.push_back(pos); int last_val = a[pos.back()]; for (int i = ((int)(pos).size()) - 1; i >= 1; --i) { a[pos[i]] = a[pos[i - 1]]; } a[pos[0]] = last_val; } } for (int i = 1; i <= n; ++i) { if (a[i] != goal[i]) { add_edge(a[i], goal[i], i); } } for (int i = 1; i <= n; ++i) { if (head[i]) { vector<int>().swap(pos); dfs(i); ans.push_back(pos); } } cout << ((int)(ans).size()) << endl; for (int i = 0; i < ((int)(ans).size()); ++i) { cout << ((int)(ans[i]).size()) << endl; for (int j = 0; j < ((int)(ans[i]).size()); ++j) { cout << ans[i][j] << " \n"[j == ((int)(ans[i]).size()) - 1]; } } return 0; }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 10; int dsu[N]; int rang[N]; int pred(int a) { if (a == dsu[a]) return a; return dsu[a] = pred(dsu[a]); } void unite(int a, int b) { a = pred(a); b = pred(b); if (a != b) { if (rang[a] < rang[b]) { swap(a, b); } dsu[b] = a; rang[a] += rang[b]; } } bool connected(int a, int b) { return pred(a) == pred(b); } bool used[N]; int color; int t; int p[N]; vector<int> colors[N]; void dfs(int v) { used[v] = 1; colors[t].push_back(v); if (!used[p[v]]) { dfs(p[v]); } } signed main() { int n, s; cin >> n >> s; for (int i = 0; i < n; i++) { dsu[i] = i; rang[i] = 1; } vector<int> a(n); vector<pair<int, int> > b; for (int i = 0; i < n; i++) { cin >> a[i]; b.push_back({a[i], i}); } vector<int> ind(n); sort(b.begin(), b.end()); for (int i = 0; i < n; i++) { p[b[i].second] = i; } for (int i = 0; i < n; i++) { if (a[i] == b[i].first && p[i] != i) { p[b[i].second] = p[i]; b[p[i]].second = b[i].second; p[i] = i; b[i].second = i; } } for (int i = 0; i < n; i++) { unite(p[i], i); } int it = -1; for (int i = 0; i < n; i++) { if (p[b[i].second] == b[i].second) { continue; } if (it >= 0 && a[it] == a[b[i].second]) { int x = it; int y = b[i].second; if (!connected(x, y)) { unite(x, y); swap(p[x], p[y]); } } it = b[i].second; } t = 0; for (int i = 0; i < n; i++) { if (!used[i] && p[i] != i) { dfs(i); t++; } } int cnt = 0; for (int i = 0; i < t; i++) { cnt += colors[i].size(); } if (cnt > s) { cout << -1; return 0; } s -= cnt; s = min(s, t); if (s <= 1) { cout << t << "\n"; for (int i = 0; i < t; i++) { cout << colors[i].size() << "\n"; for (int j = 0; j < colors[i].size(); j++) { cout << colors[i][j] + 1 << " "; } cout << "\n"; } return 0; } cout << (t - s + 2) << "\n"; for (int i = 0; i < t - s; i++) { cout << colors[i + s].size() << "\n"; for (int j = 0; j < colors[i + s].size(); j++) { cout << colors[i + s][j] + 1 << " "; } cout << "\n"; cnt -= colors[i + s].size(); } cout << cnt << "\n"; for (int i = 0; i < s; i++) { for (int j = 0; j < colors[i].size(); j++) { cout << colors[i][j] + 1 << " "; } } cout << "\n"; cout << s << "\n"; for (int i = s - 1; i >= 0; i--) { cout << colors[i][0] + 1 << " "; } return 0; }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int inf = 1e9 + 7; string to_string(string s) { return '"' + s + '"'; } string to_string(char s) { return string(1, s); } string to_string(const char *s) { return to_string((string)s); } string to_string(bool b) { return (b ? "true" : "false"); } template <typename A> string to_string(A); template <typename A, typename B> string to_string(pair<A, B> p) { return "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; } template <typename A> string to_string(A v) { bool f = 1; string r = "{"; for (const auto &x : v) { if (!f) r += ", "; f = 0; r += to_string(x); } return r + "}"; } void debug_out() { cout << endl; } template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cout << " " << to_string(H); debug_out(T...); } struct DSU { vector<int> p, r; vector<map<int, int>> nxt; int num; DSU(int n) : p(n), r(n), nxt(n), num(0) { for (auto i = (0); i <= (n - 1); ++i) p[i] = i; } int get(int i) { if (p[i] != i) p[i] = get(p[i]); return p[i]; } int connect(int i, int j) { int x = get(i), y = get(j); if (x == y) return 0; --num; if (r[x] > r[y]) swap(x, y); p[x] = y; for (auto it : nxt[x]) { nxt[y].insert(it); } assert(nxt[y].count(i) and nxt[y].count(j)); swap(nxt[y][i], nxt[y][j]); r[y] += r[x]; return 1; } }; int main() { ios::sync_with_stdio(0); cin.tie(0); srand(8); int n, s; cin >> n >> s; vector<int> a(n); for (auto i = (0); i <= (n - 1); ++i) { cin >> a[i]; } vector<int> b = a; sort((b).begin(), (b).end()); map<int, vector<int>> pos, gpos; for (auto i = (0); i <= (n - 1); ++i) if (a[i] != b[i]) { pos[b[i]].push_back(i); gpos[a[i]].push_back(i); } vector<int> vis(n); DSU ds(n); int len = 0; for (auto i = (0); i <= (n - 1); ++i) if (!vis[i] and a[i] != b[i]) { int x = i; int cur = 0; do { vis[x] = 1; int y = pos[a[x]].back(); pos[a[x]].pop_back(); ds.p[x] = i; ds.nxt[i][x] = y; x = y; ++cur; ++len; } while (x != i); ds.num++; ds.r[i] = cur; } for (auto it : gpos) { const auto &A = it.second; for (auto i = (1); i <= (int(A.size()) - 1); ++i) { ds.connect(A[i], A[i - 1]); } } vector<vector<int>> cycles; for (auto i = (0); i <= (n - 1); ++i) if (a[i] != b[i] and ds.get(i) == i) { cycles.emplace_back(); int x = i; do { cycles.back().emplace_back(x); x = ds.nxt[i][x]; } while (x != i); } s -= len; if (s < 0) { cout << "-1\n"; return 0; } int x = min(s, int(cycles.size())); vector<vector<int>> ans; if (x > 1) { ans.emplace_back(); ans.emplace_back(); for (auto i = (0); i <= (x - 1); ++i) { for (int j : cycles[i]) ans[0].emplace_back(j); ans[1].emplace_back(cycles[i][0]); } reverse((ans[1]).begin(), (ans[1]).end()); for (auto i = (x); i <= (int(cycles.size()) - 1); ++i) ans.emplace_back(cycles[i]); } else ans = cycles; cout << int(ans.size()) << "\n"; for (auto it : ans) { cout << int(it.size()) << "\n"; for (auto itt : it) cout << itt + 1 << " "; cout << "\n"; } return 0; }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int n, k; int a[200010]; int b[200010]; vector<pair<int, int> > graf[200010]; vector<int> tura; vector<int> sz; vector<vector<int> > sol; vector<vector<int> > rj; vector<int> r; int bio[200010]; int siz = 0; void dfs(int x) { bio[x] = 1; while (!graf[x].empty()) { int sus = graf[x][(int)graf[x].size() - 1].first; int br = graf[x][(int)graf[x].size() - 1].second; graf[x].pop_back(); dfs(sus); tura.push_back(br); } } void sazimanje() { for (int i = 1; i <= n; i++) sz.push_back(a[i]); sort(sz.begin(), sz.end()); sz.erase(unique(sz.begin(), sz.end()), sz.end()); siz = (int)sz.size() - 1; for (int i = 1; i <= n; i++) { a[i] = lower_bound(sz.begin(), sz.end(), a[i]) - sz.begin(); b[i - 1] = a[i]; } } int main() { scanf("%d%d", &n, &k); for (int i = 1; i <= n; i++) { scanf("%d", &a[i]); } sazimanje(); sort(b, b + n); int cnt = 0; for (int i = n - 1; i >= 0; i--) { b[i + 1] = b[i]; b[i] = 0; if (a[i + 1] != b[i + 1]) { cnt++; graf[b[i + 1]].push_back(make_pair(a[i + 1], i + 1)); } } if (cnt > k) { printf("-1"); return 0; } for (int i = 1; i <= siz; i++) { if (bio[i] == 0) { tura.clear(); dfs(i); reverse(tura.begin(), tura.end()); if (tura.size() >= 1) sol.push_back(tura); } } int sad = min((int)sol.size(), k - cnt); if (sad > 1) { for (int i = sad - 1; i >= 0; i--) { r.push_back(sol[i].back()); } rj.push_back(r); r.clear(); for (int i = 0; i < sad; i++) { for (int j = 0; j < sol[i].size(); j++) { r.push_back(sol[i][j]); } } rj.push_back(r); for (int i = sad; i < sol.size(); i++) { rj.push_back(sol[i]); } swap(sol, rj); } printf("%d\n", sol.size()); for (int i = 0; i < sol.size(); i++) { printf("%d\n", sol[i].size()); for (int j = 0; j < sol[i].size(); j++) printf("%d ", sol[i][j]); printf("\n"); } return 0; }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using namespace chrono; const int infinity = (int)1e9 + 42; const int64_t llInfinity = (int64_t)1e18 + 256; const int module = (int)1e9 + 7; const long double eps = 1e-8; mt19937_64 randGen(system_clock().now().time_since_epoch().count()); inline void raiseError(string errorCode) { cerr << "Error : " << errorCode << endl; exit(42); } signed main() { ios_base::sync_with_stdio(false); int n, s; cin >> n >> s; vector<int> v(n); map<int, int> kompr; for (int i = 0; i < n; i++) { cin >> v[i]; kompr[v[i]] = 42; } int k = 0; for (auto &it : kompr) { it.second = k++; } for (int i = 0; i < n; i++) { v[i] = kompr[v[i]]; } auto w = v; sort(w.begin(), w.end()); vector<vector<int> > g(k); map<pair<int, int>, vector<int> > indices; for (int i = 0; i < n; i++) { if (w[i] == v[i]) { continue; } s--; g[w[i]].push_back(v[i]); indices[{w[i], v[i]}].push_back(i); } if (s < 0) { cout << -1 << "\n"; return 0; } vector<int> pass; function<void(int)> dfs = [&](int v) { while (!g[v].empty()) { int to = g[v].back(); g[v].pop_back(); dfs(to); } pass.push_back(v); }; vector<vector<int> > ans; for (int i = 0; i < k; i++) { if (g[i].empty()) { continue; } pass.clear(); dfs(i); ans.emplace_back(); for (int j = 1; j < (int)pass.size(); j++) { auto &vec = indices[{pass[j], pass[j - 1]}]; assert(!vec.empty()); ans.back().push_back(vec.back()); vec.pop_back(); } reverse(ans.back().begin(), ans.back().end()); } int canUnite = min(s, (int)ans.size()); if (canUnite > 2) { vector<int> united; vector<int> loop2; for (int i = 0; i < canUnite; i++) { united.insert(united.end(), ans.back().begin(), ans.back().end()); loop2.push_back(ans.back()[0]); ans.pop_back(); } ans.push_back(united); reverse(loop2.begin(), loop2.end()); ans.push_back(loop2); } cout << ans.size() << "\n"; for (auto vec : ans) { cout << vec.size() << "\n"; for (auto it : vec) { cout << it + 1 << " "; } cout << "\n"; } return 0; }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int N, S, cnt; int A[200005], A2[200005]; bool Use[200005]; int P[200005]; map<int, int> X; int cyc, R[200005], TT[200005]; vector<int> V[200005], Cycle[200005]; void Read() { scanf("%d%d", &N, &S); for (int i = 1; i <= N; i++) { scanf("%d", &A[i]); A2[i] = A[i]; } sort(A + 1, A + N + 1); } void rebuildA() { cnt = 0; for (int i = 1; i <= N; i++) { if (A[i] != A[i - 1]) cnt++; X[A[i]] = cnt; } for (int i = 1; i <= N; i++) { A[i] = X[A[i]]; A2[i] = X[A2[i]]; if (A[i] == A2[i]) { P[i] = i; Use[i] = 1; } } for (int i = 1; i <= N; i++) { if (P[i] == 0) V[A2[i]].push_back(i); } int curr = 1; for (int i = 1; i <= cnt; i++) { for (int j = 0; j < V[i].size(); j++) { int pos = V[i][j]; while (Use[curr] == 1) ++curr; Use[curr] = 1; P[pos] = curr; } } } void Unite(int x, int y) { if (x == y) return; if (R[x] < R[y]) { TT[x] = y; } else TT[y] = x; if (R[x] == R[y]) ++R[x]; } int Father(int x) { int init = x; while (x != TT[x]) { x = TT[x]; } while (init != x) { int next = TT[init]; TT[init] = x; init = next; } return x; } void buildCycle(int ind) { cyc = 0; for (int i = 1; i <= N; i++) { Use[i] = 0; Cycle[i].clear(); } for (int i = 1; i <= N; i++) { if (P[i] == i) continue; if (Use[i] == 0) { ++cyc; int pos = i; while (Use[pos] == 0) { Use[pos] = 1; Cycle[cyc].push_back(pos); if (ind == 0) TT[pos] = i; pos = P[pos]; } } } } void findP() { for (int i = 1; i <= cnt; i++) { for (int j = 1; j < V[i].size(); j++) { int pos = V[i][j]; int prev = V[i][j - 1]; if (Father(pos) != Father(prev)) { Unite(Father(pos), Father(prev)); swap(P[pos], P[prev]); } } } buildCycle(1); } void Solve() { int c = 0, t = 0; for (int i = 1; i <= N; i++) { if (P[i] == i) continue; ++t; if (TT[i] == i) ++c; } if (S < t) { printf("-1\n"); return; } int x, y; y = min(c, S - t); x = c - y; printf("%d\n", x + min(y, 2)); if (y <= 1) x = c; for (int i = 1; i <= x; i++) { printf("%d\n", (int)Cycle[i].size()); for (int j = 0; j < Cycle[i].size(); j++) { printf("%d ", Cycle[i][j]); } printf("\n"); } if (y >= 2) { int sum = 0; for (int i = x + 1; i <= cyc; i++) { sum += (int)Cycle[i].size(); } printf("%d\n", sum); for (int i = x + 1; i <= cyc; i++) { for (int j = 0; j < Cycle[i].size(); j++) printf("%d ", Cycle[i][j]); } printf("\n"); printf("%d\n", y); for (int i = cyc; i >= x + 1; i--) printf("%d ", Cycle[i][0]); printf("\n"); } } int main() { Read(); rebuildA(); buildCycle(0); findP(); Solve(); return 0; }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 25; int a[N], b[N], c[N], cycsz; vector<int> g[N], cyc[N]; void dfs(int v) { while (!g[v].empty()) { int u = g[v].back(); g[v].pop_back(); dfs(a[u]); cyc[cycsz].push_back(u); } } int main() { ios::sync_with_stdio(false); cin.tie(NULL); cout << setprecision(32); int n, s; cin >> n >> s; for (int i = 0; i < n; i++) { cin >> a[i]; b[i] = a[i]; } sort(b, b + n); memcpy(c, b, sizeof(int) * n); int sz = unique(c, c + n) - c; for (int i = 0; i < n; i++) { a[i] = lower_bound(c, c + sz, a[i]) - c; b[i] = lower_bound(c, c + sz, b[i]) - c; if (a[i] == b[i]) continue; g[b[i]].push_back(i); } cycsz = 0; for (int i = 0; i < sz; i++) { if (g[i].empty()) continue; dfs(i); cycsz++; } int sum = 0; for (int i = 0; i < cycsz; i++) { sum += cyc[i].size(); reverse(cyc[i].begin(), cyc[i].end()); } if (sum > s) { cout << -1 << '\n'; exit(0); } int x = min(cycsz, s - sum); vector<vector<int> > ans; if (x) { vector<int> perm1, perm2; for (int i = 0; i < x; i++) { for (auto y : cyc[i]) { perm1.push_back(y); } perm2.push_back(cyc[i][0]); } reverse(perm2.begin(), perm2.end()); ans.push_back(perm1); if (x > 1) ans.push_back(perm2); } for (int i = x; i < cycsz; i++) { ans.push_back(cyc[i]); } cout << ans.size() << '\n'; for (auto vec : ans) { cout << vec.size() << '\n'; for (auto y : vec) { cout << y + 1 << " "; } cout << '\n'; } return 0; }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int const MAX = 4e5 + 41; int n, s; int a[MAX]; int b[MAX]; int c[MAX]; vector<int> e[MAX]; vector<vector<int> > ans; set<int> poses[MAX]; int cnt; int u[MAX]; int lp[MAX]; int rp[MAX]; vector<int> getposes(vector<int> st) { reverse(st.begin(), st.end()); vector<int> res; for (int i = 0; i <= (int)st.size() - 2; i++) { int x = st[i]; int y = st[(i + 1)]; assert((int)poses[y].size()); int p = (*poses[y].lower_bound(lp[x])); assert(lp[x] <= p && rp[x] >= p); res.push_back(p); poses[y].erase(p); } return res; } vector<int> st; void dfs(int x) { u[x] = 1; while ((int)e[x].size()) { int y = e[x].back(); e[x].pop_back(); dfs(y); } st.push_back(x); } void go(int *a, vector<int> v) { int vn = a[v.back()]; for (int i = (int)v.size() - 1; i >= 1; i--) { a[v[i]] = a[v[i - 1]]; } a[v[0]] = vn; } void proceed(int *a, vector<vector<int> > ans) { for (int i = 0; i <= (int)ans.size() - 1; i++) { go(a, ans[i]); } } void solve() { vector<int> v; for (int i = 1; i <= n; i++) { v.push_back(a[i]); } sort(v.begin(), v.end()); v.resize(unique(v.begin(), v.end()) - v.begin()); for (int i = 1; i <= n; i++) { b[i] = (int)(lower_bound(v.begin(), v.end(), a[i]) - v.begin()); } memcpy(c, b, sizeof(c)); sort(c + 1, c + n + 1); for (int i = 1; i <= n; i++) { rp[c[i]] = i; } for (int i = n; i >= 1; i--) { lp[c[i]] = i; } for (int i = 1; i <= n; i++) { if (b[i] == c[i]) continue; e[c[i]].push_back(b[i]); poses[b[i]].insert(i); } vector<vector<int> > cycles; for (int i = 0; i <= (int)v.size() - 1; i++) { if ((int)e[i].size()) { st.clear(); dfs(i); cycles.push_back(st); } } int sum = 0; for (int i = 0; i <= (int)cycles.size() - 1; i++) { sum += (int)cycles[i].size() - 1; } if (sum > s) { printf("-1\n"); return; } int rem = s - sum - 1; if (rem) { rem = min(rem, (int)cycles.size() - 1); } vector<int> swaps; if (rem) { for (int i = 0; i <= rem; i++) { int pos = (*poses[cycles[i][0]].begin()); swaps.push_back(pos); } } if ((int)swaps.size()) { go(b, swaps); ans.push_back(swaps); } for (int i = 0; i <= (int)v.size() - 1; i++) { poses[i].clear(); } for (int i = 1; i <= n; i++) { if (b[i] == c[i]) continue; e[c[i]].push_back(b[i]); poses[b[i]].insert(i); } int cnt = 0; for (int i = 0; i <= (int)v.size() - 1; i++) { if ((int)e[i].size()) { cnt++; st.clear(); dfs(i); ans.push_back(getposes(st)); } } printf("%d\n", (int)ans.size()); for (int i = 0; i <= (int)ans.size() - 1; i++) { printf("%d\n", (int)ans[i].size()); for (int x : ans[i]) { printf("%d ", x); } printf("\n"); } } int main() { scanf("%d %d", &n, &s); for (int i = 1; i <= n; i++) { scanf("%d", &a[i]); } solve(); return 0; }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int n, K, a[200100]; map<int, int> mp1; map<int, vector<int> > mp2; vector<vector<int> > ans; vector<int> vec; void dfs(int x) { for (int i; !mp2[x].empty(); vec.push_back(i)) { i = mp2[x].back(); mp2[x].pop_back(); dfs(a[i]); } mp2.erase(x); } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> K; for (int i = 1; i <= n; ++i) cin >> a[i], mp1[a[i]]++; int sum = 1; for (auto i : mp1) { for (int j = sum; j < sum + i.second; ++j) if (a[j] != i.first) mp2[i.first].push_back(j); sum += i.second; } for (; !mp2.empty();) { vec.clear(); dfs(mp2.begin()->first); reverse(vec.begin(), vec.end()); ans.push_back(vec); K -= vec.size(); } if (K < 0) { cout << -1 << '\n'; return 0; } K = min(K, (int)ans.size()); cout << (ans.size() - max(0, K - 2)) << '\n'; if (K >= 3) { int len = 0; for (int i = 0; i < K; ++i) len += ans[i].size(); cout << len << '\n'; for (int i = 0; i < K; ++i) for (int j : ans[i]) cout << j << ' '; cout << '\n' << K << '\n'; for (int i = K - 1; ~i; --i) cout << ans[i][0] << ' '; cout << '\n'; } else K = 0; for (int i = K; i < (int)ans.size(); ++i) { cout << ans[i].size() << '\n'; for (int j : ans[i]) cout << j << ' '; cout << '\n'; } return 0; }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int n, s, a[200020], b[200020], p[200020]; bool cmp(int i, int j) { return a[i] < a[j]; } int rt[200020]; int findrt(int x) { if (rt[x] != x) rt[x] = findrt(rt[x]); return rt[x]; } int get(int x) { return lower_bound(b + 1, b + n + 1, x) - b; } map<int, int> S; int c[200020]; bool vis[200020]; int tot; vector<int> ans[200020]; int q[200020]; int main() { cin >> n >> s; for (int i = 1; i <= n; i++) scanf("%d", &a[i]), b[i] = a[i]; sort(b + 1, b + n + 1); int cnt = n; for (int i = 1; i <= n; i++) if (a[i] == b[i]) q[i] = i, cnt--; if (cnt > s) { puts("-1"); return 0; } vector<int> v; for (int i = 1; i <= n; i++) if (!q[i]) v.push_back(i); sort(v.begin(), v.end(), cmp); for (int i = 1, j = 0; i <= n; i++) { if (q[i]) continue; q[i] = v[j]; j++; } for (int i = 1; i <= n; i++) p[q[i]] = i; for (int i = 1; i <= n; i++) assert(a[i] == b[p[i]]); for (int i = 1; i <= n; i++) rt[i] = i; for (int i = 1; i <= n; i++) { if (p[i] == i) continue; int l = findrt(i), r = findrt(p[i]); if (l == r) continue; rt[l] = r; } for (int i = 1; i <= n; i++) { if (p[i] == i) continue; if (!S.count(a[i])) { S[a[i]] = i; continue; } int l = S[a[i]]; int fl = findrt(l), fr = findrt(i); if (fl == fr) continue; rt[fr] = fl; swap(p[l], p[i]); } for (int i = 1; i <= n; i++) { if (p[i] == i) { vis[i] = 1; continue; } if (vis[i]) continue; tot++; vector<int> &cur = ans[tot]; int now = i; while (1) { vis[now] = 1; cur.push_back(now); now = p[now]; if (now == i) break; } } int k = s - cnt; k = min(tot, k); if (k >= 3) { vector<int> cur; for (int i = 0; i < k; i++) cur.push_back(ans[tot - i][0]); vector<int> &to = ans[tot - k + 1]; for (int i = k - 2; i >= 0; i--) { for (int x : ans[tot - i]) to.push_back(x); } ans[tot - k + 2] = cur; tot -= k - 2; } cout << tot << endl; for (int i = 1; i <= tot; i++) { cout << ans[i].size() << endl; for (int x : ans[i]) printf("%d ", x); puts(""); } }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int max_n = 200222, inf = 1000111222; int n, s, cnt, to[max_n]; int a[max_n], b[max_n], pos[max_n]; vector<int> all[max_n]; void compress() { pair<int, int> p[max_n]; for (int i = 0; i < n; ++i) { p[i] = {a[i], i}; } sort(p, p + n); int num = 0; for (int i = 0; i < n;) { int start = i; while (i < n && p[i].first == p[start].first) { a[p[i].second] = num; ++i; } ++num; } } vector<vector<int>> cycles; vector<pair<int, int>> g[max_n]; int parent[max_n]; void init() { for (int i = 1; i <= n; ++i) { parent[i] = i; } } int find_set(int v) { if (v == parent[v]) { return v; } return parent[v] = find_set(parent[v]); } void union_set(int v1, int v2) { v1 = find_set(v1); v2 = find_set(v2); parent[v1] = v2; } void find_all_cycles() { cycles.clear(); bool used[max_n]; memset(used, 0, sizeof(used)); for (int i = 0; i < n; ++i) { if (used[i] == 0 && to[i] != -1) { int pos = i; vector<int> cycle; while (used[pos] == 0) { used[pos] = 1; cycle.push_back(pos); g[a[pos]].push_back({cycles.size(), pos}); pos = to[pos]; } cycles.push_back(cycle); } } } int main() { scanf("%d%d", &n, &s); for (int i = 0; i < n; ++i) { scanf("%d", &a[i]); } compress(); copy(a, a + n, b); sort(b, b + n); for (int i = 0; i < n; ++i) { if (a[i] != b[i]) { all[a[i]].push_back(i); } to[i] = -1; } for (int i = 0; i < n; ++i) { if (a[i] != b[i]) { int p = all[b[i]][pos[b[i]]++]; to[p] = i; ++cnt; } } if (cnt > s) { puts("-1"); return 0; } find_all_cycles(); init(); for (int i = 0; i < n; ++i) { if (g[i].size() > 1) { const pair<int, int> &first = g[i][0]; for (const pair<int, int> &p : g[i]) { if (find_set(first.first) != find_set(p.first)) { union_set(first.first, p.first); swap(to[first.second], to[p.second]); } } } } find_all_cycles(); int ans = cycles.size(); if (s - cnt) { int q = min((int)cycles.size(), s - cnt); if (q >= 3) { ans += 2 - q; printf("%d\n", ans); vector<int> v; printf("%d\n", q); for (int i = 0; i < q; ++i) { v.push_back(cycles[i][0]); printf("%d ", cycles[i][0] + 1); } printf("\n"); int cp = to[v.back()]; for (int i = v.size() - 1; i > 0; --i) { to[v[i]] = to[v[i - 1]]; } to[v[0]] = cp; find_all_cycles(); } else { printf("%d\n", ans); } } else { printf("%d\n", ans); } for (const vector<int> &cycle : cycles) { printf("%d\n", cycle.size()); for (int pos : cycle) { printf("%d ", pos + 1); } printf("\n"); } return 0; }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int N = 4e5 + 10; int Begin[N], Next[N], to[N], e; void add(int u, int v) { to[++e] = v, Next[e] = Begin[u], Begin[u] = e; } int n, m, k, c; int A[N], rk[N], st[N], vis[N]; void DFS(int o) { vis[o] = true; for (int& i = Begin[o]; i;) { int u = to[i]; i = Next[i]; DFS(u); st[++c] = o; } } bool cmp(int x, int y) { return A[x] < A[y]; } int L[N], R[N], pos[N]; vector<int> seq[N], cyc[N]; struct Edge { int u, v, id; bool operator<(const Edge& E) const { return u != E.u ? u < E.u : v < E.v; } }; multiset<Edge> S; bool used[N]; int main() { scanf("%d%d", &n, &k); for (int i = 1; i <= n; i++) scanf("%d", &A[i]), rk[i] = i; sort(rk + 1, rk + n + 1, cmp); for (int i = 1; i <= n; i++) L[i] = A[rk[i]] == A[rk[i - 1]] ? L[i - 1] : i; for (int i = n; i >= 1; i--) R[i] = A[rk[i]] == A[rk[i + 1]] ? R[i + 1] : i; for (int i = 1; i <= n; i++) if (rk[i] >= L[i] && rk[i] <= R[i]) used[rk[i]] = true; for (int i = 1; i <= n; i++) if (L[i] == i) pos[i] = i; int ans = 0; for (int i = 1; i <= n; i++) if (rk[i] < L[i] || rk[i] > R[i]) ++ans, add(L[rk[i]], L[i]), S.insert((Edge){L[rk[i]], L[i], rk[i]}); if (ans > k) { puts("-1"); return 0; } for (int i = 1; i <= n; i++) if (!vis[i] && Begin[i]) { c = 0; DFS(i); ++m; for (int j = c; j >= 1; j--) { int v = j == 1 ? st[c] : st[j - 1]; auto it = S.lower_bound((Edge){st[j], v, 0}); seq[m].push_back(it->id); S.erase(it); } } int p = 1; c = 0; if (k - ans > 2 && m > 2) { int pr = min(m, k - ans); c = 1; for (int i = 1; i <= pr; i++) for (int v : seq[i]) cyc[c].push_back(v); c = 2; for (int i = pr; i >= 1; i--) cyc[c].push_back(seq[i].front()); p = pr + 1; } for (int i = p; i <= m; i++) cyc[++c] = seq[i]; printf("%d\n", c); for (int i = 1; i <= c; i++) { printf("%lu\n", cyc[i].size()); for (int v : cyc[i]) printf("%d%c", v, v == cyc[i].back() ? '\n' : ' '); } return 0; }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
CORRECT
java
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.util.Arrays; import java.util.Random; import java.util.ArrayList; import java.io.OutputStreamWriter; import java.io.PrintStream; import java.io.OutputStream; import java.io.IOException; import java.io.UncheckedIOException; import java.util.List; import java.io.Closeable; import java.io.Writer; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) throws Exception { Thread thread = new Thread(null, new TaskAdapter(), "", 1 << 27); thread.start(); thread.join(); } static class TaskAdapter implements Runnable { @Override public void run() { InputStream inputStream = System.in; OutputStream outputStream = System.out; FastInput in = new FastInput(inputStream); FastOutput out = new FastOutput(outputStream); ECycleSort solver = new ECycleSort(); solver.solve(1, in, out); out.close(); } } static class ECycleSort { int n; int[] a; public void solve(int testNumber, FastInput in, FastOutput out) { n = in.readInt(); int s = in.readInt(); a = new int[n]; for (int i = 0; i < n; i++) { a[i] = in.readInt(); } int[] b = a.clone(); Randomized.shuffle(b); Arrays.sort(b); int[] same = new int[n]; int sum = 0; for (int i = 0; i < n; i++) { if (a[i] == b[i]) { same[i] = 1; } } for (int x : same) { sum += x; } if (n - sum > s) { out.println(-1); return; } IntegerList permList = new IntegerList(n); for (int i = 0; i < n; i++) { if (same[i] == 0) { permList.add(i); } } int[] perm = permList.toArray(); CompareUtils.quickSort(perm, (x, y) -> Integer.compare(a[x], a[y]), 0, perm.length); DSU dsu = new DSU(n); for (int i = 0; i < perm.length; i++) { int from = perm[i]; int to = permList.get(i); dsu.merge(from, to); } for (int i = 1; i < perm.length; i++) { if (a[perm[i]] != a[perm[i - 1]]) { continue; } if (dsu.find(perm[i]) == dsu.find(perm[i - 1])) { continue; } dsu.merge(perm[i], perm[i - 1]); SequenceUtils.swap(perm, i, i - 1); } IntegerList first = new IntegerList(); if (perm.length > 0) { int remain = s - (n - sum) - 1; first.add(perm[0]); for (int i = 1; remain > 0 && i < perm.length; i++) { if (dsu.find(perm[i - 1]) != dsu.find(perm[i])) { remain--; first.add(perm[i]); dsu.merge(perm[i - 1], perm[i]); } } int last = a[first.get(0)]; for (int i = 1; i < first.size(); i++) { int y = first.get(i); int tmp = a[y]; a[y] = last; last = tmp; } a[first.get(0)] = last; System.err.println(Arrays.toString(a)); } List<IntegerList> circles = new ArrayList<>(); if (first.size() > 1) { circles.add(first); } circles.addAll(solve()); out.println(circles.size()); for (IntegerList list : circles) { out.println(list.size()); for (int i = 0; i < list.size(); i++) { out.append(list.get(i) + 1).append(' '); } out.println(); } } public List<IntegerList> solve() { int[] b = a.clone(); Randomized.shuffle(b); Arrays.sort(b); int[] same = new int[n]; for (int i = 0; i < n; i++) { if (a[i] == b[i]) { same[i] = 1; } } IntegerList permList = new IntegerList(n); for (int i = 0; i < n; i++) { if (same[i] == 0) { permList.add(i); } } int[] perm = permList.toArray(); CompareUtils.quickSort(perm, (x, y) -> Integer.compare(a[x], a[y]), 0, perm.length); DSU dsu = new DSU(n); for (int i = 0; i < perm.length; i++) { int from = perm[i]; int to = permList.get(i); dsu.merge(from, to); } for (int i = 1; i < perm.length; i++) { if (a[perm[i]] != a[perm[i - 1]]) { continue; } if (dsu.find(perm[i]) == dsu.find(perm[i - 1])) { continue; } dsu.merge(perm[i], perm[i - 1]); SequenceUtils.swap(perm, i, i - 1); } int[] index = new int[n]; for (int i = 0; i < n; i++) { if (same[i] == 1) { index[i] = i; } } for (int i = 0; i < perm.length; i++) { index[perm[i]] = permList.get(i); } PermutationUtils.PowerPermutation pp = new PermutationUtils.PowerPermutation(index); List<IntegerList> circles = pp.extractCircles(2); return circles; } } static class DSU { int[] p; int[] rank; public DSU(int n) { p = new int[n]; rank = new int[n]; reset(); } public void reset() { for (int i = 0; i < p.length; i++) { p[i] = i; rank[i] = 0; } } public int find(int a) { return p[a] == p[p[a]] ? p[a] : (p[a] = find(p[a])); } public void merge(int a, int b) { a = find(a); b = find(b); if (a == b) { return; } if (rank[a] == rank[b]) { rank[a]++; } if (rank[a] > rank[b]) { p[b] = a; } else { p[a] = b; } } } static class Randomized { private static Random random = new Random(0); public static void shuffle(int[] data) { shuffle(data, 0, data.length - 1); } public static void shuffle(int[] data, int from, int to) { to--; for (int i = from; i <= to; i++) { int s = nextInt(i, to); int tmp = data[i]; data[i] = data[s]; data[s] = tmp; } } public static int nextInt(int l, int r) { return random.nextInt(r - l + 1) + l; } } static class FastInput { private final InputStream is; private byte[] buf = new byte[1 << 20]; private int bufLen; private int bufOffset; private int next; public FastInput(InputStream is) { this.is = is; } private int read() { while (bufLen == bufOffset) { bufOffset = 0; try { bufLen = is.read(buf); } catch (IOException e) { bufLen = -1; } if (bufLen == -1) { return -1; } } return buf[bufOffset++]; } public void skipBlank() { while (next >= 0 && next <= 32) { next = read(); } } public int readInt() { int sign = 1; skipBlank(); if (next == '+' || next == '-') { sign = next == '+' ? 1 : -1; next = read(); } int val = 0; if (sign == 1) { while (next >= '0' && next <= '9') { val = val * 10 + next - '0'; next = read(); } } else { while (next >= '0' && next <= '9') { val = val * 10 - next + '0'; next = read(); } } return val; } } static class SequenceUtils { public static void swap(int[] data, int i, int j) { int tmp = data[i]; data[i] = data[j]; data[j] = tmp; } public static boolean equal(int[] a, int al, int ar, int[] b, int bl, int br) { if ((ar - al) != (br - bl)) { return false; } for (int i = al, j = bl; i <= ar; i++, j++) { if (a[i] != b[j]) { return false; } } return true; } } static class FastOutput implements AutoCloseable, Closeable, Appendable { private StringBuilder cache = new StringBuilder(10 << 20); private final Writer os; public FastOutput append(CharSequence csq) { cache.append(csq); return this; } public FastOutput append(CharSequence csq, int start, int end) { cache.append(csq, start, end); return this; } public FastOutput(Writer os) { this.os = os; } public FastOutput(OutputStream os) { this(new OutputStreamWriter(os)); } public FastOutput append(char c) { cache.append(c); return this; } public FastOutput append(int c) { cache.append(c); return this; } public FastOutput println(int c) { cache.append(c); println(); return this; } public FastOutput println() { cache.append(System.lineSeparator()); return this; } public FastOutput flush() { try { os.append(cache); os.flush(); cache.setLength(0); } catch (IOException e) { throw new UncheckedIOException(e); } return this; } public void close() { flush(); try { os.close(); } catch (IOException e) { throw new UncheckedIOException(e); } } public String toString() { return cache.toString(); } } static class IntegerList implements Cloneable { private int size; private int cap; private int[] data; private static final int[] EMPTY = new int[0]; public IntegerList(int cap) { this.cap = cap; if (cap == 0) { data = EMPTY; } else { data = new int[cap]; } } public IntegerList(IntegerList list) { this.size = list.size; this.cap = list.cap; this.data = Arrays.copyOf(list.data, size); } public IntegerList() { this(0); } public void ensureSpace(int req) { if (req > cap) { while (cap < req) { cap = Math.max(cap + 10, 2 * cap); } data = Arrays.copyOf(data, cap); } } private void checkRange(int i) { if (i < 0 || i >= size) { throw new ArrayIndexOutOfBoundsException(); } } public int get(int i) { checkRange(i); return data[i]; } public void add(int x) { ensureSpace(size + 1); data[size++] = x; } public void addAll(int[] x, int offset, int len) { ensureSpace(size + len); System.arraycopy(x, offset, data, size, len); size += len; } public void addAll(IntegerList list) { addAll(list.data, 0, list.size); } public int size() { return size; } public int[] toArray() { return Arrays.copyOf(data, size); } public String toString() { return Arrays.toString(toArray()); } public boolean equals(Object obj) { if (!(obj instanceof IntegerList)) { return false; } IntegerList other = (IntegerList) obj; return SequenceUtils.equal(data, 0, size - 1, other.data, 0, other.size - 1); } public int hashCode() { int h = 1; for (int i = 0; i < size; i++) { h = h * 31 + Integer.hashCode(data[i]); } return h; } public IntegerList clone() { IntegerList ans = new IntegerList(size); ans.addAll(this); return ans; } } static class DigitUtils { private DigitUtils() { } public static int mod(int x, int mod) { x %= mod; if (x < 0) { x += mod; } return x; } } static interface IntComparator { public int compare(int a, int b); } static class PermutationUtils { private static final long[] PERMUTATION_CNT = new long[21]; static { PERMUTATION_CNT[0] = 1; for (int i = 1; i <= 20; i++) { PERMUTATION_CNT[i] = PERMUTATION_CNT[i - 1] * i; } } public static class PowerPermutation { int[] g; int[] idx; int[] l; int[] r; int n; public List<IntegerList> extractCircles(int threshold) { List<IntegerList> ans = new ArrayList<>(n); for (int i = 0; i < n; i = r[i] + 1) { int size = r[i] - l[i] + 1; if (size < threshold) { continue; } IntegerList list = new IntegerList(r[i] - l[i] + 1); for (int j = l[i]; j <= r[i]; j++) { list.add(g[j]); } ans.add(list); } return ans; } public PowerPermutation(int[] p) { this(p, p.length); } public PowerPermutation(int[] p, int len) { n = len; boolean[] visit = new boolean[n]; g = new int[n]; l = new int[n]; r = new int[n]; idx = new int[n]; int wpos = 0; for (int i = 0; i < n; i++) { int val = p[i]; if (visit[val]) { continue; } visit[val] = true; g[wpos] = val; l[wpos] = wpos; idx[val] = wpos; wpos++; while (true) { int x = p[g[wpos - 1]]; if (visit[x]) { break; } visit[x] = true; g[wpos] = x; l[wpos] = l[wpos - 1]; idx[x] = wpos; wpos++; } for (int j = l[wpos - 1]; j < wpos; j++) { r[j] = wpos - 1; } } } public int apply(int x, int p) { int i = idx[x]; int dist = DigitUtils.mod((i - l[i]) + p, r[i] - l[i] + 1); return g[dist + l[i]]; } public String toString() { StringBuilder builder = new StringBuilder(); for (int i = 0; i < n; i++) { builder.append(apply(i, 1)).append(' '); } return builder.toString(); } } } static class CompareUtils { private static final int THRESHOLD = 4; private CompareUtils() { } public static <T> void insertSort(int[] data, IntComparator cmp, int l, int r) { for (int i = l + 1; i <= r; i++) { int j = i; int val = data[i]; while (j > l && cmp.compare(data[j - 1], val) > 0) { data[j] = data[j - 1]; j--; } data[j] = val; } } public static void quickSort(int[] data, IntComparator cmp, int f, int t) { if (t - f <= THRESHOLD) { insertSort(data, cmp, f, t - 1); return; } SequenceUtils.swap(data, f, Randomized.nextInt(f, t - 1)); int l = f; int r = t; int m = l + 1; while (m < r) { int c = cmp.compare(data[m], data[l]); if (c == 0) { m++; } else if (c < 0) { SequenceUtils.swap(data, l, m); l++; m++; } else { SequenceUtils.swap(data, m, --r); } } quickSort(data, cmp, f, l); quickSort(data, cmp, m, t); } } }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 5; int n, s, a[N], aa[N], g[N], p[N], i, j; int gfa(int x) { return g[x] == x ? x : g[x] = gfa(g[x]); } bool bb[N], vi[N]; set<int> S; unordered_map<int, int> be, en; unordered_map<int, vector<int>> mp; int main() { ios::sync_with_stdio(0); cin.tie(0); cin >> n >> s; for (i = 1; i <= n; ++i) cin >> a[i]; memcpy(aa + 1, a + 1, n << 2); sort(aa + 1, aa + n + 1); for (i = 1; i <= n; ++i) { bb[i] = a[i] == aa[i], g[i] = i; if (!bb[i]) S.insert(i); } int ss = S.size(); if (ss > s) { cout << -1 << endl; return 0; } for (i = 1; i <= n; ++i) en[aa[i]] = i; for (i = n; i; --i) be[aa[i]] = i; for (i = 1; i <= n; ++i) if (!bb[i]) p[i] = *S.lower_bound(be[a[i]]), S.erase(p[i]), g[gfa(p[i])] = gfa(i); for (i = 1; i <= n; ++i) if (!bb[i]) mp[a[i]].push_back(i); else p[i] = i; for (auto u : mp) { auto v = u.second; for (i = 1; i < v.size(); ++i) if (gfa(v[i]) != gfa(v[0])) g[gfa(v[i])] = gfa(v[0]), swap(p[v[i]], p[v[0]]); } vector<vector<int>> ans; for (i = 1; i <= n; ++i) if (p[i] != i && !vi[i]) { vector<int> ve; for (j = i; vi[j] = 1, ve.push_back(j), p[j] != i; j = p[j]) ; ans.push_back(ve); } if (ss + 3 <= s && ans.size() > 1) { vector<int> v1, v2, v3; for (i = 1; ss + i <= s && !ans.empty(); ++i) { v3 = ans.back(); ans.pop_back(); v1.insert(v1.end(), v3.begin(), v3.end()); v2.push_back(v3[0]); } reverse(v2.begin(), v2.end()); ans.push_back(v1); ans.push_back(v2); } cout << ans.size() << '\n'; for (auto u : ans) { cout << u.size() << '\n'; for (int x : u) cout << x << ' '; cout << '\n'; } return 0; }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; inline int read() { int x = 0, neg = 1; char c = getchar(); while (!isdigit(c)) { if (c == '-') neg = -1; c = getchar(); } while (isdigit(c)) x = x * 10 + c - '0', c = getchar(); return x * neg; } inline int qpow(int x, int e, int _MOD) { int ans = 1; while (e) { if (e & 1) ans = ans * x % _MOD; x = x * x % _MOD; e >>= 1; } return ans; } int n = read(), s = read(), a[200005], b[200005], c[200005], d[200005], num = 0; vector<int> g[200005], cyc[200005]; int k = 0; inline void dfs(int x) { while (!g[x].empty()) { int y = g[x].back(); g[x].pop_back(); dfs(a[y]); cyc[k].push_back(y); } } signed main() { for (int i = 1; i <= n; i++) a[i] = read(), c[i] = a[i]; sort(c + 1, c + n + 1); for (int i = 1; i <= n; i++) if (c[i] != c[i - 1]) d[++num] = c[i]; for (int i = 1; i <= n; i++) a[i] = lower_bound(d + 1, d + num + 1, a[i]) - d, b[i] = a[i]; sort(b + 1, b + n + 1); int cnt = 0; for (int i = 1; i <= n; i++) { if (a[i] != b[i]) { cnt++; g[b[i]].push_back(i); } } if (!cnt) return puts("0"), 0; if (cnt > s) return puts("-1"), 0; for (int i = 1; i <= num; i++) { if (!g[i].empty()) { k++; dfs(i); } } for (int i = 1; i <= k; i++) reverse(cyc[i].begin(), cyc[i].end()); if (k == 1) { cout << 1 << endl << cyc[1].size() << endl; for (__typeof(cyc[1].begin()) it = cyc[1].begin(); it != cyc[1].end(); it++) cout << *it << " "; return 0; } if (cnt <= s - k) { cout << 2 << endl; cout << cnt << endl; for (int i = 1; i <= k; i++) { for (__typeof(cyc[i].begin()) it = cyc[i].begin(); it != cyc[i].end(); it++) cout << *it << " "; } puts(""); cout << k << endl; for (int i = k; i >= 1; i--) cout << cyc[i][0] << " "; puts(""); return 0; } else { int t = cnt - (s - k); if (t == k) cout << t << endl; else if (t == k - 1) cout << t + 1 << endl; else cout << t + 2 << endl; for (int i = 1; i <= t; i++) { cout << cyc[i].size() << endl; for (__typeof(cyc[i].begin()) it = cyc[i].begin(); it != cyc[i].end(); it++) cout << *it << " "; puts(""); } int sum = 0; if (t != k) { for (int i = t + 1; i <= k; i++) sum += cyc[i].size(); cout << sum << endl; for (int i = t + 1; i <= k; i++) for (__typeof(cyc[i].begin()) it = cyc[i].begin(); it != cyc[i].end(); it++) cout << *it << " "; if (t != k - 1) { puts(""); cout << k - t << endl; for (int i = k; i >= t + 1; i--) cout << cyc[i][0] << " "; } } } return 0; }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int get() { char ch; while (ch = getchar(), (ch < '0' || ch > '9') && ch != '-') ; if (ch == '-') { int s = 0; while (ch = getchar(), ch >= '0' && ch <= '9') s = s * 10 + ch - '0'; return -s; } int s = ch - '0'; while (ch = getchar(), ch >= '0' && ch <= '9') s = s * 10 + ch - '0'; return s; } const int N = 2e5 + 5; int n, s; int a[N], b[N]; int to[N]; int q; bool vis[N]; struct edge { int x, id, nxt; } e[N]; int h[N], tot; int col[N]; map<int, int> id; int k; void inse(int x, int y, int id) { e[++tot].x = y; e[tot].id = id; e[tot].nxt = h[x]; h[x] = tot; } bool used[N]; int st; int u; int que[N]; pair<int, int> key[N]; int be[N]; void euler(int x) { for (; h[x];) { int p = h[x]; int id = e[p].id; int y = e[p].x; que[++u] = id; h[x] = e[p].nxt; euler(y); } } int fa[N], c; int getfather(int x) { return fa[x] == x ? x : fa[x] = getfather(fa[x]); } void add(int l, int r, int c) { to[l] = r; int x = id[a[l]]; if (be[x] && getfather(be[x]) == getfather(c)) return; int fx = getfather(be[x]), fc = getfather(c); if (!key[x].first) be[x] = c, key[x] = make_pair(l, r); else { to[l] = key[x].second; to[key[x].first] = r; key[x].first = l; fa[fx] = fc; } } void getto() { c = 0; for (int l = 1; l <= u;) { int r = l; c++; fa[c] = c; for (; a[que[r]] != b[que[l]]; r++) ; int st = id[b[que[l]]]; for (int i = l; i <= r - 1; i++) { add(que[i], que[i + 1], c); } add(que[r], que[l], c); l = r + 1; } } int Fa[N]; int getFa(int x) { return Fa[x] == x ? x : Fa[x] = getFa(Fa[x]); } void merge(int x, int y) { int tx = getFa(x), ty = getFa(y); Fa[tx] = ty; } bool ad[N]; int kp[N]; int pv[N]; int main() { n = get(); s = get(); for (int i = 1; i <= n; i++) a[i] = get(); for (int i = 1; i <= n; i++) b[i] = a[i]; sort(b + 1, b + 1 + n); int len = n; for (int i = 1; i <= n; i++) if (b[i] == a[i]) to[i] = i, len--; if (len > s) return printf("-1\n"), 0; s -= len; for (int i = 1; i <= n; i++) if (!id[b[i]]) col[id[b[i]] = ++k] = b[i]; for (int i = 1; i <= k; i++) Fa[i] = i; for (int i = 1; i <= n; i++) { int x = id[a[i]], y = id[b[i]]; int tx = getFa(x), ty = getFa(y); if (tx == ty) { ad[i] = 1; continue; } Fa[tx] = ty; } for (int i = 1; i <= n; i++) if (ad[i] && a[i] != b[i]) { int x = getFa(id[a[i]]); if (!kp[x]) kp[x] = i; } int nq = 0; for (int i = 1; i <= k; i++) if (getFa(i) == i && kp[i]) nq++; bool predo = 0; int pl = 0; if (nq > 1 && s > 1) { predo = 1; for (int i = 1; i <= k; i++) if (getFa(i) == i && kp[i]) { if (s) { pv[++pl] = kp[i]; s--; } } if (pl <= 1) predo = 0; else { int tmp = a[pv[pl]]; for (int i = pl; i >= 2; i--) a[pv[i]] = a[pv[i - 1]]; a[pv[1]] = tmp; } } for (int i = 1; i <= n; i++) if (a[i] != b[i]) inse(id[b[i]], id[a[i]], i); for (int i = 1; i <= n; i++) { st = i; u = 0; euler(i); getto(); } int q = 0; for (int i = 1; i <= n; i++) if (a[i] != b[i] && !vis[i]) { q++; vis[i] = 1; for (int x = to[i]; x != i; x = to[x]) vis[x] = 1; } printf("%d\n", q + predo); if (predo) { printf("%d\n", pl); for (int i = 1; i <= pl; i++) printf("%d ", pv[i]); putchar('\n'); } for (int i = 1; i <= n; i++) if (a[i] != b[i] && vis[i]) { int len = 1; vis[i] = 0; for (int x = to[i]; x != i; x = to[x]) vis[x] = 0, len++; printf("%d\n%d", len, i); for (int x = to[i]; x != i; x = to[x]) printf(" %d", x); putchar('\n'); } return 0; }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; template <class BidirIt> BidirIt prev(BidirIt it, typename iterator_traits<BidirIt>::difference_type n = 1) { advance(it, -n); return it; } template <class ForwardIt> ForwardIt next(ForwardIt it, typename iterator_traits<ForwardIt>::difference_type n = 1) { advance(it, n); return it; } const double EPS = 1e-9; const double PI = 3.141592653589793238462; template <typename T> inline T sq(T a) { return a * a; } const int MAXN = 4e5 + 5; int ar[MAXN], sor[MAXN]; map<int, int> dummy; bool visit[MAXN], proc[MAXN]; int nxt[MAXN]; vector<int> gr[MAXN]; vector<int> cur, tour[MAXN]; vector<vector<int> > cycles; void addEdge(int u, int v) { gr[u].push_back(v); } void dfs(int u) { visit[u] = true; while (nxt[u] < (int)gr[u].size()) { int v = gr[u][nxt[u]]; nxt[u]++; dfs(v); cur.push_back(u); } } void getcycle(int u, vector<int> &vec) { proc[u] = true; for (auto it : tour[u]) { vec.push_back(it); if (!proc[it]) getcycle(it, vec); } } int main() { int n, s; scanf("%d %d", &n, &s); for (int i = 1; i <= n; i++) { scanf("%d", &ar[i]); sor[i] = ar[i]; } sort(sor + 1, sor + n + 1); for (int i = 1; i <= n; i++) { if (ar[i] != sor[i]) dummy[ar[i]] = 0; } int cnt = 0; for (auto &it : dummy) it.second = n + (++cnt); for (int i = 1; i <= n; i++) { if (ar[i] == sor[i]) continue; addEdge(dummy[sor[i]], i); addEdge(i, dummy[ar[i]]); } for (int i = 1; i <= n + cnt; i++) { if ((i > n || ar[i] != sor[i]) && !visit[i]) { cur.clear(); dfs(i); reverse((cur).begin(), (cur).end()); vector<int> res; for (auto it : cur) if (it <= n) res.push_back(it); cycles.push_back(res); s -= (int)res.size(); } } if (s < 0) { puts("-1"); return 0; } int pos = 0; if (s > 1 && cycles.size() > 1) { printf("%d\n", max(2, (int)cycles.size() - s + 2)); int sum = 0; while (s > 0 && pos < (int)cycles.size()) { s--; sum += (int)cycles[pos].size(); pos++; } printf("%d\n", sum); vector<int> vec; for (int i = 0; i < pos; i++) { for (auto it : cycles[i]) printf("%d ", it); vec.push_back(cycles[i][0]); } puts(""); reverse((vec).begin(), (vec).end()); printf("%d\n", (int)vec.size()); for (auto it : vec) printf("%d ", it); puts(""); } else { printf("%d\n", (int)cycles.size()); } for (int i = pos; i < (int)cycles.size(); i++) { printf("%d\n", (int)cycles[i].size()); for (auto it : cycles[i]) printf("%d ", it); puts(""); } return 0; }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const long long N = 200005; template <class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &a) { return os << '(' << a.first << ", " << a.second << ')'; } template <class T> ostream &operator<<(ostream &os, const vector<T> &a) { os << '['; for (unsigned long long i = 0; i < a.size(); i++) os << a[i] << (i < a.size() - 1 ? ", " : ""); os << ']'; return os; } long long read() { long long x; cin >> x; return x; } struct dsu { vector<long long> par, sz; long long cc; void init(long long n) { par.assign(n + 5, 0); sz.assign(n + 5, 0); cc = n; for (long long i = 0; i < n + 5; i++) par[i] = i, sz[i] = 1; } long long find(long long x) { if (par[x] == x) return x; return par[x] = find(par[x]); } void uni(long long x, long long y) { x = find(x); y = find(y); if (x != y) { sz[y] += sz[x]; sz[x] = 0; par[x] = y; cc--; } } } d; long long n, m; vector<pair<long long, long long> > a, b; long long out[N], in[N]; void addEdge(long long u, long long v) { out[u] = v; in[v] = u; d.uni(u, v); } void con(long long u, long long v) { long long outu = out[u], outv = out[v]; addEdge(u, outv); addEdge(v, outu); } void noAnswer() { cout << -1 << '\n'; exit(0); } vector<vector<long long> > ans; void makeAns() { vector<bool> used(N, 0); for (long long i = (long long)0; i <= (long long)n - 1; i++) { long long u = d.find(i); if (d.sz[u] == 1 || used[u]) continue; used[u] = true; long long root = u; vector<long long> cur; cur.push_back(u); while (out[u] != root) { u = out[u]; cur.push_back(u); } ans.push_back(cur); } cout << ans.size() << '\n'; for (auto &it : ans) { cout << it.size() << '\n'; for (auto &it2 : it) cout << it2 + 1 << ' '; cout << '\n'; } exit(0); } signed main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n >> m; d.init(n); set<pair<long long, long long> > st; vector<long long> _a; for (long long i = (long long)1; i <= (long long)n; i++) { pair<long long, long long> it = make_pair(read(), i - 1); a.push_back(it); st.insert(it); _a.push_back(it.first); } sort(_a.begin(), _a.end()); for (long long i = (long long)0; i <= (long long)n - 1; i++) if (_a[i] == a[i].first) st.erase(a[i]); for (long long i = (long long)0; i <= (long long)n - 1; i++) { if (_a[i] == a[i].first) b.push_back(a[i]); else { auto it = st.lower_bound(make_pair(_a[i], -1)); b.push_back(*it); st.erase(it); addEdge(b[i].second, i); } } pair<long long, long long> last = make_pair(-1, -1); for (long long i = (long long)0; i <= (long long)b.size() - 1; i++) { auto &it = b[i]; if (d.sz[d.find(it.second)] == 1) continue; if (last.first != -1 && last.first == it.first && d.find(last.second) != d.find(it.second)) con(last.second, it.second); last = it; } vector<bool> used(N, 0); vector<long long> all; long long sz = 0; for (long long i = (long long)0; i <= (long long)n - 1; i++) { long long u = d.find(i); if (d.sz[u] == 1 || used[u]) continue; used[u] = true; sz += d.sz[u]; all.push_back(u); } if (sz > m) noAnswer(); m -= sz; m = min(m, (long long)all.size()); if (m >= 3) { vector<long long> cur; for (long long i = (long long)0; i <= (long long)m - 1; i++) { if (!cur.empty()) con(cur.front(), all[i]); cur.push_back(all[i]); } ans.push_back(cur); } makeAns(); }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7; struct fastio { char s[100000]; int it, len; fastio() { it = len = 0; } inline char get() { if (it < len) return s[it++]; it = 0; len = fread(s, 1, 100000, stdin); if (len == 0) return EOF; else return s[it++]; } bool notend() { char c = get(); while (c == ' ' || c == '\n') c = get(); if (it > 0) it--; return c != EOF; } } _buff; inline long long getnum() { long long r = 0; bool ng = 0; char c; c = _buff.get(); while (c != '-' && (c < '0' || c > '9')) c = _buff.get(); if (c == '-') ng = 1, c = _buff.get(); while (c >= '0' && c <= '9') r = r * 10 + c - '0', c = _buff.get(); return ng ? -r : r; } template <class T> inline void putnum(T x) { if (x < 0) putchar('-'), x = -x; register short a[20] = {}, sz = 0; while (x) a[sz++] = x % 10, x /= 10; if (sz == 0) putchar('0'); for (int i = sz - 1; i >= 0; i--) putchar('0' + a[i]); } inline char getreal() { char c = _buff.get(); while (c <= 32) c = _buff.get(); return c; } long long qpow(long long x, long long k) { return k == 0 ? 1 : 1ll * qpow(1ll * x * x % mod, k >> 1) * (k & 1 ? x : 1) % mod; } const int maxn = 200111; int n, m, tot; int a[maxn], b[maxn], it[maxn]; map<int, int> mpid; vector<pair<int, int>> con[maxn]; vector<vector<int>> cyc, ans; vector<int> cur; void dfs(int x) { for (int &i = it[x]; i < con[x].size();) { int u = con[x][i].first, id = con[x][i].second; i++; dfs(u); cur.push_back(id); } } int main() { n = getnum(), m = getnum(); for (int i = 1; i <= n; i++) { a[i] = getnum(); b[i] = a[i]; } sort(b + 1, b + n + 1); for (int i = 1; i <= n; i++) { int &id = mpid[b[i]]; if (id == 0) id = ++tot; b[i] = id; } for (int i = 1; i <= n; i++) a[i] = mpid[a[i]]; for (int i = 1; i <= n; i++) { if (a[i] == b[i]) continue; con[a[i]].push_back(make_pair(b[i], i)); } for (int i = 1; i <= tot; i++) it[i] = 0; int sum = 0; for (int i = 1; i <= tot; i++) { if (con[i].size() > 0 && it[i] < con[i].size()) { cur.clear(); dfs(i); cyc.push_back(cur); sum += cur.size(); } } if (sum > m) { puts("-1"); return 0; } int mx = -1; for (int i = 1; i < cyc.size(); i++) { if (sum + 1 + i <= m) { mx = i; } } if (mx != -1) { vector<int> v; for (int i = 0; i <= mx; i++) { for (int j = 0; j < cyc[i].size(); j++) { v.push_back(cyc[i][j]); } } ans.push_back(v); v.clear(); for (int i = 0; i <= mx; i++) v.push_back(cyc[i][0]); reverse(v.begin(), v.end()); ans.push_back(v); } else { for (int i = 0; i <= mx; i++) ans.push_back(cyc[i]); } for (int i = mx + 1; i < cyc.size(); i++) ans.push_back(cyc[i]); putnum(ans.size()), putchar('\n'); for (int i = 0; i < ans.size(); i++) { putnum(ans[i].size()), putchar('\n'); for (auto x : ans[i]) putnum(x), putchar(' '); puts(""); } return 0; }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using namespace chrono; const int infinity = (int)1e9 + 42; const int64_t llInfinity = (int64_t)1e18 + 256; const int module = (int)1e9 + 7; const long double eps = 1e-8; mt19937_64 randGen(system_clock().now().time_since_epoch().count()); inline void raiseError(string errorCode) { cerr << "Error : " << errorCode << endl; exit(42); } inline void test(istream &cin, ostream &cout) { int n, s; cin >> n >> s; vector<int> v(n); map<int, int> kompr; for (int i = 0; i < n; i++) { cin >> v[i]; kompr[v[i]] = 42; } int k = 0; for (auto &it : kompr) { it.second = k++; } for (int i = 0; i < n; i++) { v[i] = kompr[v[i]]; } auto w = v; sort(w.begin(), w.end()); vector<vector<int> > g(k); map<pair<int, int>, vector<int> > indices; for (int i = 0; i < n; i++) { if (w[i] == v[i]) { continue; } s--; g[w[i]].push_back(v[i]); indices[{w[i], v[i]}].push_back(i); } if (s < 0) { cout << -1 << "\n"; return; } vector<char> used(n, false); function<void(int)> pdfs = [&](int v) { if (used[v]) { return; } used[v] = true; for (int to : g[v]) { pdfs(to); } }; int comps = 0; for (int i = 0; i < k; i++) { if (!used[i]) { pdfs(i); if (!g[i].empty()) { comps++; } } } vector<int> pass; function<void(int)> dfs = [&](int v) { while (!g[v].empty()) { int to = g[v].back(); g[v].pop_back(); dfs(to); } pass.push_back(v); }; vector<vector<int> > ans; for (int i = 0; i < k; i++) { if (g[i].empty()) { continue; } pass.clear(); dfs(i); ans.emplace_back(); for (int j = 1; j < (int)pass.size(); j++) { auto &vec = indices[{pass[j], pass[j - 1]}]; assert(!vec.empty()); ans.back().push_back(vec.back()); vec.pop_back(); } reverse(ans.back().begin(), ans.back().end()); } assert((int)ans.size() == comps); int canUnite = min(s, (int)ans.size()); if (canUnite > 2) { vector<int> united; vector<int> loop2; for (int i = 0; i < canUnite; i++) { united.insert(united.end(), ans.back().begin(), ans.back().end()); loop2.push_back(ans.back()[0]); ans.pop_back(); } ans.push_back(united); reverse(loop2.begin(), loop2.end()); ans.push_back(loop2); } cout << ans.size() << "\n"; for (auto vec : ans) { cout << vec.size() << "\n"; for (auto it : vec) { cout << it + 1 << " "; } cout << "\n"; } } signed main() { ios_base::sync_with_stdio(false); test(cin, cout); return 0; }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int Maxn = 200005; int n, s, ct, tmp_n, ans_ct, a[Maxn], b[Maxn], fa[Maxn], pos[Maxn], ord[Maxn], bel[Maxn], Pos[Maxn]; vector<int> spec, Ve[Maxn]; bool vis[Maxn]; void dfs(int u) { if (vis[u]) return; bel[u] = ct, vis[u] = true, dfs(ord[u]); } void dfs2(int u) { if (vis[u]) return; Ve[ans_ct].push_back(u), vis[u] = true, dfs2(pos[u]); } int get_fa(int x) { return x == fa[x] ? x : fa[x] = get_fa(fa[x]); } int main() { scanf("%d%d", &n, &s); for (int i = 1; i <= n; i++) scanf("%d", &a[i]), b[i] = a[i]; sort(b + 1, b + 1 + n); for (int i = 1; i <= n; i++) if (a[i] != b[i]) a[++tmp_n] = a[i], Pos[tmp_n] = i; n = tmp_n; if (s < n) { puts("-1"); return 0; } s -= n; for (int i = 1; i <= n; i++) ord[i] = i; sort(ord + 1, ord + 1 + n, [](int x, int y) { return a[x] < a[y]; }); for (int i = 1; i <= n; i++) pos[ord[i]] = i; a[n + 1] = -1; for (int i = 1; i <= n; i++) if (!vis[i]) ct++, fa[ct] = ct, dfs(i); int las = 1; for (int i = 2; i <= n + 1; i++) if (a[ord[i]] != a[ord[i - 1]]) { for (int j = las; j < i; j++) if (get_fa(bel[ord[las]]) != get_fa(bel[ord[j]])) fa[get_fa(bel[ord[j]])] = get_fa(bel[ord[las]]), swap(ord[j], ord[las]); las = i; } memset(vis, 0, sizeof(bool[n + 1])); ct = 0; for (int i = 1; i <= n; i++) pos[ord[i]] = i; for (int i = 1; i <= n; i++) if (!vis[i]) { ct++; if (ct == 1 || ct > s) ++ans_ct; if (ct <= s) spec.push_back(i); dfs2(i); } printf("%d\n", ans_ct + (spec.size() > 1)); if (ans_ct) { printf("%d\n", (int)Ve[1].size()); for (vector<int>::iterator it = Ve[1].begin(); it != Ve[1].end(); it++) printf("%d ", Pos[*it]); puts(""); } if (spec.size() > 1) { printf("%d\n", (int)spec.size()); for (vector<int>::reverse_iterator it = spec.rbegin(); it != spec.rend(); it++) printf("%d ", Pos[*it]); puts(""); } for (int i = 2; i <= ans_ct; i++) { printf("%d\n", (int)Ve[i].size()); for (vector<int>::iterator it = Ve[i].begin(); it != Ve[i].end(); it++) printf("%d ", Pos[*it]); puts(""); } return 0; }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int N = 200010; struct A { int x, id; } a[N]; bool cmp(A x, A y) { return x.x < y.x; } int a0[N], s[N], f[N], pre[N], nxt[N]; int f_f(int x) { return x == f[x] ? x : f[x] = f_f(f[x]); } bool vv[N]; vector<int> t[N]; int main() { int n, S; scanf("%d%d", &n, &S); for (int i = 1; i <= n; i++) { scanf("%d", &a0[i]); a[i] = (A){a0[i], i}; } sort(a + 1, a + 1 + n, cmp); int cc = 0; for (int i = 1; i <= n; i++) { if (i == 1 || a[i].x != a[i - 1].x) s[cc] = i - 1, cc++; a0[a[i].id] = cc; } s[cc] = n; for (int i = 1; i <= n; i++) { a[i].x = a0[a[i].id], f[i] = i; if (a[i].id > s[a[i].x - 1] && a[i].id <= s[a[i].x]) vv[a[i].id] = true; } for (int i = 1; i <= n; i++) if (!vv[i]) { while (vv[s[a0[i] - 1] + 1]) s[a0[i] - 1]++; nxt[i] = s[a0[i] - 1] + 1, pre[nxt[i]] = i, s[a0[i] - 1]++; int fa = f_f(i), fb = f_f(nxt[i]); if (fa != fb) f[fa] = fb; } int la = 0; for (int i = 1; i <= n; i++) if (!vv[a[i].id]) { if (!la || a[i].x != a[la].x) la = i; else { int c0 = a[i].id, c1 = a[la].id, fa = f_f(c0), fb = f_f(c1); if (fa != fb) { swap(nxt[c0], nxt[c1]); f[fa] = fb; } } } int g0 = 0, g1 = 0; for (int i = 1; i <= n; i++) if (!vv[i]) { g1++; int tmp = nxt[i]; while (true) { g0++, t[g1].push_back(tmp); vv[tmp] = true; if (tmp == i) break; tmp = nxt[tmp]; } } if (g0 > S) { printf("-1\n"); return 0; } int tmp = max(g0 + g1 - S, 0); if (tmp + 2 > g1) { printf("%d\n", g1); for (int i = 1; i <= g1; i++) { printf("%d\n", t[i].size()); for (int j = 0; j <= t[i].size() - 1; j++) printf("%d ", t[i][j]); printf("\n"); } } else { printf("%d\n", tmp + 2); for (int i = 1; i <= tmp; i++) { printf("%d\n", t[i].size()); for (int j = 0; j <= t[i].size() - 1; j++) printf("%d ", t[i][j]); printf("\n"); } int sum = 0; for (int i = tmp + 1; i <= g1; i++) sum += t[i].size(); printf("%d\n", sum); for (int i = tmp + 1; i <= g1; i++) for (int j = 0; j <= t[i].size() - 1; j++) printf("%d ", t[i][j]); printf("\n"); printf("%d\n", g1 - tmp); for (int i = g1; i > tmp; i--) printf("%d ", t[i][0]); printf("\n"); } return 0; }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> inline int rint() { int x = 0; char s = getchar(); for (; s < '0' || '9' < s; s = getchar()) ; for (; '0' <= s && s <= '9'; s = getchar()) x = x * 10 + (s ^ '0'); return x; } template <typename Tp> inline void wint(Tp x) { if (x < 0) putchar('-'), x = -x; if (9 < x) wint(x / 10); putchar(x % 10 ^ '0'); } const int MAXN = 2e5; int n, s, ecnt = 1, a[MAXN + 5], tmp[MAXN + 5], rk[MAXN + 5], head[MAXN * 2 + 5]; bool vis[MAXN + 5]; struct Edge { int to, nxt; } graph[MAXN * 2 + 5]; std::vector<std::vector<int> > ans; inline void link(const int s, const int t) { graph[++ecnt].to = t, graph[ecnt].nxt = head[s]; head[s] = ecnt; } inline void findEC(const int u, std::vector<int>& res) { if (u <= n) vis[u] = true; for (int &i = head[u], v; i;) { v = graph[i].to, i = graph[i].nxt; findEC(v, res); } if (u <= n) res.push_back(u); } inline void printEC(const std::vector<int>& path) { for (int i = path.size() - 1; ~i; --i) { wint(path[i]); if (i) putchar(' '); } } int main() { n = rint(), s = rint(); for (int i = 1; i <= n; ++i) a[i] = tmp[i] = rint(); std::sort(tmp + 1, tmp + n + 1); int mx = std::unique(tmp + 1, tmp + n + 1) - tmp - 1; for (int i = 1; i <= n; ++i) { a[i] = std::lower_bound(tmp + 1, tmp + mx + 1, a[i]) - tmp; ++rk[a[i]]; } for (int i = 2; i <= mx; ++i) rk[i] += rk[i - 1]; int cnt = 0; for (int i = 1; i <= n; ++i) { if (rk[a[i] - 1] < i && i <= rk[a[i]]) continue; link(i, n + a[i]), ++cnt; } if ((s -= cnt) < 0) return puts("-1"), 0; for (int i = 1; i <= mx; ++i) { for (int j = rk[i - 1] + 1; j <= rk[i]; ++j) { if (a[j] == i) continue; link(n + i, j); } } std::vector<int> tmp; for (int i = 1; i <= n; ++i) { if (!vis[i]) { tmp.clear(); findEC(i, tmp); if (tmp.size() == 1) continue; tmp.pop_back(); ans.push_back(tmp); } } int sz = ans.size(); if (s > sz) s = sz; int norid = 0; if (s > 2) sz += 2 - s; wint(sz), putchar('\n'); if (s > 2) { int firs = 0; for (int i = 0; i < s; ++i) firs += ans[i].size(); wint(firs), putchar('\n'); for (int i = 0; i < s; ++i) { printEC(ans[i]), putchar(i + 1 < s ? ' ' : '\n'); } wint(s), putchar('\n'); wint(ans[0].back()); for (int i = s - 1; i; --i) { putchar(' '), wint(ans[i].back()); } putchar('\n'); norid = s; } for (; norid ^ ans.size(); ++norid) { wint(ans[norid].size()), putchar('\n'); printEC(ans[norid]), putchar('\n'); } return 0; }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> int n, s, a[200010], b[200010], nums[200010], cnt, fa[200010]; int find(int i) { return fa[i] == i ? i : fa[i] = find(fa[i]); } bool tag[200010]; struct edge { int to; edge* next; } E[200010], *fir[200010]; std::vector<int> C[200010]; void dfs(int i, int t) { while (fir[i]) { edge* e = fir[i]; fir[i] = e->next; dfs(e->to, t); C[t].push_back(e - E); } } void mf() { for (int i = 0; i < n; i++) fa[i] = i, tag[i] = 0; for (int i = 0; i < n; i++) if (find(a[i]) != find(b[i])) fa[find(a[i])] = find(b[i]), tag[find(b[i])] = 1; } void me() { for (int i = 0; i < n; i++) fir[i] = 0; for (int i = 0; i < n; i++) if (a[i] != b[i]) { E[i] = (edge){b[i], fir[a[i]]}; fir[a[i]] = E + i; } } int rs[200010]; void cyc(int c) { if (c < 2) return; int v = a[rs[c - 1]]; for (int i = c; --i;) a[rs[i]] = a[rs[i - 1]]; a[rs[0]] = v; } int main() { scanf("%d%d", &n, &s); for (int i = 0; i < n; i++) scanf("%d", a + i), b[i] = a[i], nums[cnt++] = a[i]; std::sort(b, b + n); std::sort(nums, nums + cnt); cnt = std::unique(nums, nums + cnt) - nums; for (int i = 0; i < n; i++) { a[i] = std::lower_bound(nums, nums + cnt, a[i]) - nums; b[i] = std::lower_bound(nums, nums + cnt, b[i]) - nums; } int t = 0; for (int i = 0; i < n; i++) if (a[i] != b[i]) t++; if (t > s) return puts("-1"), 0; mf(); me(); int tot = 0; for (int i = 0; i < n; i++) if (fa[i] == i && tag[i]) rs[tot++] = fir[i] - E; if (t + tot > s) tot = s - t; cyc(tot); mf(); me(); for (int i = t = 0; i < n; i++) if (fa[i] == i && tag[i]) dfs(i, t++); printf("%d\n", t + (tot > 1)); if (tot > 1) { printf("%d\n", tot); for (int i = 0; i < tot; i++) printf("%d%c", rs[i] + 1, " \n"[i == tot - 1]); } for (int i = 0; i < t; i++) { printf("%d\n", C[i].size()); for (int j = 0; j < C[i].size(); j++) printf("%d%c", C[i][j] + 1, " \n"[j == C[i].size() - 1]); } }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int MAXN = 1 << 20; int arr[MAXN], srt[MAXN]; bool bio[MAXN]; vector<pair<int, int>> edges[MAXN]; vector<vector<int>> cyc; void euler(int i) { bio[i] = true; while (!edges[i].empty()) { auto x = edges[i].back(); edges[i].pop_back(); euler(x.first); cyc.back().push_back(x.second); } } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int n, s, m, i, j; unordered_map<int, int> cpr; cin >> n >> s; for (i = 0; i < n; ++i) { cin >> arr[i]; srt[i] = arr[i]; } sort(srt, srt + n); for (i = j = 0; i < n; ++i) { if (i && srt[i - 1] != srt[i]) { ++j; cpr[srt[i]] = j; } } m = j + 1; for (i = 0; i < n; ++i) { arr[i] = cpr[arr[i]]; srt[i] = cpr[srt[i]]; } for (i = 0; i < n; ++i) if (arr[i] != srt[i]) edges[arr[i]].push_back({srt[i], i}); for (i = 0; i < m; ++i) if (!bio[i]) { cyc.emplace_back(); euler(i); if (cyc.back().empty()) cyc.pop_back(); else s -= cyc.back().size(); } if (s < 0) { cout << "-1\n"; return 0; } if (s <= 2 || cyc.size() <= 2) { cout << cyc.size() << '\n'; for (const auto& x : cyc) { cout << x.size() << '\n'; for (int y : x) cout << y + 1 << ' '; cout << '\n'; } return 0; } if (s > cyc.size()) s = cyc.size(); cout << cyc.size() - s + 2 << '\n'; m = 0; for (i = 0; i < s; ++i) { m += cyc[i].size(); } cout << m << '\n'; for (i = 0; i < s; ++i) { for (int y : cyc[i]) cout << y + 1 << ' '; } cout << s << '\n'; for (i = s - 1; i >= 0; --i) { cout << cyc[i].front() + 1 << ' '; } cout << '\n'; for (i = s; i < cyc.size(); ++i) { cout << cyc[i].size() << '\n'; for (int y : cyc[i]) cout << y + 1 << ' '; cout << '\n'; } }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int A[200005], B[200005], Q[200005]; pair<int, int> P[200005]; vector<int> getcycle(int x) { int init = x; vector<int> ret; do { ret.push_back(x); x = Q[x]; } while (x != init); return ret; } int par[200005], pos[200005]; int root(int a) { if (par[a] == a) return a; return par[a] = root(par[a]); } bool join(int x, int y) { int a = root(x), b = root(y); if (a == b) return false; par[a] = b; return true; } int main() { int n, s; scanf("%d%d", &n, &s); for (int i = 0; i < n; ++i) { scanf("%d", &A[i]); B[i] = A[i]; } sort(B, B + n); int m = 0; for (int i = 0; i < n; ++i) { if (B[i] != A[i]) { P[m] = {A[i], m}; pos[m] = i + 1; ++m; } } sort(P, P + m); vector<pair<int, int>> merges; for (int i = 0; i < m; ++i) { if (i && P[i].first == P[i - 1].first) { merges.emplace_back(P[i].second, P[i - 1].second); } Q[P[i].second] = i; } vector<bool> used(m); iota(par, par + m, 0); int numcycles = 0; for (int i = 0; i < m; ++i) { if (used[i]) continue; ++numcycles; auto cyc = getcycle(i); for (int i = 0; i < cyc.size(); ++i) { used[cyc[i]] = true; if (i) join(cyc[i - 1], cyc[i]); } } for (auto merge : merges) { if (join(merge.first, merge.second)) { swap(Q[merge.first], Q[merge.second]); --numcycles; } } if (m > s) { printf("-1\n"); return 0; } int l = s - m; vector<vector<int>> ans; if (l > 1 && numcycles > 1) { fill(used.begin(), used.end(), 0); vector<int> firsts; for (int i = 0; i < m && firsts.size() < l; ++i) { if (used[i]) continue; auto cyc = getcycle(i); for (int i = 0; i < cyc.size(); ++i) { used[cyc[i]] = true; } firsts.push_back(i); } ans.push_back(firsts); assert(firsts.size() >= 2); int qb = Q[firsts.back()]; for (int i = firsts.size() - 1; i; --i) { Q[firsts[i]] = Q[firsts[i - 1]]; } Q[0] = qb; } fill(used.begin(), used.end(), 0); for (int i = 0; i < m; ++i) { if (used[i]) continue; auto cyc = getcycle(i); for (int i = 0; i < cyc.size(); ++i) { used[cyc[i]] = true; } ans.push_back(cyc); } printf("%d\n", ans.size()); for (auto &vec : ans) { printf("%d\n", vec.size()); for (int x : vec) { printf("%d ", pos[x]); } printf("\n"); } }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const long long big = 1000000007; const long long mod = 998244353; long long n, m, k, T, q; vector<long long> A, A2; map<long long, long long> M; long long d = 0; long long pek[200001] = {0}; long long deg[200001] = {0}; long long par(long long i) { long long i2 = i; while (i2 != pek[i2]) { i2 = pek[i2]; } return i2; } void merg(long long i, long long j) { long long i2 = par(i); long long j2 = par(j); if (i2 != j2) { if (deg[i2] < deg[j2]) swap(i2, j2); deg[i2] += deg[j2]; pek[j2] = i2; } } vector<long long> extramove; vector<long long> thing; vector<set<long long> > C(400001, set<long long>()); long long indeg[400001] = {0}; long long outdeg[400001] = {0}; vector<vector<long long> > anses; vector<long long> eulertour(int i) { vector<long long> ANS; vector<long long> vts; long long j = i; while (outdeg[j] > 0) { vts.push_back(j); long long j2 = j; j = *(C[j].begin()); C[j2].erase(j); outdeg[j2]--; indeg[j]--; } ANS.push_back(i); for (int c1 = 1; c1 < (int)(vts).size(); c1++) { vector<long long> nt = eulertour(vts[c1]); for (int c2 = 0; c2 < (int)(nt).size(); c2++) { ANS.push_back(nt[c2]); } if ((int)(nt).size() > 1) { ANS.push_back(vts[c1]); } } return ANS; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); long long a, b, c; cin >> n >> m; for (int c1 = 0; c1 < n; c1++) { cin >> a; A.push_back(a); A2.push_back(a); } sort(A2.begin(), A2.end()); for (int c1 = 0; c1 < n; c1++) { if (M.find(A2[c1]) == M.end()) { M[A2[c1]] = d; d++; } } for (int c1 = 0; c1 < n; c1++) { A[c1] = M[A[c1]]; A2[c1] = M[A2[c1]]; } long long nonfix = 0; for (int c1 = 0; c1 < n; c1++) { if (A[c1] != A2[c1]) nonfix++; } if (m < nonfix) { cout << "-1\n"; return 0; } vector<long long> B; vector<long long> B2; for (int c1 = 0; c1 < n; c1++) { if (A[c1] != A2[c1]) { B.push_back(A[c1]); B2.push_back(A2[c1]); thing.push_back(c1 + 1); } } A.clear(); A2.clear(); n = (int)(B).size(); if (n == 0) { cout << "0\n"; return 0; } for (int c1 = 0; c1 < n; c1++) { A.push_back(B[c1]); A2.push_back(B2[c1]); } d = 0; M.clear(); for (int c1 = 0; c1 < n; c1++) { if (M.find(A2[c1]) == M.end()) { M[A2[c1]] = d; d++; } } for (int c1 = 0; c1 < n; c1++) { A[c1] = M[A[c1]]; A2[c1] = M[A2[c1]]; } for (int c1 = 0; c1 < d; c1++) { deg[c1] = 1; pek[c1] = c1; } long long comps = d; for (int c1 = 0; c1 < n; c1++) { if (par(A[c1]) != par(A2[c1])) { merg(A[c1], A2[c1]); comps--; } } long long leftovers = m - n; long long ans = 0; if (leftovers >= 3 && comps > 2) { extramove.push_back(0); leftovers--; for (int c1 = 0; c1 < n; c1++) { if (leftovers == 0) break; if (par(A[0]) != par(A[c1])) { leftovers--; merg(A[0], A[c1]); extramove.push_back(c1); } } long long old = A[extramove[(int)(extramove).size() - 1]]; for (int c1 = (int)(extramove).size() - 1; c1 >= 1; c1--) { A[extramove[c1]] = A[extramove[c1 - 1]]; } A[0] = old; ans++; } for (int c1 = 0; c1 < n; c1++) { if (A[c1] != A2[c1]) { C[A2[c1] + n].insert(c1); C[c1].insert(A[c1] + n); indeg[c1]++; outdeg[c1]++; indeg[A[c1] + n]++; outdeg[A2[c1] + n]++; } } for (int c1 = 0; c1 < n; c1++) { if (indeg[c1] > 0) { vector<long long> AA = eulertour(c1); vector<long long> BB; for (int c2 = 0; c2 < (int)(AA).size(); c2 += 2) { BB.push_back(AA[c2]); } anses.push_back(BB); } } cout << ans + (int)(anses).size() << "\n"; if ((int)(extramove).size() > 0) { cout << (int)(extramove).size() << "\n"; for (int c1 = 0; c1 < (int)(extramove).size(); c1++) { cout << thing[extramove[c1]] << " "; } cout << "\n"; } for (int c2 = 0; c2 < (int)(anses).size(); c2++) { cout << (int)(anses[c2]).size() << "\n"; for (int c1 = 0; c1 < (int)(anses[c2]).size(); c1++) { cout << thing[anses[c2][c1]] << " "; } cout << "\n"; } return 0; }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int a[200010], p[200010], n, s; pair<int, int> b[200010]; int fa[200010], r[200010]; int get_fa(int x) { if (fa[x] == 0) return x; fa[x] = get_fa(fa[x]); return fa[x]; } void merge(int x, int y) { int fx = get_fa(x); int fy = get_fa(y); if (fx != fy) fa[fx] = fy; } bool check(int x, int y) { return get_fa(x) == get_fa(y); } int now; bool used[200010]; vector<int> ans[200010]; void dfs(int x) { used[x] = 1; ans[now].push_back(x); if (used[p[x]] == 0) dfs(p[x]); } int main() { scanf("%d%d", &n, &s); for (int i = 1; i <= n; i++) { scanf("%d", &a[i]); b[i] = make_pair(a[i], i); } sort(b + 1, b + 1 + n); for (int i = 1; i <= n; i++) p[b[i].second] = i; for (int i = 1; i <= n; i++) if (a[i] == b[i].first && p[i] != i) { p[b[i].second] = p[i]; b[p[i]].second = b[i].second; p[i] = i; b[i].second = i; } for (int i = 1; i <= n; i++) if (p[i] != i) merge(p[i], i); int las = 0; for (int i = 1; i <= n; i++) { if (p[b[i].second] == b[i].second) continue; if (las >= 1 && a[las] == a[b[i].second]) { int x = las; int y = b[i].second; if (check(x, y)) continue; merge(x, y); swap(p[x], p[y]); } las = b[i].second; } now = 0; for (int i = 1; i <= n; i++) if (used[i] == 0 && p[i] != i) { now++; dfs(i); } int sum = 0; for (int i = 1; i <= now; i++) sum += ans[i].size(); if (sum > s) { printf("-1"); return 0; } s -= sum; s = min(s, now); if (s <= 1) { printf("%d\n", now); for (int i = 1; i <= now; i++) { printf("%d\n", ans[i].size()); for (int j = 0; j < ans[i].size(); j++) printf("%d ", ans[i][j]); puts(""); } return 0; } printf("%d\n", now - s + 2); for (int i = 1; i <= now - s; i++) { printf("%d\n", ans[i + s].size()); for (int j = 0; j < ans[i + s].size(); j++) printf("%d ", ans[i + s][j]); puts(""); sum -= ans[i + s].size(); } printf("%d\n", sum); for (int i = 1; i <= s; i++) for (int j = 0; j < ans[i].size(); j++) printf("%d ", ans[i][j]); printf("\n%d\n", s); for (int i = s; i > 0; i--) printf("%d ", ans[i][0]); }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int M = 0, fst[666666], vb[666666], nxt[666666], vc[666666]; void ad_de(int a, int b, int c) { ++M; nxt[M] = fst[a]; fst[a] = M; vb[M] = b; vc[M] = c; } void adde(int a, int b, int c) { ad_de(a, b, c); ad_de(b, a, c); } map<int, int> vis; int n, s, a[666666], b[666666], id, vv[666666], ed; vector<int> cur, vs[666666]; int vn; void dfs(int x) { for (int& e = fst[x]; e; e = nxt[e]) if (!vv[vc[e]]) { int c = vc[e]; vv[vc[e]] = 1; dfs(vb[e]); cur.push_back(c); } } int main() { scanf("%d%d", &n, &s); for (int i = 1; i <= n; ++i) scanf("%d", a + i), vis[a[i]] = 1; for (auto& t : vis) t.second = ++id; for (int i = 1; i <= n; ++i) b[i] = a[i] = vis[a[i]]; sort(b + 1, b + 1 + n); for (int i = 1; i <= n; ++i) if (a[i] != b[i]) ++ed, ad_de(a[i], b[i], i); if (ed > s) { puts("-1"); return 0; } for (int i = 1; i <= id; ++i) if (fst[i]) { dfs(i); vs[++vn] = cur; cur.clear(); } int tj = min(vn, s - ed); if (tj <= 2) tj = 0; vector<int> ta, tb; for (int i = 1; i <= tj; ++i) { ta.insert(ta.end(), vs[vn].begin(), vs[vn].end()); tb.push_back(vs[vn--].back()); } if (tj) { reverse(tb.begin(), tb.end()); vs[++vn] = tb; vs[++vn] = ta; } printf("%d\n", vn); for (int i = 1; i <= vn; ++i) { printf("%d\n", int(vs[i].size())); for (auto g : vs[i]) printf("%d ", g); puts(""); } }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int n, m, a[200010], b[200010], x[200010], y[200010], f[200010], r[200010], c[200010], p, d[200010], q; map<int, int> h; inline int fa(int i) { return f[i] == i ? i : f[i] = fa(f[i]); } inline void unit(int i, int j) { i = fa(i); j = fa(j); if (i != j) f[i] = j; } int main() { int i, j, k; scanf("%d%d", &n, &m); for (i = 1; i <= n; i++) { scanf("%d", &a[i]); b[i] = a[i]; } sort(b + 1, b + n + 1); for (i = 1; i <= n; i++) if (a[i] != b[i]) m--; if (m < 0) { printf("-1\n"); return 0; } for (i = 1; i <= n; i++) f[i] = i; for (i = n; i > 0; i--) if (a[i] != b[i]) { r[i] = h[b[i]]; h[b[i]] = i; } for (i = 1; i <= n; i++) if (a[i] != b[i]) { x[i] = h[a[i]]; y[x[i]] = i; h[a[i]] = r[h[a[i]]]; unit(i, x[i]); } for (i = 1, j = 0; i <= n; i++) if (a[i] != b[i]) { if (j && b[i] == b[j]) { if (fa(i) != fa(j)) { f[fa(i)] = fa(j); swap(y[i], y[j]); x[y[i]] = i; x[y[j]] = j; } } j = i; } for (i = 1; i <= n; i++) if (a[i] != b[i] && fa(i) == i) c[++p] = i; if (m >= 3 && p >= 3) { m = min(m, p); for (i = m; i > 0; i--) d[++q] = x[c[i]]; for (i = 1; i < m; i++) { swap(x[c[i]], x[c[i + 1]]); f[fa(c[i])] = fa(c[i + 1]); } } p = 0; for (i = 1; i <= n; i++) if (a[i] != b[i] && fa(i) == i) c[++p] = i; printf("%d\n", p + (q > 0)); for (i = 1; i <= n; i++) if (a[i] != b[i] && fa(i) == i) { for (j = x[i], k = 1; j != i; j = x[j]) k++; printf("%d\n%d", k, i); for (j = x[i], k = 1; j != i; j = x[j]) printf(" %d", j); printf("\n"); } if (q) { printf("%d\n", q); for (i = 1; i <= q; i++) printf("%d ", d[i]); printf("\n"); } return 0; }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; char buf[25]; const int maxn = 200010; struct node { int w, id; } c[maxn], d[maxn]; vector<int> v[maxn]; int a[maxn], b[maxn], f[maxn], q[maxn]; bool p[maxn]; int n, m, s, ans; int read() { int x = 0, f = 0; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') f = 1; ch = getchar(); } while (ch >= '0' && ch <= '9') { x = (x << 1) + (x << 3) + (ch ^ 48); ch = getchar(); } return f ? -x : x; } void write(int x) { if (!x) { putchar('0'); return; } if (x < 0) { putchar('-'); x = -x; } int cnt = 0; while (x) { buf[++cnt] = '0' + x % 10; x /= 10; } for (int i = cnt; i >= 1; --i) putchar(buf[i]); } bool cmp(node x, node y) { return x.w < y.w; } int find(int x) { if (f[x] != x) f[x] = find(f[x]); return f[x]; } void unions(int x, int y) { int l = find(x), r = find(y); if (l != r) f[l] = r; } int main() { n = read(); s = read(); for (int i = 1; i <= n; ++i) { a[i] = b[i] = read(); } sort(b + 1, b + 1 + n); for (int i = 1; i <= n; ++i) if (a[i] != b[i]) { ++m; c[m] = d[m] = (node){a[i], i}; } if (m > s) { puts("-1"); return 0; } sort(d + 1, d + 1 + m, cmp); for (int i = 1; i <= m; ++i) f[c[i].id] = c[i].id; for (int i = 1; i <= m; ++i) unions(d[i].id, c[i].id); for (int i = 2; i <= m; ++i) if (d[i].w == d[i - 1].w && find(d[i].id) != find(d[i - 1].id)) { swap(d[i], d[i - 1]); unions(d[i].id, d[i - 1].id); } for (int i = 1; i <= m; ++i) q[d[i].id] = c[i].id, p[d[i].id] = true; for (int i = 1; i <= m; ++i) { int x = c[i].id; if (p[x]) { ++ans; while (p[x]) { p[x] = false; v[ans].push_back(x); x = q[x]; } } } int num = min(ans, s - m); if (num > 2) { write(ans - (num - 2)); putchar('\n'); int sum = 0; for (int i = 1; i <= num; ++i) sum += v[i].size(); write(sum); putchar('\n'); for (int i = 1; i <= num; ++i) { for (int j = 0; j <= v[i].size() - 1; ++j) { write(v[i][j]); if (i != num || j != v[i].size() - 1) putchar(' '); else putchar('\n'); } } write(num); putchar('\n'); for (int i = num; i >= 1; --i) { write(v[i][0]); if (i != 1) putchar(' '); else putchar('\n'); } for (int i = num + 1; i <= ans; ++i) { write(v[i].size()); putchar('\n'); for (int j = 0; j <= v[i].size() - 1; ++j) { write(v[i][j]); if (j != v[i].size() - 1) putchar(' '); else putchar('\n'); } } } else { write(ans); putchar('\n'); for (int i = 1; i <= ans; ++i) { write(v[i].size()); putchar('\n'); for (int j = 0; j <= v[i].size() - 1; ++j) { write(v[i][j]); if (j != v[i].size() - 1) putchar(' '); else putchar('\n'); } } } return 0; }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:167772160000") using namespace std; void redirectIO() { ios::sync_with_stdio(false); cin.tie(0); } int n; int a[210000]; map<int, vector<int>> numberPositions; int s; int sa[210000]; int badPos; vector<vector<int>> vectors; bool vis[210000]; int goesTo[210000]; void doVec(int pos) { vector<int> curVector; int initialNum = a[pos]; curVector.push_back(pos); int need = sa[pos]; vis[pos] = true; while (need != initialNum) { auto it = numberPositions.find(need); int curNum = it->second.back(); it->second.pop_back(); curVector.push_back(curNum); vis[curNum] = true; need = sa[curNum]; } vectors.push_back(curVector); curVector.clear(); } vector<vector<int>> answer; void doAnsVec(int pos) { int initialPos = pos; vis[pos] = true; answer.push_back(vector<int>()); answer.back().push_back(pos); pos = goesTo[pos]; while (!vis[pos]) { vis[pos] = true; answer.back().push_back(pos); pos = goesTo[pos]; } } int par[210000]; int find(int a) { if (a == par[a]) return a; return par[a] = find(par[a]); } void unite(int a, int b) { par[find(a)] = find(b); } bool FAIL; int curNum[210000]; void applyCycle(vector<int> a) { reverse(a.begin(), a.end()); int fr = curNum[a[a.size() - 1]]; for (int i = a.size() - 1; i > 0; i--) { curNum[a[i]] = curNum[a[i - 1]]; } curNum[a[0]] = fr; } bool isGood() { for (int i = 0; i < (n); i++) curNum[i] = a[i]; for (auto x : answer) applyCycle(x); for (int i = 0; i < (n); i++) if (curNum[i] != sa[i]) return false; return true; } void solve() { for (int i = 0; i < (n); i++) sa[i] = a[i]; answer.clear(); badPos = 0; vectors.clear(); for (int i = 0; i < (n); i++) vis[i] = false; FAIL = false; sort(sa, sa + n); for (int i = 0; i < (n); i++) { if (sa[i] != a[i]) { badPos++; numberPositions[a[i]].push_back(i); } } s -= badPos; if (s < 0) { FAIL = true; return; } for (int i = 0; i < (n); i++) { if (sa[i] != a[i] && !vis[i]) doVec(i); } for (int i = 0; i < (n); i++) par[i] = i; for (int i = 0; i < (n); i++) vis[i] = false; for (auto vec : vectors) { for (int i = 0; i < (vec.size()); i++) { int nxt = i + 1; if (i + 1 == vec.size()) nxt = 0; nxt = vec[nxt]; int cur = vec[i]; goesTo[nxt] = cur; unite(cur, nxt); } } numberPositions.clear(); for (int i = 0; i < (n); i++) { if (sa[i] != a[i]) { badPos++; numberPositions[a[i]].push_back(i); } } for (auto it = numberPositions.begin(); it != numberPositions.end(); it++) { for (int i = 0; i < (it->second.size() - 1); i++) { int cur = it->second[i]; int nxt = it->second[i + 1]; if (find(cur) != find(nxt)) { swap(goesTo[cur], goesTo[nxt]); unite(cur, nxt); } } } for (int i = 0; i < (n); i++) { if (a[i] != sa[i] && !vis[i]) doAnsVec(i); } int taking = min(s, (int)answer.size()); if (taking < 2) taking = 0; if (taking > 0) { vector<int> curLong; vector<int> curShort; while (taking) { for (auto x : answer.back()) curLong.push_back(x); curShort.push_back(answer.back()[0]); answer.pop_back(); taking--; } reverse(curShort.begin(), curShort.end()); answer.push_back(curLong); answer.push_back(curShort); } } int cnt; void gen() { cnt++; n = rand() % 8 + 1; for (int i = 0; i < (n); i++) a[i] = rand() % 20; s = rand() % 5 + n; solve(); if (!isGood()) { cout << "AAAAA"; exit(0); } } int main() { redirectIO(); cin >> n >> s; for (int i = 0; i < (n); i++) { cin >> a[i]; } solve(); if (FAIL) { cout << -1; return 0; } cout << answer.size() << endl; for (auto vec : answer) { cout << vec.size() << "\n"; for (auto x : vec) cout << x + 1 << " "; cout << "\n"; } return 0; }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; inline int read() { int n = 0; char c; for (c = getchar(); c < '0' || c > '9'; c = getchar()) ; for (; c >= '0' && c <= '9'; c = getchar()) n = n * 10 + c - 48; return n; } const int maxn = 2e5 + 5; int i, j, n, s, a[maxn], f[maxn], p[maxn], cnt, sum; vector<int> an[maxn]; bool bz[maxn]; pair<int, int> b[maxn]; int get(int x) { return f[x] ? f[x] = get(f[x]) : x; } void merge(int x, int y) { x = get(x), y = get(y); if (x != y) f[x] = y; } int main() { n = read(), s = read(); for (i = 1; i <= n; i++) a[i] = read(), b[i] = make_pair(a[i], i); sort(b + 1, b + 1 + n); for (i = 1; i <= n; i++) p[b[i].second] = i; for (i = 1; i <= n; i++) if (a[i] == b[i].first && p[i] != i) { p[b[i].second] = p[i]; b[p[i]].second = b[i].second; p[i] = i, b[i].second = i; } for (i = 1; i <= n; i++) if (p[i] != i) merge(p[i], i); int x = 0; for (i = 1; i <= n; i++) { int y = b[i].second; if (p[y] == y) continue; if (a[x] == a[y]) { if (get(x) == get(y)) continue; merge(x, y); swap(p[x], p[y]); } x = y; } for (i = 1; i <= n; i++) if (p[i] != i && !bz[i]) { cnt++, x = i; for (; !bz[x]; x = p[x]) sum++, bz[x] = 1, an[cnt].push_back(x); } if (s < sum) return puts("-1"), 0; s -= sum; s = min(s, cnt); if (s <= 2) { printf("%d\n", cnt); for (i = 1; i <= cnt; i++) { printf("%d\n", an[i].size()); for (j = 0; j < an[i].size(); j++) printf("%d ", an[i][j]); puts(""); } return 0; } printf("%d\n", cnt - s + 2); for (i = 1; i <= cnt - s; i++) { printf("%d\n", an[i].size()); for (j = 0; j < an[i].size(); j++) printf("%d ", an[i][j]); sum -= an[i].size(); puts(""); } printf("%d\n", sum); for (i = cnt - s + 1; i <= cnt; i++) for (j = 0; j < an[i].size(); j++) printf("%d ", an[i][j]); puts(""); printf("%d\n", s); for (i = cnt; i >= cnt - s + 1; i--) printf("%d ", an[i][0]); puts(""); }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using namespace rel_ops; using ll = int64_t; using Pii = pair<int, int>; using ull = uint64_t; using Vi = vector<int>; void run(); int main() { cin.sync_with_stdio(0); cin.tie(0); cout << fixed << setprecision(10); run(); return 0; } vector<vector<Pii>> G; vector<Vi> cycles; void dfs(int v) { while (!G[v].empty()) { Pii e = G[v].back(); G[v].pop_back(); dfs(e.first); cycles.back().push_back(e.second); } } void run() { int n, upper; cin >> n >> upper; Vi elems(n); for (auto& e : (elems)) cin >> e; Vi sorted(n); iota((sorted).begin(), (sorted).end(), 0); sort((sorted).begin(), (sorted).end(), [&](int l, int r) { return elems[l] < elems[r]; }); int last = elems[sorted[0]], k = 0; for (int i = (0); i < (n); i++) { int e = sorted[i]; if (elems[e] != last) k++; last = elems[e]; elems[e] = k; } k++; G.resize(k); for (int i = (0); i < (n); i++) if (elems[i] != elems[sorted[i]]) { G[elems[i]].push_back({elems[sorted[i]], i}); } int len = 0; for (int i = (0); i < (k); i++) if (!G[i].empty()) { cycles.emplace_back(); dfs(i); len += int((cycles.back()).size()); } if (len > upper) { cout << "-1\n"; return; } int toMerge = min(int((cycles).size()), upper - len); if (toMerge > 2) { Vi one, two; for (int i = (0); i < (toMerge); i++) { one.insert(one.end(), (cycles.back()).begin(), (cycles.back()).end()); two.push_back(cycles.back()[0]); cycles.pop_back(); } reverse((two).begin(), (two).end()); cycles.push_back(move(one)); cycles.push_back(move(two)); } cout << int((cycles).size()) << '\n'; for (auto& c : (cycles)) { cout << int((c).size()) << '\n'; for (auto& first : (c)) cout << first + 1 << ' '; cout << '\n'; } }
1012_E. Cycle sort
You are given an array of n positive integers a_1, a_2, ..., a_n. You can perform the following operation any number of times: select several distinct indices i_1, i_2, ..., i_k (1 ≤ i_j ≤ n) and move the number standing at the position i_1 to the position i_2, the number at the position i_2 to the position i_3, ..., the number at the position i_k to the position i_1. In other words, the operation cyclically shifts elements: i_1 → i_2 → … i_k → i_1. For example, if you have n=4, an array a_1=10, a_2=20, a_3=30, a_4=40, and you choose three indices i_1=2, i_2=1, i_3=4, then the resulting array would become a_1=20, a_2=40, a_3=30, a_4=10. Your goal is to make the array sorted in non-decreasing order with the minimum number of operations. The additional constraint is that the sum of cycle lengths over all operations should be less than or equal to a number s. If it's impossible to sort the array while satisfying that constraint, your solution should report that as well. Input The first line of the input contains two integers n and s (1 ≤ n ≤ 200 000, 0 ≤ s ≤ 200 000)—the number of elements in the array and the upper bound on the sum of cycle lengths. The next line contains n integers a_1, a_2, ..., a_n—elements of the array (1 ≤ a_i ≤ 10^9). Output If it's impossible to sort the array using cycles of total length not exceeding s, print a single number "-1" (quotes for clarity). Otherwise, print a single number q— the minimum number of operations required to sort the array. On the next 2 ⋅ q lines print descriptions of operations in the order they are applied to the array. The description of i-th operation begins with a single line containing one integer k (1 ≤ k ≤ n)—the length of the cycle (that is, the number of selected indices). The next line should contain k distinct integers i_1, i_2, ..., i_k (1 ≤ i_j ≤ n)—the indices of the cycle. The sum of lengths of these cycles should be less than or equal to s, and the array should be sorted after applying these q operations. If there are several possible answers with the optimal q, print any of them. Examples Input 5 5 3 2 3 1 1 Output 1 5 1 4 2 3 5 Input 4 3 2 1 4 3 Output -1 Input 2 0 2 2 Output 0 Note In the first example, it's also possible to sort the array with two operations of total length 5: first apply the cycle 1 → 4 → 1 (of length 2), then apply the cycle 2 → 3 → 5 → 2 (of length 3). However, it would be wrong answer as you're asked to use the minimal possible number of operations, which is 1 in that case. In the second example, it's possible to the sort the array with two cycles of total length 4 (1 → 2 → 1 and 3 → 4 → 3). However, it's impossible to achieve the same using shorter cycles, which is required by s=3. In the third example, the array is already sorted, so no operations are needed. Total length of empty set of cycles is considered to be zero.
{ "input": [ "5 5\n3 2 3 1 1\n", "4 3\n2 1 4 3\n", "2 0\n2 2\n" ], "output": [ "1\n5\n1 4 2 3 5 \n", "-1\n", "0\n" ] }
{ "input": [ "5 0\n884430748 884430748 708433020 708433020 708433020\n", "2 1\n1 1\n", "2 0\n2 1\n", "5 2\n65390026 770505072 65390026 65390026 65390026\n", "5 4\n812067558 674124159 106041640 106041640 674124159\n", "5 4\n167616600 574805150 651016425 150949603 379708534\n", "5 5\n472778319 561757623 989296065 99763286 352037329\n", "5 4\n201429826 845081337 219611799 598937628 680006294\n", "2 2\n2 1\n", "5 0\n1000000000 1000000000 1000000000 1000000000 1000000000\n", "5 6\n971458729 608568364 891718769 464295315 98863653\n", "5 5\n641494999 641494999 228574099 535883079 535883079\n", "5 5\n815605413 4894095 624809427 264202135 152952491\n", "1 0\n258769137\n", "2 0\n1 1\n", "1 0\n2\n", "5 4\n335381650 691981363 691981363 335381650 335381650\n", "5 4\n81224924 319704343 319704343 210445208 128525140\n", "5 4\n579487081 564229995 665920667 665920667 644707366\n", "5 200000\n682659092 302185582 518778252 29821187 14969298\n" ], "output": [ "-1\n", "0\n", "-1\n", "1\n2\n2 5 \n", "-1\n", "-1\n", "1\n5\n1 3 5 2 4 \n", "1\n4\n2 5 4 3 \n", "1\n2\n1 2 \n", "0\n", "2\n2\n1 5\n3\n2 3 4\n", "1\n5\n1 4 2 5 3 \n", "2\n3\n1 5 2\n2\n3 4\n", "0\n", "0\n", "0\n", "1\n4\n2 4 3 5 \n", "1\n4\n2 4 3 5 \n", "2\n2\n1 2\n2\n3 5\n", "2\n2\n1 5\n3\n2 3 4\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int N = 520233; int n, s, a[N], b[N]; int fa[N]; int Find(int x) { return (fa[x] == x) ? x : (fa[x] = Find(fa[x])); } map<int, int> mp; bool Merge(int x, int y) { if (Find(x) != Find(y)) { fa[Find(x)] = Find(y); return true; } return false; } int p[N], cnt; vector<int> v[N]; bool vis[N]; void Dfs(int u) { v[cnt].emplace_back(u); vis[u] = true; if (!vis[p[u]]) Dfs(p[u]); } map<int, vector<int> > lst; map<int, int> rec; int main() { scanf("%d%d", &n, &s); for (int i = 1; i <= n; ++i) { scanf("%d", a + i); b[i] = a[i]; fa[i] = i; } sort(b + 1, b + n + 1); int least = 0; for (int i = 1; i <= n; ++i) if (a[i] != b[i]) { ++least; lst[b[i]].emplace_back(i); } if (least > s) { puts("-1"); return 0; } else if (!least) { puts("0"); return 0; } for (int i = 1; i <= n; ++i) if (a[i] != b[i]) { vector<int>& v = lst[a[i]]; p[i] = v.back(); Merge(i, p[i]); v.pop_back(); } for (int i = 1; i <= n; ++i) if (a[i] != b[i]) { int& t = rec[a[i]]; if (t && Merge(i, t)) swap(p[i], p[t]); t = i; } for (int i = 1; i <= n; ++i) if (a[i] != b[i] && !vis[i]) { ++cnt; Dfs(i); } int q = s - least; if (q > 1) --q; else q = 0; q = min(q, cnt - 1); if (q) { printf("%d\n", cnt - q + 1); int sm = 0; for (int i = 1; i <= q + 1; ++i) sm += v[i].size(); printf("%d\n", sm); for (int i = 1; i <= q + 1; ++i) for (int j : v[i]) printf("%d ", j); putchar('\n'); printf("%d\n", q + 1); for (int i = q + 1; i; --i) printf("%d ", v[i][0]); putchar('\n'); for (int i = q + 2; i <= cnt; ++i) { printf("%d\n", (int)v[i].size()); for (int j : v[i]) printf("%d ", j); putchar('\n'); } } else { printf("%d\n", cnt); for (int i = 1; i <= cnt; ++i) { printf("%d\n", (int)v[i].size()); for (int j : v[i]) printf("%d ", j); putchar('\n'); } } return 0; }