算法
文章平均质量分 60
ACLJW
这个作者很懒,什么都没留下…
展开
-
宠物小精灵之收服(二维背包)
005:宠物小精灵之收服总时间限制: 1000ms 内存限制: 65536kB描述宠物小精灵是一部讲述小智和他的搭档皮卡丘一起冒险的故事。一天,小智和皮卡丘来到了小精灵狩猎场,里面有很多珍贵的野生宠物小精灵。小智也想收服其中的一些小精灵。然而,野生的小精灵并不那么容易被收服。对于每一个野生小精灵而言,小智可能需要使用很多个精灵球才能收服它,而在收服过程中,野生小精灵也会对皮卡丘造成一定的伤害(从而...原创 2017-09-24 21:52:57 · 341 阅读 · 0 评论 -
1600:Patrol Robot
Patrol Robot这个题还是DFS,不过每个状态除了记录位置之外,还要记录剩余穿越次数,这样一来就变成了三维DFS。对于值为1的点如果剩余穿越次数大于0且该状态未遍历过的话也可以访问。像这种四个方向的话用常量数组比较方便,没必要搞个二重循环,容易出bug。#include<bits/stdc++.h>using namespace std;const int max...原创 2018-08-10 00:56:31 · 133 阅读 · 0 评论 -
10562:Undraw the Trees
Undraw the Trees这题没啥说的,利用DFS进行先序遍历即可,注意 node 可以不是字母,然后 puts() 默认换行的,太久没用都忘了。。#include<bits/stdc++.h>using namespace std;const int maxn = 200 + 5;int T;char G[maxn][maxn];bool isnode(ch...原创 2018-08-06 23:37:25 · 106 阅读 · 0 评论 -
806:Spatial Structures
Spatial Structures#include<bits/stdc++.h>using namespace std;const int maxn = 64 + 5;int n,len;int seq[maxn*maxn];char img[maxn][maxn];struct node{ char c = 0; int cnt = 4; ...原创 2018-08-11 23:25:48 · 148 阅读 · 0 评论 -
12171:Sculpture
Sculpture思路:将三维空间网格化,每个长方体占据的所有单元标记为1。求面积的话,DFS所有的单元,依次检查是上下左右前后六个方向上相邻单元是否为1,若否则是表面,面积加+1。求体积的话,从外面某个单元开始DFS,求出外面值为0的单元的个数,那么总单元个数 - 外部值为0的单元个数 = 雕塑体积。但是由于外部单元个数巨大会导致堆栈溢出,所以需要对坐标进行离散化,另外应选BFS。原来v...原创 2018-08-07 23:54:36 · 210 阅读 · 0 评论 -
524:Prime Ring Problem
Prime Ring Problem#include<bits/stdc++.h>using namespace std;const int maxn = 17;int n;int A[maxn],vis[maxn],p[2*maxn];int isPrime(int n){ for(int i = 2;i <= int(sqrt(n));i++){ ...原创 2018-08-16 00:12:41 · 113 阅读 · 0 评论 -
536:Tree Recovery
Tree Recovery#include<bits/stdc++.h>using namespace std;const int maxn = 30;struct node{ char c; struct node* l = NULL; struct node* r = NULL;};char p[maxn],m[maxn];node* bu...原创 2018-08-08 23:52:49 · 97 阅读 · 0 评论 -
140:Bandwidth
Bandwidth注意节点不一定是按字母表顺序从前到后用的,也有可能是A C 却没用B,因为这个TLE了好几次。。#include<bits/stdc++.h>using namespace std;const int maxn = 26;int L,w;char s[100];int seq[maxn],vis[maxn],res[maxn],num[maxn],...原创 2018-08-18 22:47:48 · 134 阅读 · 0 评论 -
129:Krypton Factor
Krypton Factor注意 && 别写成 & 了。。。#include<bits/stdc++.h>using namespace std;const int maxn = 80 + 5;int n,L,cnt;int A[maxn];int dfs(int cur){ if(cnt++ == n){ cnt =...原创 2018-08-17 00:38:42 · 121 阅读 · 0 评论 -
15. 3Sum
15. 3Sumclass Solution {public: vector<vector<int>> threeSum(vector<int>& nums) { sort(nums.begin(),nums.end()); vector<vector<int>> res; ...原创 2018-09-08 23:57:19 · 87 阅读 · 0 评论 -
1354:Mobile Computing
Mobile Computing枚举二叉树然后计算其宽度即可,每次枚举两个节点构造一个父节点,计算宽度时需要注意的是每棵树的左节点的右边缘可能超过其右子树的左边缘,反之亦然。#include<bits/stdc++.h>using namespace std;const int maxn = 12;int n,s,idx,w[maxn],vis[maxn];doubl...原创 2018-10-26 17:37:09 · 121 阅读 · 0 评论 -
1073C. Vasya and Robot
1073C. Vasya and Robot注意如果 d 和 n 奇偶性不一致则是不可能到达的,因为机器人每移动一步,其坐标之和的奇偶性就会发生变化。#include<bits/stdc++.h>using namespace std;typedef pair<int,int>P;const int maxn = 200000 + 5;int n,x,...原创 2018-10-31 22:39:41 · 150 阅读 · 0 评论 -
1073D. Berland Fair
1073D. Berland Fair每次先走一圈,计算出可以购买的糖果的总价格 c 和总个数 t,然后 cnt += t*(T/c) T %= c,循环至无糖果可买。时间复杂度计算如下:#include<bits/stdc++.h>using namespace std;const int maxn = 200000 + 5;typedef long lon...原创 2018-10-31 23:54:20 · 118 阅读 · 0 评论 -
1601:The Morning after Halloween(经典)
The Morning after Halloween直接BFS会超时,题目中提示过墙壁很多,那么可以将所有的空格提取出来做张图,然后记录每个空格周围的邻居,这样就不用每次都判断能不能走了。优化的话可以使用双向BFS,从每次正向(从开始位置搜索)搜索一次,反向(从目标位置搜索)搜索一次,如果有相同状态则找到最短路径。BFS(800ms):#include<bits/stdc++...原创 2018-12-05 22:26:49 · 354 阅读 · 0 评论 -
10603:Fill
Fill#include<bits/stdc++.h>using namespace std;const int maxn = 200 + 5;int T, a, b, c, d;struct node{ int v[3], dist; bool operator < (const node& rhs) const{ retu...原创 2018-12-03 23:32:32 · 154 阅读 · 0 评论 -
225:Golygons
Golygons回溯加剪枝。某个方向可走的条件是此方向上没有障碍物挡道,还有一点容易被忽视的就是不能落到之前到过的点上!!!最重要的剪枝就是每走一步就判断一下剩下的步数是否有可能回到原点,还有就是如果提前到达原点也要剪掉。#include<bits/stdc++.h>using namespace std;const int maxn = 50 + 5;int T, n...原创 2018-12-13 23:34:23 · 110 阅读 · 0 评论 -
211:The Domino Effect
The Domino Effect回溯加剪枝。只需DFS右、下两个方向即可,可以一行一行来。弄完一行之后看下该行所有数字是否都配对了,否则剪枝。还是得细心,两个 cnt 认错了找了大半天 bug 。。。#include<bits/stdc++.h>using namespace std;const int n = 7;int cnt;int dx[] = {1, 0}...原创 2018-12-16 23:41:35 · 258 阅读 · 0 评论 -
208:Firetruck
Firetruck回溯即可,不过要注意先判断是否可以到达着火点,如果到不了那就没必要回溯了,可能给的数据比较坑,到不了的数据比较多,不判断的话会超时。还有就是输出的格式和样例给的竟然不一样,真是醉了。#include<bits/stdc++.h>using namespace std;const int maxn = 25;int t, n, cnt2;int vis...原创 2018-12-11 17:09:21 · 150 阅读 · 0 评论 -
439:Knight Moves
Knight MovesBFS即可,字符串数组 size 定义成了 2 导致输入一直错误,应该是无法存入'\0' 引起的,待会儿再深究。#include<bits/stdc++.h>using namespace std;const int maxn = 8;typedef pair<int,int> P;int d[maxn][maxn];char a...原创 2018-08-09 23:10:24 · 93 阅读 · 0 评论 -
712:S-Trees
S-Trees#include<bits/stdc++.h>using namespace std;const int maxn = 7;int n,m,t,cnt = 0;char s[2];int a[maxn];char b[maxn];char leaf[int(pow(2,maxn))];char simu(){ int i = 0,j = t...原创 2018-08-09 22:11:05 · 141 阅读 · 0 评论 -
816:Abbott's Revenge
Abbott's Revenge一些细节要特别注意#include<bits/stdc++.h>using namespace std;const int maxn = 10;const char* dirs = "NESW";const char* turns = "FLR";struct Node{ int r,c,dir; Node(int a...原创 2018-07-31 23:39:16 · 167 阅读 · 0 评论 -
抽签问题
试题描述你的朋友提议玩一个游戏:将写有数字的n 个纸片放入口袋中,你可以从口袋中抽取4 次纸片,每次记下纸片上的数字后都将其放回口袋中。如果这4 个数字的和是m,就是你赢,否则就是你的朋友赢。你挑战了好几回,结果一次也没赢过,于是怒而撕破口袋,取出所有纸片,检查自己是否真的有赢的可能性。请你编写一个程序,判断当纸片上所写的数字是k1,k2, …, kn 时,是否存在抽取4 次和为m 的方案。输入第...原创 2018-03-14 22:35:24 · 379 阅读 · 0 评论 -
部分和问题
部分和问题时间限制:1000 ms | 内存限制:65535 KB难度:2描述给定整数a1、a2、.......an,判断是否可以从中选出若干数,使它们的和恰好为K。输入首先,n和k,n表示数的个数,k表示数的和。接着一行n个数。(1<=n<=20,保证不超int范围)输出如果和恰好可以为k,输出“YES”,并按输入顺序依次输出是由哪几个数的和组成,否则“NO”样例输入4 131...原创 2018-03-14 23:17:30 · 131 阅读 · 0 评论 -
2386:Lake Counting
总时间限制: 1000ms内存限制: 65536kB描述Due to recent rains, water has pooled in various places in Farmer John's field, which is represented by a rectangle of N x M (1 <= N <= 100; 1 <= M <= 100) squa...原创 2018-03-15 00:46:15 · 146 阅读 · 0 评论 -
09-排序1 排序
09-排序1 排序(25 分)给定N个(长整型范围内的)整数,要求输出从小到大排序后的结果。本题旨在测试各种不同的排序算法在各种数据情况下的表现。各组测试数据特点如下:数据1:只有1个元素;数据2:11个不相同的整数,测试基本正确性;数据3:103个随机整数;数据4:104个随机整数;数据5:105个随机整数;原创 2018-03-02 14:33:27 · 179 阅读 · 0 评论 -
1. Two Sum
Given an array of integers, return indices of the two numbers such that they add up to a specific target.You may assume that each input would have exactly one solution, and you may not use the same el...原创 2018-03-10 08:13:49 · 86 阅读 · 0 评论 -
最佳加法表达式
011:最佳加法表达式总时间限制: 1000ms 内存限制: 65536kB描述给定n个1到9的数字,要求在数字之间摆放m个加号(加号两边必须有数字),使得所得到的加法表达式的值最小,并输出该值。例如,在1234中摆放1个加号,最好的摆法就是12+34,和为36输入有不超过15组数据每组数据两行。第一行是整数m,表示有m个加号要放( 0第原创 2017-09-23 22:55:22 · 468 阅读 · 0 评论 -
大盗阿福
001:大盗阿福总时间限制: 1000ms 内存限制: 65536kB描述阿福是一名经验丰富的大盗。趁着月黑风高,阿福打算今晚洗劫一条街上的店铺。这条街上一共有 N 家店铺,每家店中都有一些现金。阿福事先调查得知,只有当他同时洗劫了两家相邻的店铺时,街上的报警系统才会启动,然后警察就会蜂拥而至。作为一向谨慎作案的大盗,阿福不愿意冒着被警察原创 2017-09-24 14:41:37 · 279 阅读 · 0 评论 -
开餐馆
004:开餐馆总时间限制: 1000ms 内存限制: 65536kB 描述 北大信息学院的同学小明毕业之后打算创业开餐馆.现在共有n 个地点可供选择。小明打算从中选择合适的位置开设一些餐馆。这 n 个地点排列在同一条直线上。我们用一个整数序列m1, m2, … mn 来表示他们的相对位置。由于地段关系,开餐馆的利润会有所不同。我们用pi 表示在mi 处开餐馆的利润。为了避免自己的餐馆的内部竞争,原创 2017-09-24 17:44:18 · 313 阅读 · 0 评论 -
4. Median of Two Sorted Arrays
There are two sorted arrays nums1 and nums2 of size m and n respectively.Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).Example 1:nums1 = [1, 3]nums2...原创 2018-03-24 18:13:02 · 79 阅读 · 0 评论 -
5. Longest Palindromic Substring
Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000.Example:Input: "babad"Output: "bab"Note: "aba" is also a valid answer. Example:I...原创 2018-03-25 12:36:18 · 108 阅读 · 0 评论 -
1742:Coins
CoinsTime Limit: 3000MS Memory Limit: 30000KTotal Submissions: 40853 Accepted: 13835DescriptionPeople in Silverland use coins.They have coins of value A1,A2,A3...An Silverland dollar.One day Tony open...原创 2018-03-25 23:49:56 · 189 阅读 · 0 评论 -
1103:Ancient Messages
Ancient Messages数一数就能发现,题目表中的6个符号从左到右依次有1,3,5,4,0,2个洞,各不相同。这样,只需要数一数输入的符号有几个“白洞”,就能准确地知道它是哪个符号了。至于具体实现,我的思路是:先确定一个符号,找到该符号的空洞个数然后将该符号“擦除”,继续确定下一个符号。具体而言,每次找到一个黑点,然后DFS,如果其周围点为黑点则递归,若为白点且为封闭块则个数加1,最后再次...原创 2018-06-02 23:52:41 · 347 阅读 · 0 评论 -
839:Not so Mobile
Not so Mobile 我的思路:可以将天平看做一棵二叉树,二叉树的每个节点要记录其父节点。然后其实就是一个建树的过程,遇到 0 节点就向下延伸,每当一个节点的左右子树确定(其重量也随之确定),就计算其是否平衡,然后一直向上追溯,直到该节点尚未平衡,继续建树,直至整棵树建好,是否平衡也随之确定。version 1(40ms):#include<bits/stdc++.h>usin...原创 2018-05-31 00:29:22 · 112 阅读 · 0 评论 -
669:The Falling Leaves
The Falling Leaves这道题还是递归,仿照上道题的思路挺容易的,不过看了看书上的代码个人觉得我的代码更简洁,思路更清晰些哈哈。我的思路:开个负下标数组,记录每个位置的权重之和,用 p 记录当前节点位置,那么其左节点位置为 p - 1,右节点位置为 p + 1,依次递归增加相应位置的权重,同时更新最右和最左边位置。#include<bits/stdc++.h>using ...原创 2018-05-31 13:25:43 · 138 阅读 · 0 评论 -
297:Quadtrees
Quadtrees建树递归,合并也递归。在合并的时候要注意只有两个没有子节点的节点之间才可以合并,可能会遇到三种情况:1. 两个都是最小单元(相对本身),直接合并;2. 其中一个有子节点,依次其将其子节点与另一个节点合并;3. 两个都有子节点,依次将其子节点按顺序进行合并(两者此时子节点面积必定相同,思考一下即可)。其中unit 为最小单元面积,每次划分都变为其本身的1/4,上述过程递归进行即可。...原创 2018-05-31 23:22:02 · 142 阅读 · 0 评论 -
572:Oil Deposits
Oil Deposits#include<bits/stdc++.h>using namespace std;const int maxn = 100 + 5;int m,n,cnt;char graph[maxn][maxn];void dfs(int x,int y){ graph[x][y] = '*'; for(int dx = -1;dx <=...原创 2018-06-01 12:58:50 · 121 阅读 · 0 评论 -
10305:Ordering Tasks
Ordering Tasks这题比较简单,就是拓扑排序,而且肯定是有向无环图,直接DFS即可。注意数据读取,只要 n 和 m 有一个不为0即可。。。不考虑是否存在环:#include<bits/stdc++.h>using namespace std;const int maxn = 100 + 10;int m,n,t;int visited[maxn];...原创 2018-08-05 00:29:29 · 173 阅读 · 0 评论 -
12113:Overlapping Squares
Overlapping Squares我的思路:可以根据所给图形计算出图形中包含的方块的个数以及它们各自的位置,方块可以根据某一个角点的位置来确定,具体方法见 count() 函数。方块确定之后,图形的各种变化就取决于方块们的不同放置顺序了,枚举所有排列,进行模拟,看看模拟结果里面有没有所给图形即可。思路没什么问题,代码了检查了好多遍,但是一直WA,暂时还没发现问题,再想想吧。#in...原创 2018-12-29 15:48:37 · 227 阅读 · 0 评论