memset

No Smoking, Please Gym - 101518H

偶尔善良 提交于 2019-11-29 06:47:33
No Smoking, Please Gym - 101518H 最小割~ 1 #include <bits/stdc++.h> 2 using namespace std; 3 const int maxv = 1000110; 4 const int maxe = 4000010; 5 const int inf = 0x3f3f3f3f; 6 struct Edge{ 7 int u, v, nex; 8 int cap, flow; 9 Edge(int u=0, int v=0, int nex=0, int cap=0, int flow=0): 10 u(u), v(v), nex(nex), cap(cap), flow(flow){} 11 }e[maxe<<1]; 12 int head[maxv]; 13 int cnt; 14 void init(){ 15 memset(head, -1, sizeof(head)); 16 cnt = 0; 17 } 18 19 void add(int u, int v, int cap){ 20 e[cnt] = Edge(u, v, head[u], cap, 0); 21 head[u] = cnt++; 22 e[cnt] = Edge(v, u, head[v], cap, 0); 23 head[v] =

Cow Exhibition

杀马特。学长 韩版系。学妹 提交于 2019-11-29 05:50:07
http://poj.org/problem?id=2184 Cow Exhibition #include<iostream> #include<string.h> #include<algorithm> using namespace std; #define maxn 105 int dp[200005]; int s[maxn]; int f[maxn]; int N; /* 思路:想要s和f之和都大于0,并且两者相加结果最大 那么可以利用dp[i]的下标记录i当前有多少smartness,dp[i]的值记录在当前smartness的限制下能获得的最大funness值 为了规避数组不能用负数来访问(有必要的,假如之前smartness负的很厉害,但是funness也很高,这时候出现一个samrtness超高同时funness一般般的往负数方向靠,那不就可以获得获得最优值的可能了吗) 需要将下界提高到100000,因为N最多100,单体smartness值最低-1000. 之后从100000的位置来寻求最优解就行了 Ps:注意此时dp值应该大于0并且要减去下界100000 最优解判断:if(j-100000+dp[j]>ans) ans=j-100000+dp[j]; 所以做一只既幽默又聪明的奶牛吧! */ /* But! ----------------

Why can it be dangerous to use this POD struct as a base class?

纵饮孤独 提交于 2019-11-29 05:41:38
问题 I had this conversation with a colleague, and it turned out to be interesting. Say we have the following POD class struct A { void clear() { memset(this, 0, sizeof(A)); } int age; char type; }; clear is intended to clear all members, setting to 0 (byte wise). What could go wrong if we use A as a base class? There's a subtle source for bugs here. 回答1: The compiler is likely to add padding bytes to A. So sizeof(A) extends beyond char type (until the end of the padding). However in case of

Is there analog of memset in go?

陌路散爱 提交于 2019-11-29 05:39:23
In C++ I can initialize an array with some value using memset : const int MAX = 1000000; int is_prime[MAX] memset(is_prime, 1, sizeof(is_prime)) What memset does, crudely can be described as filling the array with some value, but doing this really really fast. In go I can do is_prime := make([]int, 1000000) , but this will create a slice with all 0, in the similar manner I can use new([1000000]int) , but nothing will allow me to create an array/slice with all 1 or any other non-zero element. Of course I can use a loop to populate it with the value later, but the main purpose of memset is that

ICPC Asia Nanning 2017

旧巷老猫 提交于 2019-11-29 05:18:30
比赛地址: https://www.jisuanke.com/contest/3107?view=challenges A、签到,大概就是输出几个字符串 #include <bits/stdc++.h> using namespace std; int t,n; int main(){ scanf("%d",&t); while(t--){ scanf("%d",&n); for(int i=1;i<=n;++i) printf("Abiyoyo, Abiyoyo.\n"); printf("Abiyoyo, yo yoyo yo yoyo.\n"); printf("Abiyoyo, yo yoyo yo yoyo.\n"); } return 0; } F、签到2.0,上大数板子搞一下,猜了个结论:答案为最大的k使得2^k<=n 后来跟rhy讨论了下,每轮淘汰掉第1、3、5、7、9个人的话,相当于不断把n除以2,最后留下的必定是一个2^k #include <bits/stdc++.h> #define MAXN 9999 #define MAXSIZE 1000 #define DLEN 4 using namespace std; class BigNum { private: int a[210]; int len; public: BigNum(){ len = 1

GZOI 2019 旅行者

