PAT
傅里叶不想变换_
这个作者很懒,什么都没留下…
展开
-
1021 个位数统计 (15分)
题意:给一个1000位数的整数,统计各个位数的出现次数。原创 2020-05-22 19:14:12 · 87 阅读 · 0 评论 -
1087 (两种方法)All Roads Lead to Rome--条条道路通罗马
Indeed there are many different tourist routes from our city to Rome. You are supposed to find your clients the route with the least cost while gaining the most happiness.Input Specification:Each in...原创 2020-05-01 09:24:12 · 1001 阅读 · 0 评论 -
A1072(加油站)
#include<bits/stdc++.h>using namespace std;const int maxn = 1020;const int INF = 100000;int n, m, k, Ds, G[maxn][maxn];int d[maxn];bool vis[maxn] = {false};void dijkstra(int s) ...原创 2020-04-30 09:29:23 · 111 阅读 · 0 评论 -
1018 Public Bike Management
There is a public bike service in Hangzhou City which provides great convenience to the tourists from all over the world. One may rent a bike at any station and return it to any other stations in the ...原创 2020-04-29 11:45:34 · 77 阅读 · 0 评论 -
迪杰斯特拉路径保存
第一种方式:用pre[ ]数组表示从起点s到顶点v的最短路径上v的前一个顶点的编号。如果d[u] + G[u][v] < d[v],说明以u作为中介点可以使d[v]更优,此时需要令v的前驱节点为u,并且即便原先的pre[v]存放了若干个节点都应该清空,然后再添加u,因为正是因为这个中介点才使得到达v这个节点的路径变短,所有之前到达v的所有节点都需要清除,然后将u节点添加进去。如果d[u]...原创 2020-04-27 17:10:08 · 777 阅读 · 0 评论 -
1034 (DFS)Head of a Gang (并查集)
One way that the police finds the head of a gang is to check people’s phone calls. If there is a phone call between A and B, we say that A and B is related. The weight of a relation is defined to be t...原创 2020-04-27 09:45:36 · 121 阅读 · 0 评论 -
1076(并查集) Forwards on Weibo
Weibo is known as the Chinese version of Twitter. One user on Weibo may have many followers, and may follow many other users as well. Hence a social network is formed with followers relations. When a ...原创 2020-04-26 10:29:07 · 115 阅读 · 0 评论 -
1107 Social Clusters(并查集)
When register on a social network, you are always asked to specify your hobbies in order to find some potential friends with the same hobbies. A social cluster is a set of people who have some of thei...原创 2020-04-25 16:08:18 · 122 阅读 · 0 评论 -
拓扑排序
拓扑排序的重要应用就是判断有向无环图。算法思想:1、定义一个队列,所有入度为0的顶点加入队列。2、取队首顶点,输出,然后删除从该顶点出发的所有边,并令这些边到达的顶点的入度-1,如果某个顶点入度减为0,则将其加入队列。3、重复步骤2,直到队列为空。注意:如果要求有多个入度为0的顶点时选择编号最小的顶点,那么把queue改成priority_queue()即可,(priority_queu...原创 2020-04-25 10:41:54 · 97 阅读 · 0 评论 -
kruskal(克鲁斯卡尔)
克鲁斯卡尔算法思想:1、将所有边权从小到大排序2、按照边权从小到大测试所有的边,如果该边的两个顶点不在同一个连通块中,则把这条测试边加入当前最小生成树中。3、执行步骤2,知道最小生成树的边数等于顶点数-1,如果结束时最小生成树的边数小于顶点数-1,说明该图不连通。注意步骤2:测试两个顶点在不在一个连通块中,如果把连通块看成一个集合,那么就转化成判断两个顶点是否在同一个集合中,所以可以使用...原创 2020-04-25 10:10:39 · 340 阅读 · 0 评论 -
prim算法的邻接表和邻接矩阵实现
邻接矩阵版const int maxn = 1000;const int INF = 100000;int n; //n为点数int G[maxn][maxn];ind d[maxn]; //d[i]表示顶点i到集合S的最短距离bool vis[maxn] = {false};int prim(){ //初始化 fill(d, d + maxn, INF...原创 2020-04-24 22:29:35 · 729 阅读 · 0 评论 -
弗洛伊德算法
#include<bits/stdc++.h>using namespace std;const int maxn = 200;const int INF = 100000;int n, m; //n为点数,m为边数int dis[maxn][maxn]; //dis[i][j]表示i到j的距离void Floyd(){ for(int k = 0; ...原创 2020-04-24 16:20:05 · 202 阅读 · 0 评论 -
Bellman--Ford和SPFA(Bellman--Ford的改进)
Bellman–Ford:对途中的边进行V-1轮操作,每一轮操作都遍历所有的边,对每条边u->v,如果以u为中介点可以使d[v]更小就优化。此时,如果没有从源点可达的负环,那么数组d中的所有值应该已经达到最优。因此,再对所有边进行一轮操作,判断是否u->v仍然可以进行松弛操作,如果可以,说明有从源可达的负环,返回false,否则,说明数组d中的所有值已经达到最优,返回true;Bel...原创 2020-04-24 15:46:51 · 141 阅读 · 0 评论 -
图的遍历总结
DFS邻接矩阵存储图注意:如果G是一个连通图,那么只需要一次DFS就行了。const int maxn = 1000;const int INF = 1000000;int n, G[maxn][maxn];bool vis[maxn] = {false};//访问u所在的连通块void DFS(int u, int depth) //u为当前访问的顶点标号,depth为深度...原创 2020-04-24 13:52:10 · 791 阅读 · 0 评论 -
1030 Travel Plan(Dijkstra or Dijkstra+DFS)
A traveler’s map gives the distances between cities along the highways, together with the cost of each highway. Now you are supposed to write a program to help a traveler to decide the shortest path b...原创 2020-04-24 11:52:02 · 96 阅读 · 0 评论 -
1003 Emergency(迪杰斯特拉)
1003 Emergency//迪杰斯特拉//邻接矩阵版#include<bits/stdc++.h>using namespace std;const int maxn = 510; //最大顶点数const int INF = 1000000;int n, m, st, ed, weight[maxn], G[maxn][maxn]; //n为顶点数,m为边...原创 2020-04-24 00:52:46 · 150 阅读 · 0 评论 -
迪杰斯特拉
//迪杰斯特拉//邻接矩阵版#include<bits/stdc++.h>using namespace std;const int maxn = 1000; //最大顶点数const int INF = 1000000;int n, m, s, G[maxn][maxn]; //n为顶点数,m为边数,s为起点,G邻接矩阵int d[maxn]; ...原创 2020-04-23 23:33:32 · 147 阅读 · 0 评论 -
树的遍历总结
A1090 Highest Price in Supply Chain计算一棵树最深的叶子结点的个数。思路:1、维持一个最大深度max_depth,初始化为一个很小的数字(正常情况不会达到),初始化最大深度叶子结点个数num=0。2、递归边界:当前结点index没有孩子,说明已经到达叶子结点。vector<int> Node[maxn];int num = 0, max_d...原创 2020-04-19 16:02:34 · 161 阅读 · 0 评论 -
1004 Counting Leaves(求每一层有多少叶子结点) (30分)
A family hierarchy is usually presented by a pedigree tree. Your job is to count those family members who have no child.Input Specification:Each input file contains one test case. Each case starts w...原创 2020-04-19 14:48:04 · 123 阅读 · 0 评论 -
1099 Build A Binary Search Tree (30分)
注意点:根结点已经给出,就是0,二叉查找树的中序遍历是一个递增序列,所以将一个序列排序后插入二叉树就可以了。#include <cstdio>#include <queue>#include <algorithm>#include <vector>using namespace std;const int maxn = 110;int...原创 2020-04-06 22:43:52 · 73 阅读 · 0 评论 -
1102 Invert a Binary Tree (25分)
The following is from Max Howell @twitter:Google: 90% of our engineers use the software you wrote (Homebrew), but you can’t invert a binary tree on a whiteboard so fuck off.Now it’s your turn to pro...原创 2020-04-02 21:05:40 · 95 阅读 · 0 评论 -
1020 Tree Traversals (重建二叉树)
Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and inorder traversal sequences, you are supposed to output the level order traversal sequence of the cor...原创 2020-04-02 11:11:22 · 118 阅读 · 0 评论 -
1091 Acute Stroke (连通图个数)
One important factor to identify acute stroke (急性脑卒中) is the volume of the stroke core. Given the results of image analysis in which the core regions are identified in each MRI slice, your job is to c...原创 2020-04-01 21:10:59 · 157 阅读 · 0 评论 -
1097 Deduplication on a Linked List (25分)
Given a singly linked list L with integer keys, you are supposed to remove the nodes with duplicated absolute values of the keys. That is, for each value K, only the first node of which the value or a...原创 2020-04-01 11:23:01 · 54 阅读 · 0 评论 -
1052 Linked List Sorting (链表排序)
A linked list consists of a series of structures, which are not necessarily adjacent in memory. We assume that each structure contains an integer key and a Next pointer to the next structure. Now give...原创 2020-03-31 22:03:37 · 131 阅读 · 0 评论 -
1032 Sharing (输出链表的公共结点的地址)
To store English words, one method is to use linked lists and store a word letter by letter. To save some space, we may let the words share the same sublist if they share the same suffix. For example,...原创 2020-03-31 19:41:19 · 117 阅读 · 0 评论 -
1078 Hashing (二次探查法)
The task of this problem is simple: insert a sequence of distinct positive integers into a hash table, and output the positions of the input numbers. The hash function is defined to be H(key)=key%TSiz...原创 2020-03-30 20:36:31 · 675 阅读 · 0 评论 -
1015 Reversible Primes (素数+进制转换)
A reversible prime in any number system is a prime whose “reverse” in that number system is also a prime. For example in the decimal system 73 is a reversible prime because its reverse 37 is also a pr...原创 2020-03-30 19:42:39 · 146 阅读 · 0 评论 -
1049 数列的片段和 (20分)
给定一个正数数列,我们可以从中截取任意的连续的几个数,称为片段。例如,给定数列 { 0.1, 0.2, 0.3, 0.4 },我们有 (0.1) (0.1, 0.2) (0.1, 0.2, 0.3) (0.1, 0.2, 0.3, 0.4) (0.2) (0.2, 0.3) (0.2, 0.3, 0.4) (0.3) (0.3, 0.4) (0.4) 这 10 个片段。给定正整数数列,求出全部片...原创 2020-03-28 21:52:31 · 92 阅读 · 0 评论 -
1101 Quick Sort (25分)
There is a classical process named partition in the famous quick sort algorithm. In this process we typically choose one element as the pivot. Then the elements less than the pivot are moved to its le...原创 2020-03-28 21:23:13 · 65 阅读 · 0 评论 -
1054 The Dominant Color (二维数组出现次数最多的数)
Behind the scenes in the computer’s memory, color is always talked about as a series of 24 bits of information for each pixel. In an image, the color with the largest proportional area is called the d...原创 2020-03-28 15:24:38 · 191 阅读 · 0 评论 -
1048 Find Coins (25分)
Eva loves to collect coins from all over the universe, including some other planets like Mars. One day she visited a universal shopping mall which could accept all kinds of coins as payments. However,...原创 2020-03-25 14:38:16 · 131 阅读 · 0 评论 -
1029 Median (25分)
Given an increasing sequence S of N integers, the median is the number at the middle position. For example, the median of S1 = { 11, 12, 13, 14 } is 12, and the median of S2 = { 9, 10, 15, 16, 17 } is...原创 2020-03-25 14:19:14 · 92 阅读 · 0 评论 -
1044 Shopping in Mars (25分)
Shopping in Mars is quite a different experience. The Mars people pay by chained diamonds. Each diamond has a value (in Mars dollars M$). When making the payment, the chain can be cut at any position ...原创 2020-03-24 22:53:16 · 123 阅读 · 0 评论 -
B1030 完美数列 (25分)/A1085 Perfect Sequence (25分)
给定一个正整数数列,和正整数 p,设这个数列中的最大值是 M,最小值是 m,如果 M≤mp,则称这个数列是完美数列。现在给定参数 p 和一些正整数,请你从中选择尽可能多的数构成一个完美数列。输入格式:输入第一行给出两个正整数 N 和 p,其中 N(≤10^5)是输入的正整数的个数,p(≤10e9)是给定的参数。第二行给出 N 个正整数,每个数不超过 10^9 。输出格式:在一行...原创 2020-03-24 17:30:48 · 75 阅读 · 0 评论 -
1067 Sort with Swap(0, i) (25分)
1067 Sort with Swap(0, i) Given any permutation of the numbers {0, 1, 2,…, N−1}, it is easy to sort them in increasing order. But what if Swap(0, *) is the ONLY operation that is allowed to ...原创 2020-03-24 15:26:03 · 94 阅读 · 0 评论 -
1033 To Fill or Not to Fill(加不加油)
1033 To Fill or Not to FillWith highways available, driving a car from Hangzhou to any other city is easy. But since the tank capacity of a car is limited, we have to find gas stations on th...原创 2020-03-24 14:00:10 · 70 阅读 · 0 评论 -
1037 Magic Coupon(最大乘积之和)
1037 Magic CouponThe magic shop in Mars is offering some magic coupons. Each coupon has an integer N printed on it, meaning that when you use this coupon with a product, you may get N times...原创 2020-03-23 20:14:56 · 158 阅读 · 0 评论 -
1020 月饼
1020 月饼 (25分)月饼是中国人在中秋佳节时吃的一种传统食品,不同地区有许多不同风味的月饼。现给定所有种类月饼的库存量、总售价、以及市场的最大需求量,请你计算可以获得的最大收益是多少。注意:销售时允许取出一部分库存。样例给出的情形是这样的:假如我们有 3 种月饼,其库存量分别为 18、15、10 万吨,总售价分别为 75、72、45 亿元。如果市场的最大需求量只有 2...原创 2020-03-23 15:14:57 · 90 阅读 · 0 评论 -
1023 组个最小数
1023 组个最小数给定数字 0-9 各若干个。你可以以任意顺序排列这些数字,但必须全部使用。目标是使得最后得到的数尽可能小(注意 0 不能做首位)。例如:给定两个 0,两个 1,三个 5,一个 8,我们得到的最小的数就是 10015558。现给定数字,请编写程序输出能够组成的最小的数。输入格式:输入在一行中给出 10 个非负整数,顺序表示我们拥有数字 0、数字 1、……数字 ...原创 2020-03-23 10:51:31 · 94 阅读 · 0 评论