自定义博客皮肤VIP专享

*博客头图:

格式为PNG、JPG,宽度*高度大于1920*100像素,不超过2MB,主视觉建议放在右侧,请参照线上博客头图

请上传大于1920*100像素的图片!

博客底图:

图片格式为PNG、JPG,不超过1MB,可上下左右平铺至整个背景

栏目图:

图片格式为PNG、JPG,图片宽度*高度为300*38像素,不超过0.5MB

主标题颜色:

RGB颜色,例如:#AFAFAF

Hover:

RGB颜色,例如:#AFAFAF

副标题颜色:

RGB颜色,例如:#AFAFAF

自定义博客皮肤

-+
  • 博客(64)
  • 收藏
  • 关注

原创 POJ 2139 Six Degrees of Cowvin Bacon

Floyd算法,求一个和他人平均距离最短的人的距离的均值(有点绕- -)。#include #include #include #include using namespace std;const int MAXN = 305;const int INF = 1 << 29;int n, m, a[MAXN], f[MAXN][MAXN];int main(){ w

2015-12-08 01:48:10 347

原创 POJ 2105 IP Address

大水题。分四段,二进制转十进制即可。#include #include #include using namespace std;char a[32];int main(){ int t; scanf("%d", &t); while (t--) { scanf("%s", a); for (int i = 0; i < 4; i++) { int an

2015-12-08 01:43:07 343

原创 POJ 2136 Vertical Histogram

大水题,输出有点麻烦。先保存起来再输出比较顺手。#include #include #include using namespace std;const int MAXN = 35;int main(){ char s[100]; int total[MAXN], word[MAXN][100]; memset(total, 0, sizeof(total)); for

2015-12-08 01:39:17 273

原创 POJ 3278 Catch That Cow

求从一个点到另一个点的最快方法,对于每个点下一步有三种选择:1. + 12. - 13. 当前坐标*2并且三种选择都花费1秒。刚开始就想到了是BFS求最短路径,但是没想到要标记。不标记已搜过的点坐标的话确实是会TLE的。另外还有一个,如果起点大于重点的话,也就是只有不断-1可以选择的话,可以直接输出两者的差值,但也可以直接DFS做,两者相差十几ms,并不是很大区别。#inc

2015-12-08 01:30:21 207

原创 POJ 1915 Knight Moves

就是简单的BFS,刚开始还在想要不要剪枝,后来试了一下不用剪枝就能过,可能因为不是马周游吧,虽然棋盘可以很大,但递归层数不是那么深。#include #include #include #include #include using namespace std;const int MAXN = 305;int vis[MAXN][MAXN], dist[MAXN][MAXN]

2015-12-08 01:18:35 260

原创 POJ 3620 Avoid The Lakes

DFS的应用, 求最大连通分量。刚开始在想要不要从每个格子开始深搜,并不断更新最大值。后来参考了大神的博客之后发现并不需要复原,直接变成0就好。#include #include #include #include using namespace std;const int MAXN = 105;int maze[MAXN][MAXN];int dir[4][2] = {-

2015-12-06 08:27:23 260

原创 POJ 2243 Knight Moves

BFS模板题。#include #include #include #include #include #include #include using namespace std;struct P{ int x, y, steps;};bool map[10][10];int fx[8][2] = {1, 2, -1, 2, 2, 1, -2, 1, 2, -1

2015-12-06 07:53:48 204

原创 POJ 1426 Find The Multiple

因为是special judge, 找出一组解即可。用DFS, 记得及时退出。同时unsigned long long足够表示最后的答案, 不需要使用高精度。#include #include using namespace std;int flag, n;int dfs(unsigned long long a, int step){ if (flag == 1 || ste

2015-12-06 07:31:40 222

原创 POJ 1953 World Cup Noise

求出长度为n的二进制数中,没有相邻的1的个数。刚开始写了前几项,发现是斐波那契数列,就直接ac了。后来回过头来想,确实如此。fib[n] = fib[n - 1](结尾为0)+ fib[n - 2](结尾为2)#include #include #include using namespace std;int a[50];void init(){ a[0] = 1; a

