算法
中国程序狗
朽木自雕
展开
-
01背包问题详解
N个物品,每个物品有一个wi的重量和pi的价值,现在有一个重W的背包问放进物品后的最大值。//首先是记忆化搜索 //dp[i][j] 在前i件物品里面选择不超过重量j的最大价值int rec(int i, int j) { if(dp[i][j] >= 0) //在此之前需要memset(dp, -1, sizeof(dp)) return dp[i]原创 2017-04-28 15:06:56 · 219 阅读 · 0 评论 -
大数加法 可为负数
基础题#include <stdio.h> #include <assert.h> #include <math.h> #include <string.h> #include <time.h> #include <iostream> #include <sstream> #include <string> #include <vector> #include <map> #include <set原创 2017-04-29 09:59:43 · 477 阅读 · 0 评论 -
逆序数
顾名思义,例如:有序列 2 4 3 1 那么 2 4, 2 1,4 1, 3 1为逆序对,逆序数为4。#include <stdio.h> #include <assert.h> #include <math.h> #include <string.h> #include <time.h> #include <iostream> #include <sstream> #include <string原创 2017-04-30 09:18:47 · 532 阅读 · 0 评论 -
回文子串 manarcher算法
对于一般的回文子串的计算方法枚举每一个字符s[i]作为回文串的中心,然后分为奇数回文串和偶数回文串来进行遍历两端,算法复杂度O(n^2)int palindromeString(const char *s) { int len = strlen(s); int mx1, mx2, ans = 0; REP(i, len) { mx1 = 1;原创 2017-05-02 09:42:47 · 370 阅读 · 0 评论 -
线段相交
给定n条线段的起点和终点找到任意两条线段相交的最大值。对于所有的线段的起点进行排序。将每一个线段i的起点作为相交的起点,找到线段j(j < i)终点的最大值与线段i终点的最大值进行比较,获取最大相交线段长度,复杂度O(nlogn)void solve() { #ifndef ONLINE_JUDGE freopen("input.in","r",stdin); #endif pair原创 2017-05-19 10:12:48 · 286 阅读 · 0 评论 -
2 3 5 7
输入一个n,求解1到n中有多少个数不是2、3、5、7的倍数。 解析:典型的容斥原理。void solve() { #ifndef ONLINE_JUDGE freopen("input.in","r",stdin); #endif LL n, a, b, c, d, ab, ac, ad, bc, bd, cd, abc, abd, acd, bcd, abcd; RDL(原创 2017-05-21 10:12:29 · 406 阅读 · 0 评论 -
Pairwise Sum and Divide
题目:有这样一段程序,fun会对整数数组A进行求值,其中Floor表示向下取整:fun(A) sum = 0 for i = 1 to A.length for j = i+1 to A.length sum = sum + Floor((A[i]+A[j])/(A[i]*A[j])) return sum给出数组A,由你来原创 2017-05-21 10:18:59 · 256 阅读 · 0 评论 -
大鱼吃小鱼
有N条鱼每条鱼的位置及大小均不同,他们沿着X轴游动,有的向左,有的向右。游动的速度是一样的,两条鱼相遇大鱼会吃掉小鱼。从左到右给出每条鱼的大小和游动的方向(0表示向左,1表示向右)。问足够长的时间之后,能剩下多少条鱼?Input 第1行:1个数N,表示鱼的数量(1 <= N <= 100000)。 第2 - N + 1行:每行两个数A[i], B[i],中间用空格分隔,分别表示鱼的大小及游动的方原创 2017-05-21 10:21:59 · 3169 阅读 · 0 评论