一本通 排序专栏

A 明明的随机数

【问题描述】

明明想在学校中请一些同学一起做一项问卷调查,为了实验的客观性,他先用计算机生成了N个1到1000之间的随机整数(N≤100),对于其中重复的数字,只保留一个,把其余相同的数去掉,不同的数对应着不同的学生的学号。然后再把这些数从小到大排序,按照排好的顺序去找同学做调查。请你协助明明完成“去重”与“排序”的工作。

【输入形式】

有2行,第1行为1个正整数,表示所生成的随机数的个数:N

第2行有N个用空格隔开的正整数,为所产生的随机数。

【输出形式】

2行,第1行为1个正整数M,表示不相同的随机数的个数。第2行为M个用空格隔开的正整数,为从小到大排好序的不相同的随机数。

【样例输入】
10
20 40 32 67 40 20 89 300 400 15
【样例输出】
8
15 20 32 40 67 89 300 400

#pragma GCC optimize(1)
#pragma GCC optimize(2)
#pragma GCC optimize(3,"Ofast","inline")
# include<iostream>
# include<algorithm>
# include<cmath>
# include<cstdio>
# include<set>
# include<stack>
# include<queue> 
# include<map>
# include<string>
# include<cstring> 
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int > PII; 
const int mod=1e9+7;
const int MAX=2e6+10;
const int Time=86400;
const int X=131;
const int inf=0x3f3f3f3f;
const double PI = 1e-4;
double pai = 3.14159265358979323846; 

set<int>q; 
int n;
int main(){  
    std::ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    set<int>::iterator it;
    cin >> n;
    for(int i = 1 ; i <= n ; i++){
    	 int x;
    	 cin >> x;
    	 q.insert(x); 
	}
	cout<<q.size()<<"\n";
	for(it = q.begin() ; it!=q.end()  ; it++)
	 cout<<*it<<" "; 
	return 0;
}

B 车厢重组

【问题描述】

在一个旧式的火车站旁边有一座桥,其桥面可以绕河中心的桥墩水平旋转。一个车站的职工发现桥的长度最多能容纳两节车厢,如果将桥旋转180度,则可以把相邻两节车厢的位置交换,用这种方法可以重新排列车厢的顺序。于是他就负责用这座桥将进站的车厢按车厢号从小到大排列。他退休后,火车站决定将这一工作自动化,其中一项重要的工作是编一个程序,输入初始的车厢顺序,计算最少用多少步就能将车厢排序。

【输入形式】

有两行数据,第一行是车厢总数N(不大于10000),第二行是N个不同的数表示初始的车厢顺序。

【输出形式】

一个数据,是最少的旋转次数。

【样例输入】
4
4 3 2 1
【样例输出】
6

#pragma GCC optimize(1)
#pragma GCC optimize(2)
#pragma GCC optimize(3,"Ofast","inline")
# include<iostream>
# include<algorithm>
# include<cmath>
# include<cstdio>
# include<set>
# include<stack>
# include<queue> 
# include<map>
# include<string>
# include<cstring> 
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int > PII; 
const int mod=1e9+7;
const int MAX=2e6+10;
const int Time=86400;
const int X=131;
const int inf=0x3f3f3f3f;
const double PI = 1e-4;
double pai = 3.14159265358979323846; 

int a[MAX],n,ans;

int main(){  
   std::ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> n;
    for(int i = 0 ; i < n ; i ++) cin >> a[i];
	
	for(int i = 0 ; i < n - 1; i++)
	 for(int j = 0 ; j < n - 1-i;j++)
	  if(a[j] > a[j+1]) swap(a[j],a[j+1]),ans++;
	 cout<<ans; 
	return 0;
}

C 众数

【问题描述】

由文件给出N个1到30000间无序数正整数,其中1≤N≤10000,同一个正整数可能会出现多次,出现次数最多的整数称为众数。求出它的众数及它出现的次数。

【输入形式】

输入文件第一行是正整数的个数N,第二行开始为N个正整数。

【输出形式】

输出文件有若干行,每行两个数,第1 个是众数,第2 个是众数出现的次数。

【样例输入】

12
2 4 2 3 2 5 3 7 2 3 4 3

【样例输出】

2 4
3 4

#pragma GCC optimize(1)
#pragma GCC optimize(2)
#pragma GCC optimize(3,"Ofast","inline")
# include<iostream>
# include<algorithm>
# include<cmath>
# include<cstdio>
# include<set>
# include<stack>
# include<queue> 
# include<map>
# include<string>
# include<cstring> 
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int > PII; 
const int mod=1e9+7;
const int MAX=2e6+10;
const int Time=86400;
const int X=131;
const int inf=0x3f3f3f3f;
const double PI = 1e-4;
double pai = 3.14159265358979323846; 

