摘要:其实线段树的数据结构没什么可说的,但是怎么用好它就有得说了。具体的来说就是:首先就是有段关系了,这个是第一位的。接着就是有大小关系了,这个是第二位的。再者就是数据量大,这个是次要的。只要满足了这三个条件,就是用线段树的时候了。如何理解段呢?比如说让你统计小于200的数据有多少个。这个就是段了。至于大小就是小于200的200了。(1)1 3 2 4 5 63 563 23 46 56 23 455 …… 888 这个例子是落在888以前的。(2)当然还是段中段,这个就不解释了。下面来贴出一道相关的题目。http://acm.swust.edu.cn/oj/contest/29/825/View
阅读全文
摘要:将数字字符串转化成整型数据我们将用到两个函数。1. c_str它的作用是将string对象转化成char*,为什么要这样做呢,这就要说起另外一个函数了。2.atoi (它是array to integer的缩写)它的作用是将数字字符串转化成整型数据。但是要注意atoi(const char * s), 这个是它的标准用法,如果是atoi(string s)这个就不行了。具体的用法是。View Code string str="-1234";int num = atoi(str.c_str());string str = "1234";int Num =
阅读全文
摘要:No.1:用C++输入带空格的字符串, getchar()必不可少。cin>>n;getchar();while(n--) getline(cin, s);No.2:不可对string s, char *s, char s[100] 像这种的串一个字符一个字符的赋值, 会出现烫烫烫啊。但是可以对char s[100]={0}这样的字符赋值。
阅读全文
摘要:HDU 2660http://acm.hdu.edu.cn/showproblem.php?pid=2660题意就不说了,直接走代码。View Code #include<iostream>#include<cstdio>#include<cstring>#include<string>#include<algorithm>using namespace std;#define max(a, b) (a>b?a:b)int V[21], W[21], Ans, N, K, L, i;void DFS(int p, int vv,
阅读全文
摘要:先说一下多重背包问题。他是由完全背包而来的,但是不同的是,他的每件物品有一定的数量限量,而完全背包中每种物品可以有无限件。HDU 2660http://acm.hdu.edu.cn/showproblem.php?pid=2660题目大意:在N件物品中,最多可以选择K,在选择的K件物品中,总容量不能超过W.求选择的最大价值。本题就是一个任意组合问题了,当然除了这种解法以外还有一种解法。那就是DFS了。View Code #include<iostream>#include<cstdio>#include<cstring>#include<string&
阅读全文
摘要:HDU 1016http://acm.hdu.edu.cn/showproblem.php?pid=1016题目大意:给定一个数N,从1到N的这些整数构成一个环,它的目的就是让你找出第相邻两个数都是素数的环。而且是所有的环。View Code #include<iostream>#include<cstdio>#include<cstring>#include<string>#include<algorithm>using namespace std;int N;bool vis[21];int pre[21];bool prime[
阅读全文
摘要:这个奇偶剪枝相当牛B呀,佩服。HDU 1010http://acm.hdu.edu.cn/showproblem.php?pid=1010题目大意:就是让你来找一下,能否在限定的时间内从S到达D.Sample Input:4 4 5S.X...X...XD....Sample Output:NOView Code #include<iostream>#include<cstdio>#include<cstdlib>#include<cstring>#include<string>#include<algorithm>usi
阅读全文
摘要:这次说的又有点新鲜的东西。那就是回溯了。本题是一个经典回溯例题,注意体会一下OK,那么我们说说他有什么用呢?其实很简单了,它的作用就是控制DFS的搜索流程。在本题简单一点来说,就是它能使得DFS符合题意。直接看例子吧。题目:HDU 1045 http://acm.hdu.edu.cn/showproblem.php?pid=1045题意:这个是一个八皇后的变种。不算难。Sample Input:4 .X.. .... XX.. .... Sample Output:5View Code #include<iostream>#include<cstring>#includ
阅读全文
摘要:如果我们不好确定搜索的结束条件,那么我们可以假设一下他的搜索深度。将这个深度取到极限就可以了。也就是说,如果当小于这个深度有解了,那么停止搜索,得到答案;如果等于这个深度了,还没有解,那么我们就认为此种情况下无解,也停止搜索,也算是得到了答案,即无解。下面来看一个很简单的例子吧。题目:http://acm.swust.edu.cn/oj/problem/0823/View Code #include "iostream"#include "cstdio"#include "cstring"#include "string&q
阅读全文
摘要:其实就是个DFS。题目:http://acm.swust.edu.cn/oj/problem/0827/View Code #include "iostream"#include "string"#include "cstring"#include "algorithm"using namespace std;#define maxn 105#define INF 0xFFFFFFFint map[maxn][maxn], mid, k, N, M;int Link[maxn]; //记录行的状态int used[
阅读全文
摘要:lower_bound的headfile是algorithm.lower_bound的工作原理就是二分查找了。lower_bound的作用:lower_bound的返回值减去数组的地址就是要查找的元素在数组中的位置。即:Pos = lower_bound(a, a+10, 3)-a;那么Pos就是在数组a[10]中的位置了。下面给出它的具体用法并说明一些要注意的问题。View Code #include "iostream"#include "algorithm"using namespace std;int main(){ int a[4]={1, 2
阅读全文
摘要:DescriptionIn the second year of the university somebody started a study on the romantic relations between the students. The relation "romantically involved" is defined between one girl and one boy. For the study reasons it is necessary to find out the maximum set satisfying the condition:
阅读全文