POJ 1804 Brainman(5种解法,好题,【暴力】,【归并排序】,【线段树单点更新】,【树状数组】,【平衡树】)
Brainman
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 10575 | Accepted: 5489 |
Description
Raymond Babbitt drives his brother Charlie mad. Recently Raymond counted 246 toothpicks spilled all over the floor in an instant just by glancing at them. And he can even count Poker cards. Charlie would love to be able to do cool things like that, too. He wants to beat his brother in a similar task.
Problem
Here's what Charlie thinks of. Imagine you get a sequence of N numbers. The goal is to move the numbers around so that at the end the sequence is ordered. The only operation allowed is to swap two adjacent numbers. Let us try an example:Start with: 2 8 0 3
swap (2 8) 8 2 0 3
swap (2 0) 8 0 2 3
swap (2 3) 8 0 3 2
swap (8 0) 0 8 3 2
swap (8 3) 0 3 8 2
swap (8 2) 0 3 2 8
swap (3 2) 0 2 3 8
swap (3 8) 0 2 8 3
swap (8 3) 0 2 3 8So the sequence (2 8 0 3) can be sorted with nine swaps of adjacent numbers. However, it is even possible to sort it with three such swaps:Start with: 2 8 0 3
swap (8 0) 2 0 8 3
swap (2 0) 0 2 8 3
swap (8 3) 0 2 3 8The question is: What is the minimum number of swaps of adjacent numbers to sort a given sequence?Since Charlie does not have Raymond's mental capabilities, he decides to cheat. Here is where you come into play. He asks you to write a computer program for him that answers the question. Rest assured he will pay a very good prize for it.
Input
For every scenario, you are given a line containing first the length N (1 <= N <= 1000) of the sequence,followed by the N elements of the sequence (each element is an integer in [-1000000, 1000000]). All numbers in this line are separated by single blanks.
Output
Sample Input
- 4
- 4 2 8 0 3
- 10 0 1 2 3 4 5 6 7 8 9
- 6 -42 23 6 28 -100 65537
- 5 0 0 0 0 0
Sample Output
- Scenario #1:
- 3
- Scenario #2:
- 0
- Scenario #3:
- 5
- Scenario #4:
- 0
Source
- #include <iostream>
- #include <stdio.h>
- using namespace std;
- const int N=;
- int a[N],b[N];
- int main()
- {
- int n;
- scanf("%d",&n);
- for(int k=;k<=n;k++)
- {
- int m;
- scanf("%d",&m);
- for(int i=;i<=m;i++)
- scanf("%d",&a[i]);
- int ans=;
- for(int i=;i<=m;i++)
- for(int j=i+;j<=m;j++)
- if(a[i]>a[j])
- ans++;
- printf("Scenario #%d:\n%d\n\n",k,ans);
- }
- return ;
- }
第二种归并排序, 对2个已经排好序的数列,进行再排序,只需要把2个数列,从头到尾,按顺序,谁小,谁就先进入tmp数组, 最后tmp数组一定排好序了,然后把TMP数组的元素复制回原数组中即可。
同理,如果我们知道2个子序列的逆序对数量,是否可以通过归并排序一样,求出整体的数量呢?显然是可以的。
这里有一个地方,当左边的数列的a[k]要进tmp数组了, 这个时候,如果右边的指针指向a+mid+p,就说明a[k]比a[mid+1]...a[mid + 2]..a[mid+3].....a[mid+p]都要大!【重要】
也就是说,对于a[k]而言,整个数列中, mid+ mid+2...mid+p都在k后面,同时a[k]比a[mid+1],a[mid+2]...a[mid+p]都要大。 那么显然是增加逆序对数量的。 通过整个方法,计算出整个逆序对的数量即可。
下面给出AC代码:
- #include <iostream>
- #include <cstdio>
- #include <cstdlib>
- using namespace std;
- const int max_n = + ;
- int n, a[max_n];
- int tmp[max_n], ans;
- void merge(int *a, int *tmp, int l, int mid, int r)
- {
- if (l >= r) return;
- int i = l, j = mid + , k = ;
- int count = , flag = ;
- while (i <= mid && j <= r)
- {
- if (a[i] <= a[j])
- {
- tmp[k ++] = a[i++];
- ans += j - mid - ;
- }else tmp[k ++ ] = a[j++];
- }
- while (i <= mid) tmp[k ++] = a[i++], ans += r- mid;
- while (j <= r) tmp[k ++] = a[j++];
- for (i = ; i != k; ++ i) a[l + i] = tmp[i];
- }
- void mergesort(int *a, int *tmp, int l, int r)
- {
- if (l >= r) return;
- int mid = (l + r) / ;
- mergesort(a, tmp, l, mid);
- mergesort(a, tmp , mid + , r);
- merge(a, tmp, l, mid, r);
- }
- int main()
- {
- int tt;
- scanf("%d", &tt);
- for (int i = ; i <= tt; ++ i)
- {
- cout<<"Scenario #"<<i<<":"<<endl;
- scanf("%d", &n);
- ans = ;
- for (int i = ; i != n; ++ i) scanf("%d", &a[i]);
- mergesort(a, tmp, , n - );
- cout<<ans<<endl<<endl;
- }
- }
第三种线段树单点更新
- #include <map>
- #include <iostream>
- #include <set>
- #include <cstdio>
- #include <cstdlib>
- using namespace std;
- const int max_n = + ;
- int n;
- int a[max_n], count;
- map<int, int>G;
- map<int, int>::iterator it;
- struct node
- {
- int cd, key;
- int ls, rs;
- int L, R;
- node():L(),R(),ls(),rs(),cd(),key(){};
- void clear()
- {
- cd = key = ;
- }
- }t[max_n * ];
- int tail = ;
- void init()
- {
- for (int i = ; i != max_n * ; ++ i) t[i].clear();
- G.clear();
- scanf("%d", &n);
- for (int i = ; i != n; ++ i)
- {
- scanf("%d", &a[i]);
- G[a[i]] = ;
- }
- count = ;
- for (it = G.begin(); it != G.end(); ++ it) it -> second = ++ count;
- }
- void make_tree(int now, int LL, int RR)
- {
- t[now].L = LL;
- t[now].R = RR;
- if (LL == RR) return;
- int mid = (LL + RR)/ ;
- make_tree(t[now].ls = ++ tail, LL, mid);
- make_tree(t[now].rs = ++ tail, mid + , RR);
- }
- void tran(int now)
- {
- int left_son = t[now].ls, right_son = t[now].rs;
- t[left_son].cd += t[now].cd;
- t[right_son].cd += t[now].cd;
- t[now].key += t[now].cd;
- t[now].cd = ;
- }
- void ins(int now, int LL, int RR)
- {
- tran(now);
- if (t[now].L == LL && t[now].R == RR)
- {
- t[now].cd ++;
- return;
- }
- t[now].key ++;
- int mid = (t[now].L + t[now].R) / ;
- if (RR <= mid) {ins(t[now].ls, LL, RR); return;}
- if (mid < LL) {ins(t[now].rs, LL, RR); return;}
- ins(t[now].ls, LL, mid);
- ins(t[now].rs, mid + , RR);
- }
- int find(int now, int LL, int RR)//因为题目的特殊性,只会找一个……
- {
- tran(now);
- if (t[now].L == LL && t[now].R == RR) return t[now].key;
- int mid = (t[now].L + t[now].R) / ;
- if (RR <= mid) return find(t[now].ls, LL, RR);
- if (mid < LL) return find(t[now].rs, LL, RR);
- cout<<"wtf?"<<endl;
- }
- void doit()
- {
- int ans=;
- for (int i = ; i != n; ++ i)
- {
- int num = G[a[i]];
- ans += find(, num + , num + );
- ins(, , num);
- }
- cout<<ans<<endl;
- }
- int main()
- {
- int tt;
- scanf("%d",&tt);
- make_tree(, , );
- for (int i = ; i <= tt; ++ i)
- {
- cout<<"Scenario #"<<i<<":"<<endl;
- init();
- doit();
- cout<<endl;
- }
- }
另外还有几种好办法,贴一下
第四种:树状数组
树状数组, 其实和线段树道理一样。 但是对于树状数组,我会单独开一张好好研究哒。 这里就贴一下速度,并没有比线段树快很多……也许我的写法不好?【如果对树状数组有疑惑,可以看我下一篇文章,我会带领你们好好学会树状数组这个神奇的东西~】
- #include <cstdio>
- #include <cstdlib>
- #include <map>
- #include <cstring>
- using namespace std;
- #define lowbit(k) ((k)&(-k))
- const int max_n = + ;
- int n, a[max_n], s[max_n];
- map<int, int>G;
- map<int, int>::iterator it;
- int count;
- void init()
- {
- scanf("%d", &n);
- G.clear();
- count = ;
- memset(s, , sizeof(s));
- for (int i = ; i != n; ++ i)
- {
- scanf("%d", &a[i]);
- G[a[i]] = ;
- }
- for (it = G.begin(); it != G.end(); ++ it) it -> second = ++ count;
- }
- void ins(int k)
- {
- s[k] += ;
- while ((k += lowbit(k)) <= ) s[k] += ;
- }
- int ask(int k)//1..k的和
- {
- int tot = s[k];
- while (k -= lowbit(k)) tot += s[k];
- return tot;
- }
- void doit()
- {
- int ans = ;
- for (int i = ; i != n; ++ i)
- {
- int num = G[a[i]];
- ans += ask(count) - ask(num);
- ins(num);
- }
- printf("%d\n",ans);
- }
- int main()
- {
- int tt;
- scanf("%d", &tt);
- for (int i = ; i <= tt; ++ i)
- {
- printf("Scenario #%d:\n",i);
- init();
- doit();
- printf("\n");
- }
- }
第五种:平衡树
只要查找,当前在树中,有多少个数字比a[k]要大, 因为是按顺序插入的,所以这个数字的数量就是逆序对的个数
这里有一个小技巧,如果平衡树每次要删的话很麻烦,直接用写成struct,然后新的树就new,最后delete掉即可~
- #include <iostream>
- #include <cstdio>
- #include <cstdlib>
- using namespace std;
- const int max_n = + ;
- int n;
- const int maxint = 0x7fffffff;
- struct node
- {
- node *c[];
- int key;
- int size;
- node():key(),size()
- {
- c[] = c[] = this;
- }
- node(int KEY_, node *a0, node *a1):
- key(KEY_){c[] =a0, c[]=a1;}
- node* rz(){return size = c[]->size + c[]->size + , this;}
- }Tnull, *null=&Tnull;
- struct splay
- {
- node *root;
- splay()
- {
- root = (new node(*null)) -> rz();
- root -> key = maxint;
- }
- void zig(int d)
- {
- node *t = root -> c[d];
- root -> c[d] = null -> c[d];
- null -> c[d] = root;
- root = t;
- }
- void zigzig(int d)
- {
- node *t = root -> c[d] -> c[d];
- root -> c[d] -> c[d] = null -> c[d];
- null -> c[d] = root -> c[d];
- root -> c[d] = null -> c[d] -> c[!d];
- null -> c[d] -> c[!d] = root -> rz();
- root = t;
- }
- void finish(int d)
- {
- node *t = null -> c[d], *p = root -> c[!d];
- while (t != null)
- {
- t = null -> c[d] -> c[d];
- null -> c[d] -> c[d] = p;
- p = null -> c[d] -> rz();
- null -> c[d] = t;
- }
- root -> c[!d] = p;
- }
- void select(int k)//谁有k个儿子
- {
- int t;
- while ()
- {
- bool d = k > (t = root -> c[] -> size);
- if (k == t || root -> c[d] == null) break;
- if (d) k -= t + ;
- bool dd = k > (t = root -> c[d] -> c[] -> size);
- if (k == t || root -> c[d] -> c[dd] == null){zig(d); break;}
- if (dd) k -= t + ;
- d != dd ? zig(d), zig(dd) : zigzig(d);
- }
- finish(), finish();
- root -> rz();
- }
- void search(int x)
- {
- while ()
- {
- bool d = x > root -> key;
- if (root -> c[d] == null) break;
- bool dd = x > root -> c[d] -> key;
- if (root -> c[d] -> c[dd] == null){zig(d); break;}
- d != dd ? zig(d), zig(dd) : zigzig(dd);
- }
- finish(), finish();
- root -> rz();
- if (x > root -> key) select(root -> c[] -> size + );
- }
- void ins(int x)
- {
- search(x);
- node *oldroot = root;
- root = new node(x, oldroot -> c[],oldroot);
- oldroot -> c[] = null;
- oldroot -> rz();
- root -> rz();
- }
- int sel(int k){return select(k - ), root -> key;}
- int ran(int x){return search(x), root -> c[] -> size + ;}
- }*sp;
- int main()
- {
- int tt;
- scanf("%d", &tt);
- for (int i = ; i <= tt; ++ i)
- {
- sp = new splay;
- cout<<"Scenario #"<<i<<":"<<endl;
- scanf("%d", &n);
- int ans = ;
- int tmp;
- for (int i = ; i != n; ++ i)
- {
- scanf("%d", &tmp);
- tmp = - tmp;
- ans += sp -> ran(tmp) - ;
- //cout<<sp.ran(tmp) - 1<<endl;
- sp -> ins(tmp);
- }
- delete sp;
- cout<<ans<<endl<<endl;
- }
- }
POJ 1804 Brainman(5种解法,好题,【暴力】,【归并排序】,【线段树单点更新】,【树状数组】,【平衡树】)的更多相关文章
- poj 2892---Tunnel Warfare(线段树单点更新、区间合并)
题目链接 Description During the War of Resistance Against Japan, tunnel warfare was carried out extensiv ...
- HDU 1166 敌兵布阵(线段树单点更新,板子题)
敌兵布阵 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submi ...
- POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和)
POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和) 题意分析 卡卡屋前有一株苹果树,每年秋天,树上长了许多苹果.卡卡很喜欢苹果.树上有N个节点,卡卡给他们编号1到N,根 ...
- POJ.2299 Ultra-QuickSort (线段树 单点更新 区间求和 逆序对 离散化)
POJ.2299 Ultra-QuickSort (线段树 单点更新 区间求和 逆序对 离散化) 题意分析 前置技能 线段树求逆序对 离散化 线段树求逆序对已经说过了,具体方法请看这里 离散化 有些数 ...
- CDOJ 1073 线段树 单点更新+区间查询 水题
H - 秋实大哥与线段树 Time Limit:1000MS Memory Limit:65535KB 64bit IO Format:%lld & %llu Submit S ...
- POJ 1804 Brainman
Brainman Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 7787 Accepted: 4247 Descript ...
- POJ 1804 Brainman(归并排序)
传送门 Description Background Raymond Babbitt drives his brother Charlie mad. Recently Raymond counted ...
- POJ 2892 Tunnel Warfare(线段树单点更新区间合并)
Tunnel Warfare Time Limit: 1000MS Memory Limit: 131072K Total Submissions: 7876 Accepted: 3259 D ...
- poj 2828(线段树单点更新)
Buy Tickets Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 18561 Accepted: 9209 Desc ...
随机推荐
- IX-Protected Dataplane Operating System解读
一.概述 商业操作系统在应用程序每秒钟需要数百万次操作时才能保持高吞吐量和低(尾)延迟,对于最慢的请求只需几百微秒.通常认为对于高性能网络(小信息的高包率.低延迟)的构建,最好都是在内核之外构建用户态 ...
- H5之前端操作文件
js是否能够操作文件? js在HTML5以前浏览器端是无法操作文件的,但HTML5中给a标签增加了一个download属性,只要有这个属性,点击这个链接时浏览器就不在打开链接指向的文件,而是改为下载( ...
- iOS NSString 文本不同的颜色 标题+文本字体大小 行间距/删除不需要的字符 /以及自适应高度
#import <Foundation/Foundation.h> @interface TextsForRow : NSObject @property(nonatomic,copy)N ...
- Kendo UI使用笔记
1.Grid中的列字段绑定模板字段方法参数传值字符串加双引号: 上图就是个典型的例子,openSendWin方法里Id,EmergencyTitle,EmergencyDetail 三个参数,后两个参 ...
- jquery获取焦点和失去焦点事件代码
input失去焦点和获得焦点 鼠标在搜索框中点击的时候里面的文字就消失了. 我们在做网站的时候经常会用到搜索框的获得焦点和失去焦点的事件,因为懒,每次都去写非常的烦,于是就一劳永逸,遇到类似情况就来调 ...
- css scroll bug
滚动区域不能设置overflow var doc = $(document), win = $(window), h = $("#head"), b = $("#body ...
- tomcat 设置jvm 参数
在catalina.bat中设置 正确的做法是设置成这样set JAVA_OPTS=%JAVA_OPTS% -Xms256m -Xmx256m,避免JAVA_OPTS参数覆盖
- K:常见的正则表达式
@装载自:http://zxin.cnblogs.com/ 平时对字符串进行校验和处理的时候难免会用到正则表达式,通常采用的方式是去网上寻找相关的正则表达式,之后copy下来进行修改,以使其满足自己的 ...
- 深入理解cookie和session
cookie和session在java web开发中扮演了十分重要的作用,本篇文章对其中的重要知识点做一些探究和总结. 1.cookie存在于浏览器 随意打开一个网址,用火狐的调试工具,随意选取一个链 ...
- Linux中创建新用户并赋给指定文件权限
工作中用到了,写篇日志总结一下. 创建新的用户: 第一种方式: 创建用户: adduser name 创建密码: passwd name(回车后出现修改密码的提示) 该方式创建的用户目录默认在home ...