自定义博客皮肤VIP专享

*博客头图:

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

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

博客底图:

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

栏目图:

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

主标题颜色:

RGB颜色,例如:#AFAFAF

Hover:

RGB颜色,例如:#AFAFAF

副标题颜色:

RGB颜色,例如:#AFAFAF

自定义博客皮肤

-+
  • 博客(88)
  • 问答 (1)
  • 收藏
  • 关注

原创 大数——大数加法

在算法比赛中,经常会遇到一些数据很大的数,如果需要计算,就得涉及大数运算的相关算法。 虽然用JAVA的大数类实现十分简单方便,但是对于C++的实现我觉得也是有必要去研究一下的,这样才能够对数学与计算机的结合有一个更深的认识。首先整理一下大数加法大数加法问题描述: 给两个数a和b,计算a+b的值,注意这里的a和b限制为超过整型大小,所以我们不能使用平时的“+”运算,只得另寻他法。分析:使用字符数组

2017-04-29 21:38:16 380

原创 背包问题——多重背包

问题描述有N种物品和一个容量为W的背包。第i种物品最多有n[i]件可用,每件重量是w[i],价值是v[i]。求解将哪些物品装入背包可使这些物品的重量总和不超过背包容量,且价值总和最大。分析:两个最基本的算法:1.使用三重循环进行递推: 状态转移式为:dp[i][j]=max{dp[i-1][j-k*w[i]]+k*v[i]|0<=k<=n[i]} 关键代码如下:void solve(){

2017-04-28 21:39:51 329

原创 背包问题——完全背包

问题描述 有N种物品和一个容量为W的背包,每种物品都有无限件可用。第i种物品的重量是w[i],价值是v[i]。求解将哪些物品装入背包可使这些物品的费用总和不超过背包容量,且价值总和最大。分析和01背包不同的是,物品是无限件,从选择物品的角度看,它不再像01背包那样,有两种选择:选or不选, 它可以选0件、1件、2件……等等,所以我们按照01背包的状态转移思想我们便可以有这样的状态转移方程: dp

2017-04-28 16:59:13 329

原创 priority_queue 结构体类型

记录一下,以后不能忘了!!如果priority_queue插入结构体类型的变量,需要重载<运算符举例:#include<cstdio>#include<queue>using namespace std;struct Node{ int a, b;}node[100];bool operator <(const Node &x, const Node &y){ return

2017-04-28 15:33:28 904

原创 POJ - 1328 Radar Installation(贪心/最大不相交覆盖)

问题描述 Assume the coasting is an infinite straight line. Land is in one side of coasting, sea in the other. Each small island is a point locating in the sea side. And any radar installation, locating on

2017-04-28 13:16:33 558

原创 贪心——区间覆盖问题之最大不相交覆盖问题

最大不相交覆盖问题:问题描述: 给定一个长度为m的区间,再给出n条区间的起点和终点,问题是从中选取尽量多的区间,使得每个区间都是独立的,即不和其它任何区间相交。方法先将n个区间按照右端点进行递增排序。令e为覆盖的右端点,初始化e为-1,然后对之后的区间进行判断选择,如果剩下区间的左端点大于e,那么就把此区间添加进去。经典问题:会场安排问题 问题描述 学校的小礼堂每天都会有许多活动

2017-04-28 10:48:43 2496

原创 贪心——区间覆盖问题之区间完全覆盖最小区间数

问题:区间完全覆盖问题问题描述:给定一个长度为m的区间,再给出n个区间的起点和终点,求最少使用多少个区间可以将整个区间完全覆盖。方法:先将n个区间按照起点进行递增排序。令s表示已经覆盖到的区域。再剩下的区间中找出所有左端点小于等于当前已经覆盖到的区域的s并且右端点大于等于s的区间,取右端点最大的区间加入,直到已经覆盖全部的区域。举例:m为10 N = 7:[1,5]、[1,6]、[3,6]、

2017-04-28 10:14:52 12996 4

原创 POJ - 2376 Cleaning Shifts(区间覆盖/贪心)

问题描述 Farmer John is assigning some of his N (1 <= N <= 25,000) cows to do some cleaning chores around the barn. He always wants to have one cow working on cleaning things up and has divided the day in

2017-04-27 21:27:31 287

原创 背包问题——01背包

终于开启了DP的新篇章了——背包问题。很鸡冻啊!! 参考《背包九讲》进行自我整理,真的很难理解呀QAQ!01背包问题描述:有N件物品和一个容量为V的背包。第i件物品的重量是c[i],价值是w[i]。求解将哪些物品装入背包可使价值总和最大。分析:01背包中的“01”就是一种物品只有1件,你可以选择放进去背包即1,也可以选择不放入背包中即0。构造一个最优解的结构特征: 定义:dp[i][j]为前i

2017-04-26 23:59:45 258

原创 POJ - 3685 Matrix(二分/查找第k大值)

问题描述Given a N × N matrix A, whose element in the i-th row and j-th column Aij is an number that equals i^2 + 100000 × i + j^2 - 100000 × j + i × j, you are to find the M-th smallest element in the matr

2017-04-26 23:24:38 401

原创 网络流——最大流/Ford-Fulkerson算法

图论中一个关键的算法——最大流,很多实际问题都是这个思想,比如最大传输量、最大管道流量等等。根据《数据结构与算法分析》进行整理。最大流问题设给定有向图G=(V , E),其边容量为Cv,w。这些容量可以代表通过一个管道的水的流量。图中有两个顶点:一个是s(称为发点or源点),一个是t(称为收点or汇点)。对于任意一条边e∈E,最多有“流”C(e)个单位可以通过。在不是s和t的任一顶点v,总的进入的流

2017-04-26 21:19:21 1549

原创 POJ - 3579 Median(二分/查找第K大的值)

问题描述 Given N numbers, X1, X2, … , XN, let us calculate the difference of every pair of numbers: ∣Xi - Xj∣ (1 ≤ i < j ≤ N). We can get C(N,2) differences through this work, and now your task is to

2017-04-26 21:05:19 411

原创 STL函数 lower_bound / upper_bound

lower_bound 这个函数是从已经排好序的序列a中利用二分搜索找出指向满足ai >= k的ai的最小的指针,注意是指针。upper_bound函数类似,就是求出满足ai > k的ai的最小的指针。#include<cstdio>#include<algorithm>using namespace std;int a[100];int main(){ printf("输入序列个

2017-04-26 14:18:52 251

原创 POJ - 3253 Fence Repair(贪心)

问题描述 Farmer John wants to repair a small length of the fence around the pasture. He measures the fence and finds that he needs N (1 ≤ N ≤ 20,000) planks of wood, each having some integer length Li (1

2017-04-26 00:36:07 295

原创 POJ - 3069 Saruman's Army(贪心)

问题描述Saruman the White must lead his army along a straight path from Isengard to Helm’s Deep. To keep track of his forces, Saruman distributes seeing stones, known as palantirs, among the troops. Each p

2017-04-26 00:32:24 268

原创 POJ - 1018 Communication System(贪心)

问题描述We have received an order from Pizoor Communications Inc. for a special communication system. The system consists of several devices. For each device, we are free to choose from several manufacture

2017-04-25 23:00:19 366

原创 POJ - 3617 Best Cow Line(贪心)

《挑战程序设计》里的一题。 问题描述FJ is about to take his N (1 ≤ N ≤ 2,000) cows to the annual”Farmer of the Year” competition. In this contest every farmer arranges his cows in a line and herds them past the judges.

2017-04-24 23:55:49 753

原创 POJ - 1164 The Castle(图论/DFS 迷宫问题)

问题描述 1 2 3 4 5 6 7 ############################# 1 # | # | # | | # #####---#####---#---#####---# 2 # # | # # # # # #---#####---#####---#####---# 3 #

2017-04-24 20:42:15 723

原创 POJ - 3669 Meteor Shower(图论/BFS无权最短路)

问题描述 Bessie hears that an extraordinary meteor shower is coming; reports say that these meteors will crash into earth and destroy anything they hit. Anxious for her safety, she vows to find her way to

2017-04-24 20:32:11 303

原创 POJ - 2395 Out of Hay (图论/最小生成树)

问题描述 The cows have run out of hay, a horrible event that must be remedied immediately. Bessie intends to visit the other farms to survey their hay situation. There are N (2 <= N <= 2,000) farms (numbe

2017-04-24 20:25:20 364

原创 POJ - 2377 Bad Cowtractors(图论/最大生成树)

问题描述Bessie has been hired to build a cheap internet network among Farmer John’s N (2 <= N <= 1,000) barns that are conveniently numbered 1..N. FJ has already done some surveying, and found M (1 <= M <=

2017-04-24 20:17:32 292

原创 POJ - 2139 Six Degrees of Cowvin Bacon(图论/无权最短路径BFS)

问题描述 The cows have been making movies lately, so they are ready to play a variant of the famous game “Six Degrees of Kevin Bacon”. The game works like this: each cow is considered to be zero degrees o

2017-04-24 20:13:23 364

原创 POJ - 3268 Silver Cow Party(图论/dijkstra最短路)

问题描述 One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to attend the big cow party to be held at farm #X (1 ≤ X ≤ N). A total of M (1 ≤ M ≤ 100,000) unidirectional (one-w

2017-04-24 20:09:29 297

原创 POJ - 3259 Wormholes (图论/floyd判断负圈)

问题描述 While exploring his many farms, Farmer John has discovered a number of amazing wormholes. A wormhole is very peculiar because it is a one-way path that delivers you to its destination at a time t

2017-04-24 20:06:11 848

原创 POJ - 3280 Cheapest Palindrome (DP)

DP虐我千百遍,我待DP如初恋题目描述: Keeping track of all the cows can be a tricky task so Farmer John has installed a system to automate it. He has installed on each cow an electronic ID tag that the system will rea

2017-04-24 19:59:56 307

原创 POJ - 3616 Milking time (DP)

Bessie is such a hard-working cow. In fact, she is so focused on maximizing her productivity that she decides to schedule her next N (1 ≤ N ≤ 1,000,000) hours (conveniently labeled 0..N-1) so that she

2017-04-24 19:55:11 351

原创 素数的判断

素数素数是啥?知道了这个就能够知道如何判断一个数是否为素数了。这篇博文里详细说明了素数是啥 http://blog.csdn.net/karry_zzj/article/details/70147474素数: 如果一个整数a>1且只能被平凡约数1和它自身所整除,则这个数是素数。 也就是说素数是恰好有2个约数的整数,即1和它本身。素数的判断因为n的约数都不超过n,所以只要检查2~n-1的所有整数

2017-04-23 10:16:34 288

原创 递推——直线分割平面

参考这位大大的文章:写的很好! http://blog.sina.com.cn/s/blog_76eabc150100swg8.html关键就是如果想要分割最多的平面,就需要直线与之前所有直线相交,形成N个新交点,则就会产生N+1个新平面。

2017-04-22 11:43:28 426

原创 C++ STL之vector复制疑惑

昨天做了一题涉及到vector的复制,比如就是vector v1, v2两个向量,把v2复制给v1,我知道有swap函数,和assign函数都可以实现,但是就是实现不了,很多人觉得怎么可能,其实这是因为我用了二维vector,而二维复制需要确定size大小。比如简单的一个例子:#include<cstdio>#include<vector>using namespace std;int mai

2017-04-21 09:46:56 2291

原创 POJ2385 Apple Catching(DP/递推)

问题描述 It is a little known fact that cows love apples. Farmer John has two apple trees (which are conveniently numbered 1 and 2) in his field, each full of apples. Bessie cannot reach the apples wh

2017-04-20 00:34:41 244

原创 POJ3273 Monthly Expense(二分法)

问题描述: Farmer John is an astounding accounting wizard and has realized he might run out of money to run the farm. He has already calculated and recorded the exact amount of money (1 ≤ moneyi ≤ 10,000)

2017-04-20 00:14:03 360

原创 POJ3061(尺取法)

问题描述: 分析:这题当然可以用暴力的方法求,但是很明显这样时间复杂度会非常大,就和之前的连续子序列和最大,用暴力枚举起点和终点,这样是O(n^2)。另外一种思路: 由于所有的元素都大于零,如果子序列[s,t)满足as+…+at-1>=S,那么对于任何的t < t’一定有as+…+at-1 >= s。 对于区间[s,t)上的总和来说如果令sum(i) = a0 + a1 + … +ai-1

2017-04-19 23:54:59 362

原创 POJ2976 Dropping tests (最大化平均值/二分)

问题描述:这个题目,典型的最大化平均值,依然是水题,但是因为一些细节,导致提交好几次都wa。细节: 1.题目是drop k 个,所以最后转换一下思想(和牛过河搬石子是一样的),在n个里选n-k个,则相当于drop k 个。 2.题目要求the average should be rounded to the nearest integer,就因为这个,wa了几次没发现。代码如下:#include

2017-04-19 00:25:47 378

原创 POJ2229 (递推)

题目描述: 分析: 这题看起来很简单,实际上真的很简单,但是我花了好长时间都没推出来,我真菜,虽然有了些自己的理解,并且八九不离十,但是就是没有理解好。1.最容易想到的,当i为奇数,dp[i] = dp[i-1],因为再加一个1,都不会再合并成新的序列。 2.当i为偶数是我推了一个多小时没推出来的,看了其他人的题解,发现当有序列首位为1的序列时,就是dp[i],因为再也合不成新的序列了,序列首

2017-04-19 00:18:05 464

原创 二元位运算

因为做了些题都用到位运算,感觉有些运算使用位运算符更简单,现在整理一下。常见的二元位运算是与( & )、 或( | )、 非( ! )、 异或( ^ ) 。它们和对应的逻辑运算非常相似。A B A&B A|B A^B0 0 0 0 00 1 0

2017-04-18 23:51:29 634 2

原创 二分法典型应用(三)最大化平均值

和最大化最小值类似,最大化平均值也可以通过二分法求得。比如下面这个经典的问题: 有n个物品的重量和价值分别是wi和vi,从中选出k个物品使得单位重量价值最大。样例输入:3 22 25 32 1样例输出:0.75分析:一般先想到的是将每个物品的单位重量价值算出来,然后排个序,从大到小贪心进行选择,可惜这样是不对的,这样不能保证最后一定是最大平均值,直接用贪心对于这类要涉及多个因素比如求最大

2017-04-18 20:08:39 1637

原创 POJ3258 River Hopscotch(最大化最小值/二分法)

问题描述:Every year the cows hold an event featuring a peculiar version of hopscotch that involves carefully jumping from rock to rock in a river. The excitement takes place on a long, straight river with

2017-04-18 00:26:44 355

原创 二分法典型应用(二)最大化最小值 POJ2456 Aggressive cows

问题描述: Farmer John has built a new long barn, with N (2 <= N <= 100,000) stalls. The stalls are located along a straight line at positions x1,…,xN (0 <= xi <= 1,000,000,000).His C (2 <= C <= N) cow

2017-04-18 00:04:53 1232

原创 二分搜索典型应用(一)从有序数组中查找某个值

二分搜索的第一个典型应用——查找值lower_bound:问题描述:给定长度为n的单调不下降数列a0...an-1和一个数k,求满足ai >= k条件的最小i。不存在时输出n。样例输入:n = 5a = {2, 3, 3, 5, 6}k = 3样例输出:1分析这个问题完全可以暴力(暴力2333),但是有更好更高效的算法。 因为是有序性,我们可以看一下n/2的值,如果a[n/2] >= k

2017-04-17 21:14:37 795

原创 二分搜索

引言:有一个二分搜索的经典问题——猜数。犹记得小学时候,经常在中央二套看一个电视节目,名字好像叫啥,对,是“幸运52”,李咏大叔主持的,当时就有一个游戏是“猜商品的价格”。游戏规则大概是这样的,给你看一件商品,规定限时多少秒,让你猜价格是多少,李咏大叔通过你猜的价格提示“高了”还是“低了”,然后你根据他的提示,继续猜,直到猜到为止或时间截止。 -_-||| 当时这个游戏很多人都傻b地从一个数开始依

2017-04-17 15:24:40 344

空空如也

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

TA关注的人

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