- 博客(29)
- 资源 (4)
- 收藏
- 关注
原创 【LeetCode】Two Sum
题目描述:Given an array of integers, find two numbers such that they add up to a specific target number.The function twoSum should return indices of the two numbers such that they add up to the
2013-09-30 20:58:15 908
原创 【LeetCode】Binary Tree Zigzag Level Order Traversal--- 层序遍历二叉树
题目描述:Binary Tree Zigzag Level Order Traversal AC Rate: 399/1474My SubmissionsGiven a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from
2013-09-30 19:55:35 1132
原创 【LeetCode】Maximum Depth of Binary Tree (二叉树最大深度)
题目描述:Given a binary tree, find its maximum depth.The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.思路:采用递归的思想,父结点的深度 = max{左
2013-09-30 17:05:32 1159
原创 POJ 1704 Georgia and Bob (Nim游戏变形)
题目:http://poj.org/problem?id=1704思路:Nim游戏策略,做如下转换,如果N是偶数,则两两配对,将两个数之间的格子数(距离)看做成这一堆石头的数量。如果N是奇数,则将一个0的格子放在第一个。代码:#include#includeusing namespace std;const int MAXN=10000+2;int N,P[MAXN];
2013-09-25 13:56:07 1714
原创 九度OJ 9月赛第二场 题目1542:黑白迷阵 (状态压缩DP)
题目描述:黑白迷阵是一个GrassLand编写的手机游戏,它的规则非常简单,有如下4*5的棋盘,其中一些是格子是黑色,一些格子是白色的,每当点击其中某一个格子,它以及它上下左右五个格子的颜色会发出反转,如下图 游戏胜利的条件很简单,把所有的格子变为黑色即可。GrassLand想知道,给定一
2013-09-22 22:21:58 1707
原创 poj 3254 Corn Fields (状态压缩DP)
题目:http://poj.org/problem?id=3254思路见代码:#includeusing namespace std;const int MOD=100000000;const int MAX_N_M=12;int row_state[MAX_N_M];// 枚举出合法的状态数 int LegalState[2000],nstate;int dp[MAX
2013-09-20 21:54:41 1231
原创 poj 1990 MooFest (树状数组)
题目:http://poj.org/problem?id=1990思路见代码:#include#include#include#includeusing namespace std;const int MAX_N=20000+5;typedef long long ll;/*cnt:= 坐标小于 x的点的个数(cnt[x]) cntsum:= 坐标小于x的坐标点总
2013-09-19 13:24:22 955
原创 九度OJ月赛---题目1534:数组中第K小的数字 (二分搜索)
题目描述:给定两个整型数组A和B。我们将A和B中的元素两两相加可以得到数组C。譬如A为[1,2],B为[3,4].那么由A和B中的元素两两相加得到的数组C为[4,5,5,6]。现在给你数组A和B,求由A和B两两相加得到的数组C中,第K小的数字。输入:输入可能包含多个测试案例。对于每个测试案例,输入的第一行为三个整数m,n, k(1紧接着两行, 分
2013-09-18 22:33:03 1528
原创 K Best poj 3111 (01分数规划---二分搜索)
题目:http://poj.org/problem?id=3111思路:给定n个二元组(v,w)保留k个,使得 sigma(v)/sigma(w)的值最大:代码:#include#include#include#include#includeusing namespace std;const int Maxn=100001;const double eps=1e-8;
2013-09-16 21:59:38 1777
原创 poj 3104 Drying (二分搜索答案)
题目:http://poj.org/problem?id=3104思路:二分一个答案,然后判断可行性,注意这题整形用long long , 另外算中值的时候: mid = lhs+ (rhs-lhs)>>1 这种会超时,如果改成 mid= (lhs+rhs)>>1就不超时了,想不通,按理说第一种会避免中间值溢出,同样是位操作,怎么第一种会超时呢?有大神路过,求指教:代码:
2013-09-15 21:59:10 1464 1
原创 poj 3273 Monthly Expense (二分搜索,最小化最大值)
题目:http://poj.org/problem?id=3273思路:通过定义一个函数bool can(int mid):=划分后最大段和小于等于mid(即划分后所有段和都小于等于mid)这样我们转化为求 满足该函数的 最小mid。即最小化最大值,可以通过二分搜索来做,要注意二分的边界。WR了好几次。代码:#include#include#include#include
2013-09-15 13:07:22 1250
原创 poj 3258 River Hopscotch (二分搜索---最大化最小值)
题:http://poj.org/problem?id=3258思路:函数 can(int x)判断 当前的距离x能不能得到。用贪心的策略来选取N-M个点来看是否满足。注意边界条件和边界数据:#include#include#include#includeusing namespace std;const int MAXN=50005;int L,N,M,d[MAXN]
2013-09-14 16:40:53 1283
原创 poj 3259 Wormholes (判断图是否存在负圈)
题目:http://poj.org/problem?id=3259思路:根据BELLMAN-FORD算法,如果图中不存在从s可达的负圈,那么最短路不过经过同一个顶点两次,也就是说最多经过V-1条边,如果存在负圈,那么这个循环更新次数会在第n次也会更新,实际上会无限更新,越来越小,所以一开始对所以点i,都把d[i]初始化为0,那么可以检查出所有的负圈:#include#incl
2013-09-13 22:29:54 1853
原创 poj 2139 Six Degrees of Cowvin Bacon (Floyd 算法)
题:http://poj.org/problem?id=2139任意两点之间的最短路径问题:#include#includeusing namespace std;const int MAXN=301;const int INF=99999;int d[MAXN][MAXN],dis[MAXN],N,M;void solve(){ int ans=INF; for(i
2013-09-13 21:10:09 1517
原创 九度OJ 题目1371:最小的K个数
题目描述:输入n个整数,找出其中最小的K个数。例如输入4,5,1,6,2,7,3,8这8个数字,则最小的4个数字是1,2,3,4,。输入:每个测试案例包括2行:第一行为2个整数n,k(1第二行包含n个整数,表示这n个数,数组中的数的范围是[0,1000 000 000]。输出:对应每个测试案例,输出最小的k个数,并按从小到大顺序打印。样例输入:8 4
2013-09-09 00:56:11 1447
原创 2013编程之美挑战赛复赛---R2_B:招聘(01分数规划+DP)
总Time Limit: 6000ms Memory Limit: 262144kBDescriptionAlice新开了一家公司,它的下面有两个项目,分别需要N1和N2个人来完成。现在有N个人前来应聘,于是Alice通过面试来决定他们中的哪些人会被录用。Alice在面试中,会仔细考察他们能如何为公司的项目带来收益。她给每个人打了两个分值Q1和Q2,表示他加入第一个和第
2013-09-08 11:04:51 1139
原创 九度OJ 题目1529:棋盘寻宝 (简单DP)
题目:http://ac.jobdu.com/problem.php?pid=1529简单DP:#include#include#include#includeusing namespace std;int dp[9][9];int num[9][9];int main(){ //ifstream fin; //fin.open("input.txt"); while
2013-09-07 22:14:48 1207
原创 poj 2236 Wireless Network (简单的并查集应用)
题目:http://poj.org/problem?id=2236思路:每当修好一个电脑就找与其距离小于d的且已经修好的电脑与之合并即可。代码:#include#include#includeusing namespace std;const int MAXN=1005;bool hash[MAXN];int par[MAXN],rank[MAXN];int dx[MAX
2013-09-07 21:54:47 1202
原创 poj 3281 Dining (最大流)
题目;http://poj.org/problem?id=3281思路:将牛拆开,分别一一对应,然后构建有向图:S(源点) 食物(1-F) 牛(1——N) 牛(1-N) 饮料(1-D) 汇点T单向边,每条边容量为1 ,转换为最大流问题:至于为什么要把对应的牛拆成两个顶点:保证一条牛不会被分配多组食物和饮料代码:#include#include#includeus
2013-09-07 11:37:24 1702
原创 poj 3041 Asteroids (匈牙利算法---二分图最大匹配)
题目:http://poj.org/problem?id=3041在二分图中,最小点覆盖数=最大匹配数,把光束模型成顶点,小星星模型成连接对应光束的边,这样,等价于求最小点覆盖。#include#includeusing namespace std;const int MAXN=501*2;vector G[MAXN];int N,K;int match[MAXN];bool
2013-09-07 10:03:23 1207
原创 poj 1631 Bridging signals (LIS 最长递增子序列 DP-二分)
题目:http://poj.org/problem?id=1631思路:LIS 最长递增子序列,如果用一般的动态规划算法,复杂度是O(n^2),题目的数据规模下会超时,采用二分的思想:复杂度是O(nlogn)代码:首先是一般的DP: #include#include#includeusing namespace std;const int MAX=40001;int dp
2013-09-06 13:14:43 1477 4
原创 poj 1065 Wooden Sticks (贪心)
题目:http://poj.org/problem?id=1065思路:先按照L排序转换成一维,在对W贪心即可。#include#includeusing namespace std;const int MAX=5001;struct wooden{ int l,w,flag;}wd[MAX];bool Cmp(wooden x,wooden y){ if(x.l!=y.l
2013-09-05 21:10:38 1124
原创 poj 3181 Dollar Dayz (整数划分问题---递归+DP)
题目:http://poj.org/problem?id=3181思路:将整数N划分为一系列正整数之和,最大不超过K。称为整数N的K划分。递归:直接看代码:动态规划:dp[i][j]:=将整数i做j划分的方法数。 dp[i][j]=dp[i][i]; if(j>i) dp[i][j]=dp[i-j][j]+dp[i][j-1];//分j出现不出现两种情况 d
2013-09-03 22:29:51 1556
原创 poj 3046 Ant Counting (DP多重背包变形)
题目:http://poj.org/problem?id=3046思路: dp [i] [j] :=前i种 构成个数为j的方法数。#include #include #include int T,A,S,B;int hash[1010];int dp[1010][10100];const int MOD=1e6;using namespace std;int main()
2013-09-02 22:20:01 1470
原创 poj 1742 Coins 多重背包变形
题目:http://poj.org/problem?id=1742用bool dp[i]来表示价格i是否能被表示出。直接做:#include#include#include#includeusing namespace std;const int MAX_N=100001;bool dp[MAX_N];int num[105],val[105];int main(){
2013-09-02 13:54:47 2062
原创 poj 3280 Cheapest Palindrome ---(DP 回文串)
题目链接:http://poj.org/problem?id=3280思路: dp[i][j] :=第i个字符到第j个字符之间形成回文串的最小费用。dp[i][j]=min(dp[i+1][j]+cost[s[i-1]-'a'],dp[i][j-1]+cost[s[j-1]-'a']);if(s[i-1]==s[j-1]) dp[i][j]=min(dp[i+1][j-1],dp[i]
2013-09-01 17:12:57 1106
原创 poj 3616 Milking Time ---DP(带权重的区间动态规划)
题目:http://poj.org/problem?id=3616这题就是一个小小变形的带权重的任务调度问题 --interval scheduling--思路:首先按照每个区间的结束时间排序,再进行预处理:算出与每个区间相互兼容的最大区间下标保存在P数组里。状态:dp[i]=max{ dp[i-1],dp[p[i]]+w[i] } 代码:1A#include#include
2013-09-01 11:13:17 2522
原创 poj 2385 Apple Catching DP
题目链接:http://poj.org/problem?id=2385状态定义: dp[i][j] :=前i秒,移动j次接到的最大苹果数量。状态转移: dp[i][j]=dp[i-1][j]+num[0][i]; (j==0) dp[i][j]=max{dp[i-1][j-1],dp[i-1][j]}+num[j%2==1][i]; (1=这里输
2013-09-01 10:21:46 2072 1
原创 重建二叉树---根据前序和中序遍历结果重建二叉树
一颗二叉树:前序遍历结果: abdcef中序遍历结果:dbaecf基于递归的思想:在前序遍历中的第一个结点是根结点,然后在中序遍历中找到此根节点,然后递归的对左右子树分别重建。顺说一下,知道前序和后序遍历结果,我无法重建二叉树的,因为当某个结点只有一个儿子结点的时候,无法区分出到底是左还是右结点。#include #include using namespace std;
2013-09-01 09:50:24 1094
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人