
数据结构与算法
文章平均质量分 51
介绍常用的数据结构和算法,提供例题、思路及代码
Davenny
O ever youthful,O ever weeping.
展开
-
hdu 2473并查集的删除操作
题目大意: 有两种操作1:将两个相似的邮件合并,并且这种关系可以传递; 2:将某一个点孤立起来(即从关系集合中删除); 思路: 由于并查集是一种树形结构,单独直接将某一点从关系集合中删除较为复杂,那么就可以将要删除的那个点设为虚点,即将其映射到一个新的点上,即成为孤立的点;#include<iostream>#include<cstdio>#incl转载 2016-08-24 08:55:41 · 370 阅读 · 0 评论 -
UVALive 6437并查集
题目大意: n个点,其中有k个发电站,然后给你m条无向边,要求使得每个地点可以接到电,求最小花费;思路: 比赛的时候硬是没想出来,用最小生成树做,而且用的还是prime,简直傻叉!!!! 我们可以将可以发电的地方合并成一个点,因为不算点的相对差异,你连到哪一个点都是等效的,然后用最小生成的另外一种做法;#include <cstdio>#include <algorithm>#includ原创 2016-08-27 20:59:31 · 518 阅读 · 0 评论 -
ZOJ 3761 —— Easy billiards(并查集+深搜)
题目:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3761桌面上有N个球,每次可以使某个球向上,下,左,右四个方向运动,但是不能让它离开桌面。当它与另一个球碰撞时,被碰撞的球就会沿着原先球的方向滚动,原先的球会停止在被碰撞的球的位置上。而被碰撞的球可以去碰撞其他球,或是离开桌面。题目要问最少能剩下几个球,并且输出一种操作方转载 2017-08-07 14:01:46 · 374 阅读 · 0 评论 -
poj3728并查集+LCA
题目大意: 对于一棵树,树上的每点都有一个权值,对于一个询问u,v,求从u 点到v点的路径上进行买卖所能得到的最大值,并且,买在卖之前。 There are N cities in a country, and there is one and only one simple path between each pair of cities. A merchant has chosen some原创 2017-08-30 16:30:46 · 332 阅读 · 0 评论 -
【BZOJ 1901】 Zju2112 Dynamic Rankings 主席树+树状数组
说明:提交一直segmentation fault,没有accepted 题意: 带修改的区间第k打查询;#include<bits/stdc++.h>using namespace std;#define lson l,mid#define rson mid+1,rconst int maxn=60001;int hush[maxn],a[50001],ls[maxn<<3],rs[原创 2017-08-11 16:35:49 · 413 阅读 · 0 评论 -
hdu4348To the moon
Background To The Moon is a independent game released in November 2011, it is a role-playing adventure game powered by RPG Maker. The premise of To The Moon is based around a technology that allows原创 2017-08-20 11:12:55 · 474 阅读 · 0 评论 -
hdu 6035 Colorful Tree(dfs)
题意: 一棵有n个点的树,树上每个点都有颜色c[i],保证每两个点之间的路径只有一天,定义每条路径的值为这条路径上经过的不同颜色数量和。求所有路径的值。解题思路: 可以把问题转化为对每种颜色有多少条不同的路径至少经过这种颜色的点,然后加和。 求有多少条路径经过可以转换为总路径数-没有经过的路径数,只要求出没有经过的路径数就好了。对于每种颜色没有经过自己的路径条数我们可以通过一遍dfs求得。一个转载 2017-07-27 09:16:23 · 390 阅读 · 0 评论 -
hdu6058-Kanade's sum
Give you an array A[1..n]A[1..n]of length nn. Let f(l,r,k)f(l,r,k) be the k-th largest element of A[l..r]A[l..r]. Specially , f(l,r,k)=0f(l,r,k)=0 if r−l+1#include <cstdio>#include <cstring>#include原创 2017-08-02 10:46:13 · 563 阅读 · 0 评论 -
Counting Divisors
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others) Total Submission(s): 0 Accepted Submission(s): 0Problem Description In mathematics, the function d(n) denotes转载 2017-08-03 19:08:46 · 334 阅读 · 0 评论 -
hdu 6096 String 字典树
String Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others) Total Submission(s): 0 Accepted Submission(s): 0Problem Description Bob has a dictionary with N words in转载 2017-08-11 10:35:09 · 408 阅读 · 0 评论 -
hdu1166 敌兵布阵
大意: 在给定区间修改某一点的值,然后查询某一个区间的和;思路: 分块; 如果包含整区间的话,那就直接加上整个区间的值;否则暴力累加每一个点的值;#include <bits/stdc++.h>using namespace std;const int maxn=5e4+9;int a[maxn];long long sum[maxn];int main(){ int t;原创 2017-08-20 20:28:14 · 395 阅读 · 0 评论 -
HYSBZ2038 小Z的袜子(莫队算法)
原地址:http://www.cnblogs.com/chanme/p/3681999.html 今天学了一下传说中的解决离线询问不修改的一种算法。题目的意思非常简单,就是询问在一个[L,R]区间里的取两个物品,然后这两个物品颜色相同的概率。其实就是对于每种颜色i,这个区间里对应的个数cnt[i],那么答案就应该是 sigma (cnt[i]cnt[i-1]) / (R-L+1)(R-L). 问转载 2017-08-22 10:00:32 · 370 阅读 · 0 评论 -
hdu 4638 Group
There are n men ,every man has an ID(1..n).their ID is unique. Whose ID is i and i-1 are friends, Whose ID is i and i+1 are friends. These n men stand in line. Now we select an interval of men to make原创 2017-08-23 20:02:43 · 351 阅读 · 0 评论 -
树链剖分模板题-SPOJ QTREE Query on a tree
You are given a tree (an acyclic undirected connected graph) with N nodes, and edges numbered 1, 2, 3…N-1.We will ask you to perfrom some instructions of the following form:CHANGE i ti : change the cos原创 2017-07-31 10:40:08 · 353 阅读 · 0 评论 -
uva 12655 Trucks [LCA](树链剖分+MST)
The Subtle Balloons Company (SBC) is the main balloon provider for programming contests; it has huge factories and warehouses, as well as an extensive truck fleet to ensure the contestants’ happiness.原创 2017-08-05 21:07:49 · 489 阅读 · 0 评论 -
CodeForces 343D Water Tree(树链剖分+dfs时间戳)
Mad scientist Mike has constructed a rooted tree, which consists of n vertices. Each vertex is a reservoir which can be either empty or filled with water.The vertices of the tree are numbered from 1 to原创 2017-08-07 21:17:36 · 548 阅读 · 0 评论 -
线段树
【完全版】线段树(转载) 转载自:http://www.notonlysuccess.com/膜拜之。。。【完全版】线段树 很早前写的那篇线段树专辑至今一直是本博客阅读点击量最大的一片文章,当时觉得挺自豪的,还去pku打广告,但是现在我自己都不太好意思去看那篇文章了,觉得当时的代码风格实在是太丑了,很多线段树的初学者可能就是看着这篇文章来练习的,如果不小心被我培养出了这么糟糕的风格,实在是过意不转载 2016-07-23 19:37:38 · 314 阅读 · 0 评论 -
线段树,区间更新,位运算: poj2777Count Color
Description Chosen Problem Solving and Program design as an optional course, you are required to solve all kinds of problems. Here, we get a new problem. There is a very long board with length L centi原创 2016-07-24 12:52:39 · 484 阅读 · 0 评论 -
(磨人的小妖精)hdu 1542 Atlantis:线段树扫描线,离散化
http://acm.hdu.edu.cn/showproblem.php?pid=1542DescriptionThere are several ancient Greek texts that contain descriptions of the fabled island Atlantis. Some of these texts even include maps of parts o原创 2016-07-24 21:40:45 · 388 阅读 · 0 评论 -
线段树:周长并:hdu1828 Picture
http://acm.hust.edu.cn/vjudge/contest/121640#problem/RDescription A number of rectangular posters, photographs and other pictures of the same shape are pasted on a wall. Their sides are all vertical o原创 2016-07-27 15:36:29 · 338 阅读 · 0 评论 -
线段树+离散化+离线:HDU 4325
Description As is known to all, the blooming time and duration varies between different kinds of flowers. Now there is a garden planted full of flowers. The gardener wants to know how many flowers wil原创 2016-08-04 18:49:12 · 308 阅读 · 0 评论 -
hdu4288:线段树+离散化+子树:Coder
Description In mathematics and computer science, an algorithm describes a set of procedures or instructions that define a procedure. The term has become increasing popular since the advent of cheap原创 2016-08-07 15:36:06 · 342 阅读 · 0 评论 -
hdu4417:线段树单点更新区间求和,离线 Super Mario
Description Mario is world-famous plumber. His “burly” figure and amazing jumping ability reminded in our memory. Now the poor princess is in trouble again and Mario needs to save his lover. We regard原创 2016-08-07 19:14:53 · 529 阅读 · 5 评论 -
poj2828
题目大意: 告诉你有n个人,然后告诉你每个人的插队位置以及自身的值。让你按照最后的队伍顺序输出每个人的值;思路: 由于先插入的人的位置可能会被后插入的人改变,因此后插入的人的优先级会更高,因此我们采用倒插法。最后插入的人的位置肯定不会变。 用线段树维护每个区间中空的位置个数,当插入一个人后就把这个位置丢掉。 想一想为什么维护每个区间空的位置个数就是对的呢?对于一个中间插入的数,其后面插入的那原创 2017-07-24 10:01:29 · 285 阅读 · 0 评论 -
poj2750
The little cat takes over the management of a new park. There is a large circular statue in the center of the park, surrounded by N pots of flowers. Each potted flower will be assigned to an integer nu原创 2017-07-24 19:48:26 · 522 阅读 · 0 评论 -
hdu6070 Dirt Ratio
n ACM/ICPC contest, the ”Dirt Ratio” of a team is calculated in the following way. First let’s ignore all the problems the team didn’t pass, assume the team passed X problems during the contest, and su转载 2017-08-03 21:07:16 · 1406 阅读 · 2 评论 -
zoj3525扫描线
题目大意: 给你一个长方体,让你求这个长方体可以圈住的点的最小权值和; 思路: 首先枚举x坐标,找出区间长度不超过la的区间; 然后限定y坐标,当其区间长度在所给范围内则加上相应数值,若超过了则减去相应数值。 最后限定z坐标,使得更新的区间长度恰好等于所给的范围;#include<bits/stdc++.h>#define lson l,m,rt<<1#define rson m+1,原创 2017-08-14 18:58:55 · 328 阅读 · 0 评论 -
BZOJ2243
给定一棵有n个节点的无根树和m个操作,操作有2类: 1、将节点a到节点b路径上所有点都染成颜色c; 2、询问节点a到节点b路径上的颜色段数量(连续相同颜色被认为是同一段),如“ 112221 ” 由3段组成:“ 11 ” 、“ 222 ” 和“ 1 ” 。 请你写一个程序依次完成这m个操作。 Input 第一行包含2个整数n和m,分别表示节点数和操作数; 第二行包含n个正整数表示n个节点原创 2017-08-16 20:35:47 · 556 阅读 · 0 评论 -
hdu 6155 - Subsequence Count 线段树+dp+矩阵
参考的原博客:http://www.cnblogs.com/iRedBean/p/7398272.html #include <bits/stdc++.h>#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1using namespace std;int n,q;const int maxn=1e5+9;const long long mod转载 2017-08-21 11:40:21 · 460 阅读 · 0 评论 -
zoj3800线段树+离线
You are given a sequence {A0, A1, …, AN-1} and an integer set called GS. Defined a function called G(L, R) on A, where G(L, R) = GCD(Ai) (L ≤ i < R) that is the greatest common divisor of all the integ转载 2017-09-03 08:27:44 · 435 阅读 · 0 评论 -
***hdu6183
Do you like painting? Little D doesn’t like painting, especially messy color paintings. Now Little B is painting. To prevent him from drawing messy painting, Little D asks you to write a program to mai转载 2017-09-06 15:06:21 · 597 阅读 · 0 评论 -
单调队列
Description What a sunny day! Let’s go picnic and have barbecue! Today, all kids in “Sun Flower” kindergarten are prepared to have an excursion. Before kicking off, teacher Liu tells them to stand in转载 2016-07-06 20:59:52 · 465 阅读 · 2 评论 -
树状数组 模板:敌兵布阵
Description C国的死对头A国这段时间正在进行军事演习,所以C国间谍头子Derek和他手下Tidy又开始忙乎了。A国在海岸线沿直线布置了N个工兵营地,Derek和Tidy的任务就是要监视这些工兵营地的活动情况。由于采取了某种先进的监测手段,所以每个工兵营地的人数C国都掌握的一清二楚,每个工兵营地的人数都有可能发生变动,可能增加或减少若干人手,但这些都逃不过C国的监视。 中央情报局要研转载 2016-07-06 21:03:34 · 299 阅读 · 0 评论 -
单调队列: Sliding Window
Description An array of size n ≤ 10 6 is given to you. There is a sliding window of size k which is moving from the very left of the array to the very right. You can only see the k numbers in the wind原创 2016-07-09 15:10:21 · 356 阅读 · 0 评论 -
单调栈:POJ2082:Terrible Sets
http://poj.org/problem?id=2082Language: Terrible Sets Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 4355 Accepted: 2254 DescriptionLet N be the set of all natural numbers {0 ,原创 2016-07-25 14:29:41 · 253 阅读 · 0 评论 -
POJ2418:字典树:Hardwood Species(二叉搜索树)
DescriptionHardwoods are the botanical group of trees that have broad leaves, produce a fruit or nut, and generally go dormant in the winter. America’s temperate climates produce forests with hundred原创 2016-07-28 09:07:51 · 450 阅读 · 0 评论 -
栈:Parentheses Balance
SubmitStatus Description You are given a string consisting of parentheses () and []. A string of this type is said to be correct:(1) if it is the empty string(2) if A and B are correct, AB is correct原创 2016-08-03 16:10:46 · 269 阅读 · 0 评论 -
拓扑排序:Ordering Tasks
DescriptionJohn has n tasks to do. Unfortunately, the tasks are not independent and the execution of one task is only possible if other tasks have already been executed. Input The input will consist转载 2016-08-03 19:21:14 · 391 阅读 · 0 评论 -
poj2728:最优比率生成树
题目大意: 在村庄之间修路,有水平距离和垂直距离,要求水平距离之和与垂直距离之和的比值最小;思路: r=sigma(val[i])/sigma(cost[i]); 可得式子:f(l)=[sigma(val[i])-l*sigma(cost[i])]*x[i]=d[i]*x[i]; x[i]为1或0,表示要和不要这条边; 有数学知识可得: 当r最大时d[i]为零;此直线为斜率为负数,截距为原创 2016-08-22 15:29:32 · 294 阅读 · 0 评论 -
poj1639:最小度限制生成树
题目:http://poj.org/problem?id=1639 题目大意: 所有的车要去同一地点,而此地点对车子有一个最大容量K,所以车子在行驶的过程中,可以相互合并,求所有车子到达目的地的距离和的最小值;思路: 我们可以现将出目的地以外的车子建立最小生成树,然后从目的地向各子树连接最短边形成一棵树; 然后向子树中连接一条边,这样必定会形成一个环,那么就要把这个环中最大的那个边去掉,直到原创 2016-08-22 20:51:01 · 369 阅读 · 0 评论