题目传送门

根据字典序,是个人都会想到依次把目前最小的数尽量往前面移动,直到它不能再往前移动,或者已经到了它的期望位置(就是排列的那个位置 比如$i$就应该在位置$i$)为止。

所以我刚开始是这么写的:

 #include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
using namespace std;
#define N 105
#define ll long long
int n;
int a[N],pos[N];
bool vis[N];
int main()
{
int T;scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
for(int i=;i<=n;i++)
{
scanf("%d",&a[i]);
vis[i]=,pos[a[i]]=i;
}
int t=;
while()
{//第i次 i和i+1交换
//printf("**%d %d\n",t,pos[t]);
if(t>=n) break;
if(vis[pos[t]-])
{//这一个数不能再往前挪了
t++;
continue;
}
if(pos[t]<=t)
{
t++;
continue;
}
vis[pos[t]-]=;
int tmp=a[pos[t]-];
a[pos[t]-]=a[pos[t]],a[pos[t]]=tmp;
pos[tmp]++,pos[t]--;
//for(int i=1;i<n;i++)
// printf("%d ",a[i]);
//printf("%d \n",a[n]);
}
for(int i=;i<n;i++)
printf("%d ",a[i]);
printf("%d \n",a[n]);
}
return ;
}
/*
1
4
4 2 1 3
*/
/*
//-----
if(t>a[pos[t]-1])
{
t++;
continue;
}
//-----
*/

Code

然后$WA$了,一直调调调...自闭ing

后来打了一个对拍,可惜拍出来数据很大,就把错了的地方调出来,然后离散化长这个样子:

1
4
4 2 1 3

啊,我错了,应该是要现在这个数小于前面那个数才能交换,而不是搞什么期望位置啊,因为前面有可能有一些比较小的数不能够到达他的期望位置,然后就被无辜得换到后面去了。

 #include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
using namespace std;
#define N 105
#define ll long long
int n;
int a[N],pos[N];
bool vis[N];
int main()
{
int T;scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
for(int i=;i<=n;i++)
{
scanf("%d",&a[i]);
vis[i]=,pos[a[i]]=i;
}
int t=;
while()
{//第i次 i和i+1交换
//printf("**%d %d\n",t,pos[t]);
if(t>=n) break;
if(vis[pos[t]-])
{//这一个数不能再往前挪了
t++;
continue;
}
//-----
if(t>a[pos[t]-])
{
t++;
continue;
}
//-----
vis[pos[t]-]=;
int tmp=a[pos[t]-];
a[pos[t]-]=a[pos[t]],a[pos[t]]=tmp;
pos[tmp]++,pos[t]--;
//for(int i=1;i<n;i++)
// printf("%d ",a[i]);
//printf("%d \n",a[n]);
}
for(int i=;i<n;i++)
printf("%d ",a[i]);
printf("%d \n",a[n]);
}
return ;
}
/*
1
4
4 2 1 3
*/

Code

这是我做过的最自闭的一场Div.3

