
算法
文章平均质量分 68
包括pat 甲级、乙级、天梯赛和其他部分oj
zp_jit
欢迎大家关注我的博客,不定时更新
展开
-
PAT 乙级 1086
1086就不告诉你(15分)做作业的时候,邻座的小盆友问你:“五乘以七等于多少?”你应该不失礼貌地围笑着告诉他:“五十三。”本题就要求你,对任何一对给定的正整数,倒着输出它们的乘积。输入格式:输入在第一行给出两个不超过 1000 的正整数 A 和 B,其间以空格分隔。输出格式:在一行中倒着输出 A 和 B 的乘积。输入样例:5 7输出样例:53...原创 2020-02-10 10:05:51 · 278 阅读 · 0 评论 -
欧拉筛法模板
O(n)的复杂度:bool prime[MAXN+5];int ans[MAXN+5],cnt;void getPrimeFaster(int n){ memset(prime,1,sizeof(prime)); cnt=1, prime[0]=prime[1]=0; ans[0]=2; for(int i=3;i<=n;i=i+2)//除2外的偶数...原创 2018-09-05 11:59:01 · 147 阅读 · 0 评论 -
Data Structure —— Linkedlist(queue & stack in Java)
LinkedList其实也就是我们在数据结构中的链表,这种数据结构有这样的特性:分配内存空间不是必须是连续的; 插入、删除操作很快,只要修改前后指针就OK了,时间复杂度为O(1); 访问比较慢,必须得从第一个元素开始遍历,时间复杂度为O(n);在Java中,LinkedList提供了丰富的方法,可以模拟链式队列,链式堆栈等数据结构,为用户带来了极大的方便,下面看看这些方法的用法:ad...转载 2018-06-13 21:04:21 · 172 阅读 · 0 评论 -
背包九讲
背包问题九讲 v1.0目录 第一讲 01背包问题 第二讲 完全背包问题 第三讲 多重背包问题 第四讲 混合三种背包问题 第五讲 二维费用的背包问题 第六讲 分组的背包问题 第七讲 有依赖的背包问题 第八讲 泛化物品 第九讲 背包问题问法的变化 附:USACO中的背包问题 前言本篇文章是我(dd_engi)正在进行中的一个雄心勃勃的写作计划的一部分,这个计划的内容是写作一份较为完善的NOIP难度的动...转载 2018-05-22 15:54:31 · 294 阅读 · 0 评论 -
搜索剪枝问题的概述
剪枝是啥:在使用dfs和bfs搜索时,剪枝即剪去一些肯定不可能产生最优解的分支。这将可以大大缩减搜索的时间。剪枝的种类简要叙述:1.可行性剪枝:让你求走n步的可能走法,搜到n步就可以return了,不要往后搜了。即剪去那些不合法的搜索路径。2.最优性剪枝:让你求最小值,你搜索过程种求得的当前值 或者 当前值+预测后面的会产生的最小值 肯定大于之前搜索出来的最小值,那这种情况算到最后也不会产生更优解...原创 2018-05-02 12:04:24 · 638 阅读 · 0 评论 -
傻子都能看懂的 并查集入门与代码模板
并查集: 并查集算法在天梯赛和pat甲级里属于必须掌握的算法之一。 原理及适用范围: 给定一组数据及他们之间的关系,问任意两个元素是否在一个集合内。(例如,给定一个家谱,问某两个人是否是亲戚)很容易想到的是暴力搜索,但随着数据量的增加这种查找显然会超时。而并查集的原理则是在每一个集合中确定一个代表元,判断两个元素是否是一个集合只需要查看对应的代表元是否在相同...原创 2018-03-25 19:55:29 · 471 阅读 · 0 评论 -
天梯赛L3-016 二叉搜索树的结构(模拟)
L3-016 二叉搜索树的结构(30 分)二叉搜索树或者是一棵空树,或者是具有下列性质的二叉树:若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值;若它的右子树不空,则右子树上所有结点的值均大于它的根结点的值;它的左、右子树也分别为二叉搜索树。(摘自百度百科)给定一系列互不相等的整数,将它们顺次插入一棵初始为空的二叉搜索树,然后对结果树的结构进行描述。你需要能判断给定的描述...原创 2018-06-19 11:10:37 · 543 阅读 · 0 评论 -
天梯赛L3-011. 直捣黄龙(dijkstra)
L3-011. 直捣黄龙时间限制150 ms内存限制65536 kB代码长度限制8000 B判题程序Standard作者陈越本题是一部战争大片 —— 你需要从己方大本营出发,一路攻城略地杀到敌方大本营。首先时间就是生命,所以你必须选择合适的路径,以最快的速度占领敌方大本营。当这样的路径不唯一时,要求选择可以沿途解放最多城镇的路径。若这样的路径也不唯...原创 2018-04-04 10:35:36 · 443 阅读 · 0 评论 -
天梯赛L2-002. 链表去重(模拟)
L2-002. 链表去重时间限制300 ms内存限制65536 kB代码长度限制8000 B判题程序Standard作者陈越给定一个带整数键值的单链表L,本题要求你编写程序,删除那些键值的绝对值有重复的结点。即对任意键值K,只有键值或其绝对值等于K的第一个结点可以被保留。同时,所有被删除的结点必须被保存在另外一个链表中。例如:另L为21→-15→-...原创 2018-03-27 15:30:35 · 367 阅读 · 0 评论 -
天梯赛L2-001. 紧急救援(最短路dijkstra)
L2-001. 紧急救援时间限制200 ms内存限制65536 kB代码长度限制8000 B判题程序Standard作者陈越作为一个城市的应急救援队伍的负责人,你有一张特殊的全国地图。在地图上显示有多个分散的城市和一些连接城市的快速道路。每个城市的救援队数量和每一条连接两个城市的快速道路长度都标在地图上。当其他城市有紧急求助电话给你的时候,你的任务...原创 2018-03-27 15:19:40 · 465 阅读 · 0 评论 -
天梯赛L2-013. 红色警报(并查集/dfs求连通图数量)
L2-013. 红色警报时间限制400 ms内存限制65536 kB代码长度限制8000 B判题程序Standard作者陈越战争中保持各个城市间的连通性非常重要。本题要求你编写一个报警程序,当失去一个城市导致国家被分裂为多个无法连通的区域时,就发出红色警报。注意:若该国本来就不完全连通,是分裂的k个区域,而失去一个城市并不改变其他城市之间的连通性,...原创 2018-03-27 14:48:32 · 447 阅读 · 0 评论 -
天梯赛L2-007. 家庭房产(并查集+排序)
L2-007. 家庭房产时间限制400 ms内存限制65536 kB代码长度限制8000 B判题程序Standard作者陈越给定每个人的家庭成员和其自己名下的房产,请你统计出每个家庭的人口数、人均房产面积及房产套数。输入格式:输入第一行给出一个正整数N(<=1000),随后N行,每行按下列格式给出一个人的房产:编号 父 母 k 孩...原创 2018-03-27 14:31:11 · 668 阅读 · 0 评论 -
天梯赛L2-024. 部落(并查集)
L2-024. 部落时间限制120 ms内存限制65536 kB代码长度限制8000 B判题程序Standard作者陈越在一个社区里,每个人都有自己的小圈子,还可能同时属于很多不同的朋友圈。我们认为朋友的朋友都算在一个部落里,于是要请你统计一下,在一个给定社区中,到底有多少个互不相交的部落?并且检查任意两个人是否属于同一个部落。输入格式:输...原创 2018-03-26 16:41:06 · 235 阅读 · 0 评论 -
天梯赛L3-008. 喊山(bfs)
L3-008. 喊山时间限制150 ms内存限制65536 kB代码长度限制8000 B判题程序Standard作者陈越喊山,是人双手围在嘴边成喇叭状,对着远方高山发出“喂—喂喂—喂喂喂……”的呼唤。呼唤声通过空气的传递,回荡于深谷之间,传送到人们耳中,发出约定俗成的“讯号”,达到声讯传递交流的目的。原来它是彝族先民用来求援呼救的“讯号”,慢慢地人...原创 2018-03-26 16:26:28 · 398 阅读 · 0 评论 -
天梯赛L2-010. 排座位(并查集)
L2-010. 排座位时间限制150 ms内存限制65536 kB代码长度限制8000 B判题程序Standard作者陈越布置宴席最微妙的事情,就是给前来参宴的各位宾客安排座位。无论如何,总不能把两个死对头排到同一张宴会桌旁!这个艰巨任务现在就交给你,对任何一对客人,请编写程序告诉主人他们是否能被安排同席。输入格式:输入第一行给出3个正整数...原创 2018-03-26 15:34:02 · 427 阅读 · 0 评论 -
PAT 甲级 1052 Linked List Sorting(模拟)
1052 Linked List Sorting (25 分)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 pointe...原创 2018-11-21 21:56:53 · 203 阅读 · 0 评论 -
PAT 甲级 1050 String Subtraction(散列表)
1050 String Subtraction (20 分)Given two strings S1 and S2, S=S1−S2 is defined to be the remaining string after taking all the characters in S2 from S1. Your task is simply to calcu...原创 2018-11-21 21:53:41 · 284 阅读 · 0 评论 -
PAT 甲级 1045 Favorite Color Stripe(动态规划)
1045 Favorite Color Stripe (30 分)Eva is trying to make her own color stripe out of a given one. She would like to keep only her favorite colors in her favorite order by cutting off those unwanted pi...原创 2018-11-20 16:43:17 · 254 阅读 · 0 评论 -
PAT 甲级 1044 Shopping in Mars (前缀和)
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 cha...原创 2018-11-20 16:34:36 · 279 阅读 · 0 评论 -
PAT 甲级 1043 Is It a Binary Search Tree(二叉搜索树)
1043 Is It a Binary Search Tree (25 分)A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties:The left subtree of a node contains only nodes with keys...原创 2018-11-20 16:27:03 · 305 阅读 · 0 评论 -
PAT 甲级 1042 Shuffling Machine (模拟)
1042 Shuffling Machine (20 分)Shuffling is a procedure used to randomize a deck of playing cards. Because standard shuffling techniques are seen as weak, and in order to avoid "inside jobs" where emp...原创 2018-11-20 16:13:50 · 227 阅读 · 0 评论 -
PAT 甲级 1041 Be Unique(模拟)
1041 Be Unique (20 分)Being unique is so important to people on Mars that even their lottery is designed in a unique way. The rule of winning is simple: one bets on a number chosen from [1,104]. T...原创 2018-11-03 13:10:42 · 275 阅读 · 0 评论 -
PAT 甲级 1023 Have Fun with Numbers(大数模拟)
1023 Have Fun with Numbers (20 分)Notice that the number 123456789 is a 9-digit number consisting exactly the numbers from 1 to 9, with no duplication. Double it we will obtain 246913578, which happe...原创 2018-11-02 08:48:21 · 190 阅读 · 0 评论 -
PAT 甲级1035 Password(模拟)
1035 Password (20 分)To prepare for PAT, the judge sometimes has to generate random passwords for the users. The problem is that there are always some confusing passwords since it is hard to distingu...原创 2018-10-11 19:56:52 · 300 阅读 · 0 评论 -
PAT 甲级 1084 Broken Keyboard(散列表)
1084 Broken Keyboard (20 分)On a broken keyboard, some of the keys are worn out. So when you type some sentences, the characters corresponding to those keys will not appear on screen.Now given a st...原创 2018-10-10 19:07:37 · 230 阅读 · 0 评论 -
PAT 甲级 1031 Hello World for U(模拟)
1031 Hello World for U (20 分)Given any string of N (≥5) characters, you are asked to form the characters into the shape of U. For example, helloworld can be printed as:h de ll rlowoThat ...原创 2018-10-10 12:49:14 · 162 阅读 · 0 评论 -
PAT 甲级 1019 General Palindromic Number(进制转换)
1019 General Palindromic Number (20 分)A number that will be the same when it is written forwards or backwards is known as a Palindromic Number. For example, 1234321 is a palindromic number. All sing...原创 2018-10-09 18:06:08 · 165 阅读 · 0 评论 -
PAT 甲级 1037 Magic Coupon(贪心排序)
1037 Magic Coupon (25 分)The 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 t...原创 2018-10-08 22:03:11 · 135 阅读 · 0 评论 -
PAT 甲级 1025 PAT Ranking(统计+排序)
1025 PAT Ranking(25 分)Programming Ability Test (PAT) is organized by the College of Computer Science and Technology of Zhejiang University. Each test is supposed to run simultaneously in several pla...原创 2018-09-12 12:51:23 · 170 阅读 · 0 评论 -
PAT 甲级 1030 Travel Plan(dijkstra)
1030 Travel Plan(30 分)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 de...原创 2018-09-06 19:31:58 · 302 阅读 · 0 评论 -
PAT 甲级 1018 Public Bike Management(dijkstra+dfs)
1018 Public Bike Management(30 分)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 retu...原创 2018-09-06 17:54:40 · 245 阅读 · 0 评论 -
PAT 甲级 1088 Rational Arithmetic(模拟)
1088 Rational Arithmetic(20 分)For two rational numbers, your task is to implement the basic arithmetics, that is, to calculate their sum, difference, product and quotient.Input Specification:Eac...原创 2018-09-05 09:24:28 · 314 阅读 · 0 评论 -
PAT 甲级 1090 Highest Price in Supply Chain(bfs)
1090 Highest Price in Supply Chain(25 分)A supply chain is a network of retailers(零售商), distributors(经销商), and suppliers(供应商)-- everyone involved in moving a product from supplier to customer.Start...原创 2018-09-03 09:52:15 · 168 阅读 · 0 评论 -
PAT 甲级 1092 To Buy or Not to Buy(hash映射)
1092 To Buy or Not to Buy(20 分)Eva would like to make a string of beads with her favorite colors so she went to a small shop to buy some beads. There were many colorful strings of beads. However the...原创 2018-09-02 22:34:12 · 167 阅读 · 0 评论 -
PAT 甲级 1093 Count PAT's(统计)
1093 Count PAT's(25 分)The string APPAPT contains two PAT's as substrings. The first one is formed by the 2nd, the 4th, and the 6th characters, and the second one is formed by the 3rd, the 4th, and t...原创 2018-09-02 22:15:54 · 152 阅读 · 0 评论 -
PAT 甲级 1094 The Largest Generation(bfs)
1094 The Largest Generation(25 分)A family hierarchy is usually presented by a pedigree tree where all the nodes on the same level belong to the same generation. Your task is to find the generation w...原创 2018-09-02 19:57:54 · 177 阅读 · 0 评论 -
PAT 甲级 1095 Cars on Campus(模拟)
1095 Cars on Campus(30 分)Zhejiang University has 8 campuses and a lot of gates. From each gate we can collect the in/out times and the plate numbers of the cars crossing the gate. Now with all the i...原创 2018-09-02 18:44:40 · 178 阅读 · 0 评论 -
PAT 甲级 1099 Build A Binary Search Tree(中序遍历)
1099 Build A Binary Search Tree (30)(30 分)A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties:The left subtree of a node contains only nodes with ...原创 2018-08-18 14:27:31 · 243 阅读 · 0 评论 -
PAT 甲级 1097 Deduplication on a Linked List (模拟)
1097 Deduplication on a Linked List (25)(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 valu...原创 2018-08-17 14:53:58 · 156 阅读 · 0 评论 -
PAT 甲级 1096 Consecutive Factors(模拟)
1096 Consecutive Factors (20)(20 分)Among all the factors of a positive integer N, there may exist several consecutive numbers. For example, 630 can be factored as 3*5*6*7, where 5, 6, and 7 are the ...原创 2018-08-17 13:57:31 · 138 阅读 · 0 评论