2015-12-01 23:29:25 229

原创 POJ 2184 Cow Exhibition

01背包问题的变种。不过需要注意的是有两个技巧1. 将坐标轴整体右横移,这样的话就不存在负权值了,原来的零点变为现在的区间中点。2. 正权值即为正常的01背包,从大大小,但当负权值时,需要从小到大。不知道该如何严格证明,但是自己找个例子实践一下的话会发现是对的,这样的话本轮计算用到的值恰好是上轮的结果。#include #include using namespace std;

2015-12-01 12:52:08 332

原创 POJ 3624 Charm Bracelet

01背包问题的模板题,基础应用。#include #include using namespace std;const int MAXN = 12890;int f[MAXN], w[3405], d[3405];inline int max(int a, int b){ return a > b ? a : b;}int main(){ int n, m;

2015-12-01 12:42:50 183

原创 POJ 3628 Bookshelf 2

求低于给定高度的离得最近的高度。01背包的变种。初始化时由0改为最大值,再将每次更新时从max改为Min即可。#include #include #include using namespace std;const int INF = 0x3f3f3f3f;const int MAXN = 25;int n, B, s;int v[MAXN], dp[MAXN * 10

2015-12-01 12:39:34 212

原创 POJ 1928 The Peanuts

贪心法即可。先将花生最多的格点排序,然后以此判断能否到达并返回。#include #include #include #include using namespace std;const int MAXN = 55;struct Node{ int x, y, val; bool operator <(const Node& a) const { return val

2015-12-01 12:34:16 279

原创 POJ 1338 Ugly Numbers

简单的模拟题。初始化时每次挑出2 3 5的倍数中较小的记录即可。以后有时间试试STL中的优先队列。#include using namespace std;const int MAX_POS = 1500;int myMin(int a, int b, int c) { int min; min = a < b ? a : b; min = min < c ?

2015-12-01 12:29:58 175

原创 POJ 3256 Cow Picnic

一道DFS的题目。刚开始纠结了很久怎么判断牧场满不满足条件,试着去枚举所有两头牛的组合。后来发现这样的复杂度太高,反过来思考问题的话顿时简单了很多。如果一个牧场有K头牛能到达的话即满足了题意。这样O(n)时间判断牧场就可以了。另外因为是稀松图,采用邻接表储存的话会比较简单。DFS里面的下标容易搞混...需要注意#include #include #include #include

2015-12-01 12:27:09 184

原创 POJ 1664 放苹果

很有趣的递推题目。通过递推不断缩小题目规模。base case: 没有苹果或者只剩一个碟子,此时只有一种方法。如果碟子比苹果多的话,那么把多出来的碟子去掉结果是一样的(它们只能空着)否则的话,分为所有碟子都有苹果(此时将苹果总数减去碟子数,即每一个碟子都有一个苹果) + 有一个碟子没有苹果(碟子数减一)#include #include using namespace std

2015-12-01 12:17:02 232

原创 POJ 1083 Moving Tables

很有意思的一道题目。刚开始想的是用暴力枚举,复杂度有点高,但是应该也能过。后来仔细分析了一下,两个方案冲突就是说明两个方案中间有公共的过道被使用过了,也就是有过道被使用了两次。因此,统计所有过道被使用的总次数,找出最大值后乘以十即是最大值。#include #include #include #include using namespace std;int main(){ int

2015-11-10 01:56:08 385

原创 POJ 1050 To the Max

枚举 + dp, 化二维为一维, 第一次将2-n行分别加到第一行,第二次分别将3-n行加到第二行,再分别对单行求区间最大值。#include #include #include using namespace std;const int MAXN = 105;int n, temp[MAXN], a[MAXN][MAXN];inline int arrayMax(int *ar

2015-11-02 22:03:10 191

原创 POJ 1159 Palindrome

