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 ...
随机推荐
- jquery easyui 日历控件和文本框结合使用生成日期
html部分---等待接收所选日期的文本框 <td> <input name='input_date' required class='easyui-textbox' id='xiw ...
- 树莓派设定笔记(Raspberry Pi 3 B+)
树莓派默认用户名密码 pi / raspberry 一.启用root用户 设置root用户密码 sudo passwd root 开启root账户 sudo passwd --unlock root ...
- @Data注解使用后get set报错解决方法
Maven项目中已经导入相关的lombok.jar包但是使用后仍提示无set/get方法 .在idea中安装如下插件,安装后重启idea可用不报错. 转载于:https://www.cnblogs.c ...
- 我不熟悉的list
其实在日常中,链表的题目做的比较多,但是使用STL自带链表的还是比较少,所以里面的一些API不大熟悉.这边也简要介绍一些. 基本的一些API 先列举的这些和上面几篇用法几乎一样,所以不再累述. 赋值相 ...
- 记一次AD域控客户端不能正常加域的故障处理
1.1 症状现象 DNS 服务器无法在应用程序目录分区 < 分区名称 > 从 Active Directory 中打开区域 < 区域 >.这台 DNS 服务器配置获取并使用此 ...
- mysql 查询近三个月数据
select * from t_user where time>DATE_SUB(CURDATE(), INTERVAL 1 WEEK) --一周 select * from t_user wh ...
- linux输出与查看的几种方式
输出的几种方式:echo/ tee echo "postgres install Failed !!!!!!" | tee -a "$Install_log"# ...
- Jmeter性能测试环境搭建(Windows下)
最近刚开始接触Jmeter性能测试,现总结环境搭建如下: 一.windows安装JDK步骤与环境变量配置: 1.先将下载的JDK安装到其默认目录:C:\Program Files\Java\jdk1. ...
- 三、smarty--变量调节器(修改器)
变量调节器(修改器) 作用: 1. 从PHP中分配个模板的变量 2. 需要模板中对变量在输出前进行处理 3. 处理方式就是使用“函数” 4. 在smarty3中可以直接调用到PHP的函数 5. ...
- synchronized 读写同步
synchronized 读写同步 这是一道面试题,很多人也遇到了. 要求:1.读-读 不用线程同步.2.读-写 要求线程同步,写的时候不能读.3.写-写同步.写的时候不能写. java lock读写 ...