# 冒泡排序深入理解

## 1.P4378 Out of Sorts S

sorted = false
while (not sorted):
sorted = true
moo
for i = 0 to N-2:
if A[i+1] < A[i]:
swap A[i], A[i+1]
sorted = false


### 对于一个序列, 我们称之为有序的, 当且仅当对于任意一个位置前面没有比它大的数(可以模拟一下)

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N = 100500;
int d[N], n;
int x = 0;
char c = getchar();
while (!isdigit(c)) c = getchar();
while (isdigit(c)){
x = (x << 3) + (x << 1) + c - '0';
c = getchar();
}
return x;
}
struct node{
int val, pos;
bool operator < (const node &i) const{
if (val == i.val) return pos < i.pos;
return val < i.val;
}
}p[N];
inline int low(int x) {
return x & -x;
}
int get(int x) {
int tmp = 0;
for (;x;x -= low(x)) tmp += d[x];
return tmp;
}
for (;x <= n; x += low(x)) d[x]++;
}
bool cmp(node i,node j) {
return i.pos < j.pos;
}
int main() {
for (int i = 1;i <= n; i++) p[i] = (node){read(),i};
sort(p + 1,p + n + 1);
for (int i = 1;i <= n; i++) p[i].val = i;
sort(p + 1,p + n + 1, cmp);
int ans = 0;
for (int i = 1;i <= n; i++) {
ans = max(ans, i - get(p[i].val));
}
printf ("%d\n", ans+1);
return 0;
}


## 2.P4375 Out of Sorts G

sorted = false
while (not sorted):
sorted = true
moo
for i = 0 to N-2:
if A[i+1] < A[i]:
swap A[i], A[i+1]
for i = N-2 downto 0:
if A[i+1] < A[i]:
swap A[i], A[i+1]
for i = 0 to N-2:
if A[i+1] < A[i]:
sorted = false


### 第二波操作时, 又会有一个小于等于x的数插回来

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N = 100500;
int d[N], n;
int x = 0;
char c = getchar();
while (!isdigit(c)) c = getchar();
while (isdigit(c)){
x = (x << 3) + (x << 1) + c - '0';
c = getchar();
}
return x;
}
struct node{
int val, pos;
bool operator < (const node &i) const{
if (val == i.val) return pos < i.pos;
return val < i.val;
}
}p[N];
inline int low(int x) {
return x & -x;
}
int get(int x) {
int tmp = 0;
for (;x;x -= low(x)) tmp += d[x];
return tmp;
}
for (;x <= n; x += low(x)) d[x]++;
}
bool cmp(node i,node j) {
return i.pos < j.pos;
}
int main() {
for (int i = 1;i <= n; i++) p[i] = (node){read(),i};
sort(p + 1,p + n + 1);
for (int i = 1;i <= n; i++) p[i].val = i;
sort(p + 1,p + n + 1, cmp);
int ans = 1;
for (int i = 1;i <= n; i++) {
ans = max(ans, i - get(i));
}
printf ("%d\n", ans);
return 0;
}
/*
6
2 5 6 3 1 4

*/


## 3.P4372 Out of Sorts P

bubble_sort_pass (A) {
for i = 0 to length(A)-2
if A[i] > A[i+1], swap A[i] and A[i+1]
}


quickish_sort (A) {
if length(A) = 1, return
do { // Main loop
work_counter = work_counter + length(A)
bubble_sort_pass(A)
} while (no partition points exist in A)
divide A at all partition points; recursively quickish_sort each piece
}


Bessie好奇于她的代码能够运行得多快。简单起见，她计算出她得主循环的每一轮都消耗线性时间，所以她相应增加一个全局变量work_counter的值，以此来跟踪整个算法总共完成的工作量。

### 怎么求(敲重点):

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
const int N = 100500;
int d[N], n;
int x = 0;
char c = getchar();
while (!isdigit(c)) c = getchar();
while (isdigit(c)){
x = (x << 3) + (x << 1) + c - '0';
c = getchar();
}
return x;
}
struct node{
int val, pos;
bool operator < (const node &i) const{
if (val == i.val) return pos < i.pos;
return val < i.val;
}
}p[N];
bool cmp(node i,node j) {
return i.pos < j.pos;
}
int t[N], k;
int main() {
//	freopen("hs.in","r",stdin);
for (int i = 1;i <= n; i++) p[i] = (node){read(),i};
sort(p + 1,p + n + 1);
for (int i = 1;i <= n; i++) p[i].val = i;
sort(p + 1,p + n + 1, cmp);
long long ans = 0;
k = n;
for (int i = n;i >= 1; i--) {
while (p[k].val > i) k--;
t[i] = max(p[k].pos - i, 1);
}
for (int i = 0;i < n; i++) ans += max(t[i], t[i+1]);
printf ("%lld\n", ans);
return 0;
}
/*
6
2 5 6 3 1 4

*/


## 4.T99343 奇怪的排序

​ 证明: $a_1...a_ia_j...a_q...a_n$ 不断将$a_j$与它右边的数字交换直至正好换到$a_q$$a_1...a_ja_i...a_n$ 此时共交换了q - j 次

​ 再将$a_i$向右与相邻数字交换q-1-i次到$a_j$左侧 ,此时共交换2 * (q - j) 次,为偶数次,所以奇偶性不变

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
const int M = 500005;
using namespace std;
int n, sum[M];
struct Num{
int val,num;
inline friend bool operator < (Num a,Num b){
return a.val > b.val;
}
}p[M];
inline int lowbit(int x){
return x&-x;
}
while(k<=n){
sum[k]+=x;
k+=lowbit(k);
}
}
int getsum(int k){
int tmp=0;
while(k>0){
tmp+=sum[k];
k-=lowbit(k);
}
return tmp;
}
long long Ans=0;
char ss[1<<17],*A=ss,*B=ss;
inline char gc()
cin >> x ;
}
int main(){
int t;
while(t--) {
Ans = 0;
memset(sum, 0, sizeof(sum));
for(int i=1;i<=n;i++){
p[i].num=i;
}
sort(p+1,p+n+1);
for(int i=1;i<=n;i++){
Ans+=getsum(p[i].num-1);
}
//    	printf ("%lld\n", Ans);
if (n % 4 > 1)
printf("Yes\n");
else if (Ans % 2 == 1)
printf("No\n");
else
printf("Yes\n");
}
return 0;
}

posted @ 2019-10-05 23:11  Hs-black  阅读(426)  评论(1编辑  收藏