#YBT整理 排序算法的例题
车厢重组
冒泡排序,每次交换即为调转车厢,ans++
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int n;
int a[10000];
int ans;
int main(){
cin >> n;
for(int i = 1;i <= n; i++) cin >> a[i];
for(int i = 1;i <= n; i++){
for(int j = 1;j <= n - i; j++){
if(a[j] > a[j+1]){
swap(a[j],a[j+1]);
ans++;
}
}
}
cout << ans << endl;
return 0;
}//lcez_cyc
求逆序对
归并排序。合并的时候,被放进去的数字右边的所有数字都可以组成逆序对。
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int n;
long long ans = 0;
int a[500400];
int b[500400];
int L[500400],R[500400];
int RT = 0;
void msort(int l,int r){
if(l == r) return;
int mid = (l+r) >>1;
msort(l,mid);
msort(mid+1,r);
int i = l,j = mid + 1;
int cnt = l;
while(j <= r && i <= mid){
if(a[i] > a[j]){
ans += mid - i + 1;
b[cnt++] = a[j++];
}
if(a[i] <= a[j]){
b[cnt++] = a[i++];
}
}
while(j <= r)
b[cnt++] = a[j++];
while(i <= mid)
b[cnt++] = a[i++];
for(int i = l;i <= r; i++)
a[i] = b[i];
}
int main(){
cin >> n;
for(int i = 1;i <= n; i++){
cin >> a[i];
}
msort(1,n);
cout << ans << endl;
return 0;
}
谁考了第k名
这个就很水了就不说了
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int n,k;
struct STU{
int num;
double sco;
}stu[1000];
bool cmp(STU a,STU b){
return a.sco > b.sco;
}
int main(){
cin >> n >> k;
for(int i = 1;i <= n; i++)
cin >> stu[i].num >> stu[i].sco;;
sort(stu + 1,stu + 1 + n,cmp);
cout << stu[k].num << ' ' << stu[k].sco << endl;
return 0;
}
奇数单增序列
这个也挺水,直接把偶数扔掉就好了
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int n;
int a[1000];
int main(){
int tot = 1;
cin >> n;
for(int i = 1;i <= n; i++){
int f;
cin >> f;
if(f % 2)
a[tot++] = f;
}
tot--;
sort(a+1,a+tot+1);
for(int i = 1;i <= tot;i++){
cout << a[i];
if(i != tot) cout <<',';
}
return 0;
}
成绩排序
水题
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int n,k;
struct STU{
string name;
int sco;
}stu[1000];
bool cmp(STU a,STU b){
return a.sco >= b.sco;
}
int main(){
cin >> n;
for(int i = 1;i <= n; i++)
cin >> stu[i].name >> stu[i].sco;;
sort(stu + 1,stu + 1 + n,cmp);
for(int i = 1;i <= n; i++){
cout << stu[i].name << ' ' << stu[i].sco << endl;
}
return 0;
}
奖学金
多关键字排序。
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int n,k;
struct STU{
int num;
int sco;
int chi,mat,eng;
}stu[1000];
bool cmp(STU a,STU b){
if(a.sco != b.sco) return a.sco > b.sco;
if(a.chi != b.chi) return a.chi > b.chi;
return a.num < b.num;
}
int main(){
cin >> n;
for(int i = 1;i <= n; i++){
stu[i].num = i;
cin >> stu[i].chi >> stu[i].mat >> stu[i].eng;
stu[i].sco = stu[i].chi + stu[i].mat + stu[i].eng;
}
sort(stu + 1,stu + 1 + n,cmp);
for(int i = 1;i <= 5; i++){
cout << stu[i].num << ' ' << stu[i].sco << endl;
}
return 0;
}
分数线划定
先划定分数线,确定人数,再输出入选的人
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int n,k;
struct STU{
int num;
int sco;
}stu[9999];
bool cmp(STU a,STU b){
if(a.sco != b.sco) return a.sco > b.sco;
return a.num < b.num;
}
int main(){
cin >> n >> k;
for(int i = 1;i <= n; i++)
cin >> stu[i].num >> stu[i].sco;
sort(stu + 1,stu + 1 + n,cmp);
k = k *3 / 2;
int i;
for(i = 1;stu[i].sco >= stu[k].sco; i++);
i--;
cout <<stu[i].sco << ' ' << i << endl;
for(i = 1;stu[i].sco >= stu[k].sco; i++){
cout << stu[i].num << ' ' << stu[i].sco << endl;
}
return 0;
}
整数奇偶排序
设定两个指针,分别从前往后、从后往前存储,最后用两个关键字排起来
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int a[11];
bool cmp2(int a,int b){
return a > b;
}
int main(){
int tot = 1;
int tt = 10;
for(int i = 1;i <= 10; i++){
int b;
cin >> b;
if(b % 2){
a[tot++] = b;
}else{
a[tt--] = b;
}
}
sort(a+1,a+tot,cmp2);
sort(a+tot,a+11);
for(int i = 1;i <= 10; i++) cout << a[i] << ' ';
cout << endl;
return 0;
}
合影效果
同上,最后只用输出身高
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int n;
double s[1000];
bool cmp(double a,double b){
return a > b;
}
int main(){
cin >> n;
int tot = 1,tt = n;
for(int i = 1;i <= n; i++){
string in;
double b;
cin >> in >> b;
if(in == "male"){
s[tot++] = b;
}else{
s[tt--] = b;
}
}
sort(s+1,s+tot);
sort(s+tot,s+1+n,cmp);
for(int i = 1;i <= n; i++) printf("%.2f ",s[i]);
cout << endl;
return 0;
}
病人排队
看病的时候老人都要先看??
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int n;
struct PEO{
string num;
int age;
int time;
}peo[1000];
bool cmp1(PEO a,PEO b){
if(a.age != b.age) return a.age > b.age;
return a.time < b.time;
}
bool cmp2(PEO a,PEO b){
return a.time < b.time;
}
int main(){
cin >> n;
int tot = 1,tt = n;
for(int i = 1;i <= n; i++){
string in;
int b;
cin >> in >> b;
if(b >= 60){
peo[tot].time = i;
peo[tot].age = b;
peo[tot++].num = in;
}else{
peo[tt].time = i;
peo[tt].age = b;
peo[tt--].num = in;
}
}
sort(peo+1,peo+tot,cmp1);
sort(peo+tot,peo+1+n,cmp2);
for(int i = 1;i <= n; i++){
cout << peo[i].num << endl;
}
return 0;
}
明明的随机数
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int n;
int a[1000];
int main(){
cin >> n;
for(int i = 1;i <= n; i++) cin >> a[i];
sort(a+1,a+1+n);
int tot = 0;
for(int i = 1;i <= n; i++){
tot++;
while(a[i+1] == a[i]) i++;
}
cout << tot << endl;
for(int i = 1;i <= n; i++){
cout << a[i] << ' ';
while(a[i] == a[i+1]) i++;
}
return 0;
}
单词排序
我好像没有AC啊
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
string a[1000];
int main(){
char c;
int tot = 1;
while(c != '\n'){
cin >> a[tot++];
c = getchar();
}
sort(a+1,a+1+tot);
for(int i = 1;i <= tot; i++){
cout << a[i] << endl;
while(a[i] == a[i+1]) i++;
}
return 0;
}
出现次数超过一半的数
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int n;
int a[5000];
int main(){
cin >> n;
for(int i = 1;i <= n; i++) cin >> a[i];
sort(a+1,a+1+n);
bool you = 0;
for(int i = 1;i <= n; i++){
int tot = 0;
while(a[i] == a[i+1]){
i++;
tot++;
}
if(tot >= n/2){
cout << a[i] << ' ';
you = 1;
}
}
if(!you) cout << "no" << endl;
return 0;
}
统计字符数
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int n;
char a[10000];
int strlen(string a){
int tot = 0;
while(a[tot] != '\0') tot++;
return tot;
}
int main(){
cin >> a;
for(int i = 9999;i >= 1; i--) a[i] = a[i-1];
n = strlen(a);
a[0] = '0';
sort(a+1,a+1+n);
char ans;
int time = 0;
for(int i = 1;i <= n; i++){
int tim = 1;
while(a[i] == a[i+1]){
i++;
tim++;
}
if(tim > time){
time = tim;
ans = a[i];
}
}
cout << ans << ' ' << time << endl;
return 0;
}