当前位置:   article > 正文

Codeforces Global Round 25 ---- F. Inversion Composition ---- 题解_f inversion composition

f inversion composition

F. Inversion Composition:

题目描述:

思路解析:

有一个初始排列 p,然后现在让我们给出一个q排列数组,并通过p数组和q数组得到qp数组,问是否能得到某组q和qp数组使他们的逆序对数量之和为k。

假设p有一对 (i,j) 为 (pi,pj) -> (2,3), 如果q2 < q3, 那么提供的答案贡献为0,否则会提供2的答案贡献。 假设p有一对 (i,j) 为 (pi,pj) -> (3,2) , 如果p2 > p3,那么提供的答案贡献为1,否则还是提供1的答案贡献。 从这里可以分析出,p的逆序对数量应该和k奇偶性相同。

那么这样,我们就可以知道qp数组我们需要多少对逆序队了,利用这个来求得qp数组,再反解q数组,并且要保证p数组的逆序对要出现在qp中。

代码实现:

  1.  
  2. import java.io.*;
  3. import java.math.BigInteger;
  4. import java.util.*;
  5.  
  6.  
  7. public class Main {
  8. static int inf = (int) 1e9;
  9.  
  10. static int[] t = new int[300005];
  11. public static void main(String[] args) throws IOException {
  12.  
  13. int T = f.nextInt();
  14. while (T > 0) {
  15. solve();
  16. T--;
  17. for (int i = 1; i <= n; i++) {
  18. t[i] = 0;
  19. }
  20. }
  21. w.flush();
  22. w.close();
  23. br.close();
  24. }
  25.  
  26.  
  27.  
  28. static int n;
  29. public static void solve() {
  30. n = f.nextInt();
  31. long k = f.nextLong();
  32. int[] a = new int[n];
  33. long m = 0;
  34. int[] cnt = new int[n];
  35. int[] ip = new int[n];
  36. for (int i = 0; i < n; i++) {
  37. a[i] = f.nextInt() - 1;
  38. ip[a[i]] = i;
  39. cnt[i] = sum(a[i]);
  40. m += i - cnt[i];
  41. upd(a[i] + 1, 1);
  42. }
  43.  
  44. if (m > k || k > (long) (n-1) * n - m || (k - m) % 2 == 1){
  45. w.println("NO");
  46. return;
  47. }
  48. k = (k - m) / 2;
  49. int[] qp = new int[n+1];
  50. for (int i = 0; i < n; i++) {
  51. if (k > cnt[i]){
  52. k-=cnt[i];
  53. }else {
  54. for (int j = 0, v = i; j < i; j++) {
  55. qp[j] = v--;
  56. if (a[j] < a[i] && --k == 0){
  57. qp[i] = v--;
  58. }
  59. }
  60. for (int j = i+1; j < n; j++) {
  61. qp[j] = j;
  62. }
  63. break;
  64. }
  65. }
  66. w.println("YES");
  67. for (int i = 0;i < n; i++) {
  68. w.print(qp[ip[i]] + 1 + " ");
  69. }
  70. w.println();
  71.  
  72. }
  73. static int lowbit(int x) {return x & -x;}
  74. static void upd(int x, int val){
  75. for (int i = x; i <= n; i+=lowbit(i)) {
  76. t[i] += val;
  77. }
  78. }
  79.  
  80. static int sum(int x){
  81. int res = 0;
  82. for (int i = x; i >= 1; i-=lowbit(i)) {
  83. res += t[i];
  84. }
  85. return res;
  86. }
  87.  
  88.  
  89. static PrintWriter w = new PrintWriter(new OutputStreamWriter(System.out));
  90. static Input f = new Input(System.in);
  91. static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  92.  
  93. static class Input {
  94. public BufferedReader reader;
  95. public StringTokenizer tokenizer;
  96.  
  97. public Input(InputStream stream) {
  98. reader = new BufferedReader(new InputStreamReader(stream), 32768);
  99. tokenizer = null;
  100. }
  101.  
  102. public String next() {
  103. while (tokenizer == null || !tokenizer.hasMoreTokens()) {
  104. try {
  105. tokenizer = new StringTokenizer(reader.readLine());
  106. } catch (IOException e) {
  107. throw new RuntimeException(e);
  108. }
  109. }
  110. return tokenizer.nextToken();
  111. }
  112.  
  113.  
  114. public int nextInt() {
  115. return Integer.parseInt(next());
  116. }
  117.  
  118. public long nextLong() {
  119. return Long.parseLong(next());
  120. }
  121.  
  122. public Double nextDouble() {
  123. return Double.parseDouble(next());
  124. }
  125.  
  126. public BigInteger nextBigInteger() {
  127. return new BigInteger(next());
  128. }
  129. }
  130. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/1004019
推荐阅读
相关标签
  

闽ICP备14008679号