随笔分类 - Sicily
摘要:题目传送门:1150. 简单魔板思路: 这道题如果要从现在的情况通过A B C三种操作而得到目标数阵很难找到一种有效的方法。但是考虑总共的操作不会多于10步,可以考虑将10步可能产生的所有数阵记录下来,然后在寻找目标数阵,将得到这个数阵的操作过程输出即可。由于每次有3种操作,所以我用了类似于决策树的结构,如果下一步进入左子树代表A操作,中子树代表B,右子树代表C,在每一个树的节点用一个string存入得到当前节点的全部操作,然后递归的建立这棵树。 在寻找某个目标数阵的时候,用一个全局变量哨兵值进行判断是否已经找到,也是用递归的方法进行寻找,由于结果不能超过n步,所以设计函数的时候把n当做参..
阅读全文
摘要:题目传送门:1146. 采药思路: 典型的0-1背包问题,不像分数背包问题可以用贪心算法,所以只能用动态规划。dp[i][j]表示在限定时间j内,只从前i个山药里面取能得到的最大价值。dp的构建同样采用自底向上的方法。 对于第i个山药,考虑取与不取,有dp[i - 1][j]和dp[i - 1][j - t[i]] + v[i]两种情况,取较大值即可。代码: 1 #include 2 #include 3 using namespace std; 4 5 6 int main(){ 7 int t,m; 8 cin >> t >> m; 9 int times[...
阅读全文
摘要:题目传送门:1264. Atomic Car Race思路: 如图,输入a1,a2,...an,图中共有n + 1个点,现在将这些点依次标记为0,1,2...n,现在要求找出由0到n的最短时间。 很容易发现求解这个问题的过程包含求解最优子结构的过程而且子问题是有重叠的,所以使用动态规划的方法。这里用s[n + 1][n + 1]来记录2个点之间的最短时间,任意 0 2 using namespace std; 3 4 int main(){ 5 int n; 6 while(cin >> n && n != 0){ 7 int a[n + 1]; 8 ...
阅读全文
摘要:题目传送门:1687. Permutation思路: 求n个数其中有k个 ,或者这时有k - 1个的间隙插入,或者在末尾插入,共有n - k种。 这样就可以利用n -1的情况算n的情况。开始用了递归超时了,考虑到其中重复计算了很多次相同的情况,所以用数组result[n][k]把所有结果存进去,然后直接读入输出即可。代码: 1 #include 2 using namespace std; 3 4 5 const int MAXN = 101,MAXK = 101; 6 int result[MAXN][MAXK] = {{0}}; 7 8 int main(){ 9 res...
阅读全文
摘要:题目传送门:1029. Rabbit思路: 题目说的有一点奇怪,兔子要过m个月才能长大,但是第m个月的时候已经可以生孩子了,这是需要注意的。 思路也比较简单,用一个rabbits[months_to_grow + 1]的数组存储各个月年龄的兔子数量,其中刚出生的存储在下标0中,成年的存储在下标months_to_grow中。显然下个月的时候rabbit[0] = rabbit[months_to_grow],并且其他年龄段的兔子年龄会向前推进,直到成年之后就一直在rabbit[months_to_grow]中。 看到样例 1267650600228229401496703205376 的...
阅读全文
摘要:题目传送门:1093. Air Express 题目本身没有什么思维上的难度主要考察思维的严密性,注意有可能weight3 * rate3 2 using namespace std; 3 4 int min(int a,int b,int c); 5 6 int main(){ 7 int weight1,weight2,weight3,rate1,rate2,rate3,rate4,weight,set_number = 1; 8 while(cin >> weight1 >> rate1){ 9 cin >> weight2 >> rate
阅读全文
摘要:题目传送门:1049. Mondriaan思路: 找规律的一道水题。 假设长度为n的有f(n)种画法。容易求出f(0)=1,f(1)=2,f(2)=7... 找出递推式f(n)=3f(n-1)+f(n-2)-f(n-3)(推导了半节英语课。。。),打好表之后输入输出搞定。代码: 1 #include 2 using namespace std; 3 4 const int MAX=1000001; 5 int f[MAX]; 6 7 int main(){ 8 int testcases,l; 9 cin>>testcases;10 f[0]=1;11 ...
阅读全文
摘要:题目传送门:1002. Anti-prime Sequences思路: 本题使用了dfs深度优先搜索(回溯法),用递归的形式实现。 一开始见到3s的时间竟然傻乎乎的跑去用回溯法按照字典序生成全排列,然后一个个的去判断是否是anti-prime,直接被TLE拍死。 后来才突然醒悟在搜索到某个数字时可以利用2到degree的相邻数字和不是素数这个性质进行大量剪枝才搞定。 这里选择了vector作为容器是为了能同时实现对元素的快速下标访问于在某一特定位置的增删元素。 用一个vector result存储生成的anti-prime sequence,然后将n~m这些数字一个一个的插进resu...
阅读全文
摘要:题目地址:Sicily 1012. Stacking Cylinders思路: 最低层如果有n个圆,则一共会有n层,其中最高一层有1个。用n个数组记录n层圆的坐标,一开始输入底层的坐标,排序之后再不断利用下一层的坐标算出当前一层的坐标,知道最高层就行。对于每一个圆心坐标,可以用支撑它的下面两个圆心坐标通过几何计算方法算出。注意输出的格式。代码: 1 #include 2 #include 3 #include 4 #include 5 using namespace std; 6 7 struct Point{ 8 double x,y; 9 };10 11 void get_a_...
阅读全文
摘要:题目地址:1156. Binary tree思路: 简单的一道二叉树相关的题目,题目会给出一颗树现在的形态,然后用前序遍历这棵树的节点输出数据即可。 每个节点会输入该节点的identifier,有点类似于key,然后输入该节点储存的数据(char类型)和该节点左右子节点的identifier。 这里直接用开了几个数组存储数据,对于一个节点identifier,将其数据存进data[identifier]里,左右子树分别存进left[identifier],right[identifier]中。输出的时候需要找到树的根root,所以可以开一个bool数组来找根,初始化全为false,读进的...
阅读全文
摘要:题目:1934. 移动小球思路: 想了很久,即使用链表在插入和删除元素的时候比较快,但用来查找删除插入的位置的时间也太长。 看了别人的代码之后顿时开窍,用两个数组分别记录每一个球的左边和右边球的编号,这样就可以实现数组对元素的快速访问。非常高明而简单的方法!感觉有点类似于基于数组实现的双端链表。代码: 1 #include 2 using namespace std; 3 4 int lefts[500001]; //lefts[i] stores the lefts ball's number of the ball i. 5 int rights[500001]; //rights
阅读全文
摘要:题目:DescriptionFarmer John has decided to take a family portrait of some (maybeall) of the cows. In order to take the portrait, FJ has arrangedall N (1 1 1 0 1 0 1 1+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+...
阅读全文
摘要:题目:DescriptionDr lee cuts a string S into N pieces,s[1],…,s[N].Now, Dr lee gives you these N sub-strings: s[1],…s[N]. There might be several possibilities that the string S could be. For example, if Dr. lee gives you three sub-strings {“a”,“ab”,”ac”}, the string S could be “aabac”,”aacab”,”abaac”,…Y
阅读全文
摘要:题目:ConstraintsTime Limit: 1 secs, Memory Limit: 256 MBDescriptionThe programming language Better And Portable Code (BAPC) is a language for working with lists of integers. The language has two built-in functions: ‘R’ (reverse) and ‘D’ (drop).The function ‘R’ reverses its input list, and ’D’ drops th
阅读全文
摘要:题目:DescriptionGiven a list of phone numbers, determine if it is consistent in the sense that no number is the prefix of another. Let’s say the phone catalogue listed these numbers:• Emergency 911• Alice 97 625 999• Bob 91 12 54 26In this case, it’s not possible to call Bob, because the central would
阅读全文
摘要:题目:DescriptionYouhavejustmovedfromWaterlootoabigcity.Thepeopleherespeakanincomprehensibledialectofaforeignlanguage.Fortunately,youhaveadictionarytohelpyouunderstandthem.InputInputconsistsofupto100,000dictionaryentries,followedbyablankline,followedbyamessageofupto100,000words.Eachdictionaryentryisali
阅读全文
摘要:题目:ConstraintsTime Limit: 1 secs, Memory Limit: 32 MBDescriptionThe only printer in the computer science students' union is experiencing an extremely heavy workload. Sometimes there are a hundred jobs in the printer queue and you may have to wait for hours to get a single page of output. Because
阅读全文
摘要:ConstraintsTime Limit: 1 secs, Memory Limit: 32 MBDescriptionA series of brackets is complete if we can pair off each left bracket '[' with a right bracket ']' that occurs later in the series. Every bracket must participate in exactly one such pair.Given a String text add the minimal
阅读全文
摘要:Description把M个同样的鸡蛋放在N个同样的篮子里,允许有的篮子空着不放,问共有多少种不同的放法?(用K表示)5,1,1和1,5,1 是同一种分法。Input第一行是测试数据的数目t(0 n时,拆解为2种情况,第一种是全部篮子都有鸡蛋(假设每个篮子都有1个),再把剩下的m-n个分到n个篮子中,第二种情况是没有把全部篮子用上 1 #include 2 using namespace std; 3 4 int find_num_of_ways(int num_of_eggs,int num_of_baskets); //传进鸡蛋的数量与篮子的数量返回放置鸡蛋方法的种数 5 6 int ..
阅读全文
摘要:题目:Description给定两个字符串S1和S2,要求判定S2是否能够被S1做循环移位得到的字符串包含。说明:字符串S1和S2中不包含空格,每个字符串至少包含一个字符。例如:给定S1=AABCD,S2=CDAA,则返回True;给定S1=ABCD,S2=ACBD,则返回False。Input第一行为整数N(0 2 using namespace std; 3 4 int main(){ 5 int n; 6 cin>>n; 7 while(n--){ 8 string s1,s2; 9 string::size_type posit...
阅读全文

浙公网安备 33010602011771号