L(longest)C(common)S(sequence)经典问题,要用滚动数组,不然会爆内存。#include #include #include #include using namespace std;int dp[2][5005];int main() { string s1, s2; int n; while (cin >> n) {

2015-10-20 01:21:23 2601

原创 POJ 3320 Jessica's Reading Problem

白皮书尺取法例题,基本和之前一道题目思路相同。刚开始为了图省事,为中间枚举的数组也开了一个set,但是后来发现会wa,换成hash(map)之后就很好过了。另外还有一点疑惑,判断跳出条件时如果写成(t >= p)会wa,希望大神指点。#include #include #include #include #include using namespace std;int a[1000

2015-10-16 02:53:47 210

原创 POJ 2431 Expedition

贪心,及优先队列或堆的使用。当路过一个城市或加油站时,不一定需要立刻加油,而可以将其保存在一个优先队列中。当没有油时,再取出优先队列的头加入等量的油,加油的先后是等价的。以加油站为分段点考虑,并将距离转化为距起点的距离。距离需要排序,输入并不保证有序,否则会wrong answer。#include #include #include #include using namespace

2015-10-14 06:13:52 202

原创 POJ 3258 River Hopscotch

白皮书上的课后习题,二分搜索的应用。第一次卡在不知道如何判断某答案是否符合。后来发现除了常规思路考虑如何去掉其中若干个之外,也可以反向思考,如何放置剩余的石头。另,二分搜索有时候边界条件容易出错,推荐写成左闭右闭区间。/*POJ 3258 River HopscotchMain thoughts: use binary search to maximize the minimumDiffi

2015-10-14 01:30:42 288

原创 POJ 3083 Children of the Candy Corn

DFS和BFS的综合应用。分别输出一个迷宫由起点开始,左手优先路径长度,右手优先路径长度,以及最短路径长度。其中,前两个采用dfs,而最后一个采用bfs。注意下标w, h不要搞混,好容易wa...另外,用(i + 3) % 4 和 (i + 1) % 4表示方向,也是参考了别人的代码才理解的,画个图,试几个例子就很好理解了。#include #include #include #includ

2015-10-14 01:21:06 298

原创 Sicily 4423 Calculate the Sum

暴力枚举异或操作是会TLE的。按二进制每位的权重拆开,每位上用1出现的数量乘以0出现的数量,再乘以相对应的权, 这其实也就是异或操作中得到1的结果,将它们累加求和即可,结果比较大,用long long储存。// Problem#: 4423// Submission#: 3751511// The source code is licensed under Creative Commons

2015-06-04 14:54:55 305

原创 Sicily 1686 Happy Children's Day

#include #include #include using namespace std;const int MAXN = 100010;// declaration of the segment treestruct STNode{ int l, r, maxv, maxi; // left bound, right bound, max value, ma

2015-06-04 11:25:18 336

原创 Sicily 1920 Divide The Stones

每次最少分出一个,则最后每个堆的 石头数量都是1, 共有sum(a[i])堆。而分出这些石头共需要sum(a[i] - 1)次操作。对这个操作数进行奇偶判断,若为奇数则Alice胜,否则Bob必胜。#include #include #include #include using namespace std;int main(){ int t, n; scanf("%d",

2015-06-01 08:58:01 336

原创 Sicily 13981 Cow Baseball

二分查找即可,偷懒直接用了库函数#include #include #include using namespace std;int num[1005];int main(){ int n, sum = 0, i = 0; scanf("%d", &n); while (i != n) cin >> num[i++]; sor

2015-06-01 00:06:16 445

原创 Sicily 14513 Aaah!

水得毫无人性#include #include #include using namespace std;int main(){ string a, b; while (cin >> a >> b) { if (a.length() >= b.length()) printf("go\n"); else printf("no\n"); }}

2015-05-31 17:09:13 316

原创 POJ 2051 Argus

题目的意思是,给定任务的ID和执行间隔,每次按照执行的时间顺序输出前k个任务的ID号,当时间相同时,按照字典序输出ID小的。解题思路建立一个最小堆,每次输出最小堆之后将根节点加上周期重新调整堆。#include #include #include using namespace std;struct Node{ int Now; // 出堆时间 int Q; // I

2015-05-21 20:59:29 238

原创 POJ 2785 4 Values whose Sum is 0

双向搜索,分组计算ab,与cd,然后二分查找即可。#include#include#includeusing namespace std;int a[4005], b[4005], c[4005], d[4005];int cd[4005*4005];int main() { int n; while (scanf("%d", &n) != EOF) { for (int

2015-02-04 21:56:57 204

原创 POJ 3617 Best Cow Line

有点技巧的贪心#include#include#includeusing namespace std;int main() { int n; char a[2005]; string str; cin >> n; for (int i = 0; i < n; i++) { cin >> a[i]; } int i

2015-02-01 22:33:53 236

原创 POJ 3069 Saruman's Army

贪心#include#include#includeusing namespace std;int a[10005];int main() { int r, n; while (cin >> r >> n && r + n != -2) { for (int i = 0; i < n; i++) { cin >> a[i];

2015-02-01 22:30:06 276

原创 POJ 3253 Fence Repair

手动用数组模拟一个哈夫曼树即可#include#include#includeusing namespace std;int a[20005];int main() { int n; long long ans = 0; scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%d", &a[i]); while (

2015-02-01 22:27:47 238

原创 POJ 2661 Factstone Benchmark

#include#include#includeusing namespace std;int main() { int y, i; double w; while (scanf("%d", &y) && y) { w = log(4 * 1.0); for (int j = 1960; j <= y; j += 10) { w *= 2; } i = 1;

2015-02-01 22:24:02 337 1

原创 Sicily 1373 Cows Of The Round Ta

看了半天题目,好像除了暴力枚举没什么别的方法...而且题目3s,这是在暗示枚举啊...// Problem#: 1373// Submission#: 3311134// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License/

2014-12-03 23:34:22 303

原创 Sicily 1570 Hopeless Coach

给定接下来的轮次以及最低获得的积分,否则教练就会下课。刚开始推了很久的公式,觉得加起来项数好多,好烦。后来突然想到直接就是一个dp问题,按照上次的积分扫就可以,最后输出N轮时得分大于P分的总和概率即可// Problem#: 1570// Submission#: 3307174// The source code is licensed under Creative Commons At

2014-12-03 13:44:48 343

原创 Sicily 1543 Completing Brackets

简单应用栈的模拟。先将字符串内已匹配的括号pop掉,剩下的依次储存就好。#include#include#includeusing namespace std;int main() { string str; while (cin >> str) { stack stk1, stk2, stk3; // stk1存'[', stk2临时中转, stk3存']' for

2014-12-02 22:50:18 314

原创 Sicily 4428 How Many Sets

动态规划,状态转移方程dp[i][j] = dp[i - 1][j] + dp[i - 2][j - 1]其中i表示总数,j表示取出的数个数。初始化时要注意顺序问题,设取出个数为k,则所有2k-1的项都应当为0,但特殊情况是取出一个时有几个数就输出几个。另外一个特殊情况就是0,直接输出空集1。// Problem#: 4428// Submission#: 3295621// The so

2014-11-30 15:35:11 616

原创 Sicily 1021 Couples

直接用栈模拟即可// Problem#: 1021// Submission#: 2839209// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License// URI: http://creativecommons.org/l

2014-11-30 09:43:20 320

原创 Sicily 1935 二叉树重建

题目要求说的很清楚,给出一个数的先序和中序遍历结果,构建树并按照广度优先输出。先序遍历的第一个数即为根节点,在中序遍历中找到之后它左面的都是左子孙结点右边同理。// Problem#: 1935// Submission#: 3291146// The source code is licensed under Creative Commons Attribution-NonCommerc

2014-11-29 19:36:28 390

空空如也

空空如也

TA创建的收藏夹 TA关注的收藏夹

TA关注的人

提示
确定要删除当前文章?
取消 删除