[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的更多相关文章

  1. CodeChef November Challenge 2014

    重点回忆下我觉得比较有意义的题目吧.水题就只贴代码了. Distinct Characters Subsequence 水. 代码: #include <cstdio> #include ...

  2. CF380C. Sereja and Brackets[线段树 区间合并]

    C. Sereja and Brackets time limit per test 1 second memory limit per test 256 megabytes input standa ...

  3. Atitti.dw cc 2015 绿色版本安装总结

    Atitti.dw cc 2015 绿色版本安装总结 1.1. 安装程序无法初始化.请下载adobe Support Advisor检测该问题.1 1.1.1. Adobe Application M ...

  4. Permutations II

    Given a collection of numbers that might contain duplicates, return all possible unique permutations ...

  5. [LeetCode] Permutations II 全排列之二

    Given a collection of numbers that might contain duplicates, return all possible unique permutations ...

  6. [LeetCode] Permutations 全排列

    Given a collection of numbers, return all possible permutations. For example,[1,2,3] have the follow ...

  7. POJ2369 Permutations(置换的周期)

    链接:http://poj.org/problem?id=2369 Permutations Time Limit: 1000MS   Memory Limit: 65536K Total Submi ...

  8. 【Hello CC.NET】CC.NET 实现自动化集成

    一.背景 公司的某一金融项目包含 12 个子系统,新需求一般按分支来开发,测完后合并到主干发布.开发团队需要同时维护开发环境.测试环境.模拟环境(主干).目前面临最大的两个问题: 1.子系统太多,每次 ...

  9. 浅谈iptables防SYN Flood攻击和CC攻击

    ------------------------本文为自己实践所总结,概念性的东西不全,这里粗劣提下而已,网上很多,本文主要说下目前较流行的syn洪水攻击和cc攻击------------------ ...

随机推荐

  1. Linux系统下inode满了导致无法写文件的解决思路

    解决思路1:删除无用的临时文件,释放inode 进入/tmp目录,执行find -exec命令 find  /tmp  -type  f  -exec  rm  {}  \; 遍历寻找0字节的文件,并 ...

  2. 微信小程序 如何获取用户code

    1.首先需要获取code 使用 wx.login({ success: function(res) { console.log(res);//这里的返回值里面便包含code }, fail: func ...

  3. 论文阅读笔记二十四:Rich feature hierarchies for accurate object detection and semantic segmentation Tech report(R-CNN CVPR2014)

    论文源址:http://www.cs.berkeley.edu/~rbg/#girshick2014rcnn 摘要 在PASCAL VOC数据集上,最好的方法的思路是将低级信息与较高层次的上下文信息进 ...

  4. 011-Python-进程、线程于协程

    1.进程与线程 进程: 一个程序要运行时所需的所有资源的集合: 一个进程至少需要一个线程,这个线程称为主线程,一个进程里可以包含多个线程: cpu 核数越多,代表着你可以真正并发的线程越多2个进程之间 ...

  5. gradle编译命令 & 自动打包等

    ./gradlew -v 版本号,首次运行,没有gradle的要下载的哦. ./gradlew clean 删除HelloWord/app目录下的build文件夹 ./gradlew build 检查 ...

  6. jdk1.8学习、jdk1.9学习、jdk10.0学习和总结

    由于中文参考资料很少,参考链接: https://www.oschina.net/translate/109-new-features-in-jdk-10 http://chuansong.me/n/ ...

  7. java解析html的table

    import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; import org. ...

  8. 牛客网round1

    题解: 1. 二分答案之后判断 把式子移项使得x,y不关联 #include <bits/stdc++.h> using namespace std; #define rint regis ...

  9. day9.初始函数练习题

    1.写函数,检查获取传入列表或元组对象的所有奇数位索引对应的元素,并将其作为新列表返回给调用者. def new(args): a = [] for I in range(1,len(args),2) ...

  10. HDU1814 Peaceful Commission 2-sat

    原文链接http://www.cnblogs.com/zhouzhendong/p/8099115.html 题目传送门 - HDU1814 题面 Description 根据宪法,Byteland民 ...