首页 > 其他分享 >B. Sort with Step

B. Sort with Step

时间:2023-04-30 10:35:06浏览次数:24  
标签:Sort typedef int long 次数 Step

题意:

        给定一个长度为n的数组,任意两个数如果满足i-j的绝对值等于k则可以互相交换,若不能通过此操作实现数组排序,则需要使用次数来强制交换,次数小于等于1输出次数,否则输出-1.

分析:

        最优情况下,找出需要操作的数的数量然后两两交换是次数最少的。

代码:

#include <bits/stdc++.h>

#define fi first
#define se second

using namespace std;

typedef pair<int,int> pii;
typedef long long ll;

const int N=2e5+10;

int a[N];
int b[N];

void solve()
{
    int t;
    cin>>t;
    while(t--)
    {
        int n,k;
        cin>>n>>k;
        for(int i=1;i<=n;i++)
        {
            cin>>a[i];
            b[a[i]]=i;
        }
        int sum=0;
        for(int i=1;i<=n;i++)
        {
            int j=b[i];
            if(abs(j-i)%k==0) continue;
            else
            {
                sum++;
            }
        }
        if(sum==0) cout<<0<<'\n';
        else if(sum<=2) cout<<1<<'\n';
        else cout<<-1<<'\n';
    }
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    
    solve();
    
    return 0;
}

 

标签:Sort,typedef,int,long,次数,Step
From: https://www.cnblogs.com/yaowww/p/17364976.html

相关文章

  • Python 希尔排序(Shell Sort)原理以及应用
    希尔排序的原理:希尔排序是把记录按下标的一定增量分组,对每组使用直接插入排序算法排序;随着增量逐渐减少,每组包含的关键词越来越多,当增量减至1时,整个文件恰被分成一组,算法便终止。希尔排序的原理是将待排序的序列按照一定间隔分成若干个子序列,对每个子序列使用插入排序进......
  • elmentui的Steps 步骤条样式改造
    话不多说上代码<template><divstyle="padding:30px;height:300px;"><el-alert:closable="false"title="menu2"/><el-steps:active="active"finish-status="success"><......
  • pop 出栈,sorted临时排序,容器类型的数据,zip函数
    divmod(a,b)返回一对商和余数,结果和(a//b,a%b)一致 字典是Python中唯一的映射类型。 Python的源文件以"py"为扩展名,有python.exe解释运行,可在控制台下运行。"pyw"是图形开发用户接口(GUI)文件的扩展名,作为桌面应用程序,这种文件用于开发图形界面的,由pythonw.exe解释......
  • List集合排序 sort方法
    List集合排序sort方法:publicstatic voidsort(List list):将集合中元素按照默认规则排序。publicstatic voidsort(List list,Comparator<?superT>):将集合中元素按照指定规则排序。sort方法的重载使用11.字符串作为集合中的类型进行排序publicclassDe......
  • 使用sortabl对表格进行拖拉拽重新排序
    1.安装依赖npminstallsortablejs--save2.包裹拖拽内容<divclass="draggable"style="padding:20px">需要拖拽的内容,如表格</div>3.定义拖拽方法 //列拖拽  columnDrop(){    constwrapperTr=document.querySelector('.draggable.el-ta......
  • [LeetCode] 2418. Sort the People
    Youaregivenanarrayofstrings names,andanarray heights thatconsistsof distinct positiveintegers.Botharraysareoflength n.Foreachindex i, names[i] and heights[i] denotethenameandheightofthe ith person.Return names sorted......
  • [LeetCode] 1342. Number of Steps to Reduce a Number to Zero 将数字变成 0 的操作
    Givenaninteger num,return thenumberofstepstoreduceittozero.Inonestep,ifthecurrentnumberiseven,youhavetodivideitby 2,otherwise,youhavetosubtract 1 fromit.Example1:Input:num=14Output:6Explanation: Step1)14ise......
  • Sitecore10 Demo演示环境Azure一键部署(Step By Step Guide to installing Sitecore10
    本文演示SitecoreXPSingle(XP0)在Azure上的一键部署,即“30分钟生成Sitecore演示环境”的一环。关于XP(即SitecoreExperiencePlatform)roles的相关介绍移步XPSingle配置主要用来开发和测试:FourSitecoreroles:ContentDelivery,ContentManagement,Processing,andRepo......
  • Yolov5_DeepSort_Pytorch:基于 Yolov5 + Deep Sort 的实时多目标跟踪器
    Yolov5_DeepSort_Pytorch:基于Yolov5+DeepSort的实时多目标跟踪器 视界君 Python视界 昨天Python视界分享简介该存储库包含一个两阶段跟踪器。YOLOv5(一系列在COCO数据集上预训练的对象检测架构和模型)生成的检测被传递到跟踪对象的DeepSort算法。它可以跟踪Yolov5模型......
  • cpp: Ten Sort Algotrthms
     //TenSortAlgorithms.h:此文件包含"TenSortAlgotrthms"类。十个常用排序算法C++11//2023年4月5日涂聚文GeovinDuedit.#ifndefTENSORTALGORITHMS_H#defineTENSORTALGORITHMS_H#include<vector>//#includedirective#include<string>#include<......