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 ...
随机推荐
- js 设置多条css样式
如果在一个网页中给id="mydiv"的元素添加css样式,先获取该节点:var obj = document.getElementById("mydiv"); ...
- matplotlib 模块
目录 matplotlib 模块 1.条形图 2.直方图 3.折线图 4.散点图+直线图 5.饼图 6. plot 函数参数 7.图像标注参数 matplotlib 模块 1.条形图 import m ...
- python起步--windows系统下安装python解释器和PyCharm
参考教程: 1)https://www.runoob.com/w3cnote/pycharm-windows-install.html 2)https://blog.csdn.net/c_shell_ ...
- POJ 1182食物链(并查集)
食物链Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 85474 Accepted: 25549Description动物王国中有三 ...
- CF 25 E 三个字符串 KMP模板
Test Time Limit: 2000MS Memory Limit: 262144KB 64bit IO Format: %I64d & %I64u Submit Status ...
- matplotlib绘图时显示额外的“figure”浮窗
引自 https://blog.csdn.net/weixin_41571493/article/details/82690052 问题:现在默认的Pycharm绘图时,都会出现下面的情况: 不能弹出 ...
- oracle条件参数中 IN函数中的值最大只能为1000个
delete from dep where id in(1,2,3.....) 括号里面字段个数最大只能为1000个
- Java并发编程的艺术笔记(八)——线程池
一.线程池的主要处理流程 ThreadPoolExecutor执行execute方法分下面4种情况. 1)如果当前运行的线程少于corePoolSize,则创建新线程来执行任务(注意,执行这一步需要获 ...
- crm 项目的部署
发布CRM你将使用以下软件 nginx uWSGI CentOS7 CRM项目文件 virtualenv supervisor WSGI.uWSGI python web服务器开发使用WSGI协议(W ...
- Java基础教程免费分享
这是我自己早前听课时整理的Java全套知识,适用于初学者,也可以适用于中级进阶的人,你们可以下载,我认为是比较系统全面的,可以抵得上市场上90%的学习资料.讨厌那些随便乱写的资料还有拿出来卖钱的人!在 ...