UESTC 数据结构专题训练 D,E,F
2017-06-08 15:29
417 查看
D,http://acm.uestc.edu.cn/#/problem/show/1584
题意:平面上n个点,询问每个点左下方的点有多少个?
解法:排序(以Y坐标为第一关键字,X坐标为第二关键字)+树状数组
E,http://acm.uestc.edu.cn/#/problem/show/1583
题意:一个排列,用最少的交换次数次数变换到另一个排列,每次只能交换相邻两个元素。
解法:处理一个数组d,d[i]表示a[i]应该到b数组哪个位置。答案就是d数组的逆序对数
F,http://acm.uestc.edu.cn/#/problem/show/1582
题意:给你n个元素的数组{a},m次询问,每次给你一个数X,每次问你max{a1 xor X, a2 xor X, …, an xor X}
解法:对{a}里每个元素的二进制串从高位到低位建立trie(零一字典树)
询问时贪心(显然,让更高位的二进制位为1是要更优的)地向下,如果x该位是1,那么走0,如果是0,那么走1。
如果该位仅有1或0,那没办法,只能往这里走。
题意:平面上n个点,询问每个点左下方的点有多少个?
解法:排序(以Y坐标为第一关键字,X坐标为第二关键字)+树状数组
#include <bits/stdc++.h> using namespace std; const int maxn=100010; int n,rnk[maxn],c[maxn]; struct node{ int x,y; }a[maxn]; bool cmp(node x, node y){ if(x.y==y.y) return x.x<y.x; return x.y<y.y; } inline int lowbit(int x){ return x&-x; } void add(int x){ while(x<maxn){ c[x]++; x+=lowbit(x); } } int sum(int x){ int ans=0; while(x>0){ ans+=c[x]; x-=lowbit(x); } return ans; } int main() { int n; while(~scanf("%d",&n)){ for(int i=1; i<=n; i++) scanf("%d %d", &a[i].x,&a[i].y); sort(a+1,a+n+1,cmp); memset(rnk,0,sizeof(rnk)); memset(c,0,sizeof(c)); for(int i=1; i<=n; i++){ rnk[sum(a[i].x)]++; add(a[i].x); } for(int i=1; i<n; i++) printf("%d\n", rnk[i-1]); printf("%d\n", rnk[n-1]); } return 0; }
E,http://acm.uestc.edu.cn/#/problem/show/1583
题意:一个排列,用最少的交换次数次数变换到另一个排列,每次只能交换相邻两个元素。
解法:处理一个数组d,d[i]表示a[i]应该到b数组哪个位置。答案就是d数组的逆序对数
#include <bits/stdc++.h> using namespace std; const int maxn=1e5+10; typedef long long LL; int n,a[maxn],c[maxn]; void add(int x){ while(x<maxn){ c[x]++; x+=x&-x; } } int sum(int x){ int ans=0; while(x>0){ ans+=c[x]; x-=x&-x; } return ans; } int main() { while(~scanf("%d",&n)){ memset(c,0,sizeof(c)); memset(a,0,sizeof(a)); LL ans=0; for(int i=1; i<=n; i++){ int x; scanf("%d", &x); a[x]=i; } for(int i=1; i<=n; i++){ int x; scanf("%d", &x); ans+=1LL*(sum(n)-sum(a[x]-1)); add(a[x]); } printf("%lld\n", ans); } return 0; }
F,http://acm.uestc.edu.cn/#/problem/show/1582
题意:给你n个元素的数组{a},m次询问,每次给你一个数X,每次问你max{a1 xor X, a2 xor X, …, an xor X}
解法:对{a}里每个元素的二进制串从高位到低位建立trie(零一字典树)
询问时贪心(显然,让更高位的二进制位为1是要更优的)地向下,如果x该位是1,那么走0,如果是0,那么走1。
如果该位仅有1或0,那没办法,只能往这里走。
#include <bits/stdc++.h> using namespace std; const int maxn = 1e7+2; struct node{ int root,tot,ch[maxn][2]; int newnode(){ ch[tot][0]=ch[tot][1]=-1; return tot++; } void init(){ tot=0,root=newnode(); } void Insert(int x){ int now=root; for(int j=30; j>=0; j--){ int go=(x&(1<<j))?1:0; if(ch[now][go]==-1){ ch[now][go]=newnode(); now=ch[now][go]; } else{ now=ch[now][go]; } } } int query(int x){ int now=root; int ans=0; for(int j=30; j>=0; j--){ int go=(x&(1<<j))?1:0; if(ch[now][!go]!=-1) ans|=(1<<j),now=ch[now][!go]; else now=ch[now][go]; } return ans; } }trie; int main() { int n, q; while(~scanf("%d",&n)){ trie.init(); for(int i=1; i<=n; i++){ int x; scanf("%d",&x); trie.Insert(x); } scanf("%d",&q); while(q--){ int x; scanf("%d", &x); printf("%d\n", trie.query(x)); } } return 0; }
相关文章推荐
- UESTC 数据结构专题训练 K,L,M
- 【线段树+离散化】Uestc-数据结构专题训练【B】
- 【裸线段树】Uestc-数据结构专题训练【A】
- UESTC数据结构专题训练 G,H,I,J
- UESTC 数据结构专题训练 A,B,C
- 2015 UESTC 数据结构专题C题 秋实大哥与快餐店 字典树
- UESTC专题训练
- 2015 UESTC 数据结构专题D题 秋实大哥与战争 变化版本的线段树,合并区间,单点查询
- 数据结构专题训练及总结
- 2015 UESTC 数据结构专题D题 秋实大哥与战争 SET的妙用
- 暑假训练4-数据结构专题
- 2015 UESTC 数据结构专题E题 秋实大哥与家 线段树扫描线求矩形面积交
- 【暑假专题训练#数据结构】
- 2015 UESTC 数据结构专题G题 秋实大哥去打工 单调栈
- 数据结构专题训练一 C Bits
- 2015 UESTC 数据结构专题A题 秋实大哥与小朋友 线段树 区间更新,单点查询,离散化
- 2015 UESTC 数据结构专题H题 秋实大哥打游戏 带权并查集
- 2014 UESTC暑前集训数据结构专题解题报告
- 2015 UESTC 数据结构专题B题 秋实大哥与花 线段树 区间加,区间查询和
- 2015 UESTC 数据结构专题N题 秋实大哥搞算数 表达式求值/栈