int n,a[MAX],x,maxn;

int main(){  
   std::ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> n;
     for(int i=0;i<n;i++){
        cin>>x;
        a[x]++;
        maxn = max(maxn,a[x]);
      
    }
    for(int i=0;i<30001;i++)
        if(maxn==a[i])
            cout<<i<<" "<<a[i]<<endl;
        

	return 0;
}

D 第k 小整数

【问题描述】

现有n 个正整数,n≤10000,要求出这n 个正整数中的第k 个最小整数(相同大小的整数只计算一次),k≤1000,正整数均小于30000。

【输入形式】

第一行为n 和k,第二行开始为n 个正整数的值,整数间用空格隔开。

【输出形式】

第k 个最小整数的值;若无解,则输出“NO RESULT”。

【样例输入】

10 3
1 3 3 7 2 5 1 2 4 6

【样例输出】
3

#pragma GCC optimize(1)
#pragma GCC optimize(2)
#pragma GCC optimize(3,"Ofast","inline")
# include<iostream>
# include<algorithm>
# include<cmath>
# include<cstdio>
# include<set>
# include<stack>
# include<queue> 
# include<map>
# include<string>
# include<cstring> 
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int > PII; 
const int mod=1e9+7;
const int MAX=2e6+10;
const int Time=86400;
const int X=131;
const int inf=0x3f3f3f3f;
const double PI = 1e-4;
double pai = 3.14159265358979323846; 

int n,a[MAX],x,maxn,k;

int main(){  
   std::ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> n >> k;
    for(int i = 0 ; i < n ; i ++ ) cin >> a[i];
	sort(a,a+n);    
	int i = 0 , j = 1;
	while(j < n){
		 if(a[i]!=a[j]) a[++i] = a[j];
		 j++;
	}
	k --;
    if(i<k) cout<<"NO RESULT"; 
     else 	cout<<a[k];
	return 0;
}

E 军事机密

【问题描述】

军方截获的信息由n(n<=30000)个数字组成,因为是敌国的高端秘密,所以一时不能破获。最原始的想法就是对这n 个数进行从小到大排序,每个数都对应一个序号,然后对第i 个是什么数感兴趣,现在要求编程完成。
【输入形式】
第一行n,接着是n 个截获的数字,接着一行是数字k,接着是k 行要输出数的序号。

【输出形式】

k 行序号对应的数字。

【样例输入】

5
121 1 126 123 7
3
2
4
3

【样例输出】
7
123
121

#pragma GCC optimize(1)
#pragma GCC optimize(2)
#pragma GCC optimize(3,"Ofast","inline")
# include<iostream>
# include<algorithm>
# include<cmath>
# include<cstdio>
# include<set>
# include<stack>
# include<queue> 
# include<map>
# include<string>
# include<cstring> 
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int > PII; 
const int mod=1e9+7;
const int MAX=2e6+10;
const int Time=86400;
const int X=131;
const int inf=0x3f3f3f3f;
const double PI = 1e-4;
double pai = 3.14159265358979323846; 

int n , m,x,a[MAX];
int main(){  
   std::ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> n;
    for(int i = 1; i <= n ; i++ ) cin >> a[i];
    sort(a + 1,a + n + 1);
    cin >> m;
    while(m--){
    	 cin >> x;
    	 cout<<a[x]<<"\n"; 
	}
	return 0;
}

F 奖学金

【问题描述】

某小学最近得到了一笔赞助,打算拿出其中一部分为学习成绩优秀的前5 名学生发奖学金。期末,每个学生都有3 门课的成绩:语文、数学、英语。先按总分从高到低排序,如果两个同学总分相同,再按语文成绩从高到低排序,如果两个同学总分和语文成绩都相同,那么规定学号小的同学排在前面,这样,每个学生的排序是唯一确定的。任务:先根据输入的3 门课的成绩计算总分,然后按上述规则排序,最后按排名顺序输出前5 名学生的学号和总分。注意,在前5 名同学中,每个人的奖学金都不相同,因此,你必须严格按上述规则排序。例如,在某个正确答案中,如果前两行的输出数据(每行输出两个数:学号、总分)是:
7 279
5 279
这两行数据的含义是:总分最高的两个同学的学号依次是7 号、5 号。这两名同学的总分都是279(总分等于输入的语文、数学、英语三科成绩之和),但学号为7 的学生语文成绩更高一些。如果你的前两名的输出数据是:
5 279
7 279
则按输出错误处理,不能得分。

