getchar

模板,最后挣扎一下

∥☆過路亽.° 提交于 2019-12-04 13:50:39
我不想退役,一定要拿省一 奥利给 1. 堆 #include<bits/stdc++.h> using namespace std; #define rint register int int n; priority_queue< int, vector< int >, greater< int > > que; inline int read( void ){ int re = 0, f = 1; char ch = getchar(); while( ch > '9' || ch < '0' ){ if( ch == '-' ) f = -1; ch = getchar(); } while( ch >= '0' && ch <= '9' ){ re = re * 10 + ch - '0'; ch = getchar(); } return re * f; } int main( void ){ n = read(); for( rint i = 1; i <= n; i++ ){ int temp; temp = read(); if( temp == 1 ) que.push( read() ); if( temp == 2 ) printf( "%d\n", que.top() ); if( temp == 3 ) que.pop(); } return 0; }

0x40 数据结构进阶

孤人 提交于 2019-12-04 13:20:01
0x41 并查集 定义 在计算机科学中,并查集是一种树型的数据结构,用于处理一些不交集的合并及查询问题。有一个联合-查找算法定义了两个用于此数据结构的操作: \(Find\) :确定元素属于哪一个子集。它可以被用来确定两个元素是否属于同一子集。 \(Union\) :将两个子集合并成同一个集合。 由于支持这两种操作,一个不相交集也常被称为联合-查找数据结构或合并-查找集合。其他的重要方法, \(MakeSet\) ,用于创建单元素集合。有了这些方法,许多经典的划分问题可以被解决。 为了更加精确的定义这些方法,需要定义如何表示集合。一种常用的策略是为每个集合选定一个固定的元素,称为代表,以表示整个集合。接着, \(Find(x)\) 返回 \(x\) 所属集合的代表,而 $Union $使用两个集合的代表作为参数。 路径压缩与按秩合并 这是两个并查集常用的优化 当我们在寻找祖先时,一旦元素多且来,并查集就会退化成单次 \(O(n)\) 的算法,为了解决这一问题我们可以在寻找祖先的过程中直接将子节点连在祖先上,这样可以大大降低复杂度,均摊复杂度是 \(O(log(n))\) 的 按秩合并也是常见的优化方法,“秩”的定义很广泛,举个例子,在不路径压缩的情况下,常见的情况是把子树的深度定义为秩 无论如何定义通常情况是把“秩”储存在根节点,合并的过程中把秩小的根节点插到根大的根节点上

0x50 动态规划

