[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攻击------------------ ...
随机推荐
- HTMLTestRunner 美化版本
前言 最近小伙伴们在学玩python,,看着那HTMLTestRunner生成的测试报告,左右看不顺眼,终觉得太丑.搜索了一圈没有找到合适的美化报告,于是忍不住自已动手进行了修改,因习惯python ...
- Jmeter 获取CSV行数
import java.io.BufferedReader; import java.io.FileInputStream; String str = "E:\\Desktop\\WOS接口 ...
- JMeter 中跨线程组 变量值传递的方法
关于jmeter中跨线程组 变量值传递的方法 找了好久,终于找到方法了,赶紧整理下来. 1.在线程组1 中使用__setProperty函数设置jmeter属性值(此值为全局变量值), ...
- Android SQLite用法
1.创建SQLite数据库 需要自己创建一个类来继承SQLiteOpenHelper类 SQLiteOpenHelper类是一个创建SQLite数据库的辅助类 继承此类的时候需要重写三个方法 publ ...
- K8s-Pod
一:Pod-资源对象概述 Pod是k8s系统中可以创建和管理的最小单元,是资源对象模型中由用户创建或部署的最小资源对象模型,也是在k8s上运行容器化应用的资源对象,其他的资源对象都是用来支撑或者扩展P ...
- linux:安装并使用mongo
1.下载mongo: curl -O https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-3.0.6.tgz 2.解压: tar -zxvf ...
- 三.hadoop mapreduce之WordCount例子
目录: 目录见文章1 这个案列完成对单词的计数,重写map,与reduce方法,完成对mapreduce的理解. Mapreduce初析 Mapreduce是一个计算框架,既然是做计算的框架,那么表现 ...
- Django-model聚合查询与分组查询
Django-model聚合查询与分组查询 聚合函数包含:SUM AVG MIN MAX COUNT 聚合函数可以单独使用,不一定要和分组配合使用:不过聚合函数一般和group by 搭配使用 agg ...
- Caused by: java.net.ConnectException: Connection refused: master/192.168.3.129:7077
1:启动Spark Shell,spark-shell是Spark自带的交互式Shell程序,方便用户进行交互式编程,用户可以在该命令行下用scala编写spark程序. 启动Spark Shell, ...
- PHP Fatal error: SOAP-ERROR: Parsing WSDL: Couldn't load from 'http://xxxx.wsdl'
libxml_disable_entity_loader(false); $client = new \SoapClient($wsdl); 完美解决办法加上 php的soap扩展是否安装 open ...