浪子不回头ぞ 提交于 2019-11-29 03:28:26
Description 给你 \(n\) 个点 \(m\) 条边的有向图,求给定 \(k\) 个特殊点两两之间最短路的最小值。 \(T\) 组询问。 \(1 \leq T \leq 5\) \(1 \leq k \leq n \leq 10^5\) \(1 \leq m \leq 5 \times 10^5\) \(1 \leq z \leq 2 \times 10^9\) Solution 两种思路。 第一种: 对于每一条边 \((x,y,z)\) ,离 \(x\) 最近的特殊点是 \(a\) ,离 \(y\) 最近的特殊点是 \(b\) 。那么,这两个特殊点的距离是 \(dis_a + dis_b + z\) 。我们要找到最小值。 因为这是有向图,所以正反跑两次 Dijkstra 就可以了进行染色就可以了,染色是记录离每个城市最近的感兴趣的城市。如果 \(a = b\) ,那么就相当于走了一个环,所以我们要特判。 复杂度 \(O(T \times nlogn)\) 。 第二种: 将所有的点分成两个集合 \(A\) 和 \(B\) 。我们用源点 \(s\) 连接集合 \(A\) 中所有的点,边权都为 \(0\) ,再将集合 \(B\) 连向汇点 \(t\) ,边权都为 \(0\) 。集合内部边权不变。 将所有节点分成两个集合,要满足任何两个特殊点都有至少一次被分进了不同的集合

欧拉回路及例题

♀尐吖头ヾ 提交于 2019-11-29 03:24:14
欧拉回路 几个定义 性质与定理 定理1 推论1 定理2 推论2 性质1 性质2 算法主体 例题 uoj117求给定图的欧拉回路 poj1041求字典序最小的欧拉回路 poj1386Play on Words poj2230求无向图欧拉图要求每条边走两遍且方向不同 poj2513字符串的欧拉图 poj2337字典序 poj1637Sightseeing tour求混合图欧拉回路 HDU 2894Poj1392 欧拉回路 几个定义 设G (V,E)是一个图。 1.欧拉回路 图G中经过 每条边一次 并且 仅一次 的回路称作欧拉回路。 2.欧拉路径 图G中经过每条边一次并且仅一次的路径称作欧拉路径。 3.欧拉图 存在欧拉回路的图称为欧拉图。 4.半欧拉图 存在欧拉路径但不存在欧拉回路的图称为半欧拉图。 性质与定理 二、性质与定理 在以下讨论中,假设图 G不存在孤立点(度为0);否则,先将所有孤立点从图中删除。 显然,这样做并不会影响图G中欧拉回路的存在性。 我们经常需要判定一个图是否为欧拉图(或半欧拉图),并且找出一条欧拉回路(或欧 拉路径)。对于无向图有如下结论: 定理1 无向图G为欧拉图,当且仅当G为连通图且所有顶点的度为偶数。 证明: 必要性。 设图G的一条欧拉回路为C。由于C经过图G的每一条边,而图G没 有孤立点,所以C也经过图G的每一个顶点,G为连通图成立

What is the advantage of using memset() in C

六月ゝ 毕业季﹏ 提交于 2019-11-28 23:16:53
问题 I was curious as to whether or not there was any advantage in regards to efficiency to utilizing memset() in a situation similar to the one below. Given the following buffer declarations... struct More_Buffer_Info { unsigned char a[10]; unsigned char b[10]; unsigned char c[10]; }; struct My_Buffer_Type { struct More_Buffer_Info buffer_info[100]; }; struct My_Buffer_Type my_buffer[5]; unsigned char *p; p = (unsigned char *)my_buffer; Besides having less lines of code, is there an advantage to

malloc vs memset

纵饮孤独 提交于 2019-11-28 21:51:07
malloc vs memset OS内存分配过程如下: 用户态程序使用malloc接口,分配虚拟地址。 用户程序访问该虚拟地址,比如memset。 硬件(MMU)需要将虚拟地址转换为物理地址。 硬件读取页表。 硬件发现相应的页表项不存在,硬件自动触发缺页异常。 硬件自动跳转到page fault的处理程序(内核实现注册好) 内核中的page fault处理程序执行,在其中分配物理内存,然后修改页表(创建页表项) 异常处理完毕,返回程序用户态,继续执行memset相应的操作。 至此,虚拟内存和物理内存都分配完成,并完成映射。 另一个角度看,如果malloc分配内存后,一直不使用,那就一直不会分配物理内存,这种内存分配策略叫延迟分配 来源: https://www.cnblogs.com/pugang/p/11428245.html

When zeroing a struct such as sockaddr_in, sockaddr_in6 and addrinfo before use, which is correct: memset, an initializer or either?

为君一笑 提交于 2019-11-28 21:15:21
Whenever I look at real code or example socket code in books, man pages and websites, I almost always see something like: struct sockaddr_in foo; memset(&foo, 0, sizeof foo); /* or bzero(), which POSIX marks as LEGACY, and is not in standard C */ foo.sin_port = htons(42); instead of: struct sockaddr_in foo = { 0 }; /* if at least one member is initialized, all others are set to zero (as though they had static storage duration) as per ISO/IEC 9899:1999 6.7.8 Initialization */ foo.sin_port = htons(42); or: struct sockaddr_in foo = { .sin_port = htons(42) }; /* New in C99 */ or: static struct