cout

【cf741】D. Arpa’s letter-marked tree and Mehrdad’s Dokhtar-kosh paths(dsu on tree)

无人久伴 提交于 2019-12-04 13:36:38
传送门 题意: 给出一颗以 \(1\) 为根的有根树,树边带有一个字符( \(a\) ~ \(v\) )的信息。 输出对于每个结点,其子树内最长的简单路径并且满足边上的字符能够组成回文串。 思路: 显然最终的答案分为两部分,子树内部的答案,经过当前根结点的答案。 第一种答案很好处理。类似于点分治,主要处理第二种答案。 树上路径可以考虑找到 \(lca\) ,维护点到根节点的信息。 题目中的回文串可以等价于,出现奇数次的字符不超过 \(1\) 个。我们将字符状压一下,那么维护点到根的信息就很方便了;同理求出两点间的信息也很方便。 因为要枚举两条链中的结点,我们可以类似于树 \(dp\) 那样,保留之前的信息,然后枚举这条链更新答案并且更新信息。 这里显然枚举时枚举轻边最优(类似于启发式合并),那么可以采用 \(dsu\ on\ tree\) ,算法会保留重儿子的信息,我们直接暴力轻儿子即可。 根据 \(dsu\ on\ tree\) 算法的思想,每个结点只会被暴力到 \(O(logn)\) 次,所以算法的时间复杂度为 \(O(nlogn)\) 。 感觉挺考察对 \(dsu\ on\ tree\) 的理解的。。细节见代码吧: /* * Author: heyuhhh * Created Time: 2019/11/15 16:10:20 */ #include <bits/stdc++

#leetCode刷题纪实 Day17

孤街浪徒 提交于 2019-12-04 13:16:43
https://leetcode-cn.com/problems/walking-robot-simulation/ 机器人在一个无限大小的网格上行走,从点 (0, 0) 处开始出发,面向北方。该机器人可以接收以下三种类型的命令: -2:向左转 90 度 -1:向右转 90 度 1 <= x <= 9:向前移动 x 个单位长度 在网格上有一些格子被视为障碍物。 第 i 个障碍物位于网格点 (obstacles[i][0], obstacles[i][1]) 如果机器人试图走到障碍物上方,那么它将停留在障碍物的前一个网格方块上,但仍然可以继续该路线的其余部分。 返回从原点到机器人的最大欧式距离的平方。 示例 1: 输入: commands = [4,-1,3], obstacles = [] 输出: 25 解释: 机器人将会到达 (3, 4) 示例 2: 输入: commands = [4,-1,4,-2,4], obstacles = [[2,4]] 输出: 65 解释: 机器人在左转走到 (1, 8) 之前将被困在 (1, 4) 处 提示: 0 <= commands.length <= 10000 0 <= obstacles.length <= 10000 -30000 <= obstacle[i][0] <= 30000 -30000 <= obstacle[i][1] <

DFS判断图是否有环

前提是你 提交于 2019-12-04 12:17:49
  利用_DFS_来判断无向图是否存在环的条件思路,我看一次_DFS_是否能访问到之前访问到的节点,如果能够访问到,就说明图存在环,那么 关键问题就是判断是一次DFS? ,追根到_DFS_算法的实现细节,发现我们设置_visited_数组时只有设置0和1两个状态,那么就可以改进以下之前的_DFS_算法,将_visited_各个状态表示成如下状态: 0 : 没有被访问过 1 : 刚刚访问,但是邻接点没有被全部访问完 2 : 所有的邻接点都被访问完了,这里就可以判定_DFS_一定退出了   关键问题就解决了,看下面的简易的测试代码,同时也运用到了并查集的数据结构: #include<iostream> #include<stdlib.h> #define maxsize 100 #define INF 0x3f3f3f3f using namespace std; int g[maxsize][maxsize]; int vexnum, arcnum; int visited[maxsize], father[maxsize]; int flag = 0; void InitGraph(){ cout << "输入顶点数和边数: "; cin >> vexnum >> arcnum; for(int i = 0; i < vexnum; i++){ /

【cf375】D. Tree and Queries(树上启发式合并+线段树)

冷暖自知 提交于 2019-12-04 11:59:40
传送门 题意: 给出一颗以 \(1\) 为根的有根树,每个结点有个颜色 \(c_i\) 。 之后要回答 \(m\) 组询问,每组询问包含 \(v_i,k_i\) ,要回答以 \(v_i\) 为根的子树中,颜色出现次数不小于 \(k_i\) 的颜色的和。 思路: 这种静态子树上的问题,可以考虑dsu on tree。 由于要回答次数超过 \(k\) 的颜色和,那么建立一颗线段树,以出现次数为横坐标,维护颜色的和。 由于 \(dsu\ on\ tree\) ,每个点会被访问 \(O(logn)\) 次,因为每个结点有修改操作,所以会多个 \(log\) 的时间复杂度。 总的时间复杂度为 \(O(nlog^2n)\) 。 /* * Author: heyuhhh * Created Time: 2019/11/13 19:24:01 */ #include <bits/stdc++.h> #define MP make_pair #define fi first #define se second #define sz(x) (int)(x).size() #define all(x) (x).begin(), (x).end() #define INF 0x3f3f3f3f #define Local #ifdef Local #define dbg(args...) do {

