By Jalan
知识工具需求
数学
数据结构和算法
语言
- sort函数
- 加入结构体数组时按照某种方式排序好的,那么要在这个排序好的结构体数组里运用二分查找可以有以下几个方式:
- 自己写二分
- 在结构体内做封装,运用upperbound和lowerbound,
这里举例:1重载()运算符,里面封装比较,即把通过这种方式代替cmp()函数;
#include <bits/stdc++.h>
using namespace std;
struct node
{
int x,y;
};
struct cmp1
{
bool operator () (const node &a,const node &b) const {
return a.x < b.x;
}
};
int main()
{
vector<node>pq;
pq.push_back({1,3});
pq.push_back({2,8});
pq.push_back({4,3});
node a;
a.x = a.y = 3;
vector<node>::iterator it = lower_bound(begin(pq),end(pq),a,cmp1());
cout << (*it).x << endl;
}
2,在结构体内封装比较运算符.
struct MY{
int a,b;
MY(){}
MY(int a,int b):a(a),b(b){}
bool operator<(const MY m)const{ //定义比较方式,这一步很重要
return a<m.a;
}
};
int main(){
vector<MY>ve;
for(int i=0;i<10;i++){
ve.push_back(MY(i+1,2*i));
cout<<ve[i].a<<" "<<ve[i].b<<endl;
}
cout<<"请输入你需要查找的数字a:"<<endl;
int num;
cin>>num;
sort(ve.begin(),ve.end());//进行二分之前需要排序
int aa = lower_bound(ve.begin(),ve.end(),MY(num,0)) - ve.begin(); //需要把我们查找的数封装成一个结构体才能进行查找。
cout<<"查到位置为:"<<aa<<endl;
return 0;
}
题干
福布斯杂志每年都发表最富的人的排行,现在你要模拟这个工作但是专注于一个年龄范围,现在给N个人,找到在年龄范围内前M富有的人.
输入条件
第一行有两个正整数N<=10^5是总人数,K<=10^3是询问数.
下面是N行,每行包含人名<=8无空格,年龄(0,200]和富有值[-10^6,10^6]
下面是K行询问,包含M<=100最大输出数,[Amin,Amax]是年龄范围
例1
12 4
Zoe_Bill 35 2333
Bob_Volk 24 5888
Anny_Cin 95 999999
Williams 30 -22
Cindy 76 76000
Alice 18 88888
Joe_Mike 32 3222
Michael 5 300000
Rosemary 40 5888
Dobby 24 5888
Billy 24 5888
Nobody 5 0
4 15 45
4 30 35
4 5 95
1 45 50
输出条件
第一行打印:Case #X x是测试号从1开始
接下来打印M个年龄范围内的富人用这种格式:Name Age Net_Worth
输出按财富的非递增序列,财富相等时按年龄的非递减序列,都等的时候按照名字的字母表非递减序列.
没人打印None
例1
Case #1:
Alice 18 88888
Billy 24 5888
Bob_Volk 24 5888
Dobby 24 5888
Case #2:
Joe_Mike 32 3222
Zoe_Bill 35 2333
Williams 30 -22
Case #3:
Anny_Cin 95 999999
Michael 5 300000
Alice 18 88888
Cindy 76 76000
Case #4:
None
题解
第一次
思路
- 用一个List保存所有人,按要求排序所有人.
- 在每次询问中建立一个caseList数组,符合条件的加进去,直到加进了M个人
- 每次询问输出这个caseList数组,空就输出None
预期时间复杂度
nlogn
编写用时
30分钟
代码
CPP
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <stdio.h>
#include <vector>
using namespace std;
typedef struct node
{
char name[8 + 2];
int age;
int w;
} node;
int cmp(node a, node b)
{
return a.w > b.w ? (1) : (a.w == b.w ? (a.age < b.age ? (1) : (a.age == b.age ? (strcmp(a.name, b.name)<0?1:0) : (0))) : (0));
}
int main(int argc, char const *argv[])
{
int N, K;
scanf("%d%d", &N, &K);
vector<node> List(N);
for (int i = 0; i < N; i++)
{
scanf("%s %d %d", List[i].name, &List[i].age, &List[i].w);
}
sort(List.begin(), List.end(), cmp);
for (int j = 0; j < K; j++)
{
int M, Amin, Amax;
scanf("%d%d%d", &M, &Amin, &Amax);
printf("Case #%d:\n", j + 1);
int count = 0;
vector<node> caseList;
for (int i = 0; i < N; i++)
{
if (List[i].age >= Amin && List[i].age <= Amax)
{
++count;
caseList.push_back(List[i]);
}
if (count == M)
{
break;
}
}
if (!count)
{
printf("None\n");
}
else
{
for (int i = 0; i < caseList.size(); i++)
{
printf("%s %d %d\n", caseList[i].name, caseList[i].age, caseList[i].w);
}
}
}
return 0;
}
运行用时
结尾
看在我写了这么多注释的份上可以给我点个赞嘛,求求惹=]砰砰砰,给我加点写下去的油呀@.@
也欢迎关注我的CSDN账号呀,接下来两个月我应该会按这个格式更新所有的PTA甲级题目
**开心code每一天**