【输入形式】

输入文件 包含n+1 行:

第1 行为一个正整数n,表示该校参加评选的学生人数。

第2 到n+1 行,每行有3 个用空格隔开的数字,每个数字都在0 到100 之间。第j 行的3 个数字依次

表示学号为j-1 的学生的语文、数学、英语的成绩。每个学生的学号按照输入顺序编号为1~n(恰好是输

入数据的行号减1)。 所给的数据都是正确的,不必检验。

【输出形式】

输出共有5 行,每行是两个用空格隔开的正整数, 依次表示前5 名学生的学号和总分。

【样例输入1】

6
90 67 80
87 66 91
78 89 91
88 99 77
67 89 64
78 89 98

【样例输出1】

6 265
4 264
3 258
2 244
1 237

【样例输入2】

8
80 89 89
88 98 78
90 67 80
87 66 91
78 89 91
88 99 77
67 89 64
78 89 98

【样例输出2】

8 265
2 264
6 264
1 258
5 258

【样例说明】

50%的数据满足:各学生的总成绩各不相同

100%的数据满足:6<=n<=300

#pragma GCC optimize(1)
#pragma GCC optimize(2)
#pragma GCC optimize(3,"Ofast","inline")
# include<iostream>
# include<algorithm>
# include<cmath>
# include<cstdio>
# include<set>
# include<stack>
# include<queue> 
# include<map>
# include<string>
# include<cstring> 
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int > PII; 
const int mod=1e9+7;
const int MAX=2e6+10;
const int Time=86400;
const int X=131;
const int inf=0x3f3f3f3f;
const double PI = 1e-4;
double pai = 3.14159265358979323846; 

int n;

struct Node{
	int x,y,z,sum,id;
}a[MAX];

bool cmp(Node a,Node b){
	if(a.sum == b.sum && a.x == b.x ) return a.id < b.id ;
	 else if(a.sum == b.sum ) return a.x > b.x ;
	  return a.sum > b.sum ;
}
int main(){  
   std::ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> n;
    for(int i = 0 ; i < n ; i ++ ){
    	cin >> a[i].x >> a[i].y >> a[i].z ;
    	a[i].id = i + 1 ;
    	a[i].sum = a[i].x + a[i].y + a[i].z ;
	}
	sort(a,a+n,cmp);
	for(int i = 0 ; i < 5 ; i ++)
	 cout<<a[i].id <<" "<<a[i].sum <<"\n";
	return 0;
}

G 统计数字

【问题描述】

某次科研调查时得到了n个自然数,每个数均不超过1500000000(1.5*109)。已知不相同的数不超过10000 个,现在需要统计这些自然数各自出现的次数,并按照自然数从小到大的顺序输出统计结果。

【输入形式】

输入文件包含n+1 行:
第1 行是整数n,表示自然数的个数。
第2~n+1 行每行一个自然数。

【输出形式】

输出文件包含m 行(m 为n 个自然数中不相同数的个数),按照自然数从小到大的顺序输出。每行输出两个整数,分别是自然数和该数出现的次数,其间用一个空格隔开。

【样例输入】
8
2
4
2
4
5
100
2
100

【样例输出】

2 3
4 2
5 1
100 2

【样例说明】

40%的数据满足:1<=n<=1000
80%的数据满足:1<=n<=50000
100%的数据满足:1<=n<=200000,每个数均不超过1 500 000 000(1.5*109)

#pragma GCC optimize(1)
#pragma GCC optimize(2)
#pragma GCC optimize(3,"Ofast","inline")
# include<iostream>
# include<algorithm>
# include<cmath>
# include<cstdio>
# include<set>
# include<stack>
# include<queue> 
# include<map>
# include<string>
# include<cstring> 
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int > PII; 
const int mod=1e9+7;
const int MAX=2e6+10;
const int Time=86400;
const int X=131;
const int inf=0x3f3f3f3f;
const double PI = 1e-4;
double pai = 3.14159265358979323846; 

map<ll,ll>q;
ll n,x,a[MAX],j;
int main(){  
   std::ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    set<ll,ll>::iterator it;
    cin >> n ;
    for(int i = 0 ; i < n ; i  ++ ){
    	 cin >> x;
    	 q[x]++;
		 if(q[x]==1) a[j++] = x; 
	}
	sort(a,a+j);
   for(int i = 0 ; i < j ; i++)
    cout<<a[i]<<" "<<q[a[i]]<<"\n";
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值