题目传送门

根据字典序,是个人都会想到依次把目前最小的数尽量往前面移动,直到它不能再往前移动,或者已经到了它的期望位置(就是排列的那个位置 比如$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. P1081 [NOIP2012]开车旅行[倍增]

    P1081 开车旅行    题面较为啰嗦.大概概括:一个数列,只能从一个点向后走,两种方案:A.走到和自己差的绝对值次小的点B.走到和自己差的绝对值最小点:花费为此差绝对值:若干询问从规定点向后最多花 ...

  2. std::this_thread::sleep_until

    头文件:<thread>                  (C++11) template<class Clock, class Duration> void sleep_u ...

  3. 【leetcode】740. Delete and Earn

    题目如下: Given an array nums of integers, you can perform operations on the array. In each operation, y ...

  4. CSS 按钮

    总结有关按钮的各种样式 ㈠基本按钮样式 看一下没有进行css样式设计时按钮的样子与进行样式设计的按钮样子 <!DOCTYPE html> <html> <head> ...

  5. C语言 - strcat和strncat的编程实现及总结

    一.函数strcat与stcncat的函数实现 1.strcat函数的实现 要求: 原型:char * strcat(char *dest, const char *src);    头文件:#inc ...

  6. Jmeter(一) 安装

    一.检查JDK版本 执行cmd > java -version 查看本机JDK版本,JDK版本不能低于1.6 二.软件下载 登录Jmeter官网:https://jmeter.apache.or ...

  7. JS框架_(Popup.js)3D对话框窗口插件

    百度云盘 传送门 密码:afdo 3D对话框窗口插件效果: <!doctype html> <html lang="zh"> <head> &l ...

  8. docker删除容器再删除镜像

    1. 查询镜像 docker images 现在想删除第一个,ID为 99f85991949f 的镜像. docker rmi  ID 从上面看出,需要先停到ID为 67*** 的容器. 2. 查询容 ...

  9. C++入门经典-例2.8-输出整数,控制打印格式

    1:代码如下: // 2.8.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <iostream> #incl ...

  10. 191121CSS

    一.CSS 1.css选择器 css选择器的使用方法 <!DOCTYPE html> <html lang="en"> <head> <m ...