CF 1823B B. Sort with Step

这篇具有很好参考价值的文章主要介绍了CF 1823B B. Sort with Step。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

CF 1823B B. Sort with Step

Problem - B - Codeforces

题意:

给n长的数组,里边只有1-n的数字,问你能不能在k步长的交换内换成升序的1-n

AC代码:

首先给你1-n的数字还要排成增序;

对于3 4 1 2

3在第1个数, abs(3 - 1) % 2 == 0;

所以3可以在步长为2的交换中换回位置3

对于步长为2 , 如果你在1的位置,所有你能换的就是1 3 5 7 …

所以对于a[i],要想能够换到i的位置,就看abs(a[i] - i) % k == 0是否成立

先跑一遍看看有几个位置是换不到的,如果这些位置的数量超过2,那肯定是不可能提前交换一次能处理的了,直接输出-1;

如果刚好有两个位置,那就判断交换之后他们能不能让自己跑回原来的位置:

行的话输出1,不行就-1,

如果只有一个位置换不了,那寄,输出-1;

不存在需要交换的位置,赢,输出0;文章来源地址https://www.toymoban.com/news/detail-428300.html

#include<bits/stdc++.h>
#define IO std::ios::sync_with_stdio(false); \
std::cin.tie(0);                             \
std::cout.tie(0)

#define all(v) v.begin(),v.end()

#define int long long
#define PII pair<ll,int>
#define ld long double
#define x first
#define y second
#define endl '\n'
#define inf 0x3f3f3f3f
using namespace std;
const int N = 2e5+10;
void solve()
{
    int n,k;
    cin>>n>>k;
    vector<int> a(n+1),p(n+1),s;
    bool ok = 1;

    for(int i = 1;i<=n;i++){
        cin>>a[i];
        p[a[i]] = i;
        if(abs(i -  a[i]) % k != 0){
            ok = 0;
            s.push_back(a[i]);
        }
    }
    if(ok)cout<<"0"<<endl;
    else{
        if(s.size() > 2){
            cout<<"-1"<<endl;
        } else{
            if(s.size() == 2){
                auto x = s[0],y = s[1];
                if(abs(p[y] - x) % k == 0 && abs(p[x] - y) % k == 0)cout<<"1"<<endl;
                else cout<<"-1"<<endl;
            } else{
                cout<<"-1"<<endl;
            }
        }
    }
}
signed main(){
    int t;
    cin>>t;
    while(t--)
    {
    solve();
    }
}

到了这里,关于CF 1823B B. Sort with Step的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • D. Problem with Random Tests

    Problem - 1743D - Codeforces   思路:因为是或,所以答案一定会比原串更大,并且为了保留更多的1,我们可以选择原串作为其中一个串,另一个串则要找到第一个为0的位置,我们希望让这个为1,为了让这个位置在或之后为1,需要满足两个条件,假设这个位置为id,那么首先要满

    2024年02月12日
    浏览(32)
  • NC17383 A Simple Problem with Integers

    题目链接 题目描述 You have N integers A1, A2, ... , AN. You are asked to write a program to receive and execute two kinds of instructions: C a b means performing (A_i = A_i^2 mod 2018) for all Ai such that a ≤ i ≤ b. Q a b means query the sum of Aa, Aa+1, ..., Ab. Note that the sum is not taken modulo 2018. 输入描述 The first line of the i

    2024年02月01日
    浏览(33)
  • 数字逻辑Fundamentals of Digital Logic with Verilog Design | 3rd Edition Solutins Chapter 4(step by step)

    第四章 重要内容:1、多路选择器  2、采用香农展开的多路选择器综合 3、译码器  4、多路分配器  5、优先级编码器  6、代码转换器  7、算数比较电路  8、Verilog语法 纠错:4-11香农展开式最后结果应该是同或门。 Chapter 4 Chapter 4, Problem 1P Chapter 4, Problem 2P Chapter 4, Problem 3P

    2024年02月05日
    浏览(29)
  • Tree of Thoughts: Deliberate Problem Solving with Large Language Models

    本文是LLM系列的文章,针对《Tree of Thoughts: Deliberate Problem Solving with Large Language Models》的翻译。 语言模型越来越多地被部署用于解决各种任务中的一般问题,但在推理过程中仍然局限于token级别的从左到右的决策过程。这意味着他们可能无法完成需要探索、战略前瞻或初始决

    2024年02月11日
    浏览(36)
  • 【论文阅读】(2013)Exact algorithms for the bin packing problem with fragile objects

    论文来源:(2013)Exact algorithms for the bin packing problem with fragile objects 作者:Manuel A. Alba Martínez 等人 我们得到了一组物体,每个物体都具有重量和易碎性,以及大量没有容量的垃圾箱。 我们的目标是找到装满所有物体所需的最少垃圾箱数量,使每个垃圾箱中物体重量的总和小

    2024年02月11日
    浏览(34)
  • 【每日一题】—— C. Game with Reversing (Codeforces Round 879 (Div. 2))

    🌏博客主页: PH_modest的博客主页 🚩当前专栏: 每日一题 💌其他专栏: 🔴 每日反刍 🟡 C++跬步积累 🟢 C语言跬步积累 🌈座右铭: 广积粮,缓称王! 题目大意: 题目链接:C. Game with Reversing (Codeforces Round 879 (Div. 2)) 翻字符串两次不会对答案造成影响,因此统计出初始

    2024年02月16日
    浏览(34)
  • OpenCV 中的错误信息 “Layout of the output array img is incompatible with cv::Mat (step...

    OpenCV 中的错误信息 “Layout of the output array img is incompatible with cv::Mat (step[ndims-1] !)” 表示输出数组 img 的布局与 cv::Mat 类型不兼容。这种错误通常是在使用 OpenCV 进行图像处理时出现的,可能是由于输入和输出 Mat 类的尺寸不匹配、步长不符合要求等原因导致的。 为了更好地理

    2024年02月16日
    浏览(37)
  • 开源项目运行时报错A problem was found with the configuration of task ‘:app:checkDebugManifest‘

    下载开源项目后,对gradle-wrapper.properties中的gradle版本进行了升级,造成了如下问题: 1: Task failed with an exception. ----------- * What went wrong: A problem was found with the configuration of task \\\':app:checkDebugManifest\\\' (type \\\'CheckManifest\\\').   - Type \\\'com.android.build.gradle.internal.tasks.CheckManifest\\\' property \\\'manif

    2023年04月08日
    浏览(25)
  • [USF-XSim-62] ‘elaborate‘ step failed with errors.[Vivado 12-4473] Detected error while running sim

    出现的问题如下: 翻译出来:[USF-XSim-62] \\\'elaborate’步骤失败,出现错误。请检查Tcl控制台输出或’D:/vivado/fortest/fortest.sim/sim_1/behav/xsim/ elaboration .log’文件以获取更多信息。 [Vivado 12-4473] 运行模拟时检测到错误请纠正此问题并重试此操作。 方法一 :在Vivado 中的Messages无法看到

    2024年02月15日
    浏览(23)
  • 已解决note: This error originates from a subprocess,and is likely not a problem with pip.

    已解决(pip安装第三方模块lxml模块报错)Building wheels for collected packages: lxml Building wheel for lxml (setup.py) … error error: subprocess-exited-with-error python setup.py bdist_wheel did not run successfully. note: This error originates from a subprocess,and is likely not a problem with pip. ERROR: Failed building wheel for lxml n

    2024年01月18日
    浏览(28)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包