[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攻击------------------ ...
随机推荐
- scss文件使用笔记
1.编写兼容性代码 例如透明度,兼容IE @mixin mOpacity($o){ opacity:$o/100; filter:alpha(opacity=$o); } //引用 .box{ @in ...
- 数据结构C++实现代码-顺序表
参考:https://blog.csdn.net/ebowtang/article/details/43094041 //seqList.h// //包含顺序表中的声明// #include<i ...
- 解决Django + DRF:403 FORBIDDEN:CSRF令牌丢失或不正确,{"detail":"CSRF Failed: CSRF cookie not set."}
我有一个Android客户端应用程序尝试使用Django + DRF后端进行身份验证.但是,当我尝试登录时,我收到以下响应: 403: CSRF Failed: CSRF token missing ...
- A.Ocean的礼物线段树
A: Ocean的礼物 Time Limit: 2 s Memory Limit: 128 MB Submit My Status Problem Description 皇家理工存在一段很神 ...
- 20165323 结对编程之四则运算week2-整体总结
一.需求 实现一个命令行程序,要求: 1.自动生成小学四则运算题目(加.减.乘.除) 2.支持整数 3.支持多运算符(比如生成包含100个运算符的题目) 4.支持真分数 5.能判断错误,在输入错误结果 ...
- Linux下Nginx安装/启动/重启/停止
Nginx是高性能的web服务器也是非常好用反向代理服务器,可以实现负载均衡,动静分离等策略,在linux下用的非常多.下面是下载地址 http://nginx.org/en/download.h ...
- WARN conf.FlumeConfiguration: Could not configure sink sink1 due to: No channel configured for sink: sink1 org.apache.flume.conf.ConfigurationException: No channel configured for sink: sink1
1.错误如下所示,启动flume采集文件到hdfs案例的时候,出现如下所示的错误: 大概是说No channel configured for sink,所以应该是sink哪里配置出现了错误,百度了一 ...
- java.lang.NullPointerException at java.lang.ProcessBuilder.start(Unknown Source) at org.apache.hadoop.util.Shell.runCommand(Shell.java:482)
1:问题出现的原因,部署好的hadoop-2.6.4进行window10操作hadoop api出现的错误,具体错误是我向hdfs上传文件,还好点,之前解决过,这里不叙述,这里说一下从hdfs下载文件 ...
- es6 let和const
一.let 1.let块作用域 if(true){ var a=1; let b=2; } console.log("a:"+a);//a:1 console.log(" ...
- centos6.5上安装redis3.2.1遇见的坑
解决方法: 安装gcc即可解决 解决方法: make MALLOC=libc 原因分析: 说关于分配器allocator, 如果有MALLOC 这个 环境变量, 会有用这个环境变量的 去建立Redi ...