有的时间复杂度是真的玄学
思路很暴力,只需要小学二年级思维
大致思路:
首先建一个结构体
struct num{
int id;//每个数字的位置
int z;//每个数字的值
};然后定义一下
bool cmz(struct num a,struct num b){
if(a.z>b.z)
return true;
if(a.z==b.z)
if(a.id<b.id)
return true;
return false;
}
bool cmid(struct num a,struct num b){
if(a.id<b.id)
return true;
return false;
}好,有了这些后,我们就可以开始操作了
具体举例:
1.先把一串数字读入进来,并依次赋予id值
2.将数字的值排序
3.去掉重复的数字
//这里z的0表示该数被删去(实际程序中,我们用-2147483659来代替)4.将数字的位置排序
5.输出答案
注意事项:
数据范围问题:
该题给出的数在32位有符号整数范围内
,所以我们用-2147483659来表示该数已被去除 (开long long吧)
关于去重的问题:
for(int i=1;i<=n;i+=pd){
pd = 1;
for(int j=i+1;j<=n;j++){
if(s[i].z==s[j].z)s[j].z = -2147483659,pd++;
else break;
}
}变量 的存在可以帮我们稍稍优化一下去重的过程
(避免毒瘤的数据:1 1 1 1 1 1 …… 1 1 1……1 1 1 1)
关于读入的问题:
不加快读会爆掉后四个点或者后三个点 爆TLE
咳咳,至于具体多少,取决于提交代码时的欧气值
最后附上代码:
#include<bits/stdc++.h>
#define int long long
using namespace std;
struct num{
int id;
int z;
}s[50024];
int read(){
int w=1,q=0;
char ch=' ';
while(ch!='-'&&(ch<'0'||ch>'9'))ch=getchar();
if(ch=='-')w=-1,ch=getchar();
while(ch>='0'&&ch<='9')q=q*10+ch-'0',ch=getchar();
return w*q;
}
bool cmz(struct num a,struct num b){
if(a.z>b.z)
return true;
if(a.z==b.z)
if(a.id<b.id)
return true;
return false;
}
bool cmid(struct num a,struct num b){
if(a.id<b.id)
return true;
return false;
}
signed main(void){
int t,n,pd;
t = read();
while(t--){
scanf("%lld",&n);
for(int i=1;i<=n;i++){
s[i].id = i;
s[i].z = read();
}
sort(s+1,s+n+1,cmz);
for(int i=1;i<=n;i+=pd){
pd = 1;
for(int j=i+1;j<=n;j++){
if(s[i].z==s[j].z)s[j].z = -2147483659,pd++;
else break;
}
}
sort(s+1,s+n+1,cmid);
for(int i=1;i<=n;i++){
if(s[i].z!=-2147483659)printf("%lld ",s[i].z);
}
printf("\n");
}
return 0;
}(再次请求加强数据)(滑稽
这只是为了大家考场上可以多一个选择,说不定暴力踩标算呢
最好还是要掌握这题的HASH写法呀,(⑉• •⑉)‥♡
蒟蒻的第一篇绿题题解,如有错误,请指出,我一定会改正的,(●^o^●)
(显示后排版有些问题,现已修改,麻烦审核大大啦)