本萌新刚刚学玩离散化,脑子里就想起之前用计数排序做的这题。
本萌新准备用离散化解决
(闲来无事)
为什么要用离散化呢
1 如果题目的没有数字范围很大,则无法使用计数排序
2 我们有很多的空间无需用到
所以我决定用离散化解决这题
白痴0.0
问题
当芸芸众生忙着在朋友圈中发照片的时候,总有一些人因为太帅而没有朋友。本题就要求你找出那些帅到没有朋友的人。
输入格式:
输入第一行给出一个正整数N(≤100),是已知朋友圈的个数;随后N行,每行首先给出一个正整数K(≤1000),为朋友圈中的人数,然后列出一个朋友圈内的所有人——为方便起见,每人对应一个ID号,为5位数字(从00000到99999),ID间以空格分隔;之后给出一个正整数M(≤10000),为待查询的人数;随后一行中列出M个待查询的ID,以空格分隔。
注意:没有朋友的人可以是根本没安装“朋友圈”,也可以是只有自己一个人在朋友圈的人。虽然有个别自恋狂会自己把自己反复加进朋友圈,但题目保证所有K超过1的朋友圈里都至少有2个不同的人。
输出格式:
按输入的顺序输出那些帅到没朋友的人。ID间用1个空格分隔,行的首尾不得有多余空格。如果没有人太帅,则输出No one is handsome。
注意:同一个人可以被查询多次,但只输出一次。
输入样例1:
3
3 11111 22222 55555
2 33333 44444
4 55555 66666 99999 77777
8
55555 44444 10000 88888 22222 11111 23333 88888
输出样例1:
10000 88888 23333
输入样例2:
3
3 11111 22222 55555
2 33333 44444
4 55555 66666 99999 77777
4
55555 44444 22222 11111
输出样例2:
No one is handsome
好了,不说废话了,上代码
#include<iostream>
#include<vector>
#include<algorithm>
#include<utility>
using namespace std;
typedef pair<int,int> PII;
vector<int> alls,dev;
vector<PII> segs;
const int N=1e5;
int a[N];
int n,m;
int find(int x){
int l=0,r=alls.size()-1;
while(l<r){
int mid=r+l>>1;
if(alls[mid]>=x) r=mid;
else l=mid+1;
}
return r;
}
int main(){
cin>>n;
while(n--){
int T;
cin>>T;
for(int i=0;i<T;i++){
int x;
cin>>x;
alls.push_back(x);
if(T<=1) break;
segs.push_back({
x,1});
}
}
cin>>m;
while(m--){
int x;
cin>>x;
alls.push_back(x);
dev.push_back(x);
}
sort(alls.begin(),alls.end());
alls.erase(unique(alls.begin(),alls.end()),alls.end());
for(auto seg:segs){
int x=find(seg.first);
a[x]+=seg.second;
}
int k=0;
for(auto item:dev){
int y=find(item);
if(!a[y]){
if(k) cout<<" ";
printf("%05d",item);
a[y]++;
k++;
}
}
if(!k) cout<<"No one is handsome"<<endl;
return 0;
}
这题用离散化是为了帮助新学算法的萌新(和我一样)0.0
这题可以当作离散化的简单练手
谢谢大家支持