a 夏天 提交于 2019-12-04 13:19:45
0x51 线性DP LIS 最长上升子序列,给定一个长度为 \(N​\) 序列 \(A​\) ,求出数值单调递增的子序列长度最长是多少 \(O(n^2)\) 做法 \(f[i]\) 表示以 \(i\) 结尾的最长上升子序列长度是多少 自然转移方程为 \(f[i]=max(f[j])+1,(1\le j < i,A[j]<A[i] )\) for( register int i = 1 ; i <= n ; i ++ ) { f[i] = 1; for( register int j = 1 ; j < i ; j ++) { if( a[j] >= a[i] ) continue; f[i] = max( f[i] , f[j] + 1 ); } } \(O(nlog_n)\) 做法 对于 \(O(n^2)\) 的做法,我们每次都枚举 假设我们已经求的一个最长上升子序列,我们要进行转移,如果对于每一位,在不改变性质的情况下,每一位越小,后面的位接上去的可能就越大,所以对于每一位如果大于末尾一位,就把他接在末尾,否则在不改变性质的情况下,把他插入的序列中 for( register int i = 1 ; i <= n ; i ++ ) { if( a[i] > f[ tot ] ) f[ ++ tot ] = a[i]; else *upper_bound( f + 1 , f +

0x60 图论

巧了我就是萌 提交于 2019-12-04 13:19:40
0x61 最短路 Dijkstra 最短路的经典做法之一,相比后面的 \(SPFA\) , \(Dij\) 的复杂度更低,更加的稳定 但是如果出现负环时, \(Dij\) 是不能用的 #include <bits/stdc++.h> #define PII pair< int , int > #define F first #define S second using namespace std; const int N = 10005 , M = 500005 , INF = 0x7f7f7f7f; int n , m , s ; int dis[N]; vector< PII > e[N]; bool vis[N]; set<PII> q; inline int read() { register int x = 0; register char ch = getchar(); while( ch < '0' || ch > '9' ) ch = getchar(); while( ch >= '0' && ch <= '9' ) { x = ( x << 3 ) + ( x << 1 ) + ch - '0'; ch = getchar(); } return x; } inline void add( int x , int y , int w ) { e[x].push

C语言l|博客园作业08

喜夏-厌秋 提交于 2019-12-04 12:13:50
这个作业属于哪个课程 C语言程序设计II 这个作业要求在哪里 链接 我在这个课程的目标是 掌握C语言以及熟练运用 这个作业在哪个具体方面帮助我实现目标 询问同学,百度,vs2019上的报错 参考文献 链接 1.1 题目名 7-5 爬动的蠕虫 一条蠕虫长1寸,在一口深为N寸的井的底部。已知蠕虫每1分钟可以向上爬U寸,但必须休息1分钟才能接着往上爬。在休息的过程中,蠕虫又下滑了D寸。 1.1.1数据处理 整型变量:N,U,D,sum(时间),l(距离) sum = l = 0; while (l<=N)//判断条件 { l += U;//上升的距离 sum++;//计数时间 if (l >= N)//判断距离是否大于井的总深 >break; l -= D;//下降的距离 sum++; } printf("%d", sum);//输出 1.1.2实验代码截图 1.1.3 造测试数据 输入数据 输出数据 说明 12 3 1 11 样例 15 4 2 13 说明,正确 19 17 6 3 说明,正确 1.1.4 PTA提交列表及说明 编译错误:之前在vs2019上写,复制过来忘记去掉_s.vs2019报错有未赋值sum,在下滑距离那里 的下面未计数时间sum。 1.2 题目2 7-3 jmu-c-二进制转10进制 输入一组二进制字符,输出其对应的十进制数。当输入回车键时,输入结束

数据结构总结

蹲街弑〆低调 提交于 2019-12-04 12:05:19
①并查集 #include<iostream> #include<cstring> #include<cstdio> using namespace std; #define e exit(0) #define re register const int M = 10005; int n,m,fa[M]; inline int fd(){ int s=1,t=0;char c=getchar(); while(c<'0'||c>'9'){if(c=='-')s=-1;c=getchar();} while(c>='0'&&c<='9'){t=t*10+c-'0';c=getchar();} return s*t; } int find(int x){ if(fa[x] == x) return x; else return fa[x] = find(fa[x]); } int main() { freopen("P3367.in","r",stdin); freopen("P3367.out","w",stdout); n = fd(),m = fd(); for(re int i=1;i<=n;++i) fa[i] = i; for(re int i=1;i<=m;++i){ int c = fd(),x = fd(),y = fd(); if(c == 1){ int fa1

Confused about getchar() function

蓝咒 提交于 2019-12-04 10:29:21
问题 I am confused about getchar() 's role in the following code. I mean I know it's helping me see the output window which will only be closed when I press the Enter key. So getchar() is basically waiting for me to press enter and then reads a single character. What is that single character this function is reading? I did not press any key from the keyboard for it to read. Now when it's not reading anything, why does it not give an error saying "hey, you didn't enter anything for me to read"?

单调队列专题题解

流过昼夜 提交于 2019-12-04 09:09:56
\(T1:[POI2010]PIL-Pilots\) 题意:给定 \(n,k\) 和一个长度为 \(n\) 的序列,求最长的最大值最小值相差不超过 \(k\) 的序列的长度. 分析:维护两个单调队列,一个维护最大值,一个维护最小值,每次更新前先检查两个队列的队头的下标是否相差在 \(k\) 以内,如果大于则令下标较小的队头出队,同时记录当前出队的下标最小值,用于后面更新答案. #include<iostream> #include<cstdio> #include<algorithm> #include<cstring> #include<cmath> #include<queue> #include<map> #include<set> #define ll long long using namespace std; inline int read(){ int x=0,o=1;char ch=getchar(); while(ch!='-'&&(ch<'0'||ch>'9'))ch=getchar(); if(ch=='-')o=-1,ch=getchar(); while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar(); return x*o; } const int N=1e6+5; int k,n,a[N],q1[N],q2[N]

树上背包问题做题总结

狂风中的少年 提交于 2019-12-04 08:13:40
背包(一般是 \(01\) 背包吧)与树形 \(DP\) 的结合,第一维通常是节点编号,第二维通常是背包体积.由子节点向父节点转移的时候,就是一个普通的背包问题. \(T1\) :选课/ \(The\) \(more\) , \(The\) \(Better\) 洛咕 HDU(多组数据) 题意:给定一个 \(n\) 个节点的森林,带点权,选 \(m\) 个点的最大点权和,要求选子节点必须先选父节点. 分析:新建一个虚根,森林变成树.套路地,设 \(f[i][j]\) 表示以 \(i\) 点为根的子树内选择 \(j\) 个点的最大点权和.先不考虑父节点必须选的话, \(f[u][j]=max(f[u][j],f[u][j-k]+f[v][k])\) ,最后再考虑要先选父节点, \(f[u][j]=f[u][j-1]+a[u]\) . #include<iostream> #include<cstdio> #include<algorithm> #include<cstring> #include<cmath> #include<queue> #include<map> #include<set> #define ll long long using namespace std; inline int read(){ int x=0,o=1;char ch=getchar();

for-loop and getchar() in C

你说的曾经没有我的故事 提交于 2019-12-04 07:11:14
问题 Why does the code get the empty data directly in even times? I have no idea what is going on. Thank you very much. #include <stdio.h> #pragma warning(disable : 4996) void main() { int f, a = 10, b = 20; for (int i = 0; i < 5; i++) { char ch; ch = getchar(); printf("ch = %c\n", ch); switch (ch) { case '+': f = a + b; printf("f = %d\n", f); break; case '−': f = a - b; printf("f = %d\n", f); break; case '*': f = a * b; printf("f = %d\n", f); break; case '/': f = a / b; printf("f = %d\n", f);