Codeforces Global Round 25 ---- F. Inversion Composition ---- 题解

2024-04-16 05:04

本文主要是介绍Codeforces Global Round 25 ---- 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中。

代码实现:

 
import java.io.*;
import java.math.BigInteger;
import java.util.*;public class Main {static int inf = (int) 1e9;static int[] t = new int[300005];public static void main(String[] args) throws IOException {int T = f.nextInt();while (T > 0) {solve();T--;for (int i = 1; i <= n; i++) {t[i] = 0;}}w.flush();w.close();br.close();}static int n;public static void solve() {n = f.nextInt();long k = f.nextLong();int[] a = new int[n];long m = 0;int[] cnt = new int[n];int[] ip = new int[n];for (int i = 0; i < n; i++) {a[i] = f.nextInt() - 1;ip[a[i]] = i;cnt[i] = sum(a[i]);m += i - cnt[i];upd(a[i] + 1, 1);}if (m > k || k > (long) (n-1) * n - m || (k - m) % 2 == 1){w.println("NO");return;}k = (k - m) / 2;int[] qp = new int[n+1];for (int i = 0; i < n; i++) {if (k > cnt[i]){k-=cnt[i];}else {for (int j = 0, v = i; j < i; j++) {qp[j] = v--;if (a[j] < a[i] && --k == 0){qp[i] = v--;}}for (int j = i+1; j < n; j++) {qp[j] = j;}break;}}w.println("YES");for (int i = 0;i < n; i++) {w.print(qp[ip[i]] + 1 + " ");}w.println();}static int lowbit(int x) {return x & -x;}static void upd(int x, int val){for (int i = x; i <= n; i+=lowbit(i)) {t[i] += val;}}static int sum(int x){int res = 0;for (int i = x; i >= 1; i-=lowbit(i)) {res += t[i];}return res;}static PrintWriter w = new PrintWriter(new OutputStreamWriter(System.out));static Input f = new Input(System.in);static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));static class Input {public BufferedReader reader;public StringTokenizer tokenizer;public Input(InputStream stream) {reader = new BufferedReader(new InputStreamReader(stream), 32768);tokenizer = null;}public String next() {while (tokenizer == null || !tokenizer.hasMoreTokens()) {try {tokenizer = new StringTokenizer(reader.readLine());} catch (IOException e) {throw new RuntimeException(e);}}return tokenizer.nextToken();}public int nextInt() {return Integer.parseInt(next());}public long nextLong() {return Long.parseLong(next());}public Double nextDouble() {return Double.parseDouble(next());}public BigInteger nextBigInteger() {return new BigInteger(next());}}
}

这篇关于Codeforces Global Round 25 ---- F. Inversion Composition ---- 题解的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/907845

相关文章

LeetCode11. 盛最多水的容器题解

LeetCode11. 盛最多水的容器题解 题目链接: https://leetcode.cn/problems/container-with-most-water 示例 思路 暴力解法 定住一个柱子不动,然后用其他柱子与其围住面积,取最大值。 代码如下: public int maxArea1(int[] height) {int n = height.length;int

PS系统教程25

介绍软件 BR(bridge) PS 配套软件,方便素材整理、管理素材 作用:起到桥梁作用 注意:PS和BR尽量保持版本一致 下载和安装可通过CSDN社区搜索,有免费安装指导。 安装之后,我们打开照片只需双击照片,就自动在Ps软件中打开。 前提:电脑上有PS软件 三种预览格式 全屏预览 评星级 直接按数字键就可以 方向键可以更换图片 esc退出 幻灯片放

LeetCode:经典题之141、142 题解及延伸

系列目录 88.合并两个有序数组 52.螺旋数组 567.字符串的排列 643.子数组最大平均数 150.逆波兰表达式 61.旋转链表 160.相交链表 83.删除排序链表中的重复元素 389.找不同 1491.去掉最低工资和最高工资后的工资平均值 896.单调序列 206.反转链表 92.反转链表II 141.环形链表 142.环型链表 目录 系列目录141. 环形链表常量因子 1

C语言 | Leetcode C语言题解之第188题买卖股票的最佳时机IV

题目: 题解: int maxProfit(int k, int* prices, int pricesSize) {int n = pricesSize;if (n == 0) {return 0;}k = fmin(k, n / 2);int buy[k + 1], sell[k + 1];memset(buy, 0, sizeof(buy));memset(sell, 0, size

6月21日训练 (东北林业大学)(个人题解)

前言:   这次训练是大一大二一起参加的训练,总体来说难度是有的,我和队友在比赛时间内就写出了四道题,之后陆陆续续又补了了三道题,还有一道题看了学长题解后感觉有点超出我的能力范围了,就留给以后的自己吧。话不多说,上正文。 正文:   Problem:A 幸运数字: #include <bits/stdc++.h>using namespace std;int sum,ans;in

LeetCode:经典题之389 题解与延伸

系列目录 88.合并两个有序数组 52.螺旋数组 567.字符串的排列 643.子数组最大平均数 150.逆波兰表达式 61.旋转链表 160.相交链表 83.删除排序链表中的重复元素 389.找不同 1491.去掉最低工资和最高工资后的工资平均值 896.单调序列 206.反转链表 92.反转链表II 141.环形链表 142.环型链表 目录 系列目录389.找不同哈希表

【团队成长】2024-25周周报-业务介绍内容创作

大家好!我们是IndustryOR 团队,致力于分享业界落地的算法技术。欢迎关注微信公众号/知乎/CSDN【运筹匠心】 。 记录人:张哲铭,某互联网大厂算法专家 【团队成长/个人成长】系列的推文会以 【工作周报】 的方式记录IndustryOR团队及其成员的成长过程,请大家一起见证和参与我们团队从0-1-N的发展过程。 记录人顺序:张哲铭-向杜兵-高欣甜-黄世鸿-许佳鸣

Codeforces 158B

很久没有刷题了,刚刚有小学弟问了这道题,AC之后贴上来吧。水~~~ #include <cstdio>#include <cmath>int main() {int n;while(scanf("%d", &n) != EOF) {int a = 0, b = 0, c = 0, d = 0;int arr[100001];for (int i = 0; i < n; ++

Google Code Jam 2014(附官方题解)

2014年Google编程挑战赛 Problem A. Magic Trick Confused? Read the quick-start guide. Small input 6 points You have solved this input set. Note: To advance to the next rounds, you will need to s

Codeforces April Fools Day Contest 2014(附官方题解)

Codeforces2014年愚人节的坑题。。。但还是感觉挺好玩的。。。 A. The Great Game time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Two teams mee