BZOJ2214[Poi2011]Shift——模拟
题目描述
Byteasar bought his son Bytie a set of blocks numbered from to and arranged them in a row in a certain order. Bytie's goal is to rearrange the blocks so that they are ordered naturally, from the smallest number to the largest. However, the only moves Bytie is allowed to make are: putting the last block at the very beginning (move a), and putting the third block at the very beginning (move b). Help Bytie by writing a program that tells whether a given arrangement of blocks can be properly reordered, and tells the right sequence of moves if it is.
有一个1..n的排列,有两种操作:
(a) 将最后一个数移到最前面
(b) 把第三个数移到最前面
我们将连续进行k次同一个操作称为“一块操作”,表示为ka或kb。
找到一个操作序列使得进行这些操作后,排列变为1,2,3,...,n。
输入
In the first line of the standard input there is a single integer ,(1<=N<=2000). In the second line there are integers from the range to , separated by single spaces. No number appears twice, and thus they represent the initial arrangement of the blocks. .
第一行n(1<=n<=2000)
下面一行n个数表示这个排列。
输出
如果不存在这样的操作序列,输出"NIE DA SIE",否则
第一行m,表示操作的块数。
下面一行,表示这m块操作。
需要满足相邻两块操作的种类不同,每块操作中进行的次数大于0小于n。
需要满足m<=n*n
样例输入
4
1 3 2 4
Sample Output #1
4
3a 2b 2a 2b
Sample Input #2
7
1 3 2 4 5 6 7
Sample Output #2
NIE DA SIE
Sample Input #3
3
1 2 3
Sample Output #3
0
#include<map>
#include<set>
#include<queue>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
int n;
int now;
int tot;
int f[2010];
int a[2010];
int p[2010];
int c[4000010];
bool vis[4000010];
int pos(int x)
{
return (now+p[x]-1)%n+1;
}
int get(int x)
{
return ((x-now-1)%n+n)%n+1;
}
void make1(int x)
{
x%=n;
if(!x)
{
return ;
}
now+=x;
c[++tot]=x;
vis[tot]=0;
}
void make2()
{
int s1=get(1);
int s2=get(2);
int s3=get(3);
int x=f[s1];
int y=f[s2];
int z=f[s3];
int t=p[x];
p[x]=p[y];
p[y]=p[z];
p[z]=t;
f[p[x]]=x;
f[p[y]]=y;
f[p[z]]=z;
c[++tot]=1;
vis[tot]=1;
}
void print()
{
int ans=0;
for(int i=1;i<=tot;i++)
{
if(i==1||vis[i]!=vis[ans])
{
ans++;
vis[ans]=vis[i];
c[ans]=c[i];
}
else
{
c[ans]+=c[i];
}
}
tot=ans;
ans=0;
for(int i=1;i<=tot;i++)
{
if(vis[i]==0)
{
c[i]%=n;
}
else
{
c[i]%=3;
}
if(c[i])
{
c[++ans]=c[i];
vis[ans]=vis[i];
}
}
printf("%d\n",ans);
int i;
for(i=1;i<ans;i++)
{
if(vis[i]==0)
{
printf("%da ",c[i]);
}
else
{
printf("%db ",c[i]);
}
}
if(ans)
{
if(vis[i]==0)
{
printf("%da\n",c[i]);
}
else
{
printf("%db\n",c[i]);
}
}
}
int main()
{
scanf("%d",&n);
if(n==1)
{
printf("0\n");
return 0;
}
if(n==2)
{
scanf("%d%d",&a[1],&a[2]);
if(a[1]==1)
{
printf("0\n");
return 0;
}
else
{
printf("1\n1a\n");
return 0;
}
}
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
p[a[i]]=i;
f[i]=a[i];
}
for(int i=2;i<=n-2;i++)
{
int x=pos(i);
make1(n-x+1);
int t=n-pos(i-1);
while(t>1)
{
t-=2;
make1(2);
make2();
}
if(t)
{
make1(1);
make2();
make2();
}
}
if(n==3)
{
make1(n-pos(1)+1);
}
if(f[get(2)]<f[get(3)])
{
make1(n-3);
print();
}
else
{
if(!(n%2))
{
make1(n-2);
for(int i=1;i<n/2;i++)
{
make2();
make2();
make1(n-2);
}
make1(n-2);
print();
}
else
{
printf("NIE DA SIE\n");
}
}
}
BZOJ2214[Poi2011]Shift——模拟的更多相关文章
- Luogu3516 POI2011 Shift 构造
传送门 题意:给出一个长为$N$的排列,有两种操作:$A$:将最后一个数字放到第一个:$B$:将第三个数字放到第一个.一次性使用某种操作$k$次写作$kA$或$kB$,其中在$kA$中$k < ...
- CF708A Letters Cyclic Shift 模拟
You are given a non-empty string s consisting of lowercase English letters. You have to pick exactly ...
- Codeforces 960 二进制构造子序列 完全二叉树shift模拟 主席树/MAP DP
A #include <bits/stdc++.h> #define PI acos(-1.0) #define mem(a,b) memset((a),b,sizeof(a)) #def ...
- bzoj AC倒序
Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...
- POI做题笔记
POI2011 Conspiracy (2-SAT) Description \(n\leq 5000\) Solution 发现可拆点然后使用2-SAT做,由于特殊的关系,可以证明每次只能交换两个集 ...
- Codeforces #259 Div.2
A. Little Pony and Crystal Mine 模拟题. 用矩阵直接构造或者直接根据关系输出 B. Little Pony and Sort by Shift 模拟题. 通过提供的操作 ...
- [转] 有趣的JavaScript原生数组函数
在JavaScript中,可以通过两种方式创建数组,Array构造函数和 [] 便捷方式, 其中后者为首选方法.数组对象继承自Object.prototype,对数组执行typeof操作符返回‘obj ...
- JavaScript原生数组函数
有趣的JavaScript原生数组函数 在JavaScript中,可以通过两种方式创建数组,构造函数和数组直接量, 其中后者为首选方法.数组对象继承自Object.prototype,对数组执行typ ...
- 有趣的JavaScript原生数组函数
本文由 伯乐在线 - yanhaijing 翻译.未经许可,禁止转载!英文出处:flippinawesome.欢迎加入翻译小组. 在JavaScript中,可以通过两种方式创建数组,Array构造函数 ...
随机推荐
- Oracle存储过程的调试
在工作汇总有时候程序会调用存储过程来实现某些功能,因为这样的话,速度更快.所以学习如何调试存储过程就非常的重要. 首先,打开PLSQL DEVELOPMENT首先介绍一下,这个软件的各个窗口是用来干什 ...
- WPF 模拟UI 键盘录入
原文:WPF 模拟UI 键盘录入 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/yangyisen0713/article/details/1835 ...
- 【转】Raft 为什么是更易理解的分布式一致性算法
编者按:这是看过的Raft算法博客中比较通俗的一篇了,讲解问题的角度比较新奇,图文并茂,值得一看.原文链接:Raft 为什么是更易理解的分布式一致性算法 一致性问题可以算是分布式领域的一个圣殿级问题了 ...
- 浅谈博弈论中的两个基本模型——Bash Game&&Nim Game
最近在数学这一块搞了蛮多题目,已经解决了数论基础,线性代数(只有矩阵,行列式待坑),组合数学中的一些简单问题.所以接下来不可避免的对博弈论这一哲学大坑开工. 当然,由于我很菜,所以也只能从最基础最容易 ...
- Ionic 2.0 相关资料
原文发表于我的技术博客 本文汇总了学习 Ionic 2 的相关资料,也算是一个 Ionic Awesome 列表,供大家参考,有需要分享的可以留言. 原文发表于我的技术博客 1. 文档 1.1 Ion ...
- StoryLine3变量存储与跳转后台时的使用
前言 公司项目原因,接触到storyline3(后面简称SL)课件制作工具,类似ppt,但是又多了互动.交互,且页面元素可添加触发器,触发器中可执行js代码. 1.官方教程 在SL中,会有“了解详情. ...
- element-ui + vue + node.js 与 服务器 Python 应用的跨域问题
跨越问题解决的两种办法: 1. 在 config => index.js 中配置 proxyTable 代理: proxyTable: { '/charts': { target: 'http: ...
- Jmeter-使用Stepping Thread Group插件来设置负载场景
前言: 什么是实际的性能测试???1)思考时间:用户在做不同操作之间有时间停顿,或者延迟,思考时间就是模拟用户的操作过程中的停顿的间.2)步伐,速度:主要包括,大量用户进来的时间和退出时间,控制迭代之 ...
- D. Fun with Integers
链接 [http://codeforces.com/contest/1062/problem/D] 题意 给你n,让你从2到n这个区间找任意两个数,使得一个数是另一个的因子,绝对值小的可以变为绝对值大 ...
- main函数是必须的吗
研究实验4 研究过程: 问题引出:C语言编程非得用主函数main吗,不用是否可以? 对此问题进行研究,用tc.exe书写代码如下: 图1 没有main函数的c程序 对其进行编译,链接发现,编译阶段可 ...