HDU 1394 Minimum Inversion Number (线段树+区间求和)
The inversion number of a given number sequence a1, a2, ..., an is the number of pairs (ai, aj) that satisfy i < j and ai > aj.
For a given sequence of numbers a1, a2, ..., an, if we move the fi...
HDU 2795 Billboard (区间求最大值)
At the entrance to the university, there is a huge rectangular billboard of size h*w (h is its height and w is its width). The board is the place where all possible announcements are posted: nearest p...
【ACM】PAT. A1086 Tree Traversals Again【二叉树遍历】
题目链接
题目分析
Push 表示 前序队列
Pop 表示 中序队列
输出:后序遍历序列
解题思路
用前序 + 后序 构造二叉树,再遍历输出后序队列
AC程序(C++)
/**************************
*@Author: 3stone
*@ACM: PAT.A1086 Tree Traversals Again
*@Time: 18/7/26...
【ACM】PAT. A1102 Invert a Binary Tree 【二叉树】
题目链接
题目分析
输入顺序即为结点下标,输入内容为结点的左右子树下标
解题思路
1、保存输入结点信息
2、找到根结点
3、构造出二叉树
4、不用再invert,遍历时反转顺序即可!
AC程序(C++)
/**************************
*@Author: 3stone
*@ACM: PAT.A1102 Invert a Binary Tree ...
【ACM】- PAT. A1076 Forwards on Weibo【图的遍历】
题目链接
题目分析
1、L即表示最多传递L层
2、所给指向关系是反的,即所给结点号为其父节点
3、结点编号 1-N
解题思路
使用BFS遍历,遍历时记录每个结点的层数
注:不能用DFS,会遗漏某些可以更快到达的路径;
AC程序(C++)
/**********************************
*@Author: 3stone
*@ACM: PAT.A107...
【ACM】- PAT. A1013 Battle Over Cities 【图的遍历】
题目链接
题目分析
1、无向图
2、节点编号1-N
3、删去一个结点,查看变成几个连通块
解题思路
每次删除删遍历,看剩余接个连通块(DFS的次数)
AC程序(C++)
/**********************************
*@Author: 3stone
*@ACM: PAT.A1013 Battle Over Cities
*@Time: 18/8/1...
【ACM】- PAT.A1021 Deepest Root【图的遍历】
题目链接
题目分析
1、判断图是否为一棵树,是则输出最深根,不是则输出连通块数
2、节点编号1-N
3、无向图
解题思路
1、DFS判断 是否为一棵树
2、寻找最大根:
(法一)暴力搜索
结点数10000,则两重循环复杂度会达到10^8量级,
时间限制为1500ms,1s内计算次数一般为10^7-10^8,
提交发现,最大测试点花了将近1100...
PAT——A1095(排序)
题目链接:
#include<cstdio>
#include<iostream>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<map>
using namespace std;
#define maxn 10010
struct Car{
...
散列(hash)知识总结
散列是常用的算法思想之一
应用如,给出N个正整数,再给出M个正整数
问:M个数中的每个数是否在N个数中出现过
如果N,M<=10^5,可以用遍历来做,但时间复杂度很大
这时就可以用空间换时间
即设定一个bool型数组hash Table[100010],
这样在一开始读入N个正整数时就进行预处理
代码:
#include<cstdio>
#define maxn...
字符串hash初步(待补充进阶部分12.1)
如果key不是整数,设计散列函数:
例子题目:
如何将一个二维整点P的坐标映射为一个整数,使得整点P可以由该整数唯一地代表
假设一个整点P的坐标是(x,y),其中0<=x,y<=Range
那么可以令hash函数为H(P)=x*Range+y
这样对数据范围内的任意两个整点p1,p2,
H(p1)都不会等于H(p2)
就可以用H(P)来唯一地代表该整点P
接着可以通过证...
PAT——B1029(hash)
要解决的两个问题:
①如何在英文字母不区分大小写的情况下判断在的一个字符串中有哪些字符没有在第二个字符串中出现
枚举
小写变大写
找不到相等就直接输出
②如何保证同一个字符(不区分大小写)只输出一次,且英文字母均使用大写输出
设置bool HashTable[128]表示字符是否已经输出
则可以判断是否输出
注意:HashTable数组的大小只要能把题目给出的字符包括即可
一般可...
PAT——1033(hash)
一开始的题意就没有完全弄懂
+号的意思是第二行所有大写的都不输出
总是有1分过不了
因为很有可能没有坏键输入
此时scanf和cin就不能用了
可以用getline
get(不知道为何pat总显示编译错误)
但getline是string型的
#include<cstdio>
#include<cstring>
#include<iostream&g...
PAT——乙级1038(水)
题目链接:
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
int stu[110]={0};
int main()
{
int n,gg;
scanf("%d",&n);
for(int i=0;i<n;i++)
...
PAT——A1092(hash)
题目链接
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
int HashTable[1010]={0};
int miss[1010]={0};
int change(char c)
{
if(c>='0'&&c<='9...
PAT——B1042(hash)
题目链接:
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
string str;
int HashTable[30]={0};
int main()
{
getline(cin,str);
int len=str.length();
...
PAT——B1043(hash)
题目链接:
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
string str;
int HashTable[6]={0};
char zm[6]={'P','A','T','e','s','t'};
int main()
{
getline(ci...
PAT——A1041(hash)
题目链接:
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
int a[100010];
int HashTable[10001]={0};
int main()
{
int m;
scanf("%d",&m);
int fl...
PAT——B1047(hash 水)
题目链接:
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
int a[100010];
int HashTable[10001]={0};
int main()
{
int m;
int dd,rm;
string s;
i...
今日推荐
周排行