Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

day6贺达 #871

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions winter-day6/贺达/7-1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#include<bits/stdc++.h>
using namespace std;

void insertionSort(int arr[], int n)
{
int value;
for (int i=1; i<n; i++)
{
value = arr[i];

for(int j=0; j<i; j++)
{
if( value < arr[j])
{
for (int k=i; k>=j; k--)
{
arr[k]=arr[k-1];
}
arr[j]=value;
break;
}
}
for(int i=0;i<n;i++)
{
cout<<arr[i];
if(i!=n-1)
{
cout<<" ";
}
}
cout<<endl;
}
return;
}

int main()
{
int n;
while(cin>>n)
{
int a[n];
for(int i=0;i<n;i++)
{
cin>>a[i];
}
insertionSort(a,n);

}
}
62 changes: 62 additions & 0 deletions winter-day6/贺达/7-2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#include<bits/stdc++.h>
using namespace std;
int n;
struct student
{
string name;
int score;
};

student s[25];

void bubblesort()
{
int i,j;
int ts;
string tn;
for(i=0;i<n-1;i++)
{
for(j=0;j<n-1-i;j++)
{
if(s[j].score>s[j+1].score)//前比后大就交换
{
tn=s[j].name;
s[j].name=s[j+1].name;
s[j+1].name=tn;

ts=s[j].score;
s[j].score=s[j+1].score;
s[j+1].score=ts;
}
else if(s[j].score==s[j+1].score)//分数相同则看名字
{
if(s[j].name<s[j+1].name)
{
tn=s[j].name;
s[j].name=s[j+1].name;
s[j+1].name=tn;

ts=s[j].score;
s[j].score=s[j+1].score;
s[j+1].score=ts;
}
}
}
}
}

int main()
{

cin>>n;
for(int i=0;i<n;i++)
{
cin>>s[i].name>>s[i].score;
}
bubblesort();
for(int i=n-1;i>=0;i--)
{
cout<<s[i].name<<" "<<s[i].score<<endl;
}
return 0;
}
23 changes: 23 additions & 0 deletions winter-day6/贺达/7-3.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include<bits/stdc++.h>
using namespace std;

int main()
{
int N, key;
cin >> N;
map<int, int> M;
int i;
for(i = 0; i < N; i++)
{
cin >> key;//工龄
M[key]++;//对应工龄的人数加一
}

map<int, int>::iterator it;//迭代器
for(it = M.begin(); it != M.end(); it++)//it->为键值,it->second为数值
{
cout << it->first << ":" << it->second << endl;
}
return 0;

}
45 changes: 45 additions & 0 deletions winter-day6/贺达/7-4.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
const int N = 5e5 + 10;
typedef long long ll;

#define lowbit(x) (x & -x) //取一个二进制最低位的一与后边的0组成的数
ll c[N],a[N],n;
vector<ll> b;
//lower_bound( begin,end,num):从数组的begin位置到end-1位置二分查找第一个大于或等于num的数字,找到返回该数字的地址,不存在则返回end。通过返回的地址减去起始地址begin,得到找到数字在数组中的下标。
int fin(int x)
{
return lower_bound(b.begin(),b.end(),x)-b.begin();//返回下标
}

void update(int x,int v)
{
for(int i=x;i<=n;i+=lowbit(i)) c[i] += v;
}

int ask(int x)
{
int res = 0;
for(int i=x;i;i-=lowbit(i)) res += c[i];
return res;
}

int main()
{
cin>>n;
for(int i=1;i<=n;i++) cin>>a[i],b.push_back(a[i]);
sort(b.begin(),b.end());
b.erase(unique(b.begin(),b.end()),b.end());//去重
for(int i=1;i<=n;i++) a[i] = fin(a[i])+1;
ll ans = 0;
//每插入一个统计比它小的数量
for(int i=n;i>=1;i--)
{
ans += ask(a[i] - 1);
update(a[i],1);
}
cout<<ans;
return 0;
}
59 changes: 59 additions & 0 deletions winter-day6/贺达/7-5.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#include <bits/stdc++.h>

using namespace std;

const int N = 100001;

int h[N];
int n, m, len;

void down(int x)
{
if(x * 2 > len)
{
return;
}
int u = x;
if(h[u] < h[x * 2] && x * 2 <= len)
{
u = x * 2;
}
if(h[u] < h[x * 2 + 1] && x * 2 + 1 <= len)
{
u = x * 2 + 1;
}
if(u != x)
{
swap(h[x], h[u]);
down(u);
}

}

int main()
{
cin >> n;
for(int i = 1; i <= n; i++)
{
cin >> h[i];
}

len = n;
for(int i = n / 2; i > 0; i--)
{
down(i);
}
int r = n;
while(r -- )
{
for(int i = 1; i <= n; i ++)
{
cout<<h[i]<<" ";
}
cout << endl;
swap(h[1], h[len]);
len--;
down(1);
}
return 0;
}
58 changes: 58 additions & 0 deletions winter-day6/贺达/7-6.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#include<bits/stdc++.h>
using namespace std;
int n;
int a[1010];
void show();
void quicksort(int low,int high)
{
if(low>=high) return;
int i=low;
int j=high;
int mark = a[i];//以第一个数为基准
while(i<j) //终止条件
{
while(i<j && a[j]>mark)//先移动high指针,找到比标准更小的数
{
j--;
}
while(i<j && a[i] <= mark)//再移动low指针,直到找到比基准大的位置
{
i++;
}
if(i<j) swap(a[i],a[j]);
}
swap(a[low],a[i]);
int flag = 1; //避免重复无效的排序
for(int k=0;k<n;k++)
{
cout<<a[k]<<" ";
if(k!=0&&a[k-1]>a[k])
{
flag = 0;
}
}
cout<<endl;
if(flag)
{
return;
}
quicksort(low,j-1);
quicksort(i+1,high);
}



int main()
{
cin>>n;
for(int i=0;i<n;i++)
{
cin>>a[i];
}
quicksort(0,n-1);
for(int i=0;i<n;i++)
{
cout<<a[i]<<" ";
}
return 0;
}
18 changes: 18 additions & 0 deletions winter-day6/贺达/day6题解.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
day6题解:
7-1:
直接插入排序

7-2:
冒泡排序

7-3:
利用map,排序的同时统计相同工龄的人数。

7-4:
本质是求逆序对。用树状数组,逆序插入,每插入一个就统计比它小的数量。

7-5:
根据堆排序的思想,构建大顶堆,每次将最后一个与头一个交换并固定,将剩下的再弄成大顶堆,重复知道排好。

7-6:
优化了不必要交换过程,减少了无效排序的快速排序,最后还输出了一行最终排好序的结果。