CF-Div.3-B. Minimize the Permutation【模拟·需要清醒的脑子】的更多相关文章

  1. Codeforces Round #598 (Div. 3) B. Minimize the Permutation 贪心

    B. Minimize the Permutation You are given a permutation of length n. Recall that the permutation is ...

  2. Codeforces Round #598 (Div. 3) B Minimize the Permutation

    B. Minimize the Permutation You are given a permutation of length nn. Recall that the permutation is ...

  3. Codeforces Round #436 (Div. 2)D. Make a Permutation! 模拟

    D. Make a Permutation! time limit per test: 2 seconds memory limit per test: 256 megabytes input: st ...

  4. [cf div 2 706E] Working routine

    [cf div 2 706E] Working routine Vasiliy finally got to work, where there is a huge amount of tasks w ...

  5. CF&&CC百套计划3 Codeforces Round #204 (Div. 1) E. Jeff and Permutation

    http://codeforces.com/contest/351/problem/E 题意: 给出一些数,可以改变任意数的正负,使序列的逆序对数量最少 因为可以任意加负号,所以可以先把所有数看作正数 ...

  6. Codeforces Round #656 (Div. 3) B. Restore the Permutation by Merger (模拟)

    题意:有两个完全相同的排列,将其中一个的元素按相对顺序插入另外一个排列中,给你操作完的排列,求原排列. 题解:感觉看看样例就能直接写了啊,直接遍历,用桶存数字个数,如果桶为空,直接输出即可. 代码: ...

  7. Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) C.Ray Tracing (模拟或扩展欧几里得)

    http://codeforces.com/contest/724/problem/C 题目大意: 在一个n*m的盒子里,从(0,0)射出一条每秒位移为(1,1)的射线,遵从反射定律,给出k个点,求射 ...

  8. Codeforces Round #350 (Div. 2) F. Restore a Number 模拟构造题

    F. Restore a Number   Vasya decided to pass a very large integer n to Kate. First, he wrote that num ...

  9. Codeforces Round #378 (Div. 2) C. Epidemic in Monstropolis 模拟

    C. Epidemic in Monstropolis time limit per test 1 second memory limit per test 256 megabytes input s ...

随机推荐

  1. Spring MVC 基于AnnotationFormatterFactory接口实现自定义的规则

    1.创建一个类来实现AnnotationFormatterFactory接口 代码: package com.oukele.CustomAnnotation; import com.oukele.mo ...

  2. C#的Winform多语言实现(resx文件)

    1. 简体中文 2. 繁体中文 3. 英文 下面子丰介绍一下实现的过程: 1. 为每个窗口创建相应语言的resx文件.子丰以英文为例,右键->添加->新建项->资源文件,文件名为窗口 ...

  3. qt5---步长调节器SpinBox和QDoubleSpinBox

    #include <QSpinBox>            #include <QDoubleSpinBox> QSpinBox 用于整数的显示和输入,一般显示十进制数,也可 ...

  4. STM32的系统时钟设置SystemClock_Config()探究

    一.首先了解几个硬件名词: stm32有多种时钟源,为HSE.HSI.LSE.LSI.PLL,对于L系统的,还有一个专门的MSI 1.HSE是高速外部时钟,一般8M的晶振,精度比较高,比较稳定. 2. ...

  5. JAVA记录 Spring 两大特性

    1.IOC控制反转 Ioc—Inversion of Control,即“控制反转”,不是什么技术,而是一种设计思想.在Java开发中,Ioc意味着将你设计好的对象交给容器控制,而不是传统的在你的对象 ...

  6. Spring MVC 的 multipartResolver 不能同iWebOffice2006 共同使用

    转:http://jamesby.iteye.com/blog/57381 项目使用iWebOffice2006,本来可以正常使用,但是系统有文件上传需求,故定义了一个multipartResolve ...

  7. pycharm安装与永久激活

    1.Pycham下载 https://www.jetbrains.com/pycharm/download/#section=windows 直接下载专业版 2.安装 这里就不必细说,直接next就O ...

  8. Android 属性动画监听事件与一个菜单的例子

    简单监听事件 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 3 ...

  9. TCP如何保证可靠传输

    TCP 协议如何保证可靠传输   一.综述 1.确认和重传:接收方收到报文就会确认,发送方发送一段时间后没有收到确认就重传. 2.数据校验 3.数据合理分片和排序: UDP:IP数据报大于1500字节 ...

  10. R_Studio(决策树算法)鸢尾花卉数据集Iris是一类多重变量分析的数据集【精】

    鸢尾花卉数据集Iris是一类多重变量分析的数据集 通过花萼长度,花萼宽度,花瓣长度,花瓣宽度4个属性预测鸢尾花卉属于(Setosa,Versicolour,Virginica)三个种类中的哪一类 针对 ...