Can cout alter variables somehow?

梦想的初衷 提交于 2019-12-04 10:45:26
问题 So I have a function that looks something like this: float function(){ float x = SomeValue; return x / SomeOtherValue; } At some point, this function overflows and returns a really large negative value. To try and track down exactly where this was happening, I added a cout statement so that the function looked like this: float function(){ float x = SomeValue; cout << x; return x / SomeOtherValue; } and it worked! Of course, I solved the problem altogether by using a double. But I'm curious as

实时人流量监测——海康威视sdk初体验

倖福魔咒の 提交于 2019-12-04 09:43:02
本文主要是博主使用海康SDK进行人流量统计的摸索过程,在这里简单记录一下。 查询文档,能实现人流量统计大概有两种方式,报警或者监听, 这边我选择了监听方式,NET_DVR_StartListen_V30 这个接口,启动监听,它可以接收设备主动上传的报警信息并且支持多线程。 我们需要编写一个回调函数来接受数据(处理业务逻辑), NET_DVR_ALARMER 是一个报警信息结构体,这边需要 COMM_ALARM_PDC 这个类型的数据,其实是一个宏,用来判断接受数据的类型,这里对应的结构体为NET_DVR_PDC_ALRAM_INFO 具体参数可以查阅文档。到这里这里大概对于SDK的调用有个了解。那么开始动手。 首先创建一个人流量demo ,导入SDK必要头文件以及相应的资源文件。 (ps:这里很多dll并不需要) 直接上代码: 1 #include <stdio.h> 2 #include <iostream> 3 #include "HCNetSDK.h" 4 using namespace std; 5 6 void CALLBACK MessageCallback(LONG lCommand, NET_DVR_ALARMER* pAlarmer, char* pAlarmInfo, DWORD dwBufLen, void*pUser) 7 { 8 9 cout <<

Cancelling std::cout code lines using preprocessor

三世轮回 提交于 2019-12-04 09:23:44
One can remove all calls to printf() using #define printf . What if I have a lot of debug prints like std::cout << x << endl; ? How can I quickly switch off cout << statements in a single file using preprocessor? NullStream can be a good solution if you are looking for something quick that removes debug statements. However I would recommend creating your own class for debugging, that can be expanded as needed when more debug functionality is required: class MyDebug { std::ostream & stream; public: MyDebug(std::ostream & s) : stream(s) {} #ifdef NDEBUG template<typename T> MyDebug & operator<<

【cf915】E. Physical Education Lessons(线段树)

大兔子大兔子 提交于 2019-12-04 07:08:13
传送门 简单的线段树区间修改区间查询,但是因为数据范围过大,所以采用动态开点的方法(注意一下空间问题)。 也可以直接对询问区间的端点离散化然后建树,这种方法时间复杂度和空间复杂度都比较优秀。 给出动态开点的代码: /* * Author: heyuhhh * Created Time: 2019/11/12 19:33:21 */ #include <bits/stdc++.h> #define MP make_pair #define fi first #define se second #define sz(x) (int)(x).size() #define all(x) (x).begin(), (x).end() #define INF 0x3f3f3f3f #define Local #ifdef Local #define dbg(args...) do { cout << #args << " -> "; err(args); } while (0) void err() { std::cout << '\n'; } template<typename T, typename...Args> void err(T a, Args...args) { std::cout << a << ' '; err(args...); } #else #define dbg(..

tensorflow查看权重参数值

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-04 06:01:25
每个框架都有查看权重参数的机制,在tenstensorflow中 查看的例子如下 import tensorflow as tf import numpy as np reader = tf.train.NewCheckpointReader('model-100') all_variables = reader.get_variable_to_shape_map() w0 = reader.get_tensor("conv0/W") print(type(w0)) print(w0.shape) print(w0[0]) b0 = reader.get_tensor("conv0/b") print(type(b0)) print(b0.shape) print(b0) 注意这里,在保存moxi模型的目录中有checkpoint文件,有model-100.data-00000-of-00001和model-100.index文件,此处我们只写.之前的东西。 直接是Numpy.ndarray格式,这个很好。 使用txt文件保存权重的代码为 import tensorflow as tf import numpy as np reader = tf.train.NewCheckpointReader('model-100') all_variables = reader.get

std::cout doen't like std::endl and string in conditional-if

末鹿安然 提交于 2019-12-04 05:53:27
问题 main.cpp: In function ‘void PrintVector(std::vector<std::__cxx11::basic_string<char> >&, bool)’: main.cpp:16:41: error: overloaded function with no contextual type information std::cout << ((newline)? (std::endl) : ""); ^~ Why std::cout doen't like std::endl and string in conditional-if? 回答1: std::endl is a stream manipulator. It's a function. It does not have a common type with "" . So they cannot be the two types of a conditional expression. Since the common type is the type of the whole