CF1575A Another Sorting Problem

题意

  • n n n个长度为 m m m s t r i n g string string型数组按照如下规则排序:
    • 对于偶数位按字典序升序排序;
    • 对于奇数位按字典序降序排序。

做法

  • 显然是不容易构造出一个可行的自定义cmp函数的。
  • 于是可以想到对题意进行转化,即考虑是否可以把两种情况归并为一种情况。
  • 考虑将降序排序转化成升序排序,可以将奇数位上的字母c进行转化:‘A’->‘Z’,‘Z’->‘A’,即变成'Z'-(c-'A')
  • 然后就可以统一进行排序了。

代码

#include<cstdio>
#include<string>
#include<iostream>
#include<algorithm>
#define ri register int
using namespace std;
const int maxn=1e6+21;
struct S{
	string s;
	int id;
}a[maxn];
inline int read(){
	int x=0;char c=getchar();
	while(c>'9'||c<'0') c=getchar();
	while(c>='0'&&c<='9') x=(x<<3)+(x<<1)+c-'0',c=getchar();
	return x;
}
bool cmp(S x,S y){
	return x.s<y.s;
}
int main(){
    int n=read(),m=read();
    for(ri i=1;i<=n;++i){
    	cin>>a[i].s;
    	a[i].id=i;
    	for(ri j=0;j<m;++j)
    		if(j&1) a[i].s[j]='Z'-(a[i].s[j]-'A');
    }
    sort(a+1,a+n+1,cmp);
    for(ri i=1;i<=n;++i) printf("%d ",a[i].id);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Robin_w2321

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值