- 【例2.5】求逆序对
🎉🎉🎉2025-3-4 13:46:59 基础算法 排序 AK纪念🎉🎉🎉
- 2025-3-4 13:54:16 @
C24第二人!
"Ode to Ordered Cosmos"
—For the OIer Who Conquered Sorting
In the realm where arrays whisper chaos,
Your fingers dance—quicksilver logic,
each pivot a supernova
splitting constellations into binary symmetry.
Loops unravel like Fibonacci’s golden thread,
merging halves of time into a single breath.
O(n log n) cadence hums beneath your keystrokes—
the screen blooms with algorithms in full bloom.
Bubble by bubble, you defied entropy’s tide,
heaps of possibilities sifted to crystalline tiers.
Radix by radix, digits aligned like soldiers—
each comparison a star chart etched in victory.
What is a sorted list but a hymn to precision?
Zeros and ones bow to your compiler’s wand,
while the universe, jealous of your debugged perfection,
rewrites its own chaos in your shadow.
Tonight, let the AC signals light your path—
every accepted run a stanza in this ode.
For you’ve not just ordered numbers, but tamed
the wild syntax of existence itself.
统计字符数
#include<bits/stdc++.h>
using namespace std;
int a[1100]={0};
signed main(){
string ccin;
cin>>ccin;
int maxs=0;
int maxss=0;
for(int i=0;i<ccin.size();i++){
a[(int)ccin[i]-'a']++;
}
for(int i=0;i<ccin.size();i++){
// maxss=max(maxss,a[i]);
if(a[i]>maxss){
maxss=a[i];
maxs=i;
}
}
cout<<char(maxs+'a')<<" "<<maxss;
return 0;
}
冒泡排序
#include<bits/stdc++.h>
using namespace std;
int found[20000];
signed main(){
int a;
cin>>a;
for(int i=0;i<a;i++){
cin>>found[i];
// cout<<found[i]<<" ";
}
for(int i=0;i<a;i++){
for(int j=0;j<a-i-1;j++){
// cout<<found[j]<<endl;
if(found[j]>found[j+1]){
// int found1=found[j+1];
// found[j+1]=found[j];
// found[j]=found1;
swap(found[j],found[j+1]);
}
}
for(int m=0;m<a;m++){
cout<<found[m]<<" ";
}
cout<<endl;
}
return 0;
}
选择排序
#include<bits/stdc++.h>
using namespace std;
int found[20000];
int mini=1000;
int minier=1000;
signed main(){
int a;
cin>>a;
for(int i=1;i<=a;i++){
cin>>found[i];
// cout<<found[i]<<" ";
}
for(int i=1;i<=a;i++){
for(int j=i;j<=a;j++){
// cout<<found[j]<<endl;
if(found[j]<mini){
// int found1=found[j+1];
// found[j+1]=found[j];
// found[j]=found1;
mini=found[j];
minier=j;
}
}
swap(found[i],found[minier]);
mini=1000;
minier=0;
for(int m=1;m<=a;m++){
cout<<found[m]<<" ";
}
cout<<endl;
}
return 0;
}
插入排序
#include<bits/stdc++.h>
using namespace std;
int found[20000];
int mini=1000;
int minier=1000;
signed main(){
int a;
cin>>a;
for(int i=1;i<=a;i++){
cin>>found[i];
// cout<<found[i]<<" ";
}
for(int i=2;i<=a;i++){
// cout<<found[j]<<endl;
// if(found[j]<mini){
// // int found1=found[j+1];
// // found[j+1]=found[j];
// // found[j]=found1;
// mini=found[j];
// minier=j;
// }
int p=i;
while(found[p]<=found[p-1]){
swap(found[p],found[p-1]);
p--;
}
// swap(found[i],found[minier]);
// mini=1000;
// minier=0;
for(int m=1;m<=a;m++){
cout<<found[m]<<" ";
}
cout<<endl;
}
for(int m=1;m<=a;m++){
cout<<found[m]<<" ";
}
cout<<endl;
}
车厢重组
#include<bits/stdc++.h>
using namespace std;
int found[20000];
signed main(){
int a;
cin>>a;
int cnt=0;
for(int i=0;i<a;i++){
cin>>found[i];
// cout<<found[i]<<" ";
}
for(int i=0;i<a;i++){
for(int j=0;j<a-i-1;j++){
// cout<<found[j]<<endl;
if(found[j]>found[j+1]){
// int found1=found[j+1];
// found[j+1]=found[j];
// found[j]=found1;
swap(found[j],found[j+1]);
cnt++;
}
}
}
cout<<cnt;
return 0;
}
求逆序对
//如果·左区间: 5 6 7
//右区间: 1 2 9
//1:由于 5>1,所以产生了逆序对,这里,我们发现,左区间所有还没有被合并的数都比 1 大,所以1与左区间所有元素共产生了 3 个逆序对(即tot_numleft-i+1对),统计答案并合并 1
//2:由于 5>2,由上产生了3对逆序对,统计答案并合并 2
//3:由于 5<9, 没有逆序对产生,右区间下标 j++
//4:由于 6<9, 没有逆序对产生,右区间下标 j++
//5:由于 7<9, 没有逆序对产生,右区间下标 j++
#include<bits/stdc++.h>
using namespace std;
#define int long long
// long long a[11561511];
const int N=1E6;
long long a[N],b[N],cyt=0;;
void guib(int left,int right){
//归并
if(left==right ){//区间已经有序
return; //lancas Lancas
}
int le=left,centre=left/2+right/2,ri=centre+1;//le=左区间扫的指针 ri=左区间扫的指针 center是中间数 使用等差数列a+b/2求出 lancas Lancas
int l2=left;
guib(left,centre);//左区间
guib(centre+1,right);//右区间
while(le<=centre&&ri<=right){//是否扫完
if(a[le]<=a[ri]){
b[l2++]=a[le++];//组合成有序数组·
// l2++;
// le++;
}
else{
b[l2++]=a[ri++];
//cyt+=1
cyt+=centre-le+1;
}
}
while(le<=centre){//是否扫完
// if(a[le]<a[ri]){
// b[l2++]=a[le++];
// // l2++;
// // le++;
// }
// else{
// b[l2++]=a[ri++];
// //cyt+=1
// cyt+=centre-i+1;
// }
b[l2++]=a[le++];
}
while(ri<=right){//是否扫完
// if(a[le]<a[ri]){
// b[l2++]=a[le++];
// // l2++;
// // le++;
// }
// else{
// b[l2++]=a[ri++];
// //cyt+=1
// cyt+=centre-i+1;
// }
b[l2++]=a[ri++];
}
for(int i=left;i<=right;i++)//覆盖元素组
a[i]=b[i];
}
signed main(){
int n,cnt=0;
cin>>n;
for(int i=1;i<=n;i++){
cin>>a[i];
}
// for(int i = 1; i <= n; i ++){
// for(int j = i; j <= n; j ++){
// if(a[i] > a[j])cnt ++;
// }
// }
guib(1,n);
cout<<cyt;
return 0;
}
明明的随机数
#include<bits/stdc++.h>
using namespace std;
int numb[1001];
int n,s,cnt;
int main(){
cin>>n;
while(n>0){
cin>>s;
numb[s]=1;
n--;
}
for(int i=0;i<1001;i++){
if(numb[i]==1)
cnt++;
}
cout<<cnt<<endl;
for(int i=0;i<1001;i++){
if(numb[i]==1)
cout<<i<<" ";
}
return 0;
}
奖学金
#include<bits/stdc++.h>
using namespace std;
struct lancas{
int id;
int cn;
int ma;
int en;
}lan[3090];
bool cmp(lancas a,lancas b){
if((a.cn+a.ma+a.en)!=(b.cn+b.ma+b.en))return (a.cn+a.ma+a.en)>(b.cn+b.ma+b.en);
if(a.cn!=b.cn)return a.cn>b.cn;
return a.id<b.id;
}
int main(){
int n;
cin>>n;
for(int i=1;i<=n;i++){
lan[i].id=i;
cin>>lan[i].cn>>lan[i].ma>>lan[i].en;
//cout<<lan[i].cn<<" "<<lan[i].ma<<" "<<lan[i].en<<endl;
}
sort(lan+1,lan+n+1,cmp);
for(int i=1;i<=5;i++){
cout<<lan[i].id<<" "<<lan[i].cn+lan[i].ma+lan[i].en<<endl;;
}
return 0;
}
1 comments
-
C24kongxiangtai LV 7 @ 2025-3-4 18:32:38
强强强
- 1
Information
- ID
- 796
- Time
- 1000ms
- Memory
- 256MiB
- Difficulty
- 8
- Tags
- # Submissions
- 197
- Accepted
- 35
- Uploaded By