上一页 1 ··· 3 4 5 6 7 8 9 10 11 ··· 25 下一页
摘要: 训练指南p.59#include #include #include #include using namespace std;const int MAXN = 30;int N;int A[MAXN];char str[1010];map table;int bitcount( int x ){ int res = 0; while ( x ) { if ( x & 1 ) ++res; x >>= 1; } return res;}int main(){ while ( scanf( "%d", &N ) == 1 ) ... 阅读全文
posted @ 2013-09-09 16:33 冰鸮 阅读(275) 评论(0) 推荐(0)
摘要: 假设最少删除的边的个数为cost,显然,最终答案即为cost+cost+1 (因为删除一条边,就会增加一个链,所以删除cost条边后,就会有cost+1条链,将这cost+1条链连接起来的代价为cost+1, 删除cost条边的代价为cost,所以总代价为cost+cost+1)求最少删除的边数:首先我们定义一棵子树中的链不能以该子树的根为端点,以下提到的所有链均必须满足这个条件。设一棵以节点i为根的子树中,叶子节点的个数为duan,并设i的父亲为fa。那么,这棵子树至少会分割成duan-1条链(以其中两个叶子为端形成一条链,剩下的一个叶子对应一条链)。DFS,对于某棵以节点i为根的子树,如果 阅读全文
posted @ 2013-09-08 21:55 冰鸮 阅读(459) 评论(0) 推荐(0)
摘要: 双向链表直接模拟。用一个辅助数组maxSum来维护一下前k项中[1,k]的最大和。因为光标是一格一格的移动,所以每次光标右移的时候动态更新一下即可。时间复杂度O(n)。#include #include #include #include using namespace std;const int MAXN = 1000100;const int INF = 1 pre = NULL; head->next = NULL; node *cur = head; maxSum[0] = -INF; sum[0] = 0; int... 阅读全文
posted @ 2013-09-07 21:58 冰鸮 阅读(301) 评论(0) 推荐(0)
摘要: 起点和终点各做一次单源最短路, d1[i], d2[i]分别代表起点到i点的最短路和终点到i点的最短路,枚举商业线车票cost(a, b); ans = min( d1[a] + cost(a, b) + d2[b] );#include #include #include #include #include #include using namespace std;const int MAXN = 1010;const int INF = 1 rhs.d; }};struct Edge{ int from, to, dist; Edge() { } Edge( i... 阅读全文
posted @ 2013-09-07 16:45 冰鸮 阅读(211) 评论(0) 推荐(0)
摘要: 《训练指南》p.125设f[n] = gcd(1, n) + gcd(2, n) + …… + gcd(n - 1, n);则所求答案为S[n] = f[2]+f[3]+……+f[n];求出f[n]即可递推求得S[n]:S[n] = S[n - 1] + f[n];所有gcd(x, n)的值都是n的约数,按照约数进行分类,令g(n, i)表示满足gcd(x, n) = i && x #include #include #include #define LL long long intusing namespace std;const int MAXN = 4000100;LL 阅读全文
posted @ 2013-09-05 21:47 冰鸮 阅读(208) 评论(0) 推荐(0)
摘要: 构造矩阵.见《训练指南》p156...#include #include #include #define LL long long intusing namespace std;const int MAXN = 20;LL mod;struct Matrix{ LL a[MAXN][MAXN]; int r, c; friend Matrix operator*( Matrix &a, Matrix &b ); friend Matrix operator^( Matrix a, LL k );};Matrix operator*( Matrix &a, Matrix 阅读全文
posted @ 2013-09-05 09:15 冰鸮 阅读(258) 评论(0) 推荐(0)
摘要: 参考:http://www.cnblogs.com/slon/archive/2012/03/30/2426104.html题意:给一个有n个点的点集,有q个询问,每个询问询问一个点p,求与p曼哈顿距离最小的点,并输出曼哈顿距离的最小值。分析:使得abs(qx - xi) + abs(qy - yi)最小,因为带了个绝对值,所以没法直接套用一些数据结构中查询最值的操作,一时间也没什么头绪。后来看到上面的博客,才明白可以分情况讨论,把绝对值去掉。一共四种情况:qx >= xi && qy >= yiqx >= xi && qy = yiqx xi 阅读全文
posted @ 2013-09-04 22:22 冰鸮 阅读(434) 评论(0) 推荐(0)
摘要: 将每个球按输入顺序编号,建立 它第几个被扔掉->编号 的映射关系。记录当前在手里的球的编号,按扔掉的顺序查找这个球的编号,看看这个球是逆时针转到手里更近还是顺时针转到手里更近,即当前扔掉球的编号与当前手里球的编号之间有几个球。树状数组C[i]记录编号i的球是否还在。球是环形排列的,特殊处理一下。对于扔掉一个球之后下一个落在手里的球的编号,二分判定,找顺时针方向第一个有球的位置#include #include #include #include #define LL long long intusing namespace std;const int MAXN = 100100;int 阅读全文
posted @ 2013-09-02 22:24 冰鸮 阅读(220) 评论(0) 推荐(0)
摘要: 二分爆炸半径R,判断是否可行,如果可行,半径可以继续增加。之前建图有误,结果一直不对。#include #include #include #include #include #include using namespace std;const int MAXN = 222;const double eps = 1e-5;struct Point{ double x, y; Point( double x = 0.0, double y = 0.0 ): x(x), y(y) { } void readPoint() { scanf( "%lf%lf", ... 阅读全文
posted @ 2013-09-02 22:07 冰鸮 阅读(184) 评论(0) 推荐(0)
摘要: 一组数据:29 10 22 3 1 4 2 5 1 3 40 1 0 1 1 1 1 1 010 410 410 50 710 215 10 21 1 1 1 10 1 0 1 010 310 310 10 40 50 21答案:16161616156666以第一组为例:2 3 1 4 2 5 1 3 4以小于K的数为分界,将数列分成几段。对于每个数字,记录它所在段的左端点和右端点,据此求出修改前的合法对数sum。对于每个修改,查看当前修改发生在哪一段,该修改对sum产生了怎样的影响,修改sum即可。树状数组C[i]记录区间[1, i]共有多少个白点。注意修改发生在段内和段端点处要分开考虑。# 阅读全文
posted @ 2013-08-30 21:03 冰鸮 阅读(265) 评论(0) 推荐(0)
上一页 1 ··· 3 4 5 6 7 8 9 10 11 ··· 25 下一页