BestCoder Round #20小结
来源:互联网 发布:苹果5手机外壳淘宝 编辑:程序博客网 时间:2024/06/05 06:34
第一题:就是找到出现数目最多的,直接用数组记录即可
#include<cstdio>#include<cstring>int v[105];int main(){ int T; scanf("%d", &T); while(T--){ int n,x; memset(v, 0, sizeof(v)); scanf("%d", &n); for(int i = 0; i < n; i++){ scanf("%d", &x); v[x]++; } int index = -1, ma = -1; for(int i = 1; i <= n; i++){ if(v[i] > ma){ ma = v[i]; index = i; } } printf("%d\n", index); } return 0;}
第二题:经典问题,求区间最大的覆盖数,可以用线段树解,有个O(n)的算法,就是把左端点设为1,右端点+1 设为-1,然后可以求区间最大的前缀和。
线段树:
#include<cstdio>#include<cstring>#include<algorithm>using namespace std;const int N = 100009;int l[N],r[N];int a[N*2];struct Node{ int ma,c;}node[N*8];void push_down(int rt){ if(node[rt].c){ node[rt<<1].c += node[rt].c; node[rt<<1].ma += node[rt].c; node[rt<<1|1].c += node[rt].c; node[rt<<1|1].ma += node[rt].c; node[rt].c = 0; }}void push_up(int rt){ node[rt].ma = max(node[rt<<1].ma, node[rt<<1|1].ma);}void update(int rt, int l, int r, int L, int R){ if(L<=l && R>=r){ node[rt].c++; node[rt].ma++; }else{ push_down(rt); int mid = l+r>>1; if(L <= mid) update(rt<<1,l,mid,L,R); if(R > mid) update(rt<<1|1,mid+1,r,L,R); push_up(rt); }}int main(){ int T; scanf("%d", &T); while(T--){ int n; memset(node, 0, sizeof(node)); scanf("%d", &n); for(int i = 0; i < n; i++){ scanf("%d%d", &l[i], &r[i]); a[i] = l[i]; a[i+n] = r[i]; } sort(a, a+2*n); int m = unique(a, a+2*n) - a; for(int i = 0; i < n; i++){ int L = lower_bound(a,a+m,l[i])-a; int R = lower_bound(a,a+m,r[i])-a; update(1,0,m-1,L,R); } printf("%d\n", node[1].ma); } return 0;}
前缀和:
#include<cstdio>#include<cstring>int a[100009];int main(){ int T;scanf("%d", &T); while(T--){ int n,l,r, rr = 0; scanf("%d", &n); memset(a, 0, sizeof(a)); for(int i = 0; i < n; i++){ scanf("%d %d", &l, &r); a[l]++; a[r+1]--; if(r+1 > rr) rr = r+1; } int ma = 1; for(int i = 1; i <= rr; i++){ a[i] += a[i-1]; if(a[i] > ma) ma = a[i]; } printf("%d\n", ma); } return 0;}
第三题:比赛的时候过了小数据,最后的时候刷掉了,后来看了别人的方法学会了用BIT求LIS,这里就不贴出来了,看我的下一篇blog吧,
第四题:不会CDQ,学习ing。
0 0
- BestCoder Round #20小结
- BestCoder Round #80小结
- ACM-BestCoder Round #20
- BestCoder Round #20 A
- BestCoder Round #20
- BestCoder Round #20 1002
- BestCoder Round #20 B
- BestCoder Round #20 B,C
- hdu 5124 lines(Bestcoder Round #20)
- BestCoder Round #3 BestCoder Sequence
- bestcoder round #1
- BestCoder round #1
- BestCoder Round #1
- 【索引】BestCoder Round #2
- BestCoder Round #2
- 【索引】BestCoder Round #3
- BestCoder Round #3
- BestCoder Round #3 小记
- redhat系统下三种主要的软件包安装方法
- maven parent項目相關plugin
- 黑马程序员--------java面向对象 内部类、异常
- C语言练习——逆序的三位数
- Android中的android:layout_width和android:width
- BestCoder Round #20小结
- 新手玩阿里云ECS搭建tomcat7.0+JDK1.6服务器(J2EE小项目练习)
- Spring Framework源码(一):spring beans之BeanFactory
- Eclipse搭建hadoop开发环境
- 调试24L01经验总结
- 2015 v3版本正式发布ComponetOne
- ZOJ 3765 splay
- PHP 二维数组按某列进行排序
- Spiral Generator