摘要:
简单题,二分查找View Code #include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>#include <algorithm>using namespace std;#define maxn 5005struct Line{ int u, d;} line[maxn];int n, m, x1, x2, y1, y2;int toy[maxn];bool left(int x, int y, Line a){ if (x < a.d + 阅读全文
posted @ 2011-05-23 22:53
undefined2024
阅读(470)
评论(0)
推荐(1)
摘要:
题意:一个满二叉树,给所有节点按从左到右的顺序从1开始编号。现每次给定一个节点编号,问以该节点为根的子树的最小编号和最大编号节点分别为多少号。(理解不了就看原题的图)分析:使用lowbit,即二进制码中的最靠后的1和后面的0组成的数字。规律是这样的,对于以x为根的子树,以lowbit(x)为其高位和低位的分界线,该子树上的所有节点编号的高位(不包括lowbit(x)那一位)均与x相同。不在该子树上的点的编号的高位均与x不同。因此,只要算出以x的高位为高位的数字中最大和最小的两个即为所求。View Code #include <iostream>#include <cstdio 阅读全文
posted @ 2011-05-23 19:37
undefined2024
阅读(460)
评论(0)
推荐(0)
摘要:
类似gcd,节点向根行走。View Code #include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>using namespace std;int a, b;void work(){ int l = 0, r = 0; while (a != 1 && b != 1) { if (a > b) { l += a / b; a = a % b; }else { r += b / a; b = b % a; } } if (a == 1) 阅读全文
posted @ 2011-05-23 19:04
undefined2024
阅读(214)
评论(0)
推荐(0)
摘要:
简单题View Code #include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>using namespace std;#define maxn 20char id[maxn];int fac[] ={ 9, 3, 7 };int getpos(char ch){ int len = strlen(id); for (int i = 0; i < len; i++) if (id[i] == ch) return i; return -1;}bool 阅读全文
posted @ 2011-05-23 18:38
undefined2024
阅读(197)
评论(0)
推荐(0)
摘要:
题意:给定一个图,求起点到终点的第k短路。分析:先用dijkstra从t反向寻找最短路。然后使用A*算法,把f(i)=g(i) + h(i)。h(i)就是i点到t的最短距离。当某点出队次数达到k次的时候,结果为该点的当前路程+该点到t的最短距离。(我没有判断不连通的情况)为什么这样做是对的呢?我们这样来思考,如果不实用最短路,而只使用A*那么t第x次出队的结果即为第x短路的距离。继而可以想到,从第一个出队次数达到x的点,沿着最短路走到t,一定是第x短路。说实话我也没有完全理解。另外注意s==t的情况,据说k要++,不明白为啥。View Code #include <iostream> 阅读全文
posted @ 2011-05-23 18:16
undefined2024
阅读(1172)
评论(0)
推荐(0)