Acwing 正方形数组的数目(dfs去重)

解题代码
#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
#define rep(i,a,n) for(int i = a; i <= n; i++)
const int N = 20;
int a[N];
int n,ans;
int st[N];
int check(int x){
int y = sqrt(x);
return y * y == x;
}
void dfs(int u, int last){
if(u == n){
ans++;
}else{
rep(i,0,n-1){
if(st[i])continue;//如果这个数已经被搜过了,就代表不用搜了
if(i && !st[i - 1] && a[i] == a[i - 1])continue;
//首先比第一个数大,
//然后处于递归的同一层,前一个肯定已经恢复现场了,
//最后判断是否和前一个相等.
if(!check(a[i] + last)) continue;//不满足条件
st[i] = 1;
dfs(u + 1,a[i]);
st[i] = 0;
}
}
}
int main(){
cin >> n;
rep(i,0,n - 1){
cin >> a[i];
}
sort(a,a + n);
rep(i,0,n -1){
if(!i|| a[i] != a[i - 1]){
st[i] = 1;
dfs(1,a[i]);//因为我们这里已经自己选择了一个数作为第一个元素,所以这里的1代表第二层
//所以递归的出口就是 u == n
st[i] = 0;
}
}
cout << ans << endl;
return 0;
}
dfs去重输出
#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
#define rep(i,a,n) for(int i = a; i <= n; i++)
const int N = 20;
int a[N];
int n,ans;
int st[N];
int path[N];
void dfs(int u, int last){
if(u == n){
rep(i,0,n-1) cout<<path[i]<<" ";
puts("");
}else{
rep(i,0,n-1){
if(st[i])continue;
if(i && !st[i - 1] && a[i] == a[i - 1])continue;
st[i] = 1;
path[u] = a[i];
dfs(u + 1,a[i]);
st[i] = 0;
}
}
}
int main(){
cin >> n;
rep(i,0,n - 1) cin >> a[i];
sort(a,a + n);
rep(i,0,n -1){
if(!i|| a[i] != a[i - 1]){
st[i] = 1;
path[0] = a[i];
dfs(1,a[i]);
st[i] = 0;
}
}
return 0;
}
结果参考

Acwing 正方形数组的数目(dfs去重)的更多相关文章
- [Swift]LeetCode996. 正方形数组的数目 | Number of Squareful Arrays
Given an array A of non-negative integers, the array is squareful if for every pair of adjacent elem ...
- php二维数组根据某个字段去重
php的二维数组根据某个字段去重,在这默认为二维数组的结构是一样的,现在根据二维数组里的id字段去重,把id相同的重复的元素去掉 /** * 二维数组根据某个字段去重 * @param array $ ...
- java对一个int数组进行排序、去重
思路: 1.使用 HashSet 进行去重 2.将 HashSet 变为 TreeSet 3.使用 TreeSet 进行排序 4.将 Set 变为 Integer 数组 5.将 Integer 数组变 ...
- poj 1564 Sum It Up (DFS+ 去重+排序)
http://poj.org/problem?id=1564 该题运用DFS但是要注意去重,不能输出重复的答案 两种去重方式代码中有标出 第一种if(a[i]!=a[i-1])意思是如果这个数a[i] ...
- AcWing 228. 异或 (dfs+线性基)打卡
题目:https://www.acwing.com/problem/content/230/ 题意:有一个图,每条边有一个权值,现在求1-n的一条路径的最大异或和,一条边能经过多次,相应的也要计算那么 ...
- hdu 1258 Sum It Up(dfs+去重)
题目大意: 给你一个总和(total)和一列(list)整数,共n个整数,要求用这些整数相加,使相加的结果等于total,找出所有不相同的拼凑方法. 例如,total = 4,n = 6,list = ...
- JS 中数组的排序和去重
在 PHP 中,数组有很多排序方法,不过其他语言的数组中大概是不会像 JS 的数组一样,包罗万象,啥都通吃的.所以 JS 的数组排序情况就略多一些了. 简单粗暴的排序: 赤果果的sort: var ...
- 线性时间常数空间找到数组中数目超过n/5的所有元素
问题描述: Design an algorithm that, given a list of n elements in an array, finds all the elements that ...
- poj 1564 Sum It Up【dfs+去重】
Sum It Up Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 6682 Accepted: 3475 Descrip ...
随机推荐
- 基于ABP实现DDD--DDD相关概念
什么是DDD呢?领域驱动设计[DDD]是一种针对复杂需求的软件开发方法.将软件实现与不断发展的模型联系起来,专注于核心领域逻辑,而不是基础设施细节.DDD适用于复杂领域和大规模应用,而不是简单的C ...
- 减省 Java 小半内存,Solon v1.9.2 发布
相对于 Spring Boot 和 Spring Cloud 的项目: 启动快 5 - 10 倍. (更快) qps 高 2- 3 倍. (更高) 运行时内存节省 1/3 ~ 1/2. (更少) 打包 ...
- 【Kaggle】如何有效避免OOM(out of memory)和漫长的炼丹过程
本文介绍一些避免transformers的OOM以及训练等流程太漫长的方法,主要参考了kaggle notebook Optimization approaches for Transformers ...
- 更换可执行文件glibc版本的某一次挣扎
0x00:前言 在做pwn的堆题时,会遇到不同版本的glibc.为此我们会装不同版本的虚拟机去应对.一般来说会装Ubuntu16和Ubuntu18虚拟机,这两个系统对应的glibc版本差别较大,且较常 ...
- mysql Insert强化
INSERT [LOW_PRIORITY | DELAYED | HIGH_PRIORITY] [IGNORE] [INTO] tbl_name [(col_name,...)] VALUES ({e ...
- 基于.NET6、FreeSql、若依UI、LayUI、Bootstrap构建插件式的CMS
近几年,.net生态日益强大,特别是跨平台技术,性能提升,那真的是强大无比.为了日常能够快速开发,笔者基于基于.NET6.FreeSql.若依UI.LayUI.Bootstrap构建插件式的CMS,请 ...
- ESP32与MicroPython入门-01 搭建开发环境
ESP32简介 ESP32 是上海乐鑫公司开发的一款比较新的32位微控制器,它集成了WiFi及蓝牙等功能,有着性能稳定.功耗低.价格低廉等特点,非常适用于物联网开发,但也可以作为普通的MCU使用. E ...
- 定时器控制单只LED灯
点击查看代码 #include <reg51.h> #define uchar unsigned char #define uint unsigned int sbit LED=P0^0; ...
- 如何在Apple Silicon Mac上主动安装Rosetta2
前提是您的电脑搭载了Apple Silicon处理器 command + space(空格),输入"终端",打开. 输入 : softwareupdate --install-ro ...
- java.lang.UnsatisfiedLinkError报错
是因为使用maven时,运行web项目时,在maven的依赖包没有打包到tomcat中(out目录中),所以要手动加上