[CC-SEAPERM2]Sereja and Permutations
[CC-SEAPERM2]Sereja and Permutations
题目大意:
有一个\(n(n\le300)\)排列\(p\),将其中一个元素\(p_i\)拿掉,然后将原来大于\(p_i\)的元素减一,这样就得到一个新的排列。
将\(p\)中每一个数拿掉之后都会得到一个新的排列,这样就得到了\(n\)个新的排列。
现在给出最后得到的\(n\)个新的排列(顺序随机),请求出原来的排列。如果有多个解,输出字典序最小的解。保证至少有一个解。
思路:
枚举哪一个排列被删掉了\(p_1\),那么剩下排列的第一个数,要么是\(p_1\),要么是\(p_1-1\)。(如果剩下不止两种数,或者相差超过\(1\),说明被删去\(p_1\)的不可能是这个排列。)
这样,我们就可以得到\(p_1\),从而得到初始的排列,但是我们还要删去每一个数看看是否和给你的排列一样来验证。做法是hash+map。
时间复杂度\(\mathcal O(n^3)\)。
源代码:
#include<map>
#include<cstdio>
#include<cctype>
#include<climits>
#include<algorithm>
inline int getint() {
register char ch;
while(!isdigit(ch=getchar()));
register int x=ch^'0';
while(isdigit(ch=getchar())) x=(((x<<2)+x)<<1)+(ch^'0');
return x;
}
typedef unsigned long long uint64;
const int N=301;
const uint64 base=31;
bool have_ans;
int n,a[N][N],b[N],ans[N];
std::map<uint64,int> map;
inline uint64 hash(int a[]) {
uint64 ret=0;
for(register int i=1;i<n;i++) {
ret=ret*base+a[i];
}
return ret;
}
inline bool check() {
for(register int i=1;i<=n;i++) {
if(b[i]<ans[i]) return true;
if(b[i]>ans[i]) return false;
}
return false;
}
inline void upd() {
if(!have_ans||check()) {
have_ans=true;
for(register int i=1;i<=n;i++) ans[i]=b[i];
}
}
int main() {
for(register int T=getint();T;T--) {
n=getint();
if(n==1) {
puts("1");
return 0;
}
for(register int i=1;i<=n;i++) {
for(register int j=1;j<n;j++) {
a[i][j]=getint();
}
}
have_ans=false;
for(register int i=1;i<=n;i++) {
int max[2]={0,0};
bool three=false;
for(register int j=1;j<=n;j++) {
if(i!=j) {
int tmp=a[j][1];
if(tmp==max[0]||tmp==max[1]) continue;
for(register int k=0;k<2;k++) {
if(tmp>max[k]) std::swap(max[k],tmp);
}
three|=tmp;
}
}
if(three||(max[1]&&max[0]-max[1]!=1)) continue;
for(register int val=max[0];val<=max[0]+1;val++) {
for(register int i=1;i<=n;i++) map[hash(a[i])]++;
b[1]=val;
for(register int j=2;j<=n;j++) b[j]=a[i][j-1]+(a[i][j-1]>=b[1]);
for(register int i=1;i<=n;i++) {
uint64 ret=0;
for(register int j=1;j<=n;j++) {
if(i!=b[j]) ret=(uint64)ret*base+b[j]-(b[j]>i);
}
map[ret]--;
}
bool flag=true;
for(auto &i:map) {
if(i.second!=0) flag=false;
}
map.clear();
if(flag) upd();
}
}
for(register int i=1;i<=n;i++) {
printf("%d%c",ans[i]," \n"[i==n]);
}
}
return 0;
}
[CC-SEAPERM2]Sereja and Permutations的更多相关文章
- CodeChef November Challenge 2014
重点回忆下我觉得比较有意义的题目吧.水题就只贴代码了. Distinct Characters Subsequence 水. 代码: #include <cstdio> #include ...
- CF380C. Sereja and Brackets[线段树 区间合并]
C. Sereja and Brackets time limit per test 1 second memory limit per test 256 megabytes input standa ...
- Atitti.dw cc 2015 绿色版本安装总结
Atitti.dw cc 2015 绿色版本安装总结 1.1. 安装程序无法初始化.请下载adobe Support Advisor检测该问题.1 1.1.1. Adobe Application M ...
- Permutations II
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- [LeetCode] Permutations II 全排列之二
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- [LeetCode] Permutations 全排列
Given a collection of numbers, return all possible permutations. For example,[1,2,3] have the follow ...
- POJ2369 Permutations(置换的周期)
链接:http://poj.org/problem?id=2369 Permutations Time Limit: 1000MS Memory Limit: 65536K Total Submi ...
- 【Hello CC.NET】CC.NET 实现自动化集成
一.背景 公司的某一金融项目包含 12 个子系统,新需求一般按分支来开发,测完后合并到主干发布.开发团队需要同时维护开发环境.测试环境.模拟环境(主干).目前面临最大的两个问题: 1.子系统太多,每次 ...
- 浅谈iptables防SYN Flood攻击和CC攻击
------------------------本文为自己实践所总结,概念性的东西不全,这里粗劣提下而已,网上很多,本文主要说下目前较流行的syn洪水攻击和cc攻击------------------ ...
随机推荐
- Jmeter中使用外部的java文件
感觉在Jmeter中使用外部的Java文件比较方便,语法一样,而且可以直接引用,所以个人觉得这个功能还是蛮重要的,特别是在使用Jmeter的过程中,可能需要结合一定的业务场景进行判断等,那使用Jmet ...
- Oracle下载 OPatch
今天被朋友问及,如何下载OPatch ...我当时有些凌乱的.事后想想,人与人的思维是不同的,对待同一个问题,有人觉得很简单,有人觉得无从下手 . 乱不多说了.开始说明下吧. 1. 首先要有一个MOS ...
- 论文阅读笔记三十八:Deformable Convolutional Networks(ECCV2017)
论文源址:https://arxiv.org/abs/1703.06211 开源项目:https://github.com/msracver/Deformable-ConvNets 摘要 卷积神经网络 ...
- 安装好ubuntu 18.10之后,屏幕一直在自动旋转,怎么办?
sudo apt-get install okular dia gimp Gparted sudo add-apt-repository universesudo apt install gnome- ...
- MyBatis配置:在控制台打印SQL语句
1.在spring-mybatis.xml中配置语句 注意:value=”classpath:mybatis-config.xml”这个文件如果之前没有,是需要新建的 2.新建mybatis-con ...
- docker挂载点泄露问题
本来以为我不会遇到. 结果还是遇到了. 现象为k8s delete pod时,系统一直显示Terminatiing,无论多久都不能正常. 以下两个帖子大概说明了是怎么回事. https://blog. ...
- Description Resource Path Location Type Project configuration is not up-to-date with pom.xml. Select: Maven->Update Project... from the project context menu or use Quick Fix. spark-MT line 1 Maven Co
1.相信大家新建的maven项目,然后添加好依赖(即修改了pom.xml文件以后就会出现如下所示的错误): Description Resource Path Location Type Projec ...
- 旋转矩阵 The Rotation Matrix
参考: http://www.scratchapixel.com/lessons/mathematics-physics-for-computer-graphics/geometry/how-does ...
- PHP查询数据库较慢,nginx 超时 返回 504:Sorry, the page you are looking for is currently unavailable.
现象: PHP查询数据库较慢,大约 60s 后 nginx 返回 504:Sorry, the page you are looking for is currently unavailable. 检 ...
- Failed to create Accelerated Display. Please check the display hardware and drivers meet the minimum requirements.
ArcGIS Runtime for WPF开发中Map设置了属性UseAcceleratedDisplay="True",报错: Sample: LocalMap Error: ...