CF-Div.3-B. Minimize the Permutation【模拟·需要清醒的脑子】
根据字典序,是个人都会想到依次把目前最小的数尽量往前面移动,直到它不能再往前移动,或者已经到了它的期望位置(就是排列的那个位置 比如$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【模拟·需要清醒的脑子】的更多相关文章
- 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 ...
- 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 ...
- 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 ...
- [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 ...
- CF&&CC百套计划3 Codeforces Round #204 (Div. 1) E. Jeff and Permutation
http://codeforces.com/contest/351/problem/E 题意: 给出一些数,可以改变任意数的正负,使序列的逆序对数量最少 因为可以任意加负号,所以可以先把所有数看作正数 ...
- Codeforces Round #656 (Div. 3) B. Restore the Permutation by Merger (模拟)
题意:有两个完全相同的排列,将其中一个的元素按相对顺序插入另外一个排列中,给你操作完的排列,求原排列. 题解:感觉看看样例就能直接写了啊,直接遍历,用桶存数字个数,如果桶为空,直接输出即可. 代码: ...
- 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个点,求射 ...
- 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 ...
- 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 ...
随机推荐
- eclipse error pages 打红X的解决方法
建一个Maven项目转为web项目的时候,这里总有这么多文件,而且还会有一个Error Pages的错红的红叉,看着很不爽. 虽然项目中没有出现过错误,感觉是个错误,解决方法如下.原来这是eclips ...
- JavaScript面向对象OOM 2(JavaScript 创建对象的工厂模式和构造函数模式)
在创建对象的时候,使用对象字面量和 new Object() 构造函数的方式创建一个对象是最简单最方便的方式.但是凡是处于初级阶段的事物都会不可避免的存在一个问题,没有普适性,意思就是说我要为世界 ...
- qt类表
- mysql自动化审核工具Yearning
mysql自动化审核工具Yearning cd /opt/wget https://github-production-release-asset-2e65be.s3.amazonaws.com/10 ...
- OpenCascade建模:构建圆环API--BRepPrimAPI_MakeTortus()
构建圆环API--BRepPrimAPI_MakeTortus() 函数语法: BRepPrimAPI_MakeTortus( const Standard_Real R1, const Standa ...
- python 创建实例对象
实例化类其他编程语言中一般用关键字 new,但是在 Python 中并没有这个关键字,类的实例化类似函数调用方式. 以下使用类的名称 Employee 来实例化,并通过 __init__ 方法接收参数 ...
- stat:查看文件时间参数
Linux 系统中,每个文件主要拥有 3 个时间参数,分别是文件的访问时间.数据修改时间以及状态修改时间: 访问时间(Access Time,简称 atime):只要文件的内容被读取,访问时间就会更新 ...
- Jmeter -- 监听 -- 查看每个请求的启动时间等信息
步骤: 1. 添加监听器 Add --> Listener --> View Results in Table 2. 执行线程组,查看监听信息
- spring boot V部落 V人事项目
公司倒闭 1 年多了,而我在公司倒闭时候做的开源项目,最近却上了 GitHub Trending,看着这个数据,真是不胜唏嘘. 缘起 2017 年 11 月份的时候,松哥所在的公司因为经营不善要关门了 ...
- 关于Tomcat配置相关总结
特别提示:本人博客部分有参考网络其他博客,但均是本人亲手编写过并验证通过.如发现博客有错误,请及时提出以免误导其他人,谢谢!欢迎转载,但记得标明文章出处:http://www.cnblogs.com/ ...