3.22 题解报告
1001 sort
题目
Problem Description
给你n个整数,请按从大到小的顺序输出其中前m大的数。
Input
每组测试数据有两行,第一行有两个数n,m(0<n,m<1000000),第二行包含n个各不相同,且都处于区间[-500000,500000]的整数。
Output
对每组测试数据按从大到小的顺序输出前m大的数。
Sample Input
5 3
3 -35 92 213 -644
Sample Output
213 92 3
题解
就是正常sort啊!
代码
#include<stdio.h>
#include<stdlib.h>
#define N 1000001
int a[N],m;
void Qsort(int low,int high){
int k,i;
k=a[low];
int left=low;
int right=high;
if(left>right)
return ;
while(left!=right){
while(a[right]<k&&left<right)
right--;
if(left<right){
a[left]=a[right];
left++;
}
while(a[left]>k&&left<right)
left++;
if(left<right){
a[right]=a[left];
right--;
}
}
a[left]=k;
if(m<=left+1)Qsort(low,left-1);
else {
Qsort(low,left-1);
Qsort(left+1,high);
}
}
int main(){
int i,j,n;
while(scanf("%d%d",&n,&m)!=EOF){
for(i=0;i<n;i++)scanf("%d",&a[i]);
Qsort(0,n-1);
for(i=0;i<m;i++){
printf("%d",a[i]);
if(i!=m-1)
printf(" ");
}
printf("\n");
}
return 0;
}
1002 EXCEL排序
题目
Problem Description
Excel可以对一组纪录按任意指定列排序。现请你编写程序实现类似功能。
Input
测试输入包含若干测试用例。每个测试用例的第1行包含两个整数 N (<=100000) 和 C,其中 N 是纪录的条数,C 是指定排序的列号。以下有 N
行,每行包含一条学生纪录。每条学生纪录由学号(6位数字,同组测试中没有重复的学号)、姓名(不超过8位且不包含空格的字符串)、成绩(闭区间[0, 100]内的整数)组成,每个项目间用1个空格隔开。当读到 N=0 时,全部输入结束,相应的结果不要输出。
Output
对每个测试用例,首先输出1行“Case i:”,其中 i 是测试用例的编号(从1开始)。随后在 N 行中输出按要求排序后的结果,即:当 C=1 时,按学号递增排序;当 C=2时,按姓名的非递减字典序排序;当 C=3
时,按成绩的非递减排序。当若干学生具有相同姓名或者相同成绩时,则按他们的学号递增排序。
Sample Input
3 1
000007 James 85
000010 Amy 90
000001 Zoe 60
4 2
000007 James 85
000010 Amy 90
000001 Zoe 60
000002 James 98
4 3
000007 James 85
000010 Amy 90
000001 Zoe 60
000002 James 90
0 0
Sample Output
Case 1:
000001 Zoe 60
000007 James 85
000010 Amy 90
Case 2:
000010 Amy 90
000002 James 98
000007 James 85
000001 Zoe 60
Case 3:
000001 Zoe 60
000007 James 85
000002 James 90
000010 Amy 90
题解
https://blog.youkuaiyun.com/u014174811/article/details/42087077?utm_source=blogxgwz3
题意非常明确,就是排序,但在这个基础上,对关键值相等的对象按对象另一种键值做相应排序。使用STL中sort()函数可以省大量的编码时间,但运行时间相当大,这里还用到了string类的一系列运算符重载函数(<,>,=),这也使得运行时间消耗增大。
按关键值c的方法:
bool cmp(S a,S b)//用struct S的两个变量做形参{
if(a.c<b.c) return true;//按关键字c非降序排序
return false;
}
sort(arr,arr+N,cmp);//使用sort范型排序
代码
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
struct Stu
{
string num;
string name;
int score;
} stu[100000];
bool cmpNum(Stu a,Stu b){
return a.num<b.num;
}
bool cmpName(Stu a,Stu b){
if(a.name<b.name) return true;
else if(a.name==b.name) return(cmpNum(a,b));
return false;
}
bool cmpScore(Stu a,Stu b){
if(a.score<b.score) return true;
else if(a.score==b.score) return(cmpNum(a,b));
return false;
}
int main()
{
int n,c,i,cnt;
cnt=1;
while(cin>>n>>c&&n)
{
for(i=0; i<n; i++)
cin>>stu[i].num>>stu[i].name>>stu[i].score;
if(c==1)sort(stu,stu+n,cmpNum);
else if(c==2)sort(stu,stu+n,cmpName);
else sort(stu,stu+n,cmpScore);
cout<<"Case "<<cnt++<<":"<<endl;
for(i=0; i<n; i++)
cout<<stu[i].num<<" "<<stu[i].name<<" "<<stu[i].score<<endl;
}
}
1003 Design T-Shirt
题目
Problem Description
Soon after he decided to design a T-shirt for our Algorithm Board on Free-City BBS, XKA found that he was trapped by all kinds of suggestions from everyone on the board. It is indeed a mission-impossible to have everybody perfectly satisfied. So he took a poll to collect people’s opinions. Here are what he obtained: N people voted for M design elements (such as the ACM-ICPC logo, big names in computer science, well-known graphs, etc.). Everyone assigned each element a number of satisfaction. However, XKA can only put K (<=M) elements into his design. He needs you to pick for him the K elements such that the total number of satisfaction is maximized.
Input
The input consists of multiple test cases. For each case, the first line contains three positive integers N, M and K where N is the number of people, M is the number of design elements, and K is the number of elements XKA will put into his design. Then N lines follow, each contains M numbers. The j-th number in the i-th line represents the i-th person’s satisfaction on the j-th element.
Output
For each test case, print in one line the indices of the K elements you would suggest XKA to take into consideration so that the total number of satisfaction is maximized. If there are more than one solutions, you must output the one with minimal indices. The indices start from 1 and must be printed in non-increasing order. There must be exactly one space between two adjacent indices, and no extra space at the end of the line.
Sample Input
3 6 4
2 2.5 5 1 3 4
5 1 3.5 2 2 2
1 1 1 1 1 10
3 3 2
1 2 3
2 3 1
3 1 2
Sample Output
6 5 3 1
2 1
题解
https://blog.youkuaiyun.com/curson_/article/details/52182249
题意:
有n个人对m件衣服评价,